Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

182 wiersze
4.9 KiB

  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. * Class Address
  18. * @package addons\unishop\controller
  19. */
  20. class Address extends Base
  21. {
  22. /**
  23. * 允许频繁访问的接口
  24. * @var array
  25. */
  26. protected $frequently = ['area'];
  27. /**
  28. * 全部收货地址
  29. */
  30. public function all()
  31. {
  32. $page = $this->request->post('page', 1);
  33. $pagesize = $this->request->post('pagesize', 15);
  34. $data = (new AddressModel())
  35. ->with([
  36. 'province' => function($query) {$query->field('id,name');},
  37. 'city' => function($query) {$query->field('id,name');},
  38. 'area' => function($query) {$query->field('id,name');}
  39. ])
  40. ->where('user_id', $this->auth->id)
  41. ->order(['is_default' => 'desc', 'id' => 'desc'])
  42. ->limit(($page - 1) * $pagesize, $pagesize)
  43. ->select();
  44. if ($data) {
  45. $msg = '';
  46. $data = collection($data)->toArray();
  47. } else {
  48. $msg = __('No address');
  49. }
  50. $this->success($msg, $data);
  51. }
  52. /**
  53. * 添加收货地址
  54. */
  55. public function add()
  56. {
  57. $data = $this->request->post();
  58. try {
  59. $validate = Loader::validate('\\addons\\unishop\\validate\\Address');
  60. if (!$validate->check($data, [], 'add')) {
  61. throw new Exception($validate->getError());
  62. }
  63. $data['user_id'] = $this->auth->id;
  64. $addressModel = new AddressModel();
  65. if ($data['is_default'] == 1) {
  66. $addressModel->allowField(true)->save(['is_default' => 0], ['user_id' => $data['user_id']]);
  67. }
  68. if ($addressModel->where(['user_id' => $this->auth->id])->count() > 49) {
  69. throw new Exception('不能添加超过50个地址');
  70. }
  71. $addressModel = new AddressModel();
  72. if (!$addressModel->allowField(true)->save($data)) {
  73. throw new Exception($addressModel->getError());
  74. } else {
  75. $this->success('添加成功', true);
  76. }
  77. } catch (Exception $e) {
  78. $this->error($e->getMessage(), false);
  79. }
  80. }
  81. /**
  82. * 修改收货地址
  83. */
  84. public function edit()
  85. {
  86. $data = $this->request->post();
  87. try {
  88. new Validate();
  89. $validate = Loader::validate('\\addons\\unishop\\validate\\Address');
  90. if (!$validate->check($data, [], 'edit')) {
  91. throw new Exception($validate->getError());
  92. }
  93. $addressModel = new AddressModel();
  94. $data['user_id'] = $this->auth->id;
  95. if ($data['is_default'] == 1) {
  96. $addressModel->allowField(true)->save(['is_default' => 0], ['user_id' => $data['user_id']]);
  97. }
  98. $data['updatetime'] = time();
  99. if (!$addressModel->allowField(true)->save($data,['id' => $data['id'], 'user_id' => $data['user_id']])) {
  100. throw new Exception($addressModel->getError());
  101. } else {
  102. $this->success('修改成功', true);
  103. }
  104. } catch (Exception $e) {
  105. $this->error($e->getMessage(), false);
  106. }
  107. }
  108. /**
  109. * 删除收货地址
  110. */
  111. public function delete()
  112. {
  113. $address_id = $this->request->get('id', 0);
  114. $data = (new AddressModel())
  115. ->where([
  116. 'id' => $address_id,
  117. 'user_id' => $this->auth->id
  118. ])
  119. ->delete();
  120. if ($data) {
  121. $this->success('删除成功', 1);
  122. } else {
  123. $this->success('没有数据', 0);
  124. }
  125. }
  126. /**
  127. * 获取地区信息
  128. */
  129. public function area()
  130. {
  131. $pid = $this->request->get('pid', 1);
  132. Cache::clear('area_pid_'.$pid);
  133. if (Cache::has('area_pid_'.$pid)) {
  134. $area = Cache::get('area_pid_'.$pid);
  135. } else {
  136. $areaModel = new Area();
  137. $area = $areaModel
  138. ->field('name as label,pid,id,code as value')
  139. ->where(['pid' => $pid])
  140. ->order(['pid' => 'asc', 'id' => 'asc'])
  141. ->select();
  142. if ($area) {
  143. $area = collection($area)->toArray();
  144. Cache::set('area_pid_'.$pid, $area, 60);
  145. }
  146. }
  147. $this->success('', $area);
  148. }
  149. /**
  150. * 获取单个收货地址
  151. */
  152. public function info()
  153. {
  154. $id = $this->request->get('id');
  155. $address = (new AddressModel())->where(['id' => $id, 'user_id' => $this->auth->id])->find()->toArray();
  156. $this->success('', $address);
  157. }
  158. }