MasterWorkerLogic.php 3.5 KB

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