CarUser.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\Exception;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. /**
  9. * 车主认证管理
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class CarUser extends Backend
  14. {
  15. /**
  16. * CardVerified模型对象
  17. * @var \app\admin\model\CardVerified
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\CardVerified;
  24. $this->view->assign("statusList", $this->model->getStatusList());
  25. }
  26. public function import()
  27. {
  28. parent::import();
  29. }
  30. /**
  31. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  32. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  33. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  34. */
  35. /**
  36. * 查看
  37. */
  38. public function index()
  39. {
  40. //设置过滤方法
  41. $this->request->filter(['strip_tags', 'trim']);
  42. if ($this->request->isAjax()) {
  43. //如果发送的来源是Selectpage,则转发到Selectpage
  44. if ($this->request->request('keyField')) {
  45. return $this->selectpage();
  46. }
  47. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  48. $where2 = [];
  49. $where2['card_verified.status'] = '1';
  50. $list = $this->model
  51. ->with(['user'])
  52. ->where($where)
  53. ->where($where2)
  54. ->order($sort, $order)
  55. ->paginate($limit);
  56. foreach ($list as $row) {
  57. $row->getRelation('user')->visible(['username', 'mobile']);
  58. }
  59. $result = ["total" => $list->total(), "rows" => $list->items()];
  60. return json($result);
  61. }
  62. return $this->view->fetch();
  63. }
  64. /**
  65. * 编辑
  66. */
  67. public function edit($ids = null)
  68. {
  69. $row = $this->model->get($ids);
  70. // $sign_areas = Db::name('areas')->where(['id' => $row['sign_city']])->find();
  71. // $areas = Db::name('areas')->where(['id' => $row['area']])->find();
  72. // $row['sign_province'] = $sign_areas['province'];
  73. // $row['sign_city'] = $sign_areas['city'];
  74. // $row['province'] = $areas['province'];
  75. // $row['city'] = $areas['city'];
  76. // $row['area'] = $areas['district'];
  77. if (!$row) {
  78. $this->error(__('No Results were found'));
  79. }
  80. $adminIds = $this->getDataLimitAdminIds();
  81. if (is_array($adminIds)) {
  82. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  83. $this->error(__('You have no permission'));
  84. }
  85. }
  86. if ($this->request->isPost()) {
  87. $params = $this->request->post("row/a");
  88. if ($params) {
  89. $params = $this->preExcludeFields($params);
  90. $result = false;
  91. Db::startTrans();
  92. try {
  93. if($row['status'] == 1 || $row['status'] == -11){
  94. $this->error('当前状态无法操作');
  95. }
  96. //是否采用模型验证
  97. if ($this->modelValidate) {
  98. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  99. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  100. $row->validateFailException(true)->validate($validate);
  101. }
  102. $result = $row->allowField(true)->save($params);
  103. if($params['status'] == 1){
  104. Db::name('user_verified')->where('user_id', $row['user_id'])->setField('card_verified', 1);
  105. }
  106. if($params['status'] == -1){
  107. Db::name('user_verified')->where('user_id', $row['user_id'])->setField('card_verified', -1);
  108. }
  109. Db::commit();
  110. } catch (ValidateException $e) {
  111. Db::rollback();
  112. $this->error($e->getMessage());
  113. } catch (PDOException $e) {
  114. Db::rollback();
  115. $this->error($e->getMessage());
  116. } catch (Exception $e) {
  117. Db::rollback();
  118. $this->error($e->getMessage());
  119. }
  120. if ($result !== false) {
  121. $this->success();
  122. } else {
  123. $this->error(__('No rows were updated'));
  124. }
  125. }
  126. $this->error(__('Parameter %s can not be empty', ''));
  127. }
  128. $this->view->assign("row", $row);
  129. return $this->view->fetch();
  130. }
  131. /**
  132. * 批量操作
  133. * @param string $ids
  134. */
  135. public function multi($ids = "")
  136. {
  137. $params = $this->request->request('params');
  138. parse_str($params, $paramsArr);
  139. if (isset($paramsArr)) {
  140. $field = $this->model::get($ids);
  141. if ($paramsArr['status'] == 0) {
  142. $paramsArr['status'] = -1;
  143. Db::name('user_verified')->where('user_id', $field['user_id'])->setField('card_verified', -1);
  144. }
  145. if ($paramsArr['status'] == 1) {
  146. Db::name('user_verified')->where('user_id', $field['user_id'])->setField('card_verified', 1);
  147. }
  148. $field->save($paramsArr);
  149. $this->success('操作成功');
  150. }
  151. return parent::multi($ids);
  152. }
  153. }