Address.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. namespace User\Lib;
  3. use Dever;
  4. class Address
  5. {
  6. public function manage_update($id, $name, $param)
  7. {
  8. $area = Dever::param('area', $param);
  9. $type = Dever::param('type', $param);
  10. if ($area && is_array($area) && $area[0] > 0) {
  11. $update['where_id'] = $id;
  12. $update['province'] = $area[0];
  13. $update['city'] = $area[1];
  14. $update['county'] = isset($area[2]) ? $area[2] : '';
  15. $update['town'] = isset($area[3]) ? $area[3] : '';
  16. if ($type == 2) {
  17. Dever::db('user/address')->updateType(array('where_type' => 2, 'set_type' => 1));
  18. }
  19. $address = Dever::param('address', $param);
  20. if ($address) {
  21. $geo = Dever::geo($address, $update['city']);
  22. if ($geo) {
  23. $update['lng'] = $geo[0];
  24. $update['lat'] = $geo[1];
  25. }
  26. }
  27. $update['type'] = $type;
  28. Dever::db('user/address')->update($update);
  29. $call = Dever::config('base')->address;
  30. if ($call) {
  31. $source_table = Dever::param('source_table', $param);
  32. $source_id = Dever::param('source_id', $param);
  33. $address = Dever::param('address', $param);
  34. $contacts = Dever::param('contacts', $param);
  35. $phone = Dever::param('phone', $param);
  36. $address_string = Dever::load('area/api')->string($area) . ',' . $address . ',' . $contacts . ',' . $phone;
  37. Dever::load($call, $source_table, $source_id, '修改地址:' . $address_string);
  38. }
  39. }
  40. }
  41. # 按照类型获取地址
  42. public function getData($source_id, $source_table = 'user/info', $type = 2)
  43. {
  44. $where['source_table'] = $source_table;
  45. $where['source_id'] = $source_id;
  46. $where['type'] = $type;
  47. $data = Dever::db('user/address')->one($where);
  48. if ($data && Dever::project('area')) {
  49. $data = $this->info($data);
  50. }
  51. return $data;
  52. }
  53. # 获取某个收货地址
  54. public function getInfo($source_id, $id, $source_table = 'user/info')
  55. {
  56. $where['source_table'] = $source_table;
  57. $where['source_id'] = $source_id;
  58. $where['id'] = $id;
  59. $data = Dever::db('user/address')->one($where);
  60. if ($data && Dever::project('area')) {
  61. $data = $this->info($data);
  62. }
  63. return $data;
  64. }
  65. # 获取地址列表
  66. public function getList($source_id, $source_table = 'user/info')
  67. {
  68. $where['source_table'] = $source_table;
  69. $where['source_id'] = $source_id;
  70. $data = Dever::db('user/address')->getList($where);
  71. if ($data && Dever::project('area')) {
  72. foreach ($data as $k => $v) {
  73. $data[$k] = $this->info($v);
  74. }
  75. }
  76. return $data;
  77. }
  78. public function info($data)
  79. {
  80. if (is_array($data['area'])) {
  81. $data['area'] = implode(',', $data['area']);
  82. }
  83. $data['area_string'] = Dever::load('area/api')->string($data['area']);
  84. $data['province_name'] = $data['city_name'] = $data['county_name'] = $data['town_name'] = '';
  85. if ($data['town'] && $data['town'] > 0) {
  86. $info = Dever::db('area/town')->find($data['town']);
  87. if ($info) {
  88. $data['town_name'] = $info['name'];
  89. }
  90. }
  91. if ($data['county']) {
  92. $info = Dever::db('area/county')->find($data['county']);
  93. if ($info) {
  94. $data['county_name'] = $info['name'];
  95. }
  96. }
  97. if ($data['city']) {
  98. $info = Dever::db('area/city')->find($data['city']);
  99. if ($info) {
  100. $data['city_name'] = $info['name'];
  101. }
  102. }
  103. if ($data['province']) {
  104. $info = Dever::db('area/province')->find($data['province']);
  105. if ($info) {
  106. $data['province_name'] = $info['name'];
  107. }
  108. }
  109. return $data;
  110. }
  111. # 添加或者更新地址
  112. public function update($id, $source_id, $source_table, $type = 2, $mobile, $contact, $province = '', $city = '', $county = '', $town = '', $address = '', $country = '', $tag = '')
  113. {
  114. if ($contact) {
  115. $update['contact'] = $contact;
  116. }
  117. if ($mobile) {
  118. $update['mobile'] = $mobile;
  119. } else {
  120. Dever::alert('请输入手机号');
  121. }
  122. if ($province) {
  123. $update['province'] = $province;
  124. } else {
  125. Dever::alert('请选择省份');
  126. }
  127. if ($city) {
  128. $update['city'] = $city;
  129. } else {
  130. Dever::alert('请选择城市');
  131. }
  132. if ($county) {
  133. $update['county'] = $county;
  134. } else {
  135. Dever::alert('请选择区域');
  136. }
  137. if ($town) {
  138. $update['town'] = $town;
  139. }
  140. if ($province && $city && $county) {
  141. $update['area'] = $province . ',' . $city . ',' . $county;
  142. }
  143. if ($town) {
  144. $update['area'] .= ',' . $town;
  145. }
  146. $update['type'] = $type;
  147. if ($address) {
  148. $update['address'] = $address;
  149. if ($address && isset($update['city']) && $update['city']) {
  150. $geo = Dever::geo($address, $update['city']);
  151. if ($geo) {
  152. $update['lng'] = $geo[0];
  153. $update['lat'] = $geo[1];
  154. }
  155. }
  156. }
  157. if ($tag) {
  158. $update['tag'] = $tag;
  159. }
  160. if ($type == 2) {
  161. Dever::db('user/address')->updateType(array('where_type' => 2, 'set_type' => 1));
  162. }
  163. if ($id) {
  164. $update['where_id'] = $id;
  165. Dever::db('user/address')->update($update);
  166. } else {
  167. $update['source_id'] = $source_id;
  168. $update['source_table'] = $source_table;
  169. $id = Dever::db('user/address')->insert($update);
  170. }
  171. return $id;
  172. }
  173. # 删除和恢复
  174. public function delete($source_id, $id, $state = 2, $source_table = 'user/info')
  175. {
  176. $info = $this->getInfo($source_id, $id, $source_table);
  177. if ($info) {
  178. $update['where_id'] = $info['id'];
  179. $update['state'] = $state;
  180. Dever::db('user/address')->update($update);
  181. } else {
  182. Dever::alert('更新失败');
  183. }
  184. return 'ok';
  185. }
  186. public function getSource($source_table, $source_id)
  187. {
  188. $info = Dever::db($source_table)->one($source_id);
  189. return $info;
  190. }
  191. public function getManageUrl($source_table, $source_id, $type, $address = array())
  192. {
  193. $info = Dever::db('user/address')->one(array('source_table' => $source_table, 'source_id' => $source_id));
  194. $url = Dever::url('project/database/update?project=user&table=address&search_option_source_table=' . $source_table . '&search_option_source_id=' . $source_id, 'manage');
  195. if ($info) {
  196. $url .= '&where_id=' . $info['id'];
  197. }
  198. if ($address) {
  199. $url .= '&type=' . $type . '&area=' . $address['area'] . '&address=' . $address['address'] . '&contacts=' . $address['contacts'] . '&phone=' . $address['phone'];
  200. }
  201. return $url;
  202. }
  203. }