UserService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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\UserModel;
  13. use App\Services\BaseService;
  14. /**
  15. * 用户管理-服务类
  16. * @author laravel开发员
  17. * @since 2020/11/11
  18. * Class UserService
  19. * @package App\Services\Common
  20. */
  21. class UserService extends BaseService
  22. {
  23. // 静态对象
  24. protected static $instance = null;
  25. /**
  26. * 构造函数
  27. * @author laravel开发员
  28. * @since 2020/11/11
  29. * UserService constructor.
  30. */
  31. public function __construct()
  32. {
  33. $this->model = new UserModel();
  34. }
  35. /**
  36. * 静态入口
  37. * @return static|null
  38. */
  39. public static function make()
  40. {
  41. if (!self::$instance) {
  42. self::$instance = (new static());
  43. }
  44. return self::$instance;
  45. }
  46. /**
  47. * 获取用户列表
  48. * @return array
  49. * @since 2020/11/11
  50. * @author laravel开发员
  51. */
  52. public function getList()
  53. {
  54. $param = request()->all();
  55. // 查询条件
  56. $map = [];
  57. // 用户账号
  58. $username = getter($param, "username");
  59. if ($username) {
  60. $map[] = ["username", 'like', "%{$username}%"];
  61. }
  62. // 用户姓名
  63. $realname = getter($param, "realname");
  64. if ($realname) {
  65. $map[] = ['realname', 'like', "%{$realname}%"];
  66. }
  67. // 用户性别
  68. $gender = getter($param, "gender");
  69. if ($gender) {
  70. $map[] = ['gender', '=', $gender];
  71. }
  72. return parent::getList($map); // TODO: Change the autogenerated stub
  73. }
  74. /**
  75. * @param $params
  76. * @param int $pageSize
  77. * @return array
  78. */
  79. public function getDataList($params, $pageSize = 15)
  80. {
  81. $list = $this->model->from('user as a')
  82. ->where(['a.mark' => 1])
  83. ->where(function ($query) use($params){
  84. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  85. if($keyword){
  86. $query->where('a.realname','like',"%{$keyword}%");
  87. }
  88. $mobile = isset($params['mobile'])? $params['mobile'] : '';
  89. if($mobile){
  90. $query->where('a.mobile','like',"%{$mobile}%");
  91. }
  92. $status = isset($params['status'])? intval($params['status']) : 0;
  93. if($status){
  94. $query->where('a.status','=',$status);
  95. }
  96. $userType = isset($params['user_type'])? intval($params['user_type']) : 0;
  97. if($userType){
  98. $query->where('a.user_type','=',$userType);
  99. }
  100. $parentId = isset($params['parent_id'])? intval($params['parent_id']) : 0;
  101. if($parentId){
  102. $query->where('a.parent_id','=', $parentId);
  103. }
  104. })
  105. ->select(['a.*'])
  106. ->orderBy('a.create_time','desc')
  107. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  108. $list = $list? $list->toArray() :[];
  109. if($list){
  110. foreach($list['data'] as &$item){
  111. $item['create_time_text'] = isset($item['create_time']) && $item['create_time']? datetime($item['create_time'],'Y.m.d H:i') : '';
  112. $item['birthday'] = isset($item['birthday']) && $item['birthday']? datetime($item['birthday'],'Y-m-d') : '';
  113. $item['counts'] = MemberService::make()->getCountsByMarket($item['id']);
  114. }
  115. }
  116. return [
  117. 'pageSize'=> $pageSize,
  118. 'total'=>isset($list['total'])? $list['total'] : 0,
  119. 'list'=> isset($list['data'])? $list['data'] : []
  120. ];
  121. }
  122. /**
  123. * 添加或编辑用户
  124. * @return array
  125. * @since 2020/11/11
  126. * @author laravel开发员
  127. */
  128. public function edit()
  129. {
  130. // 请求参数
  131. $data = request()->all();
  132. // 用户名
  133. $username = trim($data['username']);
  134. // 密码
  135. $password = trim($data['password']);
  136. // 添加时设置密码
  137. if (empty($data['id'])) {
  138. $data['password'] = get_password($password . $username);
  139. // 用户名重复性验证
  140. $count = $this->model
  141. ->where("username", '=', $username)
  142. ->where("mark", "=", 1)
  143. ->count();
  144. if ($count > 0) {
  145. return message("系统中已存在相同的用户名", false);
  146. }
  147. } else {
  148. // 用户名重复性验证
  149. $count = $this->model
  150. ->where("username", '=', $username)
  151. ->where("id", "<>", $data['id'])
  152. ->where("mark", "=", 1)
  153. ->count();
  154. if ($count > 0) {
  155. return message("系统中已存在相同的用户名", false);
  156. }
  157. }
  158. // 头像处理
  159. if(isset($data['avatar'])){
  160. $avatar = isset($data['avatar']) ? trim($data['avatar']) : '';
  161. if (strpos($avatar, "temp")) {
  162. $data['avatar'] = save_image($avatar, 'user');
  163. } else {
  164. $data['avatar'] = str_replace(IMG_URL, "", $data['avatar']);
  165. }
  166. }
  167. $error = "";
  168. $data['birthday'] = isset($data['birthday']) && $data['birthday']? strtotime($data['birthday']) : 0;
  169. $result = $this->model->edit($data, $error);
  170. if (!$result) {
  171. return message($error, false);
  172. }
  173. // 删除已存在的用户角色关系数据
  174. $userRoleService = new UserRoleService();
  175. $userRoleService->deleteUserRole($result);
  176. // 插入用户角色关系数据
  177. $userRoleService->insertUserRole($result, $data['role_ids']);
  178. return message();
  179. }
  180. /**
  181. * 获取用户信息
  182. * @param $userId 用户ID
  183. * @return array
  184. * @author laravel开发员
  185. * @since 2020/11/10
  186. */
  187. public function getUserInfo($userId)
  188. {
  189. $userInfo = $this->model->getInfo($userId);
  190. $userInfo['roles'] = [];
  191. $userInfo['authorities'] = [];
  192. // 权限节点列表
  193. $menuService = new MenuService();
  194. $permissionList = $menuService->getPermissionsList($userId);
  195. $userInfo['permissionList'] = $permissionList;
  196. return message("操作成功", true, $userInfo);
  197. }
  198. /**
  199. * 更新个人资料
  200. * @author laravel开发员
  201. * @since 2020/11/11
  202. */
  203. public function updateUserInfo($userId)
  204. {
  205. // 参数
  206. $param = request()->all();
  207. // 个人信息
  208. $data = [
  209. 'id' => $userId,
  210. 'realname' => $param['realname'],
  211. 'nickname' => $param['nickname'],
  212. 'gender' => $param['gender'],
  213. 'mobile' => $param['mobile'],
  214. 'email' => $param['email'],
  215. 'intro' => $param['intro'],
  216. ];
  217. // 头像处理
  218. $avatar = isset($param['avatar']) ? $param['avatar'] : "";
  219. if (strpos($avatar, "data:image") !== false) {
  220. $expData = explode(';', $avatar);
  221. $fileInfo = explode('/', $expData[0]);
  222. $fileExt = $fileInfo[1] == 'jpeg' ? 'jpg' : $fileInfo[1];
  223. // 文件存储路径
  224. $filePath = create_image_path("user", $fileExt);
  225. // 获取图片流
  226. $item = explode(',', $avatar);
  227. file_put_contents(ATTACHMENT_PATH . $filePath, base64_decode($item[1]));
  228. $data['avatar'] = $filePath;
  229. } else {
  230. $data['avatar'] = str_replace(IMG_URL, "", $param['avatar']);
  231. }
  232. $result = $this->model->edit($data);
  233. if (!$result) {
  234. return message("更新资料信息失败", false);
  235. }
  236. return message("更新资料信息成功");
  237. }
  238. /**
  239. * 更新密码
  240. * @param $userId 用户ID
  241. * @return array
  242. * @author laravel开发员
  243. * @since 2020/11/14
  244. */
  245. public function updatePwd($userId)
  246. {
  247. // 获取参数
  248. $param = request()->all();
  249. // 原始密码
  250. $oldPassword = trim(getter($param, "oldPassword"));
  251. if (!$oldPassword) {
  252. return message("旧密码不能为空", false);
  253. }
  254. // 新密码
  255. $newPassword = trim(getter($param, "newPassword"));
  256. if (!$newPassword) {
  257. return message("新密码不能为空", false);
  258. }
  259. $userInfo = $this->model->getInfo($userId);
  260. if (!$userInfo) {
  261. return message("用户信息不存在", false);
  262. }
  263. if ($userInfo['password'] != get_password($oldPassword . $userInfo['username'])) {
  264. return message("旧密码输入不正确", false);
  265. }
  266. // 设置新密码
  267. $userInfo['password'] = get_password($newPassword . $userInfo['username']);
  268. $result = $this->model->edit($userInfo);
  269. if (!$result) {
  270. return message("修改失败", false);
  271. }
  272. return message("修改成功");
  273. }
  274. /**
  275. * 重置密码
  276. * @return array
  277. * @since 2020/11/14
  278. * @author laravel开发员
  279. */
  280. public function resetPwd()
  281. {
  282. // 获取参数
  283. $param = request()->all();
  284. // 用户ID
  285. $userId = getter($param, "id");
  286. if (!$userId) {
  287. return message("用户ID不能为空", false);
  288. }
  289. $userInfo = $this->model->getInfo($userId);
  290. if (!$userInfo) {
  291. return message("用户信息不存在", false);
  292. }
  293. // 设置新密码
  294. $userInfo['password'] = get_password("123456" . $userInfo['username']);
  295. $result = $this->model->edit($userInfo);
  296. if (!$result) {
  297. return message("重置密码失败", false);
  298. }
  299. return message("重置密码成功");
  300. }
  301. }