Avatar.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace app\admin\controller\applys;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 用户头像审核
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Avatar extends Backend
  11. {
  12. /**
  13. * Avatar模型对象
  14. * @var \app\admin\model\applys\Avatar
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\applys\Avatar;
  21. $this->view->assign("statusList", $this->model->getStatusList());
  22. }
  23. public function import()
  24. {
  25. parent::import();
  26. }
  27. /**
  28. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  29. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  30. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  31. */
  32. /**
  33. * 查看
  34. */
  35. public function index()
  36. {
  37. //当前是否为关联查询
  38. $this->relationSearch = true;
  39. //设置过滤方法
  40. $this->request->filter(['strip_tags', 'trim']);
  41. if ($this->request->isAjax()) {
  42. //如果发送的来源是Selectpage,则转发到Selectpage
  43. if ($this->request->request('keyField')) {
  44. return $this->selectpage();
  45. }
  46. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  47. $list = $this->model
  48. ->with(['user'])
  49. ->where($where)
  50. ->order($sort, $order)
  51. ->paginate($limit);
  52. foreach ($list as $row) {
  53. $row->getRelation('user')->visible(['nickname','province_name','city_name']);
  54. }
  55. foreach ($list as $k => $v) {
  56. $list[$k]['city'] = $v['user']['province_name'].".".$v['user']['city_name'];
  57. }
  58. $result = array("total" => $list->total(), "rows" => $list->items());
  59. return json($result);
  60. }
  61. return $this->view->fetch();
  62. }
  63. /**
  64. * 编辑
  65. */
  66. public function edit($ids = null)
  67. {
  68. $row = $this->model->get($ids);
  69. if (!$row) {
  70. $this->error(__('No Results were found'));
  71. }
  72. $adminIds = $this->getDataLimitAdminIds();
  73. if (is_array($adminIds)) {
  74. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  75. $this->error(__('You have no permission'));
  76. }
  77. }
  78. if ($this->request->isPost()) {
  79. $params = $this->request->post("row/a");
  80. $params['status'] = 1;
  81. if ($params) {
  82. $params = $this->preExcludeFields($params);
  83. $result = false;
  84. Db::startTrans();
  85. try {
  86. // if($row['status'] == 0 && $row['status'] != $params['status']) {
  87. // if($params['status'] == 1) {
  88. \app\common\model\User::update(['avatar'=>$row['avatar']],['id'=>$row['user_id']]);
  89. $title = '头像审核成功!';
  90. $content = '恭喜您,您的头像修改审核成功!';
  91. \app\common\model\SysMsg::sendSysMsg($row['user_id'],2,$title,$content);
  92. // }
  93. // if($params['status'] == -1) {
  94. // \app\common\model\User::update(['avatar'=>$params['old_avatar']],['id'=>$params['user_id']]);
  95. // $title = '头像审核失败!';
  96. // $content = '非常抱歉,您的头像由于不符合平台规范,管理员已审核拒绝!';
  97. // \app\common\model\SysMsg::sendSysMsg($row['user_id'],2,$title,$content);
  98. // }
  99. // }
  100. //是否采用模型验证
  101. if ($this->modelValidate) {
  102. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  103. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  104. $row->validateFailException(true)->validate($validate);
  105. }
  106. $result = $row->allowField(true)->save($params);
  107. delUserInfo($row['user_id']);
  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. /**
  131. * 拒绝
  132. */
  133. public function del($ids = null)
  134. {
  135. $row = $this->model->get($ids);
  136. if (!$row) {
  137. $this->error(__('No Results were found'));
  138. }
  139. $adminIds = $this->getDataLimitAdminIds();
  140. if (is_array($adminIds)) {
  141. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  142. $this->error(__('You have no permission'));
  143. }
  144. }
  145. if ($this->request->isPost()) {
  146. $params = $this->request->post("row/a");
  147. $params['status'] = -1;
  148. if ($params) {
  149. $params = $this->preExcludeFields($params);
  150. $result = false;
  151. Db::startTrans();
  152. try {
  153. // if($row['status'] == 0 && $row['status'] != $params['status']) {
  154. // if($params['status'] == 1) {
  155. // \app\common\model\User::update(['avatar'=>$params['avatar']],['id'=>$params['user_id']]);
  156. // $title = '头像审核成功!';
  157. // $content = '恭喜您,您的头像修改审核成功!';
  158. // \app\common\model\SysMsg::sendSysMsg($row['user_id'],2,$title,$content);
  159. // }
  160. // if($params['status'] == -1) {
  161. \app\common\model\User::update(['avatar'=>$row['old_avatar']],['id'=>$row['user_id']]);
  162. $title = '头像审核失败!';
  163. $content = '非常抱歉,您的头像由于不符合平台规范,管理员已审核拒绝!';
  164. \app\common\model\SysMsg::sendSysMsg($row['user_id'],2,$title,$content);
  165. // }
  166. // }
  167. //是否采用模型验证
  168. if ($this->modelValidate) {
  169. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  170. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  171. $row->validateFailException(true)->validate($validate);
  172. }
  173. $result = $row->allowField(true)->save($params);
  174. delUserInfo($row['user_id']);
  175. Db::commit();
  176. } catch (ValidateException $e) {
  177. Db::rollback();
  178. $this->error($e->getMessage());
  179. } catch (PDOException $e) {
  180. Db::rollback();
  181. $this->error($e->getMessage());
  182. } catch (Exception $e) {
  183. Db::rollback();
  184. $this->error($e->getMessage());
  185. }
  186. if ($result !== false) {
  187. $this->success();
  188. } else {
  189. $this->error(__('No rows were updated'));
  190. }
  191. }
  192. $this->error(__('Parameter %s can not be empty', ''));
  193. }
  194. $this->view->assign("row", $row);
  195. return $this->view->fetch();
  196. }
  197. }