MasterWorkerLogic.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace app\workerapi\logic;
  3. use app\common\enum\YesNoEnum;
  4. use app\common\logic\BaseLogic;
  5. use app\common\model\master_worker\MasterWorker;
  6. use think\facade\Config;
  7. /**
  8. * @author 林海涛
  9. * @date 2024/7/10 下午1:45
  10. */
  11. class MasterWorkerLogic extends BaseLogic
  12. {
  13. public static function changePassword(array $params, int $userId)
  14. {
  15. try {
  16. $user = MasterWorker::findOrEmpty($userId);
  17. if ($user->isEmpty()) {
  18. throw new \Exception('用户不存在');
  19. }
  20. // 密码盐
  21. $passwordSalt = Config::get('project.unique_identification');
  22. if (!empty($user['password'])) {
  23. if (empty($params['old_password'])) {
  24. throw new \Exception('请填写旧密码');
  25. }
  26. $oldPassword = create_password($params['old_password'], $passwordSalt);
  27. if ($oldPassword != $user['password']) {
  28. throw new \Exception('原密码不正确');
  29. }
  30. }
  31. // 保存密码
  32. $password = create_password($params['password'], $passwordSalt);
  33. $user->password = $password;
  34. $user->save();
  35. return true;
  36. } catch (\Exception $e) {
  37. self::setError($e->getMessage());
  38. return false;
  39. }
  40. }
  41. public static function changeMobile(array $params, int $userId)
  42. {
  43. try {
  44. $user = MasterWorker::findOrEmpty($userId);
  45. if ($user->isEmpty()) {
  46. throw new \Exception('用户不存在');
  47. }
  48. if($user->mobile == $params['mobile']){
  49. throw new \Exception('输入的手机号相同');
  50. }
  51. $where = [['mobile', '=', $params['mobile']]];
  52. $existUser = MasterWorker::where($where)->findOrEmpty();
  53. if (!$existUser->isEmpty()) {
  54. throw new \Exception('该手机号已被使用');
  55. }
  56. $user->password = $params['mobile'];
  57. $user->save();
  58. return true;
  59. } catch (\Exception $e) {
  60. self::setError($e->getMessage());
  61. return false;
  62. }
  63. }
  64. public static function logOff(int $userId)
  65. {
  66. try {
  67. $user = MasterWorker::findOrEmpty($userId);
  68. if ($user->isEmpty()) {
  69. throw new \Exception('用户不存在');
  70. }
  71. $user->is_disable = YesNoEnum::YES;
  72. $user->save();
  73. return true;
  74. } catch (\Exception $e) {
  75. self::setError($e->getMessage());
  76. return false;
  77. }
  78. }
  79. public static function detail($userId): array
  80. {
  81. return MasterWorker::field('id,sn,avatar,real_avatar,real_name,nickname,account,mobile,sex,estimate_money,user_money,earnest_money,exp')
  82. ->findOrEmpty($userId)
  83. ->toArray();
  84. }
  85. }