User.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\admin\model\Message;
  4. use app\common\controller\Backend;
  5. use app\common\library\Auth;
  6. /**
  7. * 会员管理
  8. *
  9. * @icon fa fa-user
  10. */
  11. class User extends Backend
  12. {
  13. protected $relationSearch = true;
  14. protected $searchFields = 'u_id,username,nickname';
  15. /**
  16. * @var \app\admin\model\User
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = model('User');
  23. $typeList = [
  24. 'isCoolList' => $this->model->getIsCoolList(),
  25. 'isManagerList' => $this->model->getIsManagerList(),
  26. ];
  27. $this->view->assign($typeList);
  28. $this->assignconfig($typeList);
  29. }
  30. /**
  31. * 查看
  32. */
  33. public function index()
  34. {
  35. $this->relationSearch = true;
  36. //设置过滤方法
  37. $this->request->filter(['strip_tags', 'trim']);
  38. if ($this->request->isAjax()) {
  39. //如果发送的来源是Selectpage,则转发到Selectpage
  40. if ($this->request->request('keyField')) {
  41. return $this->selectpage();
  42. }
  43. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  44. $list = $this->model
  45. ->with(['noble','preuser','auth','age'])
  46. ->where($where)
  47. ->order($sort, $order)
  48. ->paginate($limit);
  49. foreach ($list as $k => $v) {
  50. $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
  51. $v->hidden(['password', 'salt']);
  52. $v->getRelation('age')->visible(['name']);
  53. }
  54. $result = array("total" => $list->total(), "rows" => $list->items());
  55. return json($result);
  56. }
  57. return $this->view->fetch();
  58. }
  59. /**
  60. * 添加
  61. */
  62. public function add()
  63. {
  64. if ($this->request->isPost()) {
  65. $this->token();
  66. }
  67. return parent::add();
  68. }
  69. /**
  70. * 编辑
  71. */
  72. public function edit($ids = null)
  73. {
  74. if ($this->request->isPost()) {
  75. $this->token();
  76. }
  77. $row = $this->model->get($ids);
  78. $this->modelValidate = true;
  79. if (!$row) {
  80. $this->error(__('No Results were found'));
  81. }
  82. return parent::edit($ids);
  83. }
  84. /**
  85. * 删除
  86. */
  87. public function del($ids = "")
  88. {
  89. if (!$this->request->isPost()) {
  90. $this->error(__("Invalid parameters"));
  91. }
  92. $ids = $ids ? $ids : $this->request->post("ids");
  93. $row = $this->model->get($ids);
  94. $this->modelValidate = true;
  95. if (!$row) {
  96. $this->error(__('No Results were found'));
  97. }
  98. Auth::instance()->delete($row['id']);
  99. $this->success();
  100. }
  101. /**
  102. * 详情
  103. * @param null $ids
  104. * @return string
  105. * @throws \think\Exception
  106. * @throws \think\exception\DbException
  107. */
  108. public function detail($ids = null)
  109. {
  110. /* 判断数据是否存在*/
  111. $row = $this->model->get($ids);
  112. if (!$row) {
  113. $this->error(__('No Results were found'));
  114. }
  115. /* 判断是否有权限访问*/
  116. $adminIds = $this->getDataLimitAdminIds();
  117. if (is_array($adminIds)) {
  118. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  119. $this->error(__('You have no permission'));
  120. }
  121. }
  122. $this->view->assign("row", $row);
  123. return $this->view->fetch();
  124. }
  125. /**
  126. * 编辑
  127. */
  128. public function infocheck($ids = null)
  129. {
  130. $row = $this->model->get($ids);
  131. if (!$row) {
  132. $this->error(__('No Results were found'));
  133. }
  134. $adminIds = $this->getDataLimitAdminIds();
  135. if (is_array($adminIds)) {
  136. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  137. $this->error(__('You have no permission'));
  138. }
  139. }
  140. if ($this->request->isPost()) {
  141. $params = $this->request->post("row/a");
  142. if (!$params) {
  143. $this->error(__('Parameter %s can not be empty', ''));
  144. }
  145. $params = $this->preExcludeFields($params);
  146. $result = false;
  147. try {
  148. //是否采用模型验证
  149. if ($this->modelValidate) {
  150. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  151. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  152. $row->validateFailException(true)->validate($validate);
  153. }
  154. $nickStatusStr = $statusStr = '';
  155. if (isset($params['check_nick_status'])) {
  156. if ($params['check_nick_status'] == 1) {//审核通过 更新昵称
  157. !empty($params['pre_nickname']) && $params['nickname'] = $params['pre_nickname'];
  158. $params['pre_nickname'] = '';
  159. $nickStatusStr = '昵称通过';
  160. } else {//审核拒绝 清空新昵称
  161. $params['pre_nickname'] = '';
  162. $nickStatusStr = '昵称拒绝';
  163. }
  164. }
  165. if (isset($params['check_status'])) {
  166. if ($params['check_status'] == 1) {//审核通过 更新头像
  167. !empty($params['pre_avatar']) && $params['avatar'] = $params['pre_avatar'];
  168. $params['pre_avatar'] = '';
  169. $statusStr = '头像通过';
  170. } else {//审核拒绝 清空新头像
  171. $params['pre_avatar'] = '';
  172. $statusStr = '头像拒绝';
  173. }
  174. }
  175. $result = $row->allowField(true)->save($params);
  176. } catch (ValidateException|PDOException|Exception $e) {
  177. $this->error($e->getMessage());
  178. }
  179. if ($result == false) {
  180. $this->error(__('No rows were updated'));
  181. }
  182. if (!empty($nickStatusStr) || !empty($statusStr)) {
  183. //通过发消息
  184. $title = '用户信息变更审核通知';
  185. $content = '您申请的'.$nickStatusStr.$statusStr;
  186. Message::addMessage($row['id'],$title,$content);
  187. }
  188. $this->success();
  189. }
  190. $checkStatusList = [1=>'通过', 0=>'拒绝'];
  191. $showNickname = $showAvatar = 0;
  192. if (!empty($row['pre_nickname']) && $row['pre_nickname'] != $row['nickname']) {
  193. $showNickname = 1;
  194. }
  195. if (!empty($row['pre_avatar']) && $row['pre_avatar'] != $row['avatar']) {
  196. $showAvatar = 1;
  197. }
  198. $this->view->assign([
  199. 'row' => $row,
  200. 'checkStatusList' => $checkStatusList,
  201. 'showNickname' => $showNickname,
  202. 'showAvatar' => $showAvatar,
  203. ]);
  204. return $this->view->fetch();
  205. }
  206. }