UserService.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\ActionLogModel;
  13. use App\Models\UserModel;
  14. use App\Services\BaseService;
  15. use App\Services\RedisService;
  16. use Earnp\GoogleAuthenticator\GoogleAuthenticator;
  17. /**
  18. * 用户管理-服务类
  19. * @author laravel开发员
  20. * @since 2020/11/11
  21. * Class UserService
  22. * @package App\Services\Common
  23. */
  24. class UserService extends BaseService
  25. {
  26. /**
  27. * 构造函数
  28. * @author laravel开发员
  29. * @since 2020/11/11
  30. * UserService constructor.
  31. */
  32. public function __construct()
  33. {
  34. $this->model = new UserModel();
  35. }
  36. /**
  37. * 获取用户列表
  38. * @return array
  39. * @since 2020/11/11
  40. * @author laravel开发员
  41. */
  42. public function getList()
  43. {
  44. $param = request()->all();
  45. // 查询条件
  46. $map = [];
  47. // 用户账号
  48. $username = getter($param, "username");
  49. if ($username) {
  50. $map[] = ["username", 'like', "%{$username}%"];
  51. }
  52. // 用户姓名
  53. $realname = getter($param, "realname");
  54. if ($realname) {
  55. $map[] = ['realname', 'like', "%{$realname}%"];
  56. }
  57. // 用户性别
  58. $gender = getter($param, "gender");
  59. if ($gender) {
  60. $map[] = ['gender', '=', $gender];
  61. }
  62. return parent::getList($map); // TODO: Change the autogenerated stub
  63. }
  64. /**
  65. * 添加或编辑用户
  66. * @return array
  67. * @since 2020/11/11
  68. * @author laravel开发员
  69. */
  70. public function edit()
  71. {
  72. // 请求参数
  73. $data = request()->all();
  74. // 用户名
  75. $username = trim($data['username']);
  76. // 密码
  77. $password = trim($data['password']);
  78. // 添加时设置密码
  79. if (empty($data['id'])) {
  80. $data['password'] = get_password($password . $username);
  81. // 用户名重复性验证
  82. $count = $this->model
  83. ->where("username", '=', $username)
  84. ->where("mark", "=", 1)
  85. ->count();
  86. if ($count > 0) {
  87. return message("系统中已存在相同的用户名", false);
  88. }
  89. } else {
  90. // 用户名重复性验证
  91. $count = $this->model
  92. ->where("username", '=', $username)
  93. ->where("id", "<>", $data['id'])
  94. ->where("mark", "=", 1)
  95. ->count();
  96. if ($count > 0) {
  97. return message("系统中已存在相同的用户名", false);
  98. }
  99. }
  100. // 头像处理
  101. $avatar = isset($data['avatar']) ? trim($data['avatar']) : '';
  102. if($avatar){
  103. if (strpos($avatar, "temp")) {
  104. $data['avatar'] = save_image($avatar, 'user');
  105. } else {
  106. $data['avatar'] = str_replace(IMG_URL, "", $data['avatar']);
  107. }
  108. }
  109. // 验证绑定谷歌验证码
  110. if(isset($data['google_code']) && $data['google_code']){
  111. $googleKey = $this->model->where(['username'=> $username])->value('google_key');
  112. if(empty($googleKey)){
  113. return message("谷歌密钥生成失败,请刷新重试", false);
  114. }
  115. if (!GoogleAuthenticator::CheckCode($googleKey,$data['google_code'])){
  116. return message("谷歌验证码错误", false);
  117. }
  118. $data['google_bind'] = 1;
  119. }
  120. $error = "";
  121. $result = $this->model->edit($data, $error);
  122. if (!$result) {
  123. return message($error, false);
  124. }
  125. // 删除已存在的用户角色关系数据
  126. $userRoleService = new UserRoleService();
  127. $userRoleService->deleteUserRole($result);
  128. // 插入用户角色关系数据
  129. $userRoleService->insertUserRole($result, $data['role_ids']);
  130. ActionLogModel::setTitle("修改管理员账号");
  131. ActionLogModel::record();
  132. return message();
  133. }
  134. /**
  135. * 获取用户信息
  136. * @param $userId 用户ID
  137. * @return array
  138. * @author laravel开发员
  139. * @since 2020/11/10
  140. */
  141. public function getUserInfo($userId)
  142. {
  143. $userInfo = $this->model->getInfo($userId);
  144. $userInfo['roles'] = [];
  145. $userInfo['authorities'] = [];
  146. // 权限节点列表
  147. $menuService = new MenuService();
  148. $permissionList = $menuService->getPermissionsList($userId);
  149. $userInfo['permissionList'] = $permissionList;
  150. return message("操作成功", true, $userInfo);
  151. }
  152. /**
  153. * 更新个人资料
  154. * @author laravel开发员
  155. * @since 2020/11/11
  156. */
  157. public function updateUserInfo($userId)
  158. {
  159. // 参数
  160. $param = request()->all();
  161. // 个人信息
  162. $data = [
  163. 'id' => $userId,
  164. 'realname' => $param['realname'],
  165. 'nickname' => $param['nickname'],
  166. 'gender' => $param['gender'],
  167. 'mobile' => $param['mobile'],
  168. 'email' => $param['email'],
  169. 'intro' => $param['intro'],
  170. ];
  171. // 头像处理
  172. $avatar = isset($param['avatar']) ? $param['avatar'] : "";
  173. if (strpos($avatar, "data:image") !== false) {
  174. $expData = explode(';', $avatar);
  175. $fileInfo = explode('/', $expData[0]);
  176. $fileExt = $fileInfo[1] == 'jpeg' ? 'jpg' : $fileInfo[1];
  177. // 文件存储路径
  178. $filePath = create_image_path("user", $fileExt);
  179. // 获取图片流
  180. $item = explode(',', $avatar);
  181. file_put_contents(ATTACHMENT_PATH . $filePath, base64_decode($item[1]));
  182. $data['avatar'] = $filePath;
  183. } else {
  184. $data['avatar'] = str_replace(IMG_URL, "", $param['avatar']);
  185. }
  186. $result = $this->model->edit($data);
  187. if (!$result) {
  188. return message("更新资料信息失败", false);
  189. }
  190. ActionLogModel::setTitle("修改管理用户信息");
  191. ActionLogModel::record();
  192. return message("更新资料信息成功");
  193. }
  194. /**
  195. * 更新密码
  196. * @param $userId 用户ID
  197. * @return array
  198. * @author laravel开发员
  199. * @since 2020/11/14
  200. */
  201. public function updatePwd($userId)
  202. {
  203. // 获取参数
  204. $param = request()->all();
  205. // 原始密码
  206. $oldPassword = trim(getter($param, "oldPassword"));
  207. if (!$oldPassword) {
  208. return message("旧密码不能为空", false);
  209. }
  210. // 新密码
  211. $newPassword = trim(getter($param, "newPassword"));
  212. if (!$newPassword) {
  213. return message("新密码不能为空", false);
  214. }
  215. $userInfo = $this->model->getInfo($userId);
  216. if (!$userInfo) {
  217. return message("用户信息不存在", false);
  218. }
  219. if ($userInfo['password'] != get_password($oldPassword . $userInfo['username'])) {
  220. return message("旧密码输入不正确", false);
  221. }
  222. // 设置新密码
  223. $userInfo['password'] = get_password($newPassword . $userInfo['username']);
  224. $result = $this->model->edit($userInfo);
  225. if (!$result) {
  226. return message("修改失败", false);
  227. }
  228. ActionLogModel::setTitle("修改管理用户密码");
  229. ActionLogModel::record();
  230. return message("修改成功");
  231. }
  232. /**
  233. * 重置密码
  234. * @return array
  235. * @since 2020/11/14
  236. * @author laravel开发员
  237. */
  238. public function resetPwd()
  239. {
  240. // 获取参数
  241. $param = request()->all();
  242. // 用户ID
  243. $userId = getter($param, "id");
  244. if (!$userId) {
  245. return message("用户ID不能为空", false);
  246. }
  247. $userInfo = $this->model->getInfo($userId);
  248. if (!$userInfo) {
  249. return message("用户信息不存在", false);
  250. }
  251. // 设置新密码
  252. $userInfo['password'] = get_password("123456" . $userInfo['username']);
  253. $result = $this->model->edit($userInfo);
  254. if (!$result) {
  255. return message("重置密码失败", false);
  256. }
  257. ActionLogModel::setTitle("重置管理账号密码");
  258. ActionLogModel::record();
  259. return message("重置密码成功");
  260. }
  261. }