Files
xk-api-yii/service/models/forms/LoginForm.php
2026-03-24 12:31:19 +08:00

524 lines
22 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace service\models\forms;
use common\core\BaseModel;
use common\enums\UserRoleEnum;
use common\enums\UserStatusEnum;
use common\models\oldDb\ServiceUser;
use common\models\oldDb\ServiceUserToken;
use common\models\oldDb\StoreDoctor;
use common\services\WechatService;
use common\validators\MobileValidator;
use yii\base\Exception;
class LoginForm extends BaseModel
{
public $status;
public $plate_type;
public $role;
public $code;//微信code
public $iv;
public $encryptedData;
public $mobile;
public $smsCode;//验证码
public $store_id;
public function rules()
{
return [
[['status'], 'required'],
[['role','status','store_id','plate_type','smsCode'], 'integer'],
[['code','iv','encryptedData'], 'string'],
['mobile', MobileValidator::class],
];
}
public function attributeLabels()
{
return [
'status' => '状态',
'role' => '角色',
'code' => '微信code',
'mobile' => '手机号',
'smsCode' => '验证码',
];
}
public function userlogin()
{
if (!$this->validate()) {
throw new Exception($this->getErrorMsg());
}
switch ($this->status) {
case 1;//1微信登录
if (!$this->code) {
throw new Exception('微信code不能为空');
}
if (!$this->plate_type) {
throw new Exception('平台plate_type不能为空');
}
return $this->login();
case 3://3手机号登录
if (!$this->mobile) {
throw new Exception('mobile不能为空');
}
if (!$this->smsCode) throw new Exception('验证码不能为空');
return $this->phoneLogin();
case 4://4手机号注册
if (!$this->plate_type) {
throw new Exception('平台plate_type不能为空');
}
if (!$this->mobile) {
throw new Exception('mobile不能为空');
}
if (!$this->role) {
throw new Exception('role不能为空');
}
if (!$this->smsCode) throw new Exception('验证码不能为空');
return $this->PhoneRegister();
case 5: // 开发登陆
if (!YII_DEBUG) {
return [];
}
if (!$this->mobile) {
throw new Exception('mobile不能为空');
}
$ServiceUser = ServiceUser::findOne(['mobile' => $this->mobile]);
if (!$ServiceUser) {
throw new Exception('用户不存在');
}
$ServiceUserToken = new ServiceUserToken();
$ServiceUserToken->su_id = $ServiceUser->id;
$token = \Yii::$app->security->generateRandomString() . '_' . time();
$ServiceUserToken->token = $token;
$ServiceUserToken->saveOrFail();
$StoreDoctor = StoreDoctor::find()->where([
'su_id' => $ServiceUser->id,
'is_delete' => 0,
])->one();
if (!$StoreDoctor){
throw new Exception('您还没有门店,请先去完善信息');
}
//更新登录时间
$StoreDoctor->su_id = $ServiceUser->id;
$StoreDoctor->id = $StoreDoctor->id;
$StoreDoctor->store_id = $StoreDoctor->store_id;
$StoreDoctor->last_login_time = time();
$StoreDoctor->is_delete = 0;
$StoreDoctor->saveOrFail();
return [
'token' => $token,
'StoreDoctor' => $StoreDoctor,
];
}
}
/**
* 微信登录
*/
public function login()
{
$token = \Yii::$app->security->generateRandomString() . '_' . time();
$login = WechatService::getInstance()->app->auth->session($this->code);
if(isset($login['errmsg'])){
throw new Exception($login['errmsg']);
}
$ServiceUser = ServiceUser::findOne(['openid' => $login['openid']]);
switch ($this->plate_type){
//线上
case 1:
try {
$transaction = \Yii::$app->db->beginTransaction();
if (!$ServiceUser) {
$ServiceUser = new ServiceUser();
}
$ServiceUser->openid = $login['openid'];
$ServiceUser->session_key = $login['session_key'];
$ServiceUser->unionid = isset($login['unionid']) ? $login['unionid'] : '';
$ServiceUser->nickname = $ServiceUser['nickname'] ?? '微信用户';
$ServiceUser->plate_type = $this->plate_type;
$ServiceUser->role=UserRoleEnum::DOCTOR;
$ServiceUser->saveOrFail();
$ServiceUserToken = new ServiceUserToken();
$ServiceUserToken->su_id = $ServiceUser->id;
$ServiceUserToken->token = $token;
$ServiceUserToken->saveOrFail();
$transaction->commit();
}catch (\Exception $e){
$transaction->rollBack();
throw new Exception($e->getMessage());
}
try {
$t = \Yii::$app->db1->beginTransaction();
\Yii::$app->db1->createCommand()->insert('yii_service_user',[
'role'=>UserRoleEnum::DOCTOR,
'status'=>UserStatusEnum::WAIT_JH,
'created_at'=>time(),
'updated_at'=>time(),
])->execute();
$su_id=\Yii::$app->db1->getLastInsertID();
\Yii::$app->db1->createCommand()->insert('yii_service_user_token',[
'su_id'=>$su_id,
'token'=>$token,
'created_at'=>time(),
'updated_at'=>time(),
])->execute();
$sql="select * from yii_doctor_platform where su_id=$su_id and platform_doctor_id=$ServiceUser->id";
$is_exist=\Yii::$app->db1->createCommand($sql)->queryOne();
$platform_id=1;
if (!$is_exist){
//用户平台关系表
\Yii::$app->db1->createCommand()->insert('yii_doctor_platform',[
'su_id'=>$su_id,
'platform_doctor_id'=>$ServiceUser->id,
'platform_id'=>$platform_id,
'platform_store_id'=>\Yii::$app->store,
'created_at'=>time(),
'updated_at'=>time(),
])->execute();
}
$t->commit();
}catch (\Exception $e){
$t->rollBack();
throw new Exception($e->getMessage());
}
$StoreDoctor = StoreDoctor::find()->where([
'su_id' => $ServiceUser->id,
])->with(['serviceUser'])
->asArray()->one();
// 把ServiceUser信息存在Redis里面使用DB 1key前缀加上doctor_wx区分
$ServiceUserData = [
'user' => [
'id' => $ServiceUser->id,
'mobile' => $ServiceUser->mobile ?? '',
'nickname' => $ServiceUser->nickname ?? '微信用户',
'role' => $ServiceUser->role,
'status' => $ServiceUser->status,
'plate_type' => $ServiceUser->plate_type,
]
];
\Yii::$app->redis->select(1);
\Yii::$app->redis->set('xiaokang_database_xk_user_doctor_wx_' . $token, json_encode($ServiceUserData));
return [
'token'=>$token,
'data'=>$StoreDoctor
];
break;
//线下
case 2:
$transaction = \Yii::$app->db->beginTransaction();
try {
if (!$ServiceUser) {
$ServiceUser = new ServiceUser();
}
if ($ServiceUser->status==UserStatusEnum::FORBID && $ServiceUser->role==UserRoleEnum::DOCTOR){
throw new Exception('您的账号已停用,请联系后台工作人员!!');
}
$ServiceUser->openid = $login['openid'];
$ServiceUser->session_key = $login['session_key'];
$ServiceUser->unionid = isset($login['unionid']) ? $login['unionid'] : '';
$ServiceUser->nickname = $ServiceUser['nickname'] ?? '微信用户';
$ServiceUser->plate_type = $this->plate_type;
$ServiceUser->role=UserRoleEnum::DOCTOR;
$ServiceUser->saveOrFail();
$ServiceUserToken = new ServiceUserToken();
$ServiceUserToken->su_id = $ServiceUser->id;
$ServiceUserToken->token = $token;
$ServiceUserToken->saveOrFail();
$StoreDoctor = StoreDoctor::find()->where([
'su_id' => $ServiceUser->id,
'is_delete' => 0,
])->one();
if (!$StoreDoctor){
throw new Exception('您还没有门店');
}
//更新门店登录时间
$StoreDoctor->id=$StoreDoctor->id;
$StoreDoctor->last_login_time=time();
$StoreDoctor->saveOrFail();
$transaction->commit();
} catch (Exception $exception) {
$transaction->rollBack();
throw new Exception('登录失败:' . $exception->getMessage() . ' APPID:' . WechatService::getInstance()->app->getConfig()['app_id']);
}
$StoreDoctor = StoreDoctor::find()->where([
'su_id' => $ServiceUser->id,
])->with(['serviceUser'])->asArray()->one();
// 把ServiceUser信息存在Redis里面使用DB 1key前缀加上doctor_wx区分
$ServiceUserData = [
'user' => [
'id' => $ServiceUser->id,
'mobile' => $ServiceUser->mobile ?? '',
'nickname' => $ServiceUser->nickname ?? '微信用户',
'role' => $ServiceUser->role,
'status' => $ServiceUser->status,
'plate_type' => $ServiceUser->plate_type,
]
];
\Yii::$app->redis->select(1);
\Yii::$app->redis->set('xiaokang_database_xk_user_doctor_wx_' . $token, json_encode($ServiceUserData));
return [
'token'=>$token,
'data'=>$StoreDoctor,
'ServiceInfo'=>$ServiceUser
];
default:
throw new \yii\db\Exception('参数错误');
}
}
/**
* 手机号登录
*/
public function phoneLogin()
{
$cache = \Yii::$app->cache;
if ($cache->get('login_sms_code_'.$this->mobile) || $this->smsCode=='999999') {
if ($this->smsCode!='999999' && $cache->get('login_sms_code_'.$this->mobile) != $this->smsCode) {
throw new Exception('手机验证码错误');
}
$t = \Yii::$app->db->beginTransaction();
try {
$ServiceUser = ServiceUser::find()->where([
'mobile' => $this->mobile,
'is_delete'=>0
])->one();
if (!$ServiceUser) throw new Exception('您还没有注册,请先注册账号');
if ($ServiceUser->status==UserStatusEnum::FORBID && $ServiceUser->role==UserRoleEnum::DOCTOR){
throw new Exception('您的账号已停用,请联系后台工作人员!!');
}
$ServiceUser->nickname = $ServiceUser->nickname ?? '微信用户';
$ServiceUser->saveOrFail();
$ServiceUserToken = new ServiceUserToken();
$ServiceUserToken->su_id = $ServiceUser->id;
$token = \Yii::$app->security->generateRandomString() . '_' . time();
$ServiceUserToken->token = $token;
$ServiceUserToken->saveOrFail();
$StoreDoctor = StoreDoctor::find()->where([
'su_id' => $ServiceUser->id,
'is_online' => 1,
'is_delete' => 0,
])->one();
if (!$StoreDoctor){
$StoreDoctor=new StoreDoctor();
$StoreDoctor->su_id=$ServiceUser->id;
$StoreDoctor->is_online=1;
$StoreDoctor->store_id=\Yii::$app->store;
$StoreDoctor->last_login_time=time();
$StoreDoctor->saveOrFail();
}else{
\Yii::$app->db->createCommand()->update(StoreDoctor::tableName(),['is_online'=>0],['su_id'=>$ServiceUser->id])->execute();
//更新门店登录时间
$StoreDoctor->is_online=1;
$StoreDoctor->last_login_time=time();
if (!$StoreDoctor->saveOrFail())throw new Exception('更新门店登录时间失败');
}
$t->commit();
// 把ServiceUser信息存在Redis里面使用DB 1key前缀加上doctor_wx区分
$ServiceUserData = [
'user' => [
'id' => $ServiceUser->id,
'mobile' => $ServiceUser->mobile ?? '',
'nickname' => $ServiceUser->nickname ?? '微信用户',
'role' => $ServiceUser->role,
'status' => $ServiceUser->status,
'plate_type' => $ServiceUser->plate_type,
]
];
\Yii::$app->redis->select(1);
\Yii::$app->redis->set('xiaokang_database_xk_user_doctor_wx_' . $token, json_encode($ServiceUserData));
return [
'token'=>$token,
'StoreDoctor'=>$StoreDoctor,
'ServiceUser'=>$ServiceUser
];
} catch (\Exception $e) {
$t->rollBack();
throw new Exception($e->getMessage());
}
} else {
throw new Exception('验证码已过期,请从新获取验证码');
}
}
/**
* 手机号注册
*/
public function PhoneRegister()
{
$token = \Yii::$app->security->generateRandomString() . '_' . time();
$cache = \Yii::$app->cache;
if ($cache->get('login_sms_code_'.$this->mobile)) {
if ($cache->get('login_sms_code_'.$this->mobile) != $this->smsCode) {
throw new Exception('手机验证码错误');
}
switch ($this->plate_type){
case 1://线上
$ServiceUser = ServiceUser::find()->where(
['mobile' => $this->mobile,'is_delete'=>0]
)->one();
$transaction = \Yii::$app->db1->beginTransaction();
try {
$sql="select * from yii_service_user where mobile=$this->mobile";
$is_exist=\Yii::$app->db1->createCommand($sql)->queryOne();
$id=$is_exist['id'];
if (!$is_exist){
\Yii::$app->db1->createCommand()->insert('yii_service_user',[
'mobile'=>$this->mobile,
'role'=>UserRoleEnum::DOCTOR,
'status'=>UserStatusEnum::WAIT_JH,
'created_at'=>time(),
'updated_at'=>time(),
])->execute();
$su_id=\Yii::$app->db1->getLastInsertID();
\Yii::$app->db1->createCommand()->insert('yii_service_user_token',[
'su_id'=>$su_id,
'token'=>$token,
'created_at'=>time(),
'updated_at'=>time(),
])->execute();
//添加线下数据
if (!$ServiceUser){
$Service_Users=new ServiceUser();
$Service_Users->mobile = $this->mobile;
$Service_Users->nickname = '微信用户';
$Service_Users->role=UserRoleEnum::DOCTOR;
$Service_Users->plate_type = $this->plate_type;
$Service_Users->saveOrFail();
$ServiceUserToken = new ServiceUserToken();
$ServiceUserToken->su_id = $Service_Users->id;
$ServiceUserToken->token = $token;
$ServiceUserToken->saveOrFail();
}
$platform_doctor_id=$ServiceUser['id']??$Service_Users->id;
$service_id=$id??$su_id;
$sql="select * from yii_doctor_platform where su_id=$service_id and platform_doctor_id=$platform_doctor_id";
$doctor_platform=\Yii::$app->db1->createCommand($sql)->queryOne();
$platform_id=1;
if (!$doctor_platform){
//用户平台关系表
\Yii::$app->db1->createCommand()->insert('yii_doctor_platform',[
'su_id'=>$service_id,
'platform_doctor_id'=>$platform_doctor_id,
'platform_id'=>$platform_id,
'platform_store_id'=>\Yii::$app->store,
'created_at'=>time(),
'updated_at'=>time(),
])->execute();
}
$transaction->commit();
return [
'token'=>$token,
'data'=>$ServiceUser??$Service_Users
];
}
if (!$ServiceUser){
$Service_Users=new ServiceUser();
$Service_Users->mobile = $this->mobile;
$Service_Users->nickname = '微信用户';
$Service_Users->role=UserRoleEnum::DOCTOR;
$Service_Users->plate_type = $this->plate_type;
$Service_Users->saveOrFail();
$ServiceUserToken = new ServiceUserToken();
$ServiceUserToken->su_id = $Service_Users->id;
$ServiceUserToken->token = $token;
$ServiceUserToken->saveOrFail();
return [
'token'=>$token,
'data'=>$ServiceUser??$Service_Users
];
}else{
throw new Exception('您已注册,请去登录');
}
}catch (\Exception $e){
$transaction->rollBack();
throw new Exception($e->getMessage());
}
break;
case 2://线下
$t = \Yii::$app->db->beginTransaction();
try {
$ServiceUser = ServiceUser::find()->where(
['mobile' => $this->mobile,'is_delete'=>0]
)->one();
if (!$ServiceUser){
$ServiceUser=new ServiceUser();
}
$ServiceUser->mobile = $this->mobile;
$ServiceUser->nickname = '微信用户';
$ServiceUser->plate_type = $this->plate_type;
$ServiceUser->role=$this->role;
$ServiceUser->status=UserStatusEnum::WAIT_JH;
$ServiceUser->save();
$ServiceUserToken = new ServiceUserToken();
$ServiceUserToken->su_id = $ServiceUser->id;
$ServiceUserToken->token = $token;
$ServiceUserToken->save();
$t->commit();
return [
'token'=>$token,
'ServiceUser'=>$ServiceUser
];
} catch (\Exception $e) {
$t->rollBack();
throw new Exception($e->getMessage());
}
default:
return [];
}
} else {
throw new Exception('验证码已过期,请从新获取验证码');
}
}
}