User.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 中闽 < 1464674022@qq.com >
  5. * Date: 2019/12/5
  6. * Time: 17:44
  7. */
  8. namespace app\admin\controller;
  9. use app\admin\controller\base\Permissions;
  10. use app\common\model\PointLog as pointLogModel;
  11. use app\common\model\User as userModel;
  12. use think\Db;
  13. class User extends Permissions
  14. {
  15. public function index()
  16. {
  17. if ($this->request->isAjax()) {
  18. $post = $this->request->param();
  19. $where = [];
  20. if (isset($post['ids']) and !empty($post['ids'])) {
  21. $where['id'] = ['in', $post['ids']];
  22. }
  23. if (isset($post['pid']) and !empty($post['pid'])) {
  24. $where['pid'] = intval($post['pid']);
  25. }
  26. if (isset($post["level"]) and "" != $post["level"]) {
  27. $where["level"] = $post["level"];
  28. }
  29. if (isset($post['nickname']) and !empty($post['nickname'])) {
  30. $where['nickname'] = ['like', '%' . $post['nickname'] . '%'];
  31. }
  32. if (isset($post['user_type']) and "" != $post['user_type']) {
  33. $where['user_type'] = $post['user_type'];
  34. }
  35. if (isset($post['user_cate']) and "" != $post['user_cate']) {
  36. $where['user_cate'] = $post['user_cate'];
  37. }
  38. if (isset($post['ip']) and !empty($post['ip'])) {
  39. $where['ip'] = $post['ip'];
  40. }
  41. if (isset($post['create_time']) and !empty($post['create_time'])) {
  42. $timerang = explode(' - ', $post['create_time']);
  43. $min_time = strtotime($timerang[0]);
  44. $max_time = strtotime($timerang[1]);
  45. $where['create_time'] = [['>=', $min_time], ['<=', $max_time]];
  46. }
  47. $model = new userModel();
  48. $count = $model->where($where)->count();
  49. $data = $model->where($where)->page($post['page']??0, $post['limit']??15)->order('id desc')->select();
  50. foreach ($data as $k => $v) {
  51. $v['user_cate_text'] = $v->user_cate_text;
  52. $v['user_type_text'] = $v->user_type_text;
  53. $v['status_text'] = $v->status_text;
  54. $value['sex_text'] = $v->sex_text;
  55. $data[$k] = $v;
  56. }
  57. return array('code' => 0, 'count' => $count, 'data' => $data);
  58. } else {
  59. return $this->fetch();
  60. }
  61. }
  62. public function publish()
  63. {
  64. $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
  65. $model = new userModel();
  66. $post = $this->request->post();
  67. if (!$this->request->isPost()) {
  68. $this->assign('user_cates', userModel::USER_CATES);
  69. $this->assign('user_types', userModel::USER_TYPES);
  70. } else {
  71. $validate = new \think\Validate([
  72. ['passport', 'require', '账号不能为空'],
  73. ['passport', 'max:50', '账号长度要小于50'],
  74. ['passport', 'unique:user', '该账号已经存在'],
  75. ]);
  76. if (!$validate->check($post)) {
  77. $this->error('提交失败:' . $validate->getError());
  78. }
  79. }
  80. if ($id > 0) {
  81. //修改操作
  82. if ($this->request->isPost()) {
  83. $user = $model->where('id', $id)->find();
  84. if (empty($user)) {
  85. $this->error('id不正确');
  86. }
  87. unset($post['passport']);
  88. if (false == $model->allowField(true)->save($post, ['id' => $id])) {
  89. $this->error('修改失败' . $model->getError());
  90. } else {
  91. $this->success('修改成功', 'index');
  92. }
  93. } else {
  94. $user = $model->where('id', $id)->find();
  95. if (!empty($user)) {
  96. $this->assign('user', $user);
  97. return $this->fetch();
  98. } else {
  99. $this->error('id不正确');
  100. }
  101. }
  102. } else {
  103. //新增操作
  104. if ($this->request->isPost()) {
  105. $post = $this->request->post();
  106. $post['ip'] = $this->request->ip();
  107. $post['create_time'] = time();
  108. $post['login_time'] = time();
  109. if (false == $model->allowField(true)->save($post)) {
  110. $this->error('添加失败');
  111. } else {
  112. $this->success('添加成功', 'index');
  113. }
  114. } else {
  115. return $this->fetch();
  116. }
  117. }
  118. }
  119. public function delete()
  120. {
  121. if ($this->request->isAjax()) {
  122. $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
  123. if (false == Db::name('user')->where('id', $id)->delete()) {
  124. $this->error('删除失败');
  125. } else {
  126. $this->success('删除成功', 'index');
  127. }
  128. }
  129. }
  130. public function status()
  131. {
  132. if ($this->request->isPost()) {
  133. $post = $this->request->post();
  134. if (false == Db::name('user')->where('id', $post['id'])->update(['status' => $post['status']])) {
  135. $this->error('设置失败');
  136. } else {
  137. $this->success('设置成功', 'index');
  138. }
  139. }
  140. }
  141. public function setPoint()
  142. {
  143. if ($this->request->isAjax()) {
  144. $post = $this->request->param();
  145. $ids = $post['ids'];
  146. $value = abs($this->request->param('value', 0, 'intval'));
  147. if (empty($value)) {
  148. $this->error("请输入积分数量");
  149. }
  150. $type = $post['type']??pointLogModel::TYPE_ADMIN_UPDATE;
  151. $action = $post['action']??1;
  152. $model = new userModel();
  153. $users = $model->where('id', 'in', $ids)->select();
  154. // 启动事务
  155. Db::startTrans();
  156. try {
  157. foreach ($users as $user) {
  158. $pointLog = new pointLogModel();
  159. $final = $action == 1 ? $user->point + $value : $user->point - $value;
  160. $log = [
  161. "user_id" => $user->id,
  162. "before" => $user->point,
  163. "symbol" => $action == 1 ? '+' : '-',
  164. "change" => $value,
  165. "final" => $final,
  166. "type" => $type,
  167. "remark" => "管理员" . session(self::ADMIN_ID) . ':' . session(self::ADMIN_NAME) . "操作",
  168. ];
  169. if (!$pointLog->save($log)) {
  170. throw new \Exception("insert point_log fail");
  171. }
  172. $user->point = $final;
  173. $user->save();
  174. }
  175. // 提交事务
  176. Db::commit();
  177. } catch (\Exception $e) {
  178. Db::rollback();
  179. $this->error("修改失败" . $e->getMessage());
  180. }
  181. $this->success('修改成功');
  182. }
  183. }
  184. //重置密码
  185. public function resetpass()
  186. {
  187. if ($this->request->isAjax()) {
  188. $id = $this->request->param('id', 0, 'intval');
  189. if (false == Db::name('user')->where('id', $id)->update(['password' => password(123456)])) {
  190. $this->error('重置失败');
  191. } else {
  192. $this->success('重置成功', 'index');
  193. }
  194. }
  195. }
  196. }