diff --git a/app/Amqp/Producer/SendStationMessageProducer.php b/app/Amqp/Producer/SendStationMessageProducer.php index a13695c..4215243 100644 --- a/app/Amqp/Producer/SendStationMessageProducer.php +++ b/app/Amqp/Producer/SendStationMessageProducer.php @@ -14,6 +14,7 @@ class SendStationMessageProducer extends ProducerMessage * @param $data * [ * "user_id":"用户id(接受者)", + * "user_type":"用户类型(接受者)", * "notice_type":"消息类型(1:医生服务通知 2:医生系统公告 3:患者系统消息)", * "notice_system_type":"系统消息类型(患者端系统消息存在 1:服务消息 2:福利消息 3:退款消息 4:物流消息)", * "inquiry_type:"问诊类型(医生端服务通知存在 1:专家问诊 2:快速问诊 3:公益问诊 4:问诊购药)", diff --git a/app/Services/CouponService.php b/app/Services/CouponService.php index bdf2a14..629af51 100644 --- a/app/Services/CouponService.php +++ b/app/Services/CouponService.php @@ -27,7 +27,7 @@ class CouponService extends BaseService * @param string $user_id 用户id * @return bool */ - protected function GrantUserCoupon(string $coupon_id,string $user_id): bool + public function GrantUserCoupon(string $coupon_id,string $user_id): bool { // 获取患者数据 $params = array(); @@ -97,7 +97,7 @@ class CouponService extends BaseService 'user_coupon_id' => (string)$user_coupon->user_coupon_id, // 优惠卷金额 'coupon_price' => $coupon['coupon_price'], // 优惠卷金额 'application_scope' => $coupon['application_scope'], // 适用范围(1:全场通用 2:问诊 3:按品牌适用 4:按类别类别适用 5:单品使用) - 'inquiry_type' => $coupon['inquiry_type'], // 关联问诊类型,逗号分隔,适用范围为问诊时存在生效(1:专家问诊 2:快速问诊 3:公益问诊 4:问诊购药 5:检测) + 'inquiry_type' => $coupon['inquiry_type'], // 关联问诊类型,application_scope=问诊时存在生效,逗号分隔(1:专家问诊 2:快速问诊 3:公益问诊 4:问诊购药 5:检测) 'valid_type' => $coupon['valid_type'], // 有效类型(1:绝对时效,xxx-xxx时间段有效 2:相对时效 n天内有效) 'valid_days' => $coupon['valid_days'], // 自领取之日起有效天数 'valid_start_time' => $coupon['valid_start_time'], // 开始使用时间 diff --git a/app/Services/LoginService.php b/app/Services/LoginService.php index 7fde8dc..7618934 100644 --- a/app/Services/LoginService.php +++ b/app/Services/LoginService.php @@ -38,29 +38,38 @@ class LoginService extends BaseService $wx_code = $this->request->input('wx_code'); $user_type = $this->request->input('user_type'); - $weChat = new Wechat($user_type); - - // 获取手机号 - $phone_info = $weChat->getPhone($phone_code); - - if (empty($phone_info) || empty($phone_info['phone_info']) || empty($phone_info['phone_info']['purePhoneNumber'])) { - return fail(HttpEnumCode::GET_WX_ERROR); - } - - // 获取用户openid - $wx_info_data = $weChat->codeToSession($wx_code); - if (empty($wx_info_data['session_key']) || empty($wx_info_data['openid'])) { - return fail(HttpEnumCode::GET_WX_ERROR); - } - - Db::beginTransaction(); - try { - // 获取用户信息 - $params = array(); - $params['mobile'] = $phone_info['phone_info']['purePhoneNumber']; - $user = UserModel::getOne($params); - if (empty($user)) { + $weChat = new Wechat($user_type); + + // 获取手机号 + $phone_info = $weChat->getPhone($phone_code); + + if (empty($phone_info) || empty($phone_info['phone_info']) || empty($phone_info['phone_info']['purePhoneNumber'])) { + return fail(HttpEnumCode::GET_WX_ERROR); + } + + // 获取用户openid + $wx_info_data = $weChat->codeToSession($wx_code); + if (empty($wx_info_data['session_key']) || empty($wx_info_data['openid'])) { + return fail(HttpEnumCode::GET_WX_ERROR); + } + }catch (\Throwable $e){ + return fail(HttpEnumCode::GET_WX_ERROR); + } + + // 定义是否新注册用户标识 + $is_new_register = 0; + + // 获取用户信息 + $params = array(); + $params['mobile'] = $phone_info['phone_info']['purePhoneNumber']; + $user = UserModel::getOne($params); + + // 新用户 + if (empty($user)){ + Db::beginTransaction(); + + try { // 处理药师特殊情况,后台添加,前台不允许注册 if ($user_type == 3) { Db::rollBack(); @@ -144,18 +153,22 @@ class LoginService extends BaseService // 创建单个账号 $account->createAccount($user->user_id, $user->user_name, addAliyunOssWebsite($avatar)); - if ($user['user_type'] == 1) { - // 发放注册用户优惠卷 - $CouponService = new CouponService(); - $res = $CouponService->GrantRegisterCoupon($user->user_id); - if (!$res) { - Db::rollBack(); - return fail(HttpEnumCode::SERVER_ERROR); - } - } - } else { - // 已注册用户 - // 重复注册不同端 + // 标识为新注册用户 + $is_new_register = 1; + }catch (\Throwable $e){ + Db::rollBack(); + return fail(HttpEnumCode::SERVER_ERROR, $e->getMessage()); + } + + Db::commit(); + } + + // 已注册用户 + if (!empty($user)){ + Db::beginTransaction(); + + try { + // 判断是否重复注册不同端 if ($user_type != $user['user_type']) { Db::rollBack(); $result = UserTypeToString($user['user_type']); @@ -216,63 +229,88 @@ 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::SERVER_ERROR); } if (!$res) { Db::rollBack(); return fail(HttpEnumCode::SERVER_ERROR); } - - try { - $avatar = addAliyunOssWebsite($user['avatar']); - - $account = new Account(); - // 查询账号导入状态 - $res = $account->checkAccountStatus($user['user_id']); - if (!$res) { - // 创建单个账号 - $account->createAccount($user['user_id'], $user['user_name'], $avatar); - } - } catch (\Throwable $e) { - Log::getInstance()->error("IM账号倒入失败"); - } + }catch (\Throwable $e){ + Db::rollBack(); + return fail(HttpEnumCode::SERVER_ERROR, $e->getMessage()); } - // 记录用户登陆记录 - $Http = new Http(); - $login_ip = $Http->getIp(); - $params = array(); - $params['user_id'] = $user['user_id']; - - $data = array(); - $data['login_ip'] = $login_ip ?? ""; - $data['last_login_at'] = date('Y-m-d H:i:s', time()); - UserModel::editUser($params, $data); - - // 组合生成token的数据 - $token_user_data = array(); - $token_user_data['user_id'] = (string)$user['user_id']; // 用户id - $token_user_data['user_type'] = $user['user_type'];// 用户类型 - $token_user_data['open_id'] = $wx_info_data['openid'];// open_id - $token_user_data['client_user_id'] = (string)$client_user_id;// 对应客户端id - - // 发放token - $Jwt = new Jwt(); - $token = $Jwt->encode($token_user_data); - - // 组合返回数据 - $data = array(); - $data['token'] = $token; - $data['user_id'] = (string)$user['user_id']; - $data['client_user_id'] = (string)$client_user_id; - Db::commit(); - return success($data); - } catch (\Exception $e) { + try { + $avatar = addAliyunOssWebsite($user['avatar']); + + $account = new Account(); + // 查询账号导入状态 + $res = $account->checkAccountStatus($user['user_id']); + if (!$res) { + // 创建单个账号 + $account->createAccount($user['user_id'], $user['user_name'], $avatar); + } + } catch (\Throwable $e) { + Log::getInstance()->error("IM账号导入失败"); + } + + + } + + // 发放优惠卷-患者 + Db::beginTransaction(); + + try { + if ($user['user_type'] == 1 && $is_new_register == 1) { + $CouponService = new CouponService(); + $res = $CouponService->GrantRegisterCoupon($user->user_id); + if (!$res) { + Db::rollBack(); + return fail(HttpEnumCode::SERVER_ERROR); + } + } + }catch (\Throwable $e){ Db::rollBack(); return fail(HttpEnumCode::SERVER_ERROR, $e->getMessage()); } + + Db::commit(); + + // 记录用户登陆记录 + $Http = new Http(); + $login_ip = $Http->getIp(); + $params = array(); + $params['user_id'] = $user['user_id']; + + $data = array(); + $data['login_ip'] = $login_ip ?? ""; + $data['last_login_at'] = date('Y-m-d H:i:s', time()); + UserModel::editUser($params, $data); + + + // 组合生成token的数据 + $token_user_data = array(); + $token_user_data['user_id'] = (string)$user['user_id']; // 用户id + $token_user_data['user_type'] = $user['user_type'];// 用户类型 + $token_user_data['open_id'] = $wx_info_data['openid'];// open_id + $token_user_data['client_user_id'] = (string)$client_user_id;// 对应客户端id + + // 发放token + $Jwt = new Jwt(); + $token = $Jwt->encode($token_user_data); + + // 组合返回数据 + $data = array(); + $data['token'] = $token; + $data['user_id'] = (string)$user['user_id']; + $data['client_user_id'] = (string)$client_user_id; + + return success($data); } /** @@ -309,19 +347,24 @@ class LoginService extends BaseService $wx_info_data = $weChat->codeToSession($wx_code); $session_key = $wx_info_data['session_key'] ?? ""; $open_id = $wx_info_data['openid'] ?? ""; - } catch (\Exception $e) { + } catch (\Throwable $e) { // 此处不进行处理 Log::getInstance()->info($e->getMessage()); } - Db::beginTransaction(); + // 定义是否新注册用户标识 + $is_new_register = 0; - try { - // 获取用户信息 - $params = array(); - $params['mobile'] = $phone; - $user = UserModel::getOne($params); - if (empty($user)) { + // 获取用户信息 + $params = array(); + $params['mobile'] = $phone; + $user = UserModel::getOne($params); + + // 新用户 + if (empty($user)) { + Db::beginTransaction(); + + try { // 处理药师特殊情况,后台添加,前台不允许注册 if ($user_type == 3) { Db::rollBack(); @@ -409,16 +452,21 @@ class LoginService extends BaseService // 创建单个账号 $account->createAccount($user->user_id, $user->user_name, addAliyunOssWebsite($avatar)); - if ($user['user_type'] == 1) { - // 发放注册用户优惠卷 - $CouponService = new CouponService(); - $res = $CouponService->GrantRegisterCoupon($user->user_id); - if (!$res) { - Db::rollBack(); - return fail(HttpEnumCode::SERVER_ERROR); - } - } - } else { + // 标识为新注册用户 + $is_new_register = 1; + }catch (\Throwable $e){ + Db::rollBack(); + return fail(HttpEnumCode::HTTP_ERROR, $e->getMessage()); + } + + Db::commit(); + } + + // 已注册用户 + if (!empty($user)){ + Db::beginTransaction(); + + try { // 已注册用户 if ($user_type != $user['user_type']) { Db::rollBack(); @@ -491,62 +539,82 @@ class LoginService extends BaseService Db::rollBack(); return fail(HttpEnumCode::SERVER_ERROR); } - - try { - $avatar = addAliyunOssWebsite($user['avatar']); - - $account = new Account(); - // 查询账号导入状态 - $res = $account->checkAccountStatus($user['user_id']); - if (!$res) { - // 创建单个账号 - $account->createAccount($user['user_id'], $user['user_name'], $avatar); - } - } catch (\Throwable $e) { - Log::getInstance()->error("IM账号倒入失败"); - } + }catch (\Throwable $e){ + Db::rollBack(); + return fail(HttpEnumCode::HTTP_ERROR, $e->getMessage()); } - // 记录用户登陆记录 - $Http = new Http(); - $login_ip = $Http->getIp(); - $params = array(); - $params['user_id'] = $user['user_id']; - - $data = array(); - $data['login_ip'] = $login_ip ?? ""; - $data['last_login_at'] = date('Y-m-d H:i:s', time()); - UserModel::editUser($params, $data); - - // 组合生成token的数据 - $token_user_data = array(); - $token_user_data['user_id'] = (string)$user['user_id']; // 用户id - $token_user_data['user_type'] = $user['user_type'];// 用户类型 - $token_user_data['open_id'] = $open_id ?? "";// open_id - $token_user_data['client_user_id'] = (string)$client_user_id;// 对应客户端id - - // 发放token - $Jwt = new Jwt(); - $token = $Jwt->encode($token_user_data); - - // 登录成功,删除验证码 - if ($app_env != 'dev') { - $redis->del("login_code" . $phone); - } - - // 组合返回数据 - $data = array(); - $data['token'] = $token; - $data['user_id'] = (string)$user['user_id']; - $data['client_user_id'] = (string)$client_user_id; - Db::commit(); - return success($data); - } catch (\Throwable $e) { - Db::rollBack(); - return fail(HttpEnumCode::HTTP_ERROR, $e->getMessage()); + try { + $avatar = addAliyunOssWebsite($user['avatar']); + + $account = new Account(); + // 查询账号导入状态 + $res = $account->checkAccountStatus($user['user_id']); + if (!$res) { + // 创建单个账号 + $account->createAccount($user['user_id'], $user['user_name'], $avatar); + } + } catch (\Throwable $e) { + Log::getInstance()->error("IM账号导入失败"); + } } + + // 发放优惠卷-患者 + Db::beginTransaction(); + + try { + if ($user['user_type'] == 1 && $is_new_register == 1) { + $CouponService = new CouponService(); + $res = $CouponService->GrantRegisterCoupon($user->user_id); + if (!$res) { + Db::rollBack(); + return fail(HttpEnumCode::SERVER_ERROR); + } + } + }catch (\Throwable $e){ + Db::rollBack(); + return fail(HttpEnumCode::SERVER_ERROR, $e->getMessage()); + } + + Db::commit(); + + // 记录用户登陆记录 + $Http = new Http(); + $login_ip = $Http->getIp(); + $params = array(); + $params['user_id'] = $user['user_id']; + + $data = array(); + $data['login_ip'] = $login_ip ?? ""; + $data['last_login_at'] = date('Y-m-d H:i:s', time()); + UserModel::editUser($params, $data); + + // 组合生成token的数据 + $token_user_data = array(); + $token_user_data['user_id'] = (string)$user['user_id']; // 用户id + $token_user_data['user_type'] = $user['user_type'];// 用户类型 + $token_user_data['open_id'] = $open_id ?? "";// open_id + $token_user_data['client_user_id'] = (string)$client_user_id;// 对应客户端id + + // 发放token + $Jwt = new Jwt(); + $token = $Jwt->encode($token_user_data); + + // 登录成功,删除验证码 + if ($app_env != 'dev') { + $redis->del("login_code" . $phone); + } + + // 组合返回数据 + $data = array(); + $data['token'] = $token; + $data['user_id'] = (string)$user['user_id']; + $data['client_user_id'] = (string)$client_user_id; + + Db::commit(); + return success($data); } /**