User.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\common\controller\Backend;
  4. use app\common\library\Auth;
  5. use app\common\model\Category;
  6. use app\common\model\user\Guide;
  7. use fast\Random;
  8. use think\Db;
  9. use think\exception\PDOException;
  10. use think\exception\ValidateException;
  11. /**
  12. * 会员管理
  13. *
  14. * @icon fa fa-user
  15. */
  16. class User extends Backend
  17. {
  18. protected $relationSearch = true;
  19. protected $searchFields = 'id,username,nickname';
  20. /**
  21. * @var \app\admin\model\User
  22. */
  23. protected $model = null;
  24. public function _initialize()
  25. {
  26. parent::_initialize();
  27. $this->model = model('User');
  28. }
  29. /**
  30. * 查看
  31. */
  32. public function index()
  33. {
  34. //设置过滤方法
  35. $this->request->filter(['strip_tags', 'trim']);
  36. if ($this->request->isAjax()) {
  37. //如果发送的来源是Selectpage,则转发到Selectpage
  38. if ($this->request->request('keyField')) {
  39. return $this->selectpage();
  40. }
  41. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  42. $list = $this->model
  43. ->with(['group', 'category'])
  44. ->where($where)
  45. ->order($sort, $order)
  46. ->paginate($limit);
  47. foreach ($list as $k => $v) {
  48. $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
  49. $v->hidden(['password', 'salt']);
  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 = $this->preExcludeFields($params);
  65. $username = $params['username'];
  66. $nickname = $params['nickname'];
  67. $password = $params['password'];
  68. $mobile = $params['mobile'];
  69. $email = $params['email'];
  70. // 检测用户名、昵称、邮箱、手机号是否存在
  71. if (\app\common\model\User::getByUsername($username)) {
  72. $this->error(__('Username already exist'));
  73. }
  74. if (\app\common\model\User::getByNickname($nickname)) {
  75. $this->error(__('Nickname already exist'));
  76. }
  77. if ($email && \app\common\model\User::getByEmail($email)) {
  78. $this->error(__('Email already exist'));
  79. }
  80. if ($mobile && \app\common\model\User::getByMobile($mobile)) {
  81. $this->error(__('Mobile already exist'));
  82. }
  83. $ip = request()->ip();
  84. $time = time();
  85. $data = [
  86. 'nickname' => preg_match("/^1[3-9]{1}\d{9}$/",$nickname) ? substr_replace($nickname,'****',3,4) : $nickname,
  87. 'username' => $username,
  88. 'password' => $password,
  89. 'email' => $email,
  90. 'mobile' => $mobile,
  91. 'level' => 1,
  92. 'score' => 0,
  93. 'avatar' => '',
  94. ];
  95. $params = array_merge($data, [
  96. 'group_id' => $params['group_id'],
  97. 'category_id' => $params['category_id'],
  98. 'salt' => Random::alnum(),
  99. 'jointime' => $time,
  100. 'joinip' => $ip,
  101. 'logintime' => $time,
  102. 'loginip' => $ip,
  103. 'prevtime' => $time,
  104. 'status' => $params['status'],
  105. ]);
  106. $params['password'] = $this->getEncryptPassword($password, $params['salt']);
  107. $params = array_merge($params, []);
  108. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  109. $params[$this->dataLimitField] = $this->auth->id;
  110. }
  111. $result = false;
  112. Db::startTrans();
  113. try {
  114. //是否采用模型验证
  115. // if ($this->modelValidate) {
  116. // $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  117. // $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  118. // $this->model->validateFailException(true)->validate($validate);
  119. // }
  120. $result = $this->model->allowField(true)->insertGetId($params);
  121. // 添加用户后创建对应空引导页数据
  122. $guide = new Guide();
  123. $guide->user_id = $result;
  124. $guide->image = '#';
  125. $guide->url = '#';
  126. $guide->save();
  127. Db::commit();
  128. } catch (ValidateException $e) {
  129. Db::rollback();
  130. $this->error($e->getMessage());
  131. } catch (PDOException $e) {
  132. Db::rollback();
  133. $this->error($e->getMessage());
  134. } catch (Exception $e) {
  135. Db::rollback();
  136. $this->error($e->getMessage());
  137. }
  138. if ($result !== false) {
  139. $this->success();
  140. } else {
  141. $this->error(__('No rows were inserted'));
  142. }
  143. }
  144. $this->error(__('Parameter %s can not be empty', ''));
  145. }
  146. $this->view->assign('groupList', build_select('row[group_id]', \app\admin\model\UserGroup::column('id,name'), '', ['class' => 'form-control selectpicker']));
  147. $this->view->assign('categoryList', build_select('row[category_id]', Category::where(['pid' => 0])->column('id,name'), '', ['class' => 'form-control selectpicker']));
  148. return parent::add();
  149. }
  150. /**
  151. * 编辑
  152. */
  153. public function edit($ids = null)
  154. {
  155. if ($this->request->isPost()) {
  156. $this->token();
  157. }
  158. $row = $this->model->get($ids);
  159. // $this->modelValidate = true;
  160. if (!$row) {
  161. $this->error(__('No Results were found'));
  162. }
  163. $this->view->assign('groupList', build_select('row[group_id]', \app\admin\model\UserGroup::column('id,name'), $row['group_id'], ['class' => 'form-control selectpicker']));
  164. $this->view->assign('categoryList', build_select('row[category_id]', Category::where(['pid' => 0])->column('id,name'), $row['category_id'], ['class' => 'form-control selectpicker']));
  165. return parent::edit($ids);
  166. }
  167. /**
  168. * 删除
  169. */
  170. public function del($ids = "")
  171. {
  172. if (!$this->request->isPost()) {
  173. $this->error(__("Invalid parameters"));
  174. }
  175. $ids = $ids ? $ids : $this->request->post("ids");
  176. $row = $this->model->get($ids);
  177. $this->modelValidate = true;
  178. if (!$row) {
  179. $this->error(__('No Results were found'));
  180. }
  181. Auth::instance()->delete($row['id']);
  182. $this->success();
  183. }
  184. /**
  185. * 获取密码加密后的字符串
  186. * @param string $password 密码
  187. * @param string $salt 密码盐
  188. * @return string
  189. */
  190. public function getEncryptPassword($password, $salt = '')
  191. {
  192. return md5(md5($password) . $salt);
  193. }
  194. }