MasterWorkerLogic.php 3.6 KB

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