From 9b096e0060a2e8e1640acf1af9608a87b4c8bc92 Mon Sep 17 00:00:00 2001 From: wucongxing <815046773@qq.com> Date: Mon, 25 Sep 2023 11:32:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B3=A8=E5=86=8C=E7=94=A8=E6=88=B7=EF=BC=8C?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=94=A8=E6=88=B7=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Services/LoginService.php | 111 ++++++++++++++++++++++------------ app/Services/UserService.php | 8 ++- extend/TencentIm/Account.php | 4 +- 3 files changed, 82 insertions(+), 41 deletions(-) diff --git a/app/Services/LoginService.php b/app/Services/LoginService.php index 7f51a52..ef40a64 100644 --- a/app/Services/LoginService.php +++ b/app/Services/LoginService.php @@ -8,6 +8,7 @@ use App\Model\User as UserModel; use App\Model\UserDoctor as UserDoctorModel; use App\Model\UserPatient as UserPatientModel; use App\Model\UserPharmacist as UserPharmacistModel; +use App\Model\UserSystem; use App\Utils\Http; use App\Utils\Jwt; use App\Utils\Log; @@ -42,7 +43,7 @@ class LoginService extends BaseService // 获取手机号 $phone_info = $weChat->getPhone($phone_code); - if (empty($phone_info) || empty($phone_info['phone_info']) || empty($phone_info['phone_info']['purePhoneNumber'])){ + if (empty($phone_info) || empty($phone_info['phone_info']) || empty($phone_info['phone_info']['purePhoneNumber'])) { return fail(HttpEnumCode::GET_WX_ERROR); } @@ -63,7 +64,7 @@ class LoginService extends BaseService // 处理药师特殊情况,后台添加,前台不允许注册 if ($user_type == 3) { Db::rollBack(); - return fail(HttpEnumCode::HTTP_ERROR,"账号不存在,请联系客服"); + return fail(HttpEnumCode::HTTP_ERROR, "账号不存在,请联系客服"); } // 获取用户默认头像oss地址 @@ -72,7 +73,7 @@ class LoginService extends BaseService // 新增用户表 $data = array(); - $data['user_name'] = 'gdxz' . substr($phone_info['phone_info']['purePhoneNumber'],-4) . mt_rand(10000,99999); + $data['user_name'] = 'gdxz' . substr($phone_info['phone_info']['purePhoneNumber'], -4) . mt_rand(10000, 99999); $data['mobile'] = $phone_info['phone_info']['purePhoneNumber']; $data['wx_mobile'] = $phone_info['phone_info']['purePhoneNumber']; $data['user_type'] = $user_type; @@ -115,17 +116,32 @@ class LoginService extends BaseService $client_user_id = $user_doctor['doctor_id']; } + // 新增用户配置表 + $params = array(); + $params['user_id'] = $user['user_id']; + $user_system = UserSystem::getOne($params); + if (empty($user_system)) { + $data = array(); + $data['user_id'] = $user['user_id']; + $data['is_accept_im_message_push'] = 1; // 默认新增一下配置表 + $user_system = UserSystem::addUserSystem($data); + if (empty($user_system)) { + Db::rollBack(); + return fail(HttpEnumCode::SERVER_ERROR); + } + } + // 创建im账号 $account = new Account(); // 创建单个账号 - $account->createAccount($user->user_id,$user->user_name,addAliyunOssWebsite($avatar)); + $account->createAccount($user->user_id, $user->user_name, addAliyunOssWebsite($avatar)); - if ($user['user_type'] == 1){ + if ($user['user_type'] == 1) { // 发放用户优惠卷 $CouponService = new CouponService(); - $res = $CouponService->DistributeCoupon(1,(string)$user->user_id,$user_patient['patient_id']); - if (!$res){ + $res = $CouponService->DistributeCoupon(1, (string)$user->user_id, $user_patient['patient_id']); + if (!$res) { Db::rollBack(); return fail(HttpEnumCode::SERVER_ERROR); } @@ -144,17 +160,17 @@ class LoginService extends BaseService $params['user_id'] = $user->user_id; if ($user['user_type'] == 1) { $result = UserPatientModel::getOne($params); - if (!empty($result)){ + if (!empty($result)) { $client_user_id = $result['patient_id']; } } elseif ($user['user_type'] == 2) { $result = UserDoctorModel::getOne($params); - if (!empty($result)){ + if (!empty($result)) { $client_user_id = $result['doctor_id']; } } elseif ($user['user_type'] == 3) { $result = UserPharmacistModel::getOne($params); - if (!empty($result)){ + if (!empty($result)) { $client_user_id = $result['pharmacist_id']; } } @@ -210,7 +226,7 @@ class LoginService extends BaseService // 创建单个账号 $account->createAccount($user['user_id'], $user['user_name'], $avatar); } - }catch(\Exception $e){ + } catch (\Exception $e) { Log::getInstance()->error("IM账号倒入失败"); } } @@ -224,8 +240,7 @@ class LoginService extends BaseService $data = array(); $data['login_ip'] = $login_ip ?? ""; $data['last_login_at'] = date('Y-m-d H:i:s', time()); - - UserModel::editUser($params,$data); + UserModel::editUser($params, $data); // 组合生成token的数据 $token_user_data = array(); @@ -266,16 +281,16 @@ class LoginService extends BaseService $user_type = $this->request->input('user_type'); $wx_code = $this->request->input('wx_code'); - $app_env = config('app_env','dev'); - if ($app_env != 'dev'){ + $app_env = config('app_env', 'dev'); + if ($app_env != 'dev') { $redis = $this->container->get(Redis::class); // 验证验证码 $sms_code = $redis->get("login_code" . $phone); - if (empty($sms_code)){ + if (empty($sms_code)) { return fail(HttpEnumCode::CODE_EXPIRED); } - if ($sms_code != $code){ + if ($sms_code != $code) { return fail(HttpEnumCode::CODE_ERROR); } } @@ -303,7 +318,7 @@ class LoginService extends BaseService // 处理药师特殊情况,后台添加,前台不允许注册 if ($user_type == 3) { Db::rollBack(); - return fail(HttpEnumCode::HTTP_ERROR,"账号不存在,请联系客服"); + return fail(HttpEnumCode::HTTP_ERROR, "账号不存在,请联系客服"); } // 获取用户默认头像oss地址 @@ -312,7 +327,7 @@ class LoginService extends BaseService // 新增用户表 $data = array(); - $data['user_name'] = 'gdxz' . substr($phone,-4) . mt_rand(10000,99999);; + $data['user_name'] = 'gdxz' . substr($phone, -4) . mt_rand(10000, 99999);; $data['mobile'] = $phone; $data['wx_mobile'] = $phone; $data['user_type'] = $user_type; @@ -351,25 +366,40 @@ class LoginService extends BaseService return fail(HttpEnumCode::SERVER_ERROR); } $client_user_id = $user_doctor['doctor_id']; - }elseif($user['user_type'] == 3){ + } elseif ($user['user_type'] == 3) { Db::rollBack(); return fail(HttpEnumCode::SERVER_ERROR); - }else{ + } else { Db::rollBack(); return fail(HttpEnumCode::SERVER_ERROR); } + // 新增用户配置表 + $params = array(); + $params['user_id'] = $user['user_id']; + $user_system = UserSystem::getOne($params); + if (empty($user_system)) { + $data = array(); + $data['user_id'] = $user['user_id']; + $data['is_accept_im_message_push'] = 1; // 默认新增一下配置表 + $user_system = UserSystem::addUserSystem($data); + if (empty($user_system)) { + Db::rollBack(); + return fail(HttpEnumCode::SERVER_ERROR); + } + } + // 创建im账号 $account = new Account(); // 创建单个账号 - $account->createAccount($user->user_id,$user->user_name,addAliyunOssWebsite($avatar)); + $account->createAccount($user->user_id, $user->user_name, addAliyunOssWebsite($avatar)); - if ($user['user_type'] == 1){ + if ($user['user_type'] == 1) { // 发放用户优惠卷 $CouponService = new CouponService(); - $res = $CouponService->DistributeCoupon(1,(string)$user->user_id,$user_patient['patient_id']); - if (!$res){ + $res = $CouponService->DistributeCoupon(1, (string)$user->user_id, $user_patient['patient_id']); + if (!$res) { Db::rollBack(); return fail(HttpEnumCode::SERVER_ERROR); } @@ -386,20 +416,20 @@ class LoginService extends BaseService $params['user_id'] = $user->user_id; if ($user['user_type'] == 1) { $result = UserPatientModel::getOne($params); - if (!empty($result)){ + if (!empty($result)) { $client_user_id = $result['patient_id']; } } elseif ($user['user_type'] == 2) { $result = UserDoctorModel::getOne($params); - if (!empty($result)){ + if (!empty($result)) { $client_user_id = $result['doctor_id']; } } elseif ($user['user_type'] == 3) { $result = UserPharmacistModel::getOne($params); - if (!empty($result)){ + if (!empty($result)) { $client_user_id = $result['pharmacist_id']; } - }else{ + } else { Db::rollBack(); return fail(HttpEnumCode::SERVER_ERROR); } @@ -438,6 +468,9 @@ class LoginService extends BaseService } elseif ($user['user_type'] == 3) { $params['pharmacist_id'] = $result['pharmacist_id']; $res = UserPharmacistModel::editUserPharmacist($params, $data); + } else { + Db::rollBack(); + return fail(HttpEnumCode::USER_STATUS_ERROR); } if (!$res) { @@ -455,7 +488,7 @@ class LoginService extends BaseService // 创建单个账号 $account->createAccount($user['user_id'], $user['user_name'], $avatar); } - }catch(\Exception $e){ + } catch (\Throwable $e) { Log::getInstance()->error("IM账号倒入失败"); } } @@ -469,7 +502,7 @@ class LoginService extends BaseService $data = array(); $data['login_ip'] = $login_ip ?? ""; $data['last_login_at'] = date('Y-m-d H:i:s', time()); - UserModel::editUser($params,$data); + UserModel::editUser($params, $data); // 组合生成token的数据 $token_user_data = array(); @@ -483,7 +516,9 @@ class LoginService extends BaseService $token = $Jwt->encode($token_user_data); // 登录成功,删除验证码 -// $redis->del("login_code" . $phone); + if ($app_env != 'dev') { + $redis->del("login_code" . $phone); + } // 组合返回数据 $data = array(); @@ -516,7 +551,7 @@ class LoginService extends BaseService $user_info = $this->request->getAttribute("userInfo") ?? []; $wx_code = $this->request->input('wx_code'); - if (empty($user_info)){ + if (empty($user_info)) { return fail(HttpEnumCode::GET_WX_ERROR); } @@ -524,7 +559,7 @@ class LoginService extends BaseService $params = array(); $params['user_id'] = $user_info['user_id']; $user = UserModel::getOne($params); - if (empty($user)){ + if (empty($user)) { return fail(); } @@ -567,7 +602,7 @@ class LoginService extends BaseService $data['open_id'] = $wx_info_data['openid']; } - if (!empty($data)){ + if (!empty($data)) { $data['updated_at'] = date('Y-m-d H:i:s', time()); $params = array(); @@ -580,7 +615,7 @@ class LoginService extends BaseService } elseif ($user['user_type'] == 3) { $params['pharmacist_id'] = $result['pharmacist_id']; $res = UserPharmacistModel::editUserPharmacist($params, $data); - }else{ + } else { $res = false; } @@ -588,9 +623,9 @@ class LoginService extends BaseService return fail(HttpEnumCode::SERVER_ERROR); } } - } catch (\Exception $e) { + } catch (\Throwable $e) { // 此处不进行处理 - return fail(HttpEnumCode::GET_WX_ERROR,$e->getMessage()); + return fail(HttpEnumCode::GET_WX_ERROR, $e->getMessage()); } return success(); diff --git a/app/Services/UserService.php b/app/Services/UserService.php index f632526..0f2cf37 100644 --- a/app/Services/UserService.php +++ b/app/Services/UserService.php @@ -857,7 +857,13 @@ class UserService extends BaseService $params['user_id'] = $user['user_id']; $user_system = UserSystem::getOne($params); if (empty($user_system)){ - return false; + $data = array(); + $data['user_id'] = $user['user_id']; + $data['is_accept_im_message_push'] = 1; // 默认新增一下配置表 + $user_system = UserSystem::addUserSystem($data); + if (empty($user_system)){ + return false; + } } $message = "您有一条新的问诊消息"; diff --git a/extend/TencentIm/Account.php b/extend/TencentIm/Account.php index 7640af2..2a539fc 100644 --- a/extend/TencentIm/Account.php +++ b/extend/TencentIm/Account.php @@ -36,7 +36,7 @@ class Account extends Base $path = $this->config['base_url'] . $this->version . "/im_open_login_svc/account_import?" . $this->buildRequestParams(); return $this->postRequest($path,$options); - }catch (\Exception $e) { + }catch (\Throwable $e) { throw new BusinessException($e->getMessage(), HttpEnumCode::SERVER_ERROR); } } @@ -74,7 +74,7 @@ class Account extends Base } return true; - }catch (\Exception $e) { + }catch (\Throwable $e) { throw new BusinessException($e->getMessage(), HttpEnumCode::SERVER_ERROR); } }