Pcadmin.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use Exception;
  6. use think\db\exception\BindParamException;
  7. use think\db\exception\DataNotFoundException;
  8. use think\db\exception\ModelNotFoundException;
  9. use think\exception\DbException;
  10. use think\exception\PDOException;
  11. use think\exception\ValidateException;
  12. use fast\Random;
  13. /**
  14. * 维保公司管理员
  15. *
  16. * @icon fa fa-circle-o
  17. */
  18. class Pcadmin extends Backend
  19. {
  20. /**
  21. * Pcadmin模型对象
  22. * @var \app\admin\model\Pcadmin
  23. */
  24. protected $model = null;
  25. public function _initialize()
  26. {
  27. parent::_initialize();
  28. $this->model = new \app\admin\model\Pcadmin;
  29. $this->view->assign("statusList", $this->model->getStatusList());
  30. $this->view->assign("isKefuList", $this->model->getIsKefuList());
  31. }
  32. /**
  33. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  34. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  35. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  36. */
  37. /**
  38. * 查看
  39. */
  40. public function index()
  41. {
  42. //当前是否为关联查询
  43. $this->relationSearch = true;
  44. //设置过滤方法
  45. $this->request->filter(['strip_tags', 'trim']);
  46. if ($this->request->isAjax()) {
  47. //如果发送的来源是Selectpage,则转发到Selectpage
  48. if ($this->request->request('keyField')) {
  49. return $this->selectpage();
  50. }
  51. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  52. $list = $this->model
  53. ->with(['company'])
  54. ->where($where)
  55. ->order($sort, $order)
  56. ->paginate($limit);
  57. foreach ($list as $row) {
  58. $row->getRelation('company')->visible(['companyname']);
  59. }
  60. $result = array("total" => $list->total(), "rows" => $list->items());
  61. return json($result);
  62. }
  63. return $this->view->fetch();
  64. }
  65. /**
  66. * 管理员
  67. */
  68. public function pcadmin()
  69. {
  70. $company_id = input('company_id',0);
  71. $adminid = Db::name('pc_admin')->where('company_id',$company_id)->order('id asc')->value('id');
  72. $adminid = input('id',$adminid);
  73. $row = $this->model->get($adminid);
  74. if (!$row) {
  75. $this->error(__('No Results were found'));
  76. }
  77. if (false === $this->request->isPost()) {
  78. $this->view->assign('row', $row);
  79. return $this->view->fetch();
  80. }
  81. $params = $this->request->post('row/a');
  82. if (empty($params)) {
  83. $this->error(__('Parameter %s can not be empty', ''));
  84. }
  85. $params = $this->preExcludeFields($params);
  86. $result = false;
  87. Db::startTrans();
  88. try {
  89. $adminmobile = $params['mobile'];
  90. if (!$adminmobile || !\think\Validate::regex($adminmobile, "^1\d{10}$")) {
  91. abort(500,'手机号格式不正确');
  92. }
  93. $check = Db::name('pc_admin')->where('username',$adminmobile)->where('id','neq',$adminid)->find();
  94. if(!empty($check)){
  95. abort(500,'该手机号已被其他管理员注册');
  96. }
  97. if (!empty($params['password'])) {
  98. if (!\think\Validate::is($params['password'], "/^[\S]{6,30}$/")) {
  99. abort(500,__("Please input correct password"));
  100. }
  101. $params['salt'] = Random::alnum();
  102. $params['password'] = md5(md5($params['password']) . $params['salt']);
  103. }else{
  104. unset($params['password']);
  105. }
  106. //是否采用模型验证
  107. if ($this->modelValidate) {
  108. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  109. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  110. $row->validateFailException()->validate($validate);
  111. }
  112. $result = $row->allowField(true)->save($params);
  113. Db::commit();
  114. } catch (ValidateException|PDOException|Exception $e) {
  115. Db::rollback();
  116. $this->error($e->getMessage());
  117. }
  118. if (false === $result) {
  119. $this->error(__('No rows were updated'));
  120. }
  121. $this->success('操作成功');
  122. }
  123. }