UserService.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 (strpos($avatar, "temp")) {
  103. $data['avatar'] = save_image($avatar, 'user');
  104. } else {
  105. $data['avatar'] = str_replace(IMG_URL, "", $data['avatar']);
  106. }
  107. // 验证绑定谷歌验证码
  108. if(isset($data['google_code']) && $data['google_code']){
  109. $googleKey = $this->model->where(['username'=> $username])->value('google_key');
  110. if(empty($googleKey)){
  111. return message("谷歌密钥生成失败,请刷新重试", false);
  112. }
  113. if (!GoogleAuthenticator::CheckCode($googleKey,$data['google_code'])){
  114. return message("谷歌验证码错误", false);
  115. }
  116. $data['google_bind'] = 1;
  117. }
  118. $error = "";
  119. $result = $this->model->edit($data, $error);
  120. if (!$result) {
  121. return message($error, false);
  122. }
  123. // 删除已存在的用户角色关系数据
  124. $userRoleService = new UserRoleService();
  125. $userRoleService->deleteUserRole($result);
  126. // 插入用户角色关系数据
  127. $userRoleService->insertUserRole($result, $data['role_ids']);
  128. ActionLogModel::setTitle("修改管理员账号");
  129. ActionLogModel::record();
  130. return message();
  131. }
  132. /**
  133. * 获取用户信息
  134. * @param $userId 用户ID
  135. * @return array
  136. * @author laravel开发员
  137. * @since 2020/11/10
  138. */
  139. public function getUserInfo($userId)
  140. {
  141. $userInfo = $this->model->getInfo($userId);
  142. $userInfo['roles'] = [];
  143. $userInfo['authorities'] = [];
  144. // 权限节点列表
  145. $menuService = new MenuService();
  146. $permissionList = $menuService->getPermissionsList($userId);
  147. $userInfo['permissionList'] = $permissionList;
  148. return message("操作成功", true, $userInfo);
  149. }
  150. /**
  151. * 更新个人资料
  152. * @author laravel开发员
  153. * @since 2020/11/11
  154. */
  155. public function updateUserInfo($userId)
  156. {
  157. // 参数
  158. $param = request()->all();
  159. // 个人信息
  160. $data = [
  161. 'id' => $userId,
  162. 'realname' => $param['realname'],
  163. 'nickname' => $param['nickname'],
  164. 'gender' => $param['gender'],
  165. 'mobile' => $param['mobile'],
  166. 'email' => $param['email'],
  167. 'intro' => $param['intro'],
  168. ];
  169. // 头像处理
  170. $avatar = isset($param['avatar']) ? $param['avatar'] : "";
  171. if (strpos($avatar, "data:image") !== false) {
  172. $expData = explode(';', $avatar);
  173. $fileInfo = explode('/', $expData[0]);
  174. $fileExt = $fileInfo[1] == 'jpeg' ? 'jpg' : $fileInfo[1];
  175. // 文件存储路径
  176. $filePath = create_image_path("user", $fileExt);
  177. // 获取图片流
  178. $item = explode(',', $avatar);
  179. file_put_contents(ATTACHMENT_PATH . $filePath, base64_decode($item[1]));
  180. $data['avatar'] = $filePath;
  181. } else {
  182. $data['avatar'] = str_replace(IMG_URL, "", $param['avatar']);
  183. }
  184. $result = $this->model->edit($data);
  185. if (!$result) {
  186. return message("更新资料信息失败", false);
  187. }
  188. return message("更新资料信息成功");
  189. }
  190. /**
  191. * 更新密码
  192. * @param $userId 用户ID
  193. * @return array
  194. * @author laravel开发员
  195. * @since 2020/11/14
  196. */
  197. public function updatePwd($userId)
  198. {
  199. // 获取参数
  200. $param = request()->all();
  201. // 原始密码
  202. $oldPassword = trim(getter($param, "oldPassword"));
  203. if (!$oldPassword) {
  204. return message("旧密码不能为空", false);
  205. }
  206. // 新密码
  207. $newPassword = trim(getter($param, "newPassword"));
  208. if (!$newPassword) {
  209. return message("新密码不能为空", false);
  210. }
  211. $userInfo = $this->model->getInfo($userId);
  212. if (!$userInfo) {
  213. return message("用户信息不存在", false);
  214. }
  215. if ($userInfo['password'] != get_password($oldPassword . $userInfo['username'])) {
  216. return message("旧密码输入不正确", false);
  217. }
  218. // 设置新密码
  219. $userInfo['password'] = get_password($newPassword . $userInfo['username']);
  220. $result = $this->model->edit($userInfo);
  221. if (!$result) {
  222. return message("修改失败", false);
  223. }
  224. return message("修改成功");
  225. }
  226. /**
  227. * 重置密码
  228. * @return array
  229. * @since 2020/11/14
  230. * @author laravel开发员
  231. */
  232. public function resetPwd()
  233. {
  234. // 获取参数
  235. $param = request()->all();
  236. // 用户ID
  237. $userId = getter($param, "id");
  238. if (!$userId) {
  239. return message("用户ID不能为空", false);
  240. }
  241. $userInfo = $this->model->getInfo($userId);
  242. if (!$userInfo) {
  243. return message("用户信息不存在", false);
  244. }
  245. // 设置新密码
  246. $userInfo['password'] = get_password("123456" . $userInfo['username']);
  247. $result = $this->model->edit($userInfo);
  248. if (!$result) {
  249. return message("重置密码失败", false);
  250. }
  251. return message("重置密码成功");
  252. }
  253. }