UserService.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. <?php
  2. namespace app\wechat\service;
  3. use app\model\Consultant;
  4. use app\model\MarketCustomer;
  5. use app\model\MarketCustomerFollow;
  6. use app\model\SysDept;
  7. use Carbon\Carbon;
  8. use support\Db;
  9. use support\exception\BusinessException;
  10. use Tinywan\Jwt\JwtToken;
  11. class UserService
  12. {
  13. /**
  14. * Notes: 获取所有顾问
  15. * User: yb
  16. * Date: 2024/8/15
  17. * Time: 11:51
  18. * @param $params
  19. */
  20. public static function getAll($params)
  21. {
  22. $ids = self::getIds(1);
  23. $where = [
  24. [function($query) use ($ids) {
  25. $query->whereIn('id', $ids);
  26. }]
  27. ];
  28. if (!empty($params['not_id'])) {
  29. $where[] = ['id', '<>', $params['not_id']];
  30. }
  31. //获取所有团队
  32. $teamKeys = TeamService::getTeams()->pluck('dept_name', 'dept_id');
  33. $list = Consultant::where($where)->select(['id','name','mobile','gender','status','dept_id','created_at'])->orderBy('created_at', 'desc')->get();
  34. if (!empty($list)) {
  35. foreach ($list as $item) {
  36. $item->dept_name = $teamKeys[$item->dept_id] ?? '';
  37. }
  38. }
  39. return json_success('请求成功', $list);
  40. }
  41. /**
  42. * Notes: 团队成员
  43. * User: yb
  44. * Date: 2024/8/14
  45. * Time: 15:34
  46. * @param $params
  47. */
  48. public static function index($params)
  49. {
  50. $page = $params['page'] ?? 1;
  51. $size = $params['size'] ?? 10;
  52. $ids = self::getIds(1);
  53. $where = [
  54. [function($query) use ($ids) {
  55. $query->whereIn('id', $ids);
  56. }]
  57. ];
  58. if (!empty($params['status'])) {
  59. $where[] = ['status', '=', $params['status']];
  60. }
  61. if (!empty($params['consultant'])) {
  62. $keywords = $params['consultant'];
  63. $where[] = [function($query) use ($keywords) {
  64. $query->orWhere('name', 'like', "%{$keywords}%")->orWhere('mobile', 'like', "%{$keywords}%");
  65. }];
  66. }
  67. if (!empty($params['dept_id'])) {
  68. $deptIds = $params['dept_id'];
  69. $deptId = end($deptIds);
  70. $where[] = ['dept_id', '=', $deptId];
  71. }
  72. if (!empty($params['created_at'])) {
  73. $datetime = $params['created_at'];
  74. $startTime = strtotime($datetime['start'].' 00:00:00');
  75. $endTime = strtotime($datetime['end'].' 23:59:59');
  76. $where[] = [function($query) use ($startTime, $endTime) {
  77. $query->whereBetween('created_at', [$startTime, $endTime]);
  78. }];
  79. }
  80. $paginator = Consultant::where($where)->orderBy('created_at', 'desc')->paginate($size, ['id','name','mobile','gender','status','dept_id','created_at'], 'page', $page);
  81. $total = $paginator->total();
  82. $items = $paginator->items();
  83. $data = [
  84. 'total' => $total,
  85. 'rows' => $items
  86. ];
  87. return json_success('success', $data);
  88. }
  89. /**
  90. * Notes: 删除成员
  91. * User: yb
  92. * Date: 2024/8/15
  93. * Time: 11:39
  94. * @param $id
  95. */
  96. public static function del($id)
  97. {
  98. if(MarketCustomer::where('consultant_id', $id)->exists()) {
  99. return json_fail('成员下存在客户,请移交客户后删除成员');
  100. }
  101. $result = Consultant::where('id', $id)->delete();
  102. if ($result) {
  103. return json_success('删除成功');
  104. } else {
  105. return json_fail('删除失败');
  106. }
  107. }
  108. /**
  109. * Notes: 成员详情
  110. * User: yb
  111. * Date: 2024/8/15
  112. * Time: 10:19
  113. * @param $id
  114. */
  115. public static function getInfoById($id)
  116. {
  117. $info = Consultant::firstWhere(['id' => $id]);
  118. if (empty($info)) {
  119. return json_fail('成员信息不存在');
  120. }
  121. return json_success('请求成功', $info);
  122. }
  123. /**
  124. * Notes: 编辑成员
  125. * User: yb
  126. * Date: 2024/8/15
  127. * Time: 10:28
  128. * @param $params
  129. */
  130. public static function updateById($params)
  131. {
  132. if (empty($params['id'])) {
  133. return json_fail('成员id不能为空');
  134. }
  135. //查找员工信息
  136. $info = Consultant::find($params['id']);
  137. $oldDeptId = $info->dept_id;
  138. if (empty($info)) {
  139. return json_fail('成员不存在');
  140. }
  141. if (!self::checkMobile($params['mobile'])) {
  142. return json_fail('请输入正确的手机号');
  143. }
  144. //查询员工是否已被注册
  145. if (Consultant::where('mobile', $params['mobile'])->where('id', '<>', $params['id'])->exists()) {
  146. return json_fail('手机号已存在,成员已被注册');
  147. }
  148. //校验密码规则
  149. if (!empty($params['password'])) {
  150. $passwordLen = strlen($params['password']);
  151. if ($passwordLen > 20 || $passwordLen < 6) {
  152. return json_fail('请输入6-20位密码');
  153. }
  154. $password = self::handlePassword($params['password']);
  155. }
  156. //查询上级团队
  157. $topDeptId = SysDept::where('dept_id', $params['dept_id'])->where('dept_category', TeamService::DEPT_CATEGORY)->value('dept_super_id');
  158. if (!is_numeric($topDeptId)) {
  159. return json_fail('团队不存在');
  160. }
  161. $updateData = [
  162. 'name' => $params['name'],
  163. 'mobile' => $params['mobile'],
  164. 'dept_id' => $params['dept_id'],
  165. 'top_dept_id' => $topDeptId,
  166. 'gender' => $params['gender'] ?? 1,
  167. 'status' => $params['status'] ?? 1,
  168. 'updated_at' => time()
  169. ];
  170. if (!empty($params['password'])) {
  171. $updateData['password'] = $password;
  172. }
  173. $result = false;
  174. Db::beginTransaction();
  175. try {
  176. $result = Consultant::where('id', $params['id'])->update($updateData);
  177. if ($updateData['dept_id'] != $oldDeptId) {
  178. //修改成员所属部门
  179. MarketCustomer::where('consultant_id', '=', $params['id'])->update(['updated_at' => time(), 'dept_id' => $updateData['dept_id']]);
  180. }
  181. Db::commit();
  182. }catch (BusinessException|\Exception $e) {
  183. Db::rollBack();
  184. return json_fail($e->getMessage());
  185. }
  186. if ($result) {
  187. return json_success('编辑成功');
  188. } else {
  189. return json_fail('编辑失败');
  190. }
  191. }
  192. /**
  193. * Notes: 添加员工
  194. * User: yb
  195. * Date: 2024/8/15
  196. * Time: 9:55
  197. * @param $params
  198. */
  199. public static function add($params)
  200. {
  201. if (!self::checkMobile($params['mobile'])) {
  202. return json_fail('请输入正确的手机号');
  203. }
  204. //查询员工是否已被注册
  205. if (Consultant::where('mobile', $params['mobile'])->exists()) {
  206. return json_fail('手机号已存在,成员已被注册');
  207. }
  208. //校验密码规则
  209. if (!empty($params['password'])) {
  210. $passwordLen = strlen($params['password']);
  211. if ($passwordLen > 20 || $passwordLen < 6) {
  212. return json_fail('请输入6-20位密码');
  213. }
  214. }
  215. //查询上级团队
  216. $topDeptId = SysDept::where('dept_id', $params['dept_id'])->where('dept_category', TeamService::DEPT_CATEGORY)->value('dept_super_id');
  217. if (!is_numeric($topDeptId)) {
  218. return json_fail('团队不存在');
  219. }
  220. if (empty($params['password'])) {
  221. $showPassword = substr($params['mobile'], 5);
  222. $password = self::handlePassword($showPassword);
  223. } else {
  224. $password = self::handlePassword($params['password']);
  225. }
  226. $insertData = [
  227. 'name' => $params['name'],
  228. 'mobile' => $params['mobile'],
  229. 'dept_id' => $params['dept_id'],
  230. 'top_dept_id' => $topDeptId,
  231. 'gender' => $params['gender'] ?? 1,
  232. 'password' => $password,
  233. 'status' => $params['status'] ?? 1,
  234. 'relation_user_id' => $params['relation_user_id'] ?? null,
  235. 'type' => 2,
  236. 'created_at' => time()
  237. ];
  238. $result = Consultant::insert($insertData);
  239. if ($result) {
  240. return json_success('新增成功');
  241. } else {
  242. return json_fail('新增失败');
  243. }
  244. }
  245. /**
  246. * Notes: 登录
  247. * User: yb
  248. * Date: 2024/8/8
  249. * Time: 9:20
  250. * @param $data
  251. */
  252. public static function login($data)
  253. {
  254. $password = $data['password'];
  255. $mobile = $data['mobile'];
  256. $userInfo = Consultant::firstWhere(['mobile' => $mobile]);
  257. if (empty($userInfo)) {
  258. return json_fail('用户不存在');
  259. }
  260. //校验密码
  261. $confoundPassword = $userInfo->password;
  262. if ($confoundPassword != md5(md5($password))) {
  263. return json_fail('密码错误');
  264. }
  265. $status = $userInfo->status;
  266. if ($status != 1) {
  267. return json_fail('用户已离职');
  268. }
  269. $extend = [
  270. 'id' => $userInfo->id,
  271. 'client' => 'wechat',
  272. 'name'=> $userInfo->name,
  273. 'user_type' => $userInfo->type
  274. ];
  275. $token = JwtToken::generateToken($extend);
  276. $token['user_type'] = $extend['user_type'];
  277. return json_success('', $token);
  278. }
  279. /**
  280. * Notes: 获取身份信息
  281. * User: yb
  282. * Date: 2024/8/8
  283. * Time: 10:09
  284. */
  285. public static function auth()
  286. {
  287. $userId = JwtToken::getCurrentId();
  288. $userInfo = Consultant::firstWhere(['id' => $userId]);
  289. if (empty($userInfo)) {
  290. return json_fail('用户不存在');
  291. }
  292. $status = $userInfo->status;
  293. if ($status != 1) {
  294. return json_fail('用户已离职');
  295. }
  296. $type = $userInfo->type;
  297. $relationUserId = $userInfo->relation_user_id;
  298. return json_success('请求成功', ['type' => $type, 'relation_user_id' => $relationUserId]);
  299. }
  300. /**
  301. * Notes: 获取需要查询的ids
  302. * User: yb
  303. * Date: 2024/8/13
  304. * Time: 15:58
  305. * @return array
  306. */
  307. public static function getIds($clearSelf = 0)
  308. {
  309. $userId = JwtToken::getCurrentId();
  310. $userInfo = Consultant::firstWhere(['id' => $userId]);
  311. if (empty($userInfo)) {
  312. return [];
  313. }
  314. $ids = [];
  315. if ($userInfo->type == 1) {
  316. //管理账号
  317. $deptId = $userInfo->dept_id;
  318. $deptIds = TeamService::getIds($deptId);
  319. //团队下的所有员工
  320. $where = [
  321. [function($query) use ($deptIds) {
  322. $query->whereIn('dept_id', $deptIds);
  323. }]
  324. ];
  325. if ($clearSelf == 1) {
  326. $where[] = ['id', '<>', $userId];
  327. }
  328. $ids = Consultant::where($where)->pluck('id')->toArray();
  329. } else if ($userInfo->type == 2){
  330. //普通账号
  331. $ids[] = $userId;
  332. } else if ($userInfo->type == 3) {
  333. //判客账户
  334. $ids = Consultant::pluck('id')->toArray();
  335. } else if ($userInfo->type == 4) {
  336. //外渠账号
  337. $ids[] = $userId;
  338. }
  339. return $ids;
  340. }
  341. /**
  342. * Notes: 获取用户类型
  343. * User: yb
  344. * Date: 2024/9/13
  345. * Time: 13:57
  346. * @return mixed
  347. */
  348. public static function getUserType()
  349. {
  350. $userId = JwtToken::getCurrentId();
  351. return Consultant::where('id', $userId)->value('type');
  352. }
  353. /**
  354. * Notes: 首页统计分析
  355. * User: yb
  356. * Date: 2024/8/14
  357. * Time: 10:23
  358. */
  359. public static function statistics($params)
  360. {
  361. //统计客户总数
  362. $userIds = self::getIds();
  363. $currentTime = time();
  364. $diffNums = CustomService::DIFF_TIME;
  365. $userType = self::getUserType();
  366. $whereFollow = [
  367. [function($query) use ($userIds) {
  368. $query->whereIn('consultant_id', $userIds);
  369. }]
  370. ];
  371. $where = [];
  372. $where[] = [function($query) use ($userIds) {
  373. $query->orWhereIn('consultant_id', $userIds)->orWhereIn('report_consultant_id', $userIds);
  374. }];
  375. if ($userType != 4) {
  376. $where[] = [function($query) use ($diffNums, $currentTime){
  377. $query->orWhereRaw("check_status IN (-1, 1)")
  378. ->orWhereRaw("check_status = 2 AND current_status IN (-1,3,4)")
  379. ->orWhereRaw("((visit_time + {$diffNums}) - {$currentTime} > 0 AND current_status = 2 AND check_status = 2)")
  380. ->orWhereRaw("((visit_time + {$diffNums}) - {$currentTime} <= 0 AND (visit_time + ({$diffNums} * 4)) - {$currentTime} > 0 AND current_status = 2 AND check_status = 2 AND belong_status = 1)")->whereOr('belong_status','<>', 1);
  381. }];
  382. }
  383. //客户总量
  384. $customNums = MarketCustomer::where($where)->count();
  385. if (!empty($params['time'])) {
  386. $time = $params['time'];
  387. $start = strtotime($time[0]);
  388. $end = strtotime($time[1]);
  389. } else {
  390. $startDate = date('Y-m-01', strtotime(date('Y-m-d')));
  391. $endDate = date('Y-m-t', strtotime(date('Y-m-d')));
  392. $start = strtotime($startDate.' 00:00:00');
  393. $end = strtotime($endDate.' 23:59:59');
  394. }
  395. $where[] = [function($query) use ($start, $end) {
  396. $query->whereBetween('created_at', [$start, $end]);
  397. }];
  398. $whereFollow[] = [function($query) use ($start, $end) {
  399. $query->whereBetween('created_at', [$start, $end]);
  400. }];
  401. //待转到访客户数量
  402. $checkCustomNums = MarketCustomer::where($where)->where('check_status', 1)->where('current_status', 1)->count();
  403. //新增客户
  404. $newCustomNums = MarketCustomer::where($where)->count();
  405. //已报备客户数
  406. $reportCustomNums = MarketCustomer::where($where)->where('current_status', 1)->count();
  407. //已到访客户数量
  408. $visitCustomNums = MarketCustomer::where($where)->where('current_status', 2)->where('check_status', 2)->count();
  409. //已缴费客户数量
  410. $payCustomNums = MarketCustomer::where($where)->where('current_status', 3)->where('check_status', 2)->count();
  411. //已成交客户数量
  412. $dealCustomNums = MarketCustomer::where($where)->where('current_status', 4)->where('check_status', 2)->count();
  413. //新增跟进记录数量
  414. $newFollowNums = MarketCustomerFollow::where($whereFollow)->count();
  415. /* 男女数量 */
  416. $genderDataNums = [
  417. ['label' => '男', 'value' => 1, 'nums' => 0],
  418. ['label' => '女', 'value' => 2, 'nums' => 0],
  419. ];
  420. $genderData = MarketCustomer::where($where)->select(Db::raw('gender, count(*) as nums'))->groupBy('gender')->get();
  421. if (!$genderData->isEmpty()) {
  422. $genderData = $genderData->toArray();
  423. foreach ($genderDataNums as $genderKey => $genderVal) {
  424. foreach ($genderData as $genderItem) {
  425. if ($genderItem['gender'] == $genderVal['value']) {
  426. $genderDataNums[$genderKey]['nums'] = $genderItem['nums'];
  427. }
  428. }
  429. }
  430. }
  431. /* 客户级别 */
  432. $levelDataNums = [
  433. ['label' => 'A类', 'value' => 1, 'nums' => 0],
  434. ['label' => 'B类', 'value' => 2, 'nums' => 0],
  435. ['label' => 'C类', 'value' => 3, 'nums' => 0],
  436. ['label' => 'D类', 'value' => 4, 'nums' => 0],
  437. ['label' => '其他', 'value' => "", 'nums' => 0],
  438. ];
  439. $levelData = MarketCustomer::where($where)->select(Db::raw('level, count(*) as nums'))->groupBy('level')->get();
  440. if (!$levelData->isEmpty()) {
  441. $levelData = $levelData->toArray();
  442. $levelData = array_column($levelData, 'nums', 'level');
  443. foreach ($levelDataNums as $levelKey => $levelVal) {
  444. if (isset($levelData[$levelVal['value']])) {
  445. $levelDataNums[$levelKey]['nums'] = $levelData[$levelVal['value']];
  446. }
  447. }
  448. }
  449. $data = [
  450. 'custom_nums' => $customNums,
  451. 'check_custom_nums' => $checkCustomNums,
  452. 'new_custom_nums' => $newCustomNums,
  453. 'report_custom_nums' => $reportCustomNums,
  454. 'visit_custom_nums' => $visitCustomNums,
  455. 'pay_custom_nums' => $payCustomNums,
  456. 'deal_custom_nums' => $dealCustomNums,
  457. 'new_follow_nums' => $newFollowNums,
  458. 'gender_data_nums' => $genderDataNums,
  459. 'level_data_nums' => $levelDataNums
  460. ];
  461. return json_success('请求成功', $data);
  462. }
  463. /**
  464. * Notes: 近7日客户走势
  465. * User: yb
  466. * Date: 2024/8/17
  467. * Time: 17:44
  468. */
  469. public static function customTrend($params)
  470. {
  471. $currentTime = time();
  472. $diffNums = CustomService::DIFF_TIME;
  473. //统计客户总数
  474. $userIds = self::getIds();
  475. $userType = self::getUserType();
  476. $where = [
  477. [function($query) use ($userIds) {
  478. $query->whereIn('consultant_id', $userIds);
  479. }]
  480. ];
  481. if ($userType != 4) {
  482. $where[] = [function($query) use ($diffNums, $currentTime){
  483. $query->orWhereRaw("check_status IN (-1, 1)")
  484. ->orWhereRaw("check_status = 2 AND current_status IN (-1,3,4)")
  485. ->orWhereRaw("((visit_time + {$diffNums}) - {$currentTime} > 0 AND current_status = 2 AND check_status = 2)");
  486. }];
  487. }
  488. $whereTrend = [];
  489. if (!empty($params['type'])) {
  490. $whereTrend[] = ['current_status', '=', $params['type']];
  491. }
  492. // 获取当前日期
  493. $today = Carbon::now();
  494. // 初始化日期数组和数量数组
  495. $dates = [date('Y-m-d')];
  496. // 循环7次,获取从今天开始往前推7天的日期
  497. for ($i = 0; $i < 6; $i++) {
  498. // 将日期推回到7天前
  499. $date = $today->subDay()->toDateString();
  500. // 将日期字符串存入数组
  501. $dates[] = $date;
  502. }
  503. // 逆序日期数组,因为我们是从今天往前计算的
  504. $dates = array_reverse($dates);
  505. $dateList = [];
  506. foreach ($dates as $item) {
  507. $dateList[] = ['label' => $item, 'nums' => 0, 'label_sim' => date('m/d', strtotime($item))];
  508. }
  509. $datesStr = implode('","', $dates);
  510. $datesStr = '"'.$datesStr.'"';
  511. $dateRaw = Db::raw("DATE_FORMAT(FROM_UNIXTIME(created_at), '%Y-%m-%d') IN ({$datesStr})");
  512. // 构建查询,使用whereBetween或whereIn根据需要筛选日期
  513. $results = MarketCustomer::select(Db::raw("DATE_FORMAT(FROM_UNIXTIME(created_at), '%Y-%m-%d') AS date,count(*) AS nums"))
  514. ->where($where)
  515. ->where($whereTrend)
  516. ->whereRaw($dateRaw)
  517. ->groupBy('date')->get();
  518. if (!$results->isEmpty()) {
  519. $results = $results->toArray();
  520. $results = array_column($results, 'nums', 'date');
  521. foreach ($dateList as $key => $val) {
  522. $dateList[$key]['nums'] = $results[$val['label']] ?? 0;
  523. }
  524. }
  525. //总数
  526. $customNums = MarketCustomer::where($where)->whereRaw($dateRaw)->count();
  527. //到访数
  528. $visitCustomNums = MarketCustomer::where($where)->whereRaw($dateRaw)->where('current_status', 2)->count();
  529. //已缴费数
  530. $payCustomNums = MarketCustomer::where($where)->whereRaw($dateRaw)->where('current_status', 3)->count();
  531. //已成交数
  532. $dealCustomNums = MarketCustomer::where($where)->whereRaw($dateRaw)->where('current_status', 4)->count();
  533. $data = [
  534. 'dateList' => $dateList,
  535. 'visit_nums' => $visitCustomNums,
  536. 'pay_nums' => $payCustomNums,
  537. 'deal_nums' => $dealCustomNums,
  538. 'total_nums' => $customNums
  539. ];
  540. return json_success('请求成功', $data);
  541. }
  542. /**
  543. * Notes: 用户详情
  544. * User: yb
  545. * Date: 2024/8/14
  546. * Time: 13:35
  547. */
  548. public static function info()
  549. {
  550. $userId = JwtToken::getCurrentId();
  551. $info = Consultant::firstWhere(['id' => $userId]);
  552. if (!empty($info)) {
  553. $info->dept_name = SysDept::where('dept_id', $info->dept_id)->value('dept_name');
  554. }
  555. return json_success('请求成功', $info);
  556. }
  557. /**
  558. * Notes: 修改个人信息
  559. * User: yb
  560. * Date: 2024/8/14
  561. * Time: 14:19
  562. * @param $params
  563. */
  564. public static function update($params)
  565. {
  566. $userId = JwtToken::getCurrentId();
  567. if (!self::checkMobile($params['mobile'])) {
  568. return json_fail('请输入正确的手机号');
  569. }
  570. //查询员工是否已被注册
  571. if (Consultant::where('mobile', $params['mobile'])->where('id', '<>', $userId)->exists()) {
  572. return json_fail('手机号已存在');
  573. }
  574. $info = Consultant::firstWhere(['id' => $userId]);
  575. if (empty($info)) {
  576. return json_fail('用户不存在');
  577. }
  578. $info->name = $params['name'];
  579. $info->gender = $params['gender'];
  580. $info->mobile = $params['mobile'];
  581. $result = $info->save();
  582. if ($result) {
  583. return json_success('修改成功');
  584. } else {
  585. return json_fail('修改失败');
  586. }
  587. }
  588. /**
  589. * Notes: 修改密码
  590. * User: yb
  591. * Date: 2024/8/14
  592. * Time: 14:20
  593. * @param $params
  594. */
  595. public static function editPassword($params)
  596. {
  597. $oldPassword = $params['old_password'];
  598. $password = $params['new_password'];
  599. $rePassword = $params['check_password'] ?? '';
  600. $len = mb_strlen($password);
  601. if ($len < 6) {
  602. return json_fail('密码长度最少为6位');
  603. }
  604. if ($len > 20) {
  605. return json_fail('密码长度最大为20位');
  606. }
  607. if ($password != $rePassword) {
  608. return json_fail('密码与确认密码不一致');
  609. }
  610. $userId = JwtToken::getCurrentId();
  611. $info = Consultant::firstWhere(['id' => $userId]);
  612. if (empty($info)) {
  613. return json_fail('用户不存在');
  614. }
  615. //校验密码
  616. $confoundPassword = $info->password;
  617. if ($confoundPassword != md5(md5($oldPassword))) {
  618. return json_fail('旧密码错误');
  619. }
  620. $info->password = md5(md5($password));
  621. $result = $info->save();
  622. if ($result) {
  623. return json_success('修改成功');
  624. } else {
  625. return json_fail('修改失败');
  626. }
  627. }
  628. /**
  629. * Notes: 团队列表
  630. * User: yb
  631. * Date: 2024/8/14
  632. * Time: 16:32
  633. */
  634. public static function getTeamList()
  635. {
  636. $userId = JwtToken::getCurrentId();
  637. $info = Consultant::firstWhere(['id' => $userId]);
  638. if (empty($info)) {
  639. return json_success('请求成功');
  640. }
  641. $deptId = $info->dept_id;
  642. $list = TeamService::getTeamList($deptId);
  643. return $list;
  644. }
  645. /**
  646. * Notes:校验手机号
  647. * User: yb
  648. * Date: 2024/8/2
  649. * Time: 11:12
  650. * @param $mobile
  651. * @return bool
  652. */
  653. protected static function checkMobile($mobile)
  654. {
  655. if (preg_match('/^1[0-9]\d{9}$/', $mobile)) {
  656. return true;
  657. } else {
  658. return false;
  659. }
  660. }
  661. /**
  662. * Notes: 处理密码
  663. * User: yb
  664. * Date: 2024/8/2
  665. * Time: 11:19
  666. * @param $password
  667. * @return mixed
  668. */
  669. protected static function handlePassword($password)
  670. {
  671. return md5(md5($password));
  672. }
  673. }