User.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\AdminBaseController;
  4. use app\common\model\UserFollowModel;
  5. use app\common\model\UserModel;
  6. use app\common\validate\UserValidate;
  7. use think\exception\ValidateException;
  8. class User extends AdminBaseController
  9. {
  10. /**
  11. * 用户列表
  12. */
  13. public function index()
  14. {
  15. $gender_list = UserModel::GENDER;
  16. $gender_list[0] = '全部';
  17. return view('', [
  18. 'status_list' => UserModel::STATUS,
  19. 'gender_list' => $gender_list,
  20. ]);
  21. }
  22. public function listUser()
  23. {
  24. $map = $this->dealEqualInput($this->dealLikeInput([], ['keywords' => 'account|nickname|realname', 'mobile']), ['gender', 'status']);
  25. $list = UserModel::where($map)
  26. ->order('id', 'desc')
  27. ->limit(input('limit'))
  28. ->page(input('page'))
  29. ->append(['status_text', 'gender_text'])->select();
  30. $count = UserModel::where($map)->count();
  31. if ($count == 0) {
  32. ajax_return(1, '未查询到数据');
  33. }
  34. list_return($list, $count);
  35. }
  36. /**
  37. * 用户跟进记录
  38. */
  39. public function follow()
  40. {
  41. $id = input('id/d');
  42. $user = UserModel::find($id);
  43. $list = UserFollowModel::where('user_id', $id)->order('id', 'desc')->limit(100)->select();
  44. return view('', [
  45. 'user' => $user,
  46. 'list' => $list,
  47. 'type_list' => UserFollowModel::TYPE,
  48. ]);
  49. }
  50. public function editFollow()
  51. {
  52. $id = input('id/d', 0);
  53. $user = UserModel::find($id);
  54. if (empty($user)) {
  55. ajax_return(1, '用户信息不存在。');
  56. }
  57. UserFollowModel::create([
  58. 'user_id' => $id,
  59. 'type' => input('type/d', 1),
  60. 'remark' => input('remark/s', ""),
  61. ]);
  62. ajax_return();
  63. }
  64. /**
  65. * 编辑用户
  66. */
  67. public function userForm()
  68. {
  69. $id = input('id/d', 0);
  70. $info = UserModel::find($id);
  71. return view('', [
  72. 'info' => $info,
  73. 'gender_list' => UserModel::GENDER,
  74. 'status_list' => UserModel::STATUS,
  75. ]);
  76. }
  77. public function editUser()
  78. {
  79. $data = input('post.');
  80. try {
  81. validate(UserValidate::class)->check($data);
  82. } catch (ValidateException $e) {
  83. ajax_return(1, $e->getError());
  84. }
  85. //用户名
  86. $check_account_where = [['account', '=', $data['account']]];
  87. if (!empty($data['id'])) {
  88. $check_account_where[] = ['id', '<>', $data['id']];
  89. }
  90. $check_account = UserModel::where($check_account_where)->find();
  91. if (!empty($check_account)) {
  92. ajax_return(1, '用户名已存在');
  93. }
  94. //手机号
  95. $check_mobile_where = [['mobile', '=', $data['mobile']]];
  96. if (!empty($data['id'])) {
  97. $check_mobile_where[] = ['id', '<>', $data['id']];
  98. }
  99. $check_mobile = UserModel::where($check_mobile_where)->find();
  100. if (!empty($check_mobile)) {
  101. ajax_return(1, '手机号已存在');
  102. }
  103. //密码
  104. if (empty($data['id']) && empty($data['password'])) {
  105. ajax_return(1,'请输入一个初始密码');
  106. }
  107. if (empty($data['password'])) {
  108. unset($data['password']);
  109. } else {
  110. $data['salt'] = rand_str();
  111. $data['password'] = md5(md5($data['salt']) . $data['password']);
  112. }
  113. if (empty($data['id'])) {
  114. UserModel::create($data);
  115. } else {
  116. UserModel::update($data, ['id' => $data['id']]);
  117. }
  118. ajax_return();
  119. }
  120. }