Address.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zhengmingwei
  5. * Date: 2019/11/5
  6. * Time: 10:33 下午
  7. */
  8. namespace addons\unishop\controller;
  9. use \addons\unishop\model\Address as AddressModel;
  10. use addons\unishop\model\Area;
  11. use think\Cache;
  12. use think\Exception;
  13. use think\Loader;
  14. use think\Validate;
  15. /**
  16. * 收货地址
  17. */
  18. class Address extends Base
  19. {
  20. /**
  21. * 允许频繁访问的接口
  22. * @var array
  23. */
  24. protected $frequently = ['area'];
  25. /**
  26. * @ApiTitle (全部收货地址)
  27. * @ApiSummary (用户收货地址列表)
  28. * @ApiMethod (POST)
  29. * @ApiHeaders (name=token, type=string, required=true, description="用户登录的Token", sample="a2e3cc70-d2d1-41e6-9c14-f1d774ee5e1e")
  30. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  31. * @ApiReturn ({"code":1,"msg":"","data":[]})
  32. * @ApiReturnParams (name="id", type="integer", description="地址id")
  33. * @ApiReturnParams (name="user_id", type="integer", description="用户id")
  34. * @ApiReturnParams (name="name", type="string", description="收货人名称")
  35. * @ApiReturnParams (name="mobile", type="string", description="收货人电话")
  36. * @ApiReturnParams (name="address", type="string", description="收货详细地址")
  37. * @ApiReturnParams (name="province_id", type="integer", description="省份id")
  38. * @ApiReturnParams (name="city_id", type="integer", description="城市id")
  39. * @ApiReturnParams (name="area_id", type="integer", description="地区id")
  40. * @ApiReturnParams (name="is_default", type="integer", description="是否默认")
  41. * @ApiReturnParams (name="province", type="json", description="{'name':'北京'}")
  42. * @ApiReturnParams (name="city", type="json", description="{'name':'北京市'}")
  43. * @ApiReturnParams (name="area", type="json", description="{'name':'东城区'}")
  44. */
  45. public function all()
  46. {
  47. $page = $this->request->post('page', 1);
  48. $pagesize = $this->request->post('pagesize', 15);
  49. $data = (new AddressModel())
  50. ->with([
  51. 'province' => function($query) {$query->field('id,name');},
  52. 'city' => function($query) {$query->field('id,name');},
  53. 'area' => function($query) {$query->field('id,name');}
  54. ])
  55. ->where('user_id', $this->auth->id)
  56. ->order(['is_default' => 'desc', 'id' => 'desc'])
  57. ->limit(($page - 1) * $pagesize, $pagesize)
  58. ->select();
  59. if ($data) {
  60. $msg = '';
  61. $data = collection($data)->toArray();
  62. } else {
  63. $msg = __('No address');
  64. }
  65. $this->success($msg, $data);
  66. }
  67. /**
  68. * @ApiTitle (添加收货地址)
  69. * @ApiSummary (添加收货地址)
  70. * @ApiMethod (POST)
  71. * @ApiHeaders (name=token, type=string, required=true, description="用户登录的Token", sample="a2e3cc70-d2d1-41e6-9c14-f1d774ee5e1e")
  72. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  73. * @ApiParams (name="name", type="string", required=true, description="名字")
  74. * @ApiParams (name="mobile", type="string", required=true, description="电话号码")
  75. * @ApiParams (name="address", type="string", required=true, description="详细地址")
  76. * @ApiParams (name="province_id", type="integer", required=true, description="省份id")
  77. * @ApiParams (name="city_id", type="integer", required=true, description="城市id")
  78. * @ApiParams (name="area_id", type="integer", required=true, description="区域id")
  79. * @ApiParams (name="is_default", type="integer", required=true, description="是否默认", sample="1")
  80. * @ApiReturn ({"code":1,"msg":"添加成功","data":true})
  81. */
  82. public function add()
  83. {
  84. $data = $this->request->post();
  85. try {
  86. $validate = Loader::validate('\\addons\\unishop\\validate\\Address');
  87. if (!$validate->check($data, [], 'add')) {
  88. throw new Exception($validate->getError());
  89. }
  90. $data['user_id'] = $this->auth->id;
  91. $addressModel = new AddressModel();
  92. if ($data['is_default'] == 1) {
  93. $addressModel->allowField(true)->save(['is_default' => 0], ['user_id' => $data['user_id']]);
  94. }
  95. if ($addressModel->where(['user_id' => $this->auth->id])->count() > 49) {
  96. throw new Exception('不能添加超过50个地址');
  97. }
  98. $addressModel = new AddressModel();
  99. if (!$addressModel->allowField(true)->save($data)) {
  100. throw new Exception($addressModel->getError());
  101. }
  102. } catch (Exception $e) {
  103. $this->error($e->getMessage(), false);
  104. }
  105. $this->success('添加成功', true);
  106. }
  107. /**
  108. * @ApiTitle (修改收货地址)
  109. * @ApiSummary (修改收货地址)
  110. * @ApiMethod (POST)
  111. * @ApiHeaders (name=token, type=string, required=true, description="用户登录的Token", sample="a2e3cc70-d2d1-41e6-9c14-f1d774ee5e1e")
  112. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  113. * @ApiParams (name="id", type="integer", required=true, description="地址id")
  114. * @ApiParams (name="name", type="string", required=true, description="名字")
  115. * @ApiParams (name="mobile", type="string", required=true, description="电话号码")
  116. * @ApiParams (name="address", type="string", required=true, description="详细地址")
  117. * @ApiParams (name="province_id", type="integer", required=true, description="省份id")
  118. * @ApiParams (name="city_id", type="integer", required=true, description="城市id")
  119. * @ApiParams (name="area_id", type="integer", required=true, description="区域id")
  120. * @ApiParams (name="is_default", type="integer", required=true, description="是否默认", sample="1")
  121. * @ApiReturn ({"code":1,"msg":"修改成功","data":true})
  122. */
  123. public function edit()
  124. {
  125. $data = $this->request->post();
  126. try {
  127. new Validate();
  128. $validate = Loader::validate('\\addons\\unishop\\validate\\Address');
  129. if (!$validate->check($data, [], 'edit')) {
  130. throw new Exception($validate->getError());
  131. }
  132. $addressModel = new AddressModel();
  133. $data['user_id'] = $this->auth->id;
  134. if ($data['is_default'] == 1) {
  135. $addressModel->allowField(true)->save(['is_default' => 0], ['user_id' => $data['user_id']]);
  136. }
  137. $data['updatetime'] = time();
  138. if (!$addressModel->allowField(true)->save($data,['id' => $data['id'], 'user_id' => $data['user_id']])) {
  139. throw new Exception($addressModel->getError());
  140. }
  141. } catch (Exception $e) {
  142. $this->error($e->getMessage(), false);
  143. }
  144. $this->success('修改成功', true);
  145. }
  146. /**
  147. * @ApiTitle (删除收货地址)
  148. * @ApiSummary (删除收货地址)
  149. * @ApiMethod (GET)
  150. * @ApiHeaders (name=token, type=string, required=true, description="用户登录的Token", sample="a2e3cc70-d2d1-41e6-9c14-f1d774ee5e1e")
  151. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  152. * @ApiParams (name="id", type="integer", required=true, description="地址id")
  153. * @ApiReturn ({"code":1,"msg":"删除成功","data":1})
  154. */
  155. public function delete()
  156. {
  157. $address_id = $this->request->get('id', 0);
  158. $data = (new AddressModel())
  159. ->where([
  160. 'id' => $address_id,
  161. 'user_id' => $this->auth->id
  162. ])
  163. ->delete();
  164. if ($data) {
  165. $this->success('删除成功', 1);
  166. } else {
  167. $this->success('没有数据', 0);
  168. }
  169. }
  170. /**
  171. * @ApiTitle (获取地区信息)
  172. * @ApiSummary (获取地区信息)
  173. * @ApiMethod (GET)
  174. * @ApiHeaders (name=token, type=string, required=true, description="用户登录的Token", sample="a2e3cc70-d2d1-41e6-9c14-f1d774ee5e1e")
  175. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  176. * @ApiParams (name="pid", type="integer", required=true, description="省市区的id")
  177. * @ApiReturn ({"code":1,"msg":"","data":[]})
  178. * @ApiReturnParams (name="id", type="integer", description="省市区的id")
  179. * @ApiReturnParams (name="pid", type="integer", description="上级id")
  180. * @ApiReturnParams (name="label", type="integer", description="省市区简称")
  181. */
  182. public function area()
  183. {
  184. $pid = $this->request->get('pid', 1);
  185. Cache::clear('area_pid_'.$pid);
  186. if (Cache::has('area_pid_'.$pid)) {
  187. $area = Cache::get('area_pid_'.$pid);
  188. } else {
  189. $areaModel = new Area();
  190. $area = $areaModel
  191. ->field('name as label,pid,id,code as value')
  192. ->where(['pid' => $pid])
  193. ->order(['pid' => 'asc', 'id' => 'asc'])
  194. ->select();
  195. if ($area) {
  196. $area = collection($area)->toArray();
  197. Cache::set('area_pid_'.$pid, $area, 60);
  198. }
  199. }
  200. $this->success('', $area);
  201. }
  202. /**
  203. * @ApiTitle (获取单个收货地址)
  204. * @ApiSummary (获取单个收货地址)
  205. * @ApiMethod (GET)
  206. * @ApiHeaders (name=token, type=string, required=true, description="用户登录的Token", sample="a2e3cc70-d2d1-41e6-9c14-f1d774ee5e1e")
  207. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  208. * @ApiParams (name="id", type="integer", required=true, description="省市区的id")
  209. * @ApiReturn ({"code":1,"msg":"","data":{}})
  210. * @ApiReturnParams (name="id", type="integer", description="地址id")
  211. * @ApiReturnParams (name="user_id", type="integer", description="用户id")
  212. * @ApiReturnParams (name="name", type="string", description="收货人名称")
  213. * @ApiReturnParams (name="mobile", type="string", description="收货人电话")
  214. * @ApiReturnParams (name="address", type="string", description="收货详细地址")
  215. * @ApiReturnParams (name="province_id", type="integer", description="省份id")
  216. * @ApiReturnParams (name="city_id", type="integer", description="城市id")
  217. * @ApiReturnParams (name="area_id", type="integer", description="地区id")
  218. * @ApiReturnParams (name="is_default", type="integer", description="是否默认")
  219. */
  220. public function info()
  221. {
  222. $id = $this->request->get('id');
  223. $address = (new AddressModel())->where(['id' => $id, 'user_id' => $this->auth->id])->find()->toArray();
  224. $this->success('', $address);
  225. }
  226. }