Address.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 get($source_id, $source_table, $uid, $address_id)
  43. {
  44. $info = $this->getData($source_id, $source_table, 1);
  45. if (!$info) {
  46. $info = $this->getInfo($uid, $address_id);
  47. }
  48. return $info;
  49. }
  50. # 按照类型获取地址
  51. public function getData($source_id, $source_table = 'user/info', $type = 2)
  52. {
  53. $where['source_table'] = $source_table;
  54. $where['source_id'] = $source_id;
  55. $where['type'] = $type;
  56. $data = Dever::db('user/address')->one($where);
  57. if ($data && Dever::project('area')) {
  58. $data = $this->info($data);
  59. }
  60. return $data;
  61. }
  62. # 获取某个收货地址
  63. public function getInfo($source_id, $id, $source_table = 'user/info')
  64. {
  65. $where['source_table'] = $source_table;
  66. $where['source_id'] = $source_id;
  67. $where['id'] = $id;
  68. $data = Dever::db('user/address')->one($where);
  69. if ($data && Dever::project('area')) {
  70. $data = $this->info($data);
  71. }
  72. return $data;
  73. }
  74. # 获取地址列表
  75. public function getList($source_id, $source_table = 'user/info')
  76. {
  77. $where['source_table'] = $source_table;
  78. $where['source_id'] = $source_id;
  79. $data = Dever::db('user/address')->getList($where);
  80. if ($data && Dever::project('area')) {
  81. foreach ($data as $k => $v) {
  82. $data[$k] = $this->info($v);
  83. }
  84. }
  85. return $data;
  86. }
  87. public function info($data)
  88. {
  89. if (is_array($data['area'])) {
  90. $data['area'] = implode(',', $data['area']);
  91. }
  92. $data['area_string'] = Dever::load('area/api')->string($data['area']);
  93. $data['province_name'] = $data['city_name'] = $data['county_name'] = $data['town_name'] = '';
  94. if ($data['town'] && $data['town'] > 0) {
  95. $info = Dever::db('area/town')->find($data['town']);
  96. if ($info) {
  97. $data['town_name'] = $info['name'];
  98. }
  99. }
  100. if ($data['county']) {
  101. $info = Dever::db('area/county')->find($data['county']);
  102. if ($info) {
  103. $data['county_name'] = $info['name'];
  104. }
  105. }
  106. if ($data['city']) {
  107. $info = Dever::db('area/city')->find($data['city']);
  108. if ($info) {
  109. $data['city_name'] = $info['name'];
  110. }
  111. }
  112. if ($data['province']) {
  113. $info = Dever::db('area/province')->find($data['province']);
  114. if ($info) {
  115. $data['province_name'] = $info['name'];
  116. }
  117. }
  118. return $data;
  119. }
  120. # 添加或者更新地址
  121. public function update($id, $source_id, $source_table, $type = 2, $mobile, $contact, $province = '', $city = '', $county = '', $town = '', $address = '', $tag = '')
  122. {
  123. if ($contact) {
  124. $update['contact'] = $contact;
  125. }
  126. if ($mobile) {
  127. $update['phone'] = $mobile;
  128. } else {
  129. Dever::alert('请输入手机号');
  130. }
  131. if ($province) {
  132. $update['province'] = $province;
  133. } else {
  134. Dever::alert('请选择省份');
  135. }
  136. if ($city) {
  137. $update['city'] = $city;
  138. } else {
  139. Dever::alert('请选择城市');
  140. }
  141. if ($county) {
  142. $update['county'] = $county;
  143. } else {
  144. Dever::alert('请选择区域');
  145. }
  146. if ($town) {
  147. $update['town'] = $town;
  148. }
  149. if ($province && $city && $county) {
  150. $update['area'] = $province . ',' . $city . ',' . $county;
  151. }
  152. if ($town) {
  153. $update['area'] .= ',' . $town;
  154. }
  155. $update['type'] = $type;
  156. if ($address) {
  157. $update['address'] = $address;
  158. if ($address && isset($update['city']) && $update['city']) {
  159. $geo = Dever::geo($address, $update['city']);
  160. if ($geo) {
  161. $update['lng'] = $geo[0];
  162. $update['lat'] = $geo[1];
  163. }
  164. }
  165. }
  166. if ($tag) {
  167. $update['tag'] = $tag;
  168. }
  169. if ($type == 2) {
  170. Dever::db('user/address')->updateType(array('where_type' => 2, 'set_type' => 1));
  171. }
  172. if ($id) {
  173. $update['where_id'] = $id;
  174. Dever::db('user/address')->update($update);
  175. } else {
  176. $update['source_id'] = $source_id;
  177. $update['source_table'] = $source_table;
  178. $id = Dever::db('user/address')->insert($update);
  179. }
  180. return $id;
  181. }
  182. # 删除和恢复
  183. public function delete($source_id, $id, $state = 2, $source_table = 'user/info')
  184. {
  185. $info = $this->getInfo($source_id, $id, $source_table);
  186. if ($info) {
  187. $update['where_id'] = $info['id'];
  188. $update['state'] = $state;
  189. Dever::db('user/address')->update($update);
  190. } else {
  191. Dever::alert('更新失败');
  192. }
  193. return 'ok';
  194. }
  195. public function getSource($source_table, $source_id)
  196. {
  197. $info = Dever::db($source_table)->one($source_id);
  198. return $info;
  199. }
  200. public function getManageUrl($source_table, $source_id, $type, $address = array())
  201. {
  202. $info = Dever::db('user/address')->one(array('source_table' => $source_table, 'source_id' => $source_id));
  203. $url = Dever::url('project/database/update?project=user&table=address&search_option_source_table=' . $source_table . '&search_option_source_id=' . $source_id, 'manage');
  204. if ($info) {
  205. $url .= '&where_id=' . $info['id'];
  206. }
  207. if ($address) {
  208. $url .= '&type=' . $type . '&area=' . $address['area'] . '&address=' . $address['address'] . '&contacts=' . $address['contacts'] . '&phone=' . $address['phone'];
  209. }
  210. return $url;
  211. }
  212. }