Address.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace Passport\Src;
  3. use Dever;
  4. use Passport\Lib\Base;
  5. class Address extends Base
  6. {
  7. # 获取默认地址
  8. public function getDefaultAddress($uid)
  9. {
  10. $where['uid'] = $uid;
  11. $where['type'] = 2;
  12. $data = Dever::db('passport/address')->one($where);
  13. return $data;
  14. }
  15. # 获取某个收货地址
  16. public function getOne($uid, $id)
  17. {
  18. $where['uid'] = $uid;
  19. $where['id'] = $id;
  20. $data = Dever::db('passport/address')->find($where);
  21. if ($data && Dever::project('area')) {
  22. $data = $this->getInfo($data);
  23. }
  24. return $data;
  25. }
  26. # 获取地址列表
  27. public function getAddress($uid)
  28. {
  29. $where['uid'] = $uid;
  30. $data = Dever::db('passport/address')->getList($where);
  31. if ($data && Dever::project('area')) {
  32. foreach ($data as $k => $v) {
  33. $data[$k] = $this->getInfo($v);
  34. }
  35. }
  36. return $data;
  37. }
  38. private function getInfo($data)
  39. {
  40. $data['address_info'] = $data['add_string'] = $data['address'] . $data['house_number'];
  41. $data['province_name'] = $data['city_name'] = $data['county_name'] = '';
  42. if ($data['county']) {
  43. $info = Dever::db('area/county')->find($data['county']);
  44. $data['county_name'] = $info['name'];
  45. if (!$data['city']) {
  46. $data['city'] = $info['city_id'];
  47. }
  48. $info = Dever::db('area/city')->find($data['city']);
  49. $data['city_name'] = $info['name'];
  50. if (!$data['province']) {
  51. $data['province'] = $info['province_id'];
  52. }
  53. $info = Dever::db('area/province')->find($data['province']);
  54. $data['province_name'] = $info['name'];
  55. //$data['address_info'] = $data['province_name'] . $data['city_name'] . $data['county_name'] .','. $data['address'] .','. $data['house_number'];
  56. $data['add_string'] = $data['province_name'] . $data['city_name'] . $data['county_name'] .','. $data['address'] .','. $data['house_number'] .','. $data['contact'] .','. $data['mobile'];
  57. } else {
  58. if (strstr($data['address'], '省')) {
  59. $temp = explode('省', $data['address']);
  60. $data['province_name'] = $temp[0] . '省';
  61. unset($temp[0]);
  62. $city = implode('市', $temp);
  63. } elseif (strstr($data['address'], '市')) {
  64. $temp = explode('市', $data['address']);
  65. $data['province_name'] = $temp[0] . '市';
  66. $city = '';
  67. unset($temp[0]);
  68. $city = implode('市', $temp);
  69. } elseif (strstr($data['address'], '区')) {
  70. $temp = explode('区', $data['address']);
  71. $data['province_name'] = $temp[0] . '区';
  72. $city = '';
  73. unset($temp[0]);
  74. $city = implode('区', $temp);
  75. }
  76. if (strstr($city, '市')) {
  77. $temp = explode('市', $city);
  78. $data['city_name'] = $temp[0] . '市';
  79. }
  80. $data['county_name'] = $temp[1];
  81. }
  82. return $data;
  83. }
  84. # 删除和恢复
  85. public function del($uid, $id, $state = 2)
  86. {
  87. $info = $this->getOne($uid, $id);
  88. if ($info) {
  89. $update['where_id'] = $info['id'];
  90. $update['state'] = $state;
  91. Dever::db('passport/address')->update($update);
  92. } else {
  93. Dever::alert('更新失败');
  94. }
  95. return 'ok';
  96. }
  97. # 添加或者更新地址接口
  98. public function up()
  99. {
  100. $uid = $this->check();
  101. $id = Dever::input('id');
  102. $type = Dever::input('type', 2);
  103. $province = Dever::input('province');
  104. $city = Dever::input('city');
  105. $county = Dever::input('county');
  106. $contact = Dever::input('contact');
  107. $address = Dever::input('address');
  108. $country = Dever::input('country');
  109. $mobile = Dever::input('mobile');
  110. $sex = Dever::input('sex');
  111. $house_number = Dever::input('house_number');
  112. $tag = Dever::input('tag');
  113. return $this->upAddress($id, $uid, $type, $mobile, $contact, $province, $city, $county, $address, $country, $sex, $house_number, $tag);
  114. }
  115. # 添加或者更新地址
  116. public function upAddress($id, $uid, $type = 2, $mobile, $contact, $province = '', $city = '', $county = '', $address = '', $country = '', $sex = '', $house_number = '', $tag = '')
  117. {
  118. if ($contact) {
  119. $update['contact'] = $contact;
  120. }
  121. if ($country) {
  122. $update['country'] = $country;
  123. }
  124. if ($mobile) {
  125. $update['mobile'] = $mobile;
  126. }
  127. if ($province) {
  128. $update['province'] = $province;
  129. }
  130. if ($city) {
  131. $update['city'] = $city;
  132. }
  133. if ($county) {
  134. $update['county'] = $county;
  135. }
  136. if ($province && $city && $county) {
  137. $update['area'] = $province . ',' . $city . ',' . $county;
  138. }
  139. $update['type'] = $type;
  140. if ($address) {
  141. $update['address'] = $address;
  142. }
  143. if ($sex) {
  144. $update['sex'] = $sex;
  145. }
  146. if ($house_number) {
  147. $update['house_number'] = $house_number;
  148. }
  149. if ($tag) {
  150. $update['tag'] = $tag;
  151. }
  152. if ($type == 2) {
  153. Dever::db('passport/address')->updateType(array('where_type' => 2, 'set_type' => 1));
  154. }
  155. if ($id) {
  156. $update['where_id'] = $id;
  157. Dever::db('passport/address')->update($update);
  158. } else {
  159. $update['uid'] = $uid;
  160. $id = Dever::db('passport/address')->insert($update);
  161. }
  162. return $id;
  163. }
  164. }