Address.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace app\admin\controller\shop;
  3. use app\common\controller\Backend;
  4. use Exception;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. use app\admin\model\shop\Area;
  9. /**
  10. * 收货地址
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Address extends Backend
  15. {
  16. /**
  17. * Address模型对象
  18. * @var \app\admin\model\shop\Address
  19. */
  20. protected $model = null;
  21. protected $noNeedRight = ['area'];
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\shop\Address;
  26. $this->view->assign("statusList", $this->model->getStatusList());
  27. }
  28. /**
  29. * 查看
  30. */
  31. public function index()
  32. {
  33. //设置过滤方法
  34. $this->request->filter(['strip_tags', 'trim']);
  35. if ($this->request->isAjax()) {
  36. //如果发送的来源是Selectpage,则转发到Selectpage
  37. if ($this->request->request('keyField')) {
  38. return $this->selectpage();
  39. }
  40. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  41. $list = $this->model
  42. ->with(['User', 'Province', 'City', 'Area'])
  43. ->where($where)
  44. ->order($sort, $order)
  45. ->paginate($limit);
  46. foreach ($list as $index => $item) {
  47. if ($item->user) {
  48. $item->user->visible(['id', 'nickname', 'avatar']);
  49. }
  50. }
  51. $result = array("total" => $list->total(), "rows" => $list->items());
  52. return json($result);
  53. }
  54. return $this->view->fetch();
  55. }
  56. /**
  57. * 添加
  58. */
  59. public function add()
  60. {
  61. if ($this->request->isPost()) {
  62. $params = $this->request->post("row/a");
  63. if ($params) {
  64. $params['province_id'] = $this->request->post('province');
  65. $params['city_id'] = $this->request->post('city');
  66. $params['area_id'] = $this->request->post('area');
  67. $params = $this->preExcludeFields($params);
  68. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  69. $params[$this->dataLimitField] = $this->auth->id;
  70. }
  71. $result = false;
  72. Db::startTrans();
  73. try {
  74. //是否采用模型验证
  75. if ($this->modelValidate) {
  76. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  77. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  78. $this->model->validateFailException(true)->validate($validate);
  79. }
  80. $result = $this->model->allowField(true)->save($params);
  81. Db::commit();
  82. } catch (ValidateException $e) {
  83. Db::rollback();
  84. $this->error($e->getMessage());
  85. } catch (PDOException $e) {
  86. Db::rollback();
  87. $this->error($e->getMessage());
  88. } catch (Exception $e) {
  89. Db::rollback();
  90. $this->error($e->getMessage());
  91. }
  92. if ($result !== false) {
  93. $this->success();
  94. } else {
  95. $this->error(__('No rows were inserted'));
  96. }
  97. }
  98. $this->error(__('Parameter %s can not be empty', ''));
  99. }
  100. return $this->view->fetch();
  101. }
  102. /**
  103. * 编辑
  104. */
  105. public function edit($ids = null)
  106. {
  107. $row = $this->model->get($ids, ['Province', 'City', 'Area']);
  108. if (!$row) {
  109. $this->error(__('No Results were found'));
  110. }
  111. $adminIds = $this->getDataLimitAdminIds();
  112. if (is_array($adminIds)) {
  113. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  114. $this->error(__('You have no permission'));
  115. }
  116. }
  117. if ($this->request->isPost()) {
  118. $params = $this->request->post("row/a");
  119. if ($params) {
  120. $params['province_id'] = $this->request->post('province');
  121. $params['city_id'] = $this->request->post('city');
  122. $params['area_id'] = $this->request->post('area');
  123. $params = $this->preExcludeFields($params);
  124. $result = false;
  125. Db::startTrans();
  126. try {
  127. //是否采用模型验证
  128. if ($this->modelValidate) {
  129. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  130. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  131. $row->validateFailException(true)->validate($validate);
  132. }
  133. $result = $row->allowField(true)->save($params);
  134. Db::commit();
  135. } catch (ValidateException $e) {
  136. Db::rollback();
  137. $this->error($e->getMessage());
  138. } catch (PDOException $e) {
  139. Db::rollback();
  140. $this->error($e->getMessage());
  141. } catch (Exception $e) {
  142. Db::rollback();
  143. $this->error($e->getMessage());
  144. }
  145. if ($result !== false) {
  146. $this->success();
  147. } else {
  148. $this->error(__('No rows were updated'));
  149. }
  150. }
  151. $this->error(__('Parameter %s can not be empty', ''));
  152. }
  153. $this->view->assign("row", $row);
  154. return $this->view->fetch();
  155. }
  156. /**
  157. * 读取省市区数据,联动列表
  158. */
  159. public function area()
  160. {
  161. $params = $this->request->get("row/a");
  162. if (!empty($params)) {
  163. $province = isset($params['province']) ? $params['province'] : '';
  164. $city = isset($params['city']) ? $params['city'] : '';
  165. } else {
  166. $province = $this->request->get('province', '');
  167. $city = $this->request->get('city', '');
  168. }
  169. $where = ['pid' => 0, 'level' => 1];
  170. $provincelist = null;
  171. if ($province !== '') {
  172. $where['pid'] = $province;
  173. $where['level'] = 2;
  174. if ($city !== '') {
  175. $where['pid'] = $city;
  176. $where['level'] = 3;
  177. }
  178. }
  179. $provincelist = Area::where($where)->field('id as value,name')->where('status', 'normal')->select();
  180. $this->success('', '', $provincelist);
  181. }
  182. }