User.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\admin\model\Message;
  4. use app\admin\model\UserPower;
  5. use app\common\controller\Backend;
  6. use app\common\library\Auth;
  7. use fast\Random;
  8. use think\Db;
  9. use think\Exception;
  10. use think\exception\PDOException;
  11. use think\exception\ValidateException;
  12. /**
  13. * 会员管理
  14. *
  15. * @icon fa fa-user
  16. */
  17. class User extends Backend
  18. {
  19. protected $relationSearch = true;
  20. protected $searchFields = 'u_id,username,nickname';
  21. /**
  22. * @var \app\admin\model\User
  23. */
  24. protected $model = null;
  25. public function _initialize()
  26. {
  27. parent::_initialize();
  28. $this->model = model('User');
  29. $typeList = [
  30. 'genderList' => $this->model->getGenderList(),
  31. 'isCoolList' => $this->model->getIsCoolList(),
  32. 'isManagerList' => $this->model->getIsManagerList(),
  33. ];
  34. $this->view->assign($typeList);
  35. $this->assignconfig($typeList);
  36. }
  37. /**
  38. * 查看
  39. */
  40. public function index()
  41. {
  42. $this->relationSearch = true;
  43. //设置过滤方法
  44. $this->request->filter(['strip_tags', 'trim']);
  45. if ($this->request->isAjax()) {
  46. //如果发送的来源是Selectpage,则转发到Selectpage
  47. if ($this->request->request('keyField')) {
  48. return $this->selectpage();
  49. }
  50. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  51. $list = $this->model
  52. ->with(['noble','preuser','auth','age'])
  53. ->where($where)
  54. ->order($sort, $order)
  55. ->paginate($limit);
  56. foreach ($list as $k => $v) {
  57. $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
  58. $v->hidden(['password', 'salt']);
  59. $v->getRelation('age')->visible(['name']);
  60. }
  61. $result = array("total" => $list->total(), "rows" => $list->items());
  62. return json($result);
  63. }
  64. return $this->view->fetch();
  65. }
  66. /**
  67. * 添加
  68. */
  69. /**
  70. * 添加
  71. */
  72. public function add()
  73. {
  74. if ($this->request->isPost()) {
  75. $params = $this->request->post("row/a");
  76. $params = $this->preExcludeFields($params);
  77. if (!$params) {
  78. $this->error(__('Parameter %s can not be empty', ''));
  79. }
  80. $result = false;
  81. Db::startTrans();
  82. try {
  83. //是否采用模型验证
  84. if ($this->modelValidate) {
  85. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  86. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  87. $this->model->validateFailException(true)->validate($validate);
  88. }
  89. if (empty($params['avatar'])) {
  90. $params['avatar'] = '/assets/img/default_avatar.png';
  91. }
  92. $ids = $this->model->column("u_id");
  93. $invite_no = $this->model->column("invite_no");
  94. $params['u_id'] = $this->model->getUinqueId(8, [$ids]);
  95. $params['invite_no'] = $this->model->getUinqueNo(8, $invite_no);
  96. if (empty($params['nickname'])) {
  97. $params['nickname'] = 'gg_'.$params['u_id'];
  98. }
  99. $params['image'] = '/assets/img/default_avatar.png';
  100. $params['username'] = $params['mobile'];
  101. $params['status'] = 'normal';
  102. $params['salt'] = Random::alnum();
  103. $params['has_info'] = 1;
  104. $result = $this->model->allowField(true)->save($params);
  105. $userId = $this->model->id;
  106. $userPower = new UserPower();
  107. $userPowerData['user_id'] = $userId;
  108. $userPowerRes = $userPower->insertGetId($userPowerData);
  109. if (!$userPowerRes) {
  110. throw new Exception('创建用户权限失败');
  111. }
  112. } catch (ValidateException|PDOException|Exception $e) {
  113. Db::rollback();
  114. $this->error($e->getMessage());
  115. }
  116. if ($result == false) {
  117. $this->error(__('No rows were inserted'));
  118. }
  119. Db::commit();
  120. $this->success();
  121. }
  122. return $this->view->fetch();
  123. }
  124. /**
  125. * 编辑
  126. */
  127. public function edit($ids = null)
  128. {
  129. $row = $this->model->get($ids);
  130. if (!$row) {
  131. $this->error(__('No Results were found'));
  132. }
  133. $adminIds = $this->getDataLimitAdminIds();
  134. if (is_array($adminIds)) {
  135. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  136. $this->error(__('You have no permission'));
  137. }
  138. }
  139. if ($this->request->isPost()) {
  140. $params = $this->request->post("row/a");
  141. if (!$params) {
  142. $this->error(__('Parameter %s can not be empty', ''));
  143. }
  144. $params = $this->preExcludeFields($params);
  145. $result = false;
  146. try {
  147. //是否采用模型验证
  148. if ($this->modelValidate) {
  149. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  150. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  151. $row->validateFailException(true)->validate($validate);
  152. }
  153. if (!empty($params['u_id'])) {
  154. $userWhere['u_id'] = $params['u_id'];
  155. $user = $this->model->where($userWhere)->find();
  156. if (!empty($user)) {
  157. throw new Exception('前端用户ID已存在');
  158. }
  159. }
  160. if (!empty($params['mobile'])) {
  161. $userWhere['mobile'] = $params['mobile'];
  162. $user = $this->model->where($userWhere)->find();
  163. if (!empty($user)) {
  164. throw new Exception('手机号已存在');
  165. }
  166. }
  167. $result = $row->allowField(true)->save($params);
  168. } catch (ValidateException|PDOException|Exception $e) {
  169. $this->error($e->getMessage());
  170. }
  171. if ($result == false) {
  172. $this->error(__('No rows were updated'));
  173. }
  174. $this->success();
  175. }
  176. $this->view->assign("row", $row);
  177. return $this->view->fetch();
  178. }
  179. /**
  180. * 删除
  181. */
  182. public function del($ids = "")
  183. {
  184. if (!$this->request->isPost()) {
  185. $this->error(__("Invalid parameters"));
  186. }
  187. $ids = $ids ? $ids : $this->request->post("ids");
  188. $row = $this->model->get($ids);
  189. $this->modelValidate = true;
  190. if (!$row) {
  191. $this->error(__('No Results were found'));
  192. }
  193. Auth::instance()->delete($row['id']);
  194. $this->success();
  195. }
  196. /**
  197. * 详情
  198. * @param null $ids
  199. * @return string
  200. * @throws \think\Exception
  201. * @throws \think\exception\DbException
  202. */
  203. public function detail($ids = null)
  204. {
  205. /* 判断数据是否存在*/
  206. $row = $this->model->get($ids);
  207. if (!$row) {
  208. $this->error(__('No Results were found'));
  209. }
  210. /* 判断是否有权限访问*/
  211. $adminIds = $this->getDataLimitAdminIds();
  212. if (is_array($adminIds)) {
  213. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  214. $this->error(__('You have no permission'));
  215. }
  216. }
  217. $this->view->assign("row", $row);
  218. return $this->view->fetch();
  219. }
  220. /**
  221. * 编辑
  222. */
  223. public function infocheck($ids = null)
  224. {
  225. $row = $this->model->get($ids);
  226. if (!$row) {
  227. $this->error(__('No Results were found'));
  228. }
  229. $adminIds = $this->getDataLimitAdminIds();
  230. if (is_array($adminIds)) {
  231. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  232. $this->error(__('You have no permission'));
  233. }
  234. }
  235. if ($this->request->isPost()) {
  236. $params = $this->request->post("row/a");
  237. if (!$params) {
  238. $this->error(__('Parameter %s can not be empty', ''));
  239. }
  240. $params = $this->preExcludeFields($params);
  241. $result = false;
  242. try {
  243. //是否采用模型验证
  244. if ($this->modelValidate) {
  245. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  246. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  247. $row->validateFailException(true)->validate($validate);
  248. }
  249. $nickStatusStr = $statusStr = '';
  250. if (isset($params['check_nick_status'])) {
  251. if ($params['check_nick_status'] == 1) {//审核通过 更新昵称
  252. !empty($params['pre_nickname']) && $params['nickname'] = $params['pre_nickname'];
  253. $params['pre_nickname'] = '';
  254. $nickStatusStr = '昵称通过';
  255. } else {//审核拒绝 清空新昵称
  256. $params['pre_nickname'] = '';
  257. $nickStatusStr = '昵称拒绝';
  258. }
  259. }
  260. if (isset($params['check_status'])) {
  261. if ($params['check_status'] == 1) {//审核通过 更新头像
  262. !empty($params['pre_avatar']) && $params['avatar'] = $params['pre_avatar'];
  263. $params['pre_avatar'] = '';
  264. $statusStr = '头像通过';
  265. } else {//审核拒绝 清空新头像
  266. $params['pre_avatar'] = '';
  267. $statusStr = '头像拒绝';
  268. }
  269. }
  270. $result = $row->allowField(true)->save($params);
  271. } catch (ValidateException|PDOException|Exception $e) {
  272. $this->error($e->getMessage());
  273. }
  274. if ($result == false) {
  275. $this->error(__('No rows were updated'));
  276. }
  277. if (!empty($nickStatusStr) || !empty($statusStr)) {
  278. //通过发消息
  279. $title = '用户信息变更审核通知';
  280. $content = '您申请的'.$nickStatusStr.$statusStr;
  281. Message::addMessage($row['id'],$title,$content);
  282. }
  283. $this->success();
  284. }
  285. $checkStatusList = [1=>'通过', 0=>'拒绝'];
  286. $showNickname = $showAvatar = 0;
  287. if (!empty($row['pre_nickname']) && $row['pre_nickname'] != $row['nickname']) {
  288. $showNickname = 1;
  289. }
  290. if (!empty($row['pre_avatar']) && $row['pre_avatar'] != $row['avatar']) {
  291. $showAvatar = 1;
  292. }
  293. $this->view->assign([
  294. 'row' => $row,
  295. 'checkStatusList' => $checkStatusList,
  296. 'showNickname' => $showNickname,
  297. 'showAvatar' => $showAvatar,
  298. ]);
  299. return $this->view->fetch();
  300. }
  301. }