DriverVerified.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 DriverVerified extends Backend
  14. {
  15. /**
  16. * DriverVerified模型对象
  17. * @var \app\admin\model\DriverVerified
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\DriverVerified;
  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->relationSearch = true;
  42. //设置过滤方法
  43. $this->request->filter(['strip_tags', 'trim']);
  44. if ($this->request->isAjax()) {
  45. //如果发送的来源是Selectpage,则转发到Selectpage
  46. if ($this->request->request('keyField')) {
  47. return $this->selectpage();
  48. }
  49. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  50. $list = $this->model
  51. ->with(['user'])
  52. ->where($where)
  53. ->order($sort, $order)
  54. ->paginate($limit);
  55. foreach ($list as $row){
  56. $sign_areas = Db::name('areas')->where(['id' => $row['sign_city']])->find();
  57. $areas = Db::name('areas')->where(['id' => $row['area']])->find();
  58. $row['sign_province_name'] = $sign_areas['province'];
  59. $row['sign_city_name'] = $sign_areas['city'];
  60. $row['province'] = $areas['province'];
  61. $row['city'] = $areas['city'];
  62. $row['area'] = $areas['district'];
  63. $row->getRelation('user')->visible(['username','mobile']);
  64. }
  65. $result = array("total" => $list->total(), "rows" => $list->items());
  66. return json($result);
  67. }
  68. return $this->view->fetch();
  69. }
  70. /**
  71. * 编辑
  72. */
  73. public function edit($ids = null)
  74. {
  75. $row = $this->model->get($ids);
  76. if (!$row) {
  77. $this->error(__('No Results were found'));
  78. }
  79. $adminIds = $this->getDataLimitAdminIds();
  80. if (is_array($adminIds)) {
  81. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  82. $this->error(__('You have no permission'));
  83. }
  84. }
  85. if ($this->request->isPost()) {
  86. $params = $this->request->post("row/a");
  87. if ($params) {
  88. if($row['status'] == 1){
  89. \exception('当前状态无法操作');
  90. }
  91. $params = $this->preExcludeFields($params);
  92. $result = false;
  93. Db::startTrans();
  94. try {
  95. //是否采用模型验证
  96. if ($this->modelValidate) {
  97. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  98. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  99. $row->validateFailException(true)->validate($validate);
  100. }
  101. $result = $row->allowField(true)->save($params);
  102. if($params['status'] == 1){
  103. Db::name('user_verified')->where('user_id', $row['user_id'])->setField('driver_verified', 1);
  104. }
  105. if($params['status'] == -1){
  106. Db::name('user_verified')->where('user_id', $row['user_id'])->setField('driver_verified', -1);
  107. }
  108. Db::commit();
  109. } catch (ValidateException $e) {
  110. Db::rollback();
  111. $this->error($e->getMessage());
  112. } catch (PDOException $e) {
  113. Db::rollback();
  114. $this->error($e->getMessage());
  115. } catch (Exception $e) {
  116. Db::rollback();
  117. $this->error($e->getMessage());
  118. }
  119. if ($result !== false) {
  120. $this->success();
  121. } else {
  122. $this->error(__('No rows were updated'));
  123. }
  124. }
  125. $this->error(__('Parameter %s can not be empty', ''));
  126. }
  127. $this->view->assign("row", $row);
  128. return $this->view->fetch();
  129. }
  130. }