Files
xk-api-yii/service/modules/v1/controllers/UserController.php
2025-07-22 15:11:14 +08:00

287 lines
9.0 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\modules\v1\controllers;
use common\core\BaseServiceController;
use common\models\oldDb\DoctorInfo;
use common\models\oldDb\HospitalYard;
use common\models\oldDb\ServiceUserToken;
use common\services\SmsService;
use common\services\WeappService;
use service\models\forms\AgreementForm;
use service\models\forms\LoginForm;
use service\models\forms\MobileForm;
use service\models\forms\ResetPasswordForm;
use service\models\forms\UpdatePasswordForm;
use service\models\ServiceUser;
use yii\db\Exception;
/**
* @doc-module 用户
*/
class UserController extends BaseServiceController
{
public $optional = ['user-login', 'send-code', 'forget-password', 'agreement', 'reset-password', 'mobile-send','check-idcard'];
/**
* @doc-name 服务端登录注册
* @doc-desc 手机号注册和微信登录时必传plate_type,注册时必传role
* @doc-param int role 平台:2角色1医生2药师3导医4客服 / optional
* @doc-param int plate_type 平台:2线下 2 optional
* @doc-param string mobile 手机号 / optional
* @doc-param string smsCode 验证码 / optional
* @doc-param string code 微信code / optional
* @doc-param int status 1微信登录3手机号登录4手机号注册5开发登录
* @doc-return mixed @StoreDoctor{*} 医生门店信息
* @doc-return mixed @ServiceUser{*} 服务端用户信息
*/
public function actionUserLogin()
{
$post = \Yii::$app->request->post();
$this->requestValidate($post, [
['status', 'required']
]);
$form = new LoginForm();
$form->attributes = $post;
return $form->userlogin();
}
/**
* @doc-name 选择角色
* @doc-param int role 角色1医生2药师3导医4客服
* @doc-param string mobile 手机号 / optional
*/
public function actionRole()
{
$post = \Yii::$app->request->post();
$this->requestValidate($post, [
[['role', 'mobile'], 'required']
]);
$ServiceUser = \common\models\oldDb\ServiceUser::findOne(['mobile' => $post['mobile']]);
if (!$ServiceUser) {
throw new Exception('您还没有任何账号');
}
ServiceUser::updateAll(['role' => $post['role']], [
'mobile' => $post['mobile'],
'is_delete' => 0
]);
return [];
}
/**
* @doc-name 忘记密码重置密码
* @doc-param string mobile 手机号
* @doc-param string password_new 新密码
* @doc-param string mobile_code 验证码
*/
public function actionResetPassword()
{
$form = new ResetPasswordForm();
$form->attributes = \Yii::$app->request->post();
return $form->resetPasswordAndLogin();
}
/**
* @doc-name 忘记密码发送短信
* @doc-param string mobile 手机号
*/
public function actionMobileSend()
{
$form = new MobileForm;
$form->attributes = \Yii::$app->request->post();
return $form->sendCode();
}
/**
* @doc-name 用户退出
*/
public function actionLogout()
{
$token = ServiceUser::$token;
ServiceUserToken::disableToken($token);
return [];
}
/**
* @doc-name 用户信息
* @doc-return mixed ServiceUser{*} 用户信息
*/
public function actionInfo()
{
return \Yii::$app->user->identity;
}
/**
* @doc-name 协议
* @doc-param int end 1用户端2服务端
*/
public function actionAgreement()
{
$form = new AgreementForm();
$form->attributes = \Yii::$app->request->post();
return $form->getAgreement();
}
/**
* @doc-name 修改密码
* @doc-param string password_old 旧密码
* @doc-param string password_new 新密码
*/
public function actionUpdatePassword()
{
$form = new UpdatePasswordForm();
$form->attributes = \Yii::$app->request->post();
return $form->updatePassword();
}
/**
* @doc-name 手机号登录发送短信
* @doc-param string mobile 手机号码
* @doc-return string smsCode 验证码
*/
public function actionSendCode()
{
$code = (string)mt_rand(100000, 999999);
$post = \Yii::$app->request->post();
$this->requestValidate($post, [
['mobile', 'required']
]);
$message = [
'content' => '您的验证码为:" ' . $code . ' ",请勿泄露与他人',
'template' => 'SMS_274460204',
'data' => [
'code' => $code
]
];
$cache = \Yii::$app->cache;
$cache->set( 'login_sms_code_'.$post['mobile'], $code, 600);
$cache->set( 'login_sms_time_'.$post['mobile'], time(), 600);
$response = (new SmsService())->sendCaptcha($post['mobile'], $code);
return [$response];
}
public function actionToken()
{
return [
'token' => (new WeappService())->getAccessToken()
];
}
/**
* @doc-name 修改医生信息
* @doc-param string avatar 头像 '' optional
* @doc-param int title_id 职称(1主任医师2副主任医师3主治医师4执业医师) 0 optional
* @doc-param int yard_id 医院院区 0 optional
* @doc-param int depart_id 科室 0 optional
* @doc-param string good_at 擅长 '' optional
* @doc-param string intro 简介 '' optional
* @doc-param string work_avator 资格证书 '' optional
* @return array
*/
public function actionUpdateUserInfo()
{
$attributes = \Yii::$app->request->post();
//$model = new DoctorInfo();
$model = DoctorInfo::find()->where([
'su_id' => \Yii::$app->user->identity->id,
])->one();
#修改头像
if(isset($attributes['avatar']) && !empty($attributes['avatar'])){
$model->avatar = $attributes['avatar'];
}
#修改职称
if(isset($attributes['title_id']) && !empty($attributes['title_id'])){
$model->title_id = $attributes['title_id'];
}
#修改医院院区
if(isset($attributes['yard_id']) && !empty($attributes['yard_id'])){
$yard = HospitalYard::find()->where([
'id' => $attributes['yard_id'],
])->one();
$model->hospital_id = $yard->hospital_id;
$model->yard_id = $attributes['yard_id'];
}
#修改科室
if(isset($attributes['depart_id']) && !empty($attributes['depart_id'])){
$model->depart_id = $attributes['depart_id'];
}
#修改擅长
if(isset($attributes['good_at']) && !empty($attributes['good_at'])){
$model->good_at = $attributes['good_at'];
}
#修改简介
if(isset($attributes['intro']) && !empty($attributes['intro'])){
$model->intro = $attributes['intro'];
}
$model->saveOrFail();
}
public function actionCheckIdcard(){
$post = \Yii::$app->request->post();
// TODO 暂时取消验证
$this->requestValidate($post, [
[['name', 'idcard'], 'required']
]);
/* $response = (new Client(['http_errors' => false]))->post("https://eid.shumaidata.com/eid/check", [
'headers' => ['Authorization' => "APPCODE f98bba3251714c759f5bd29d00e1c46e"],
'query' => [
'idcard' => $post['idcard'],
'name' => $post['name']
],
]);
$result = json_decode($response->getBody(), true);
if (!($result['code'] == 0 && $result['result']['res'] == 1)) {
throw new Exception('姓名和身份证号码不符');
}*/
return ['success'];
}
/**
* @doc-name 审核失败后重新注册账号
* @doc-param int id 用户ID
*/
public function actionFailRegister()
{
$get = \Yii::$app->request->get();
$this->requestValidate($get, [
['id', 'required']
]);
$ServiceUser=ServiceUser::findOne(['id'=>$get['id'],'status'=>3]);
if (!$ServiceUser) throw new Exception('账号不存在或账号非审核失败状态');
$t=\Yii::$app->db->beginTransaction();
try {
$ServiceUser->status=3;
$ServiceUser->reason=null;
$ServiceUser->saveOrFail();
// if ($ServiceUser->role==UserRoleEnum::DOCTOR){
// $DoctorInfo=DoctorInfo::find()->where(['su_id'=>$get['id']])->one();
// if ($DoctorInfo){
// $DoctorInfo->is_delete=1;
// $DoctorInfo->saveOrFail();
// }
// }
//
// if ($ServiceUser->role==UserRoleEnum::DRUG){
// $PharmacistrInfo=PharmacistrInfo::find()->where(['su_id'=>$get['id']])->one();
// if ($PharmacistrInfo){
// $PharmacistrInfo->delete();
// }
// }
$t->commit();
}catch (\Exception $e){
$t->rollBack();
throw new Exception($e->getMessage());
}
}
}