MasterWokerLogic.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace app\workerapi\logic;
  3. use app\common\logic\BaseLogic;
  4. use app\common\model\master_worker\MasterWorker;
  5. use think\facade\Config;
  6. /**
  7. * @author 林海涛
  8. * @date 2024/7/10 下午1:45
  9. */
  10. class MasterWokerLogic extends BaseLogic
  11. {
  12. public static function changePassword(array $params, int $userId)
  13. {
  14. try {
  15. $user = MasterWorker::findOrEmpty($userId);
  16. if ($user->isEmpty()) {
  17. throw new \Exception('用户不存在');
  18. }
  19. // 密码盐
  20. $passwordSalt = Config::get('project.unique_identification');
  21. if (!empty($user['password'])) {
  22. if (empty($params['old_password'])) {
  23. throw new \Exception('请填写旧密码');
  24. }
  25. $oldPassword = create_password($params['old_password'], $passwordSalt);
  26. if ($oldPassword != $user['password']) {
  27. throw new \Exception('原密码不正确');
  28. }
  29. }
  30. // 保存密码
  31. $password = create_password($params['password'], $passwordSalt);
  32. $user->password = $password;
  33. $user->save();
  34. return true;
  35. } catch (\Exception $e) {
  36. self::setError($e->getMessage());
  37. return false;
  38. }
  39. }
  40. public static function changeMobile(array $params, int $userId)
  41. {
  42. try {
  43. $user = MasterWorker::findOrEmpty($userId);
  44. if ($user->isEmpty()) {
  45. throw new \Exception('用户不存在');
  46. }
  47. if($user->mobile == $params['mobile']){
  48. throw new \Exception('输入的手机号相同');
  49. }
  50. $where = [['mobile', '=', $params['mobile']]];
  51. $exitUser = MasterWorker::where($where)->findOrEmpty();
  52. if (!$exitUser->isEmpty()) {
  53. throw new \Exception('该手机号已被使用');
  54. }
  55. $user->password = $params['mobile'];
  56. $user->save();
  57. return true;
  58. } catch (\Exception $e) {
  59. self::setError($e->getMessage());
  60. return false;
  61. }
  62. }
  63. }