cxgj/modules/api/controllers/AuthController.php
2023-11-27 09:45:13 +08:00

397 lines
16 KiB
PHP
Raw Permalink 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
/**
* @author Any
* @description KISS
* @date 2020-12-2
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\modules\api\controllers;
use app\modules\api\behaviors\LoginBehavior;
use app\modules\api\models\LoginForm;
use app\modules\api\models\LogoutForm;
use app\modules\api\models\LoginByMobileForm;
use app\modules\api\models\LoginByWxmpMobileForm;
use app\modules\api\models\SignupByMobileForm;
use app\modules\api\models\LoginByWxmpForm;
use app\modules\api\models\ResetPasswordForm;
use app\modules\api\models\BindMobilePhoneForm;
use app\modules\api\models\BindOauthWxmpForm;
use app\modules\api\models\WxmpAuthMobileForm;
use app\models\UserToken;
use app\components\auth\AToken;
class AuthController extends Controller
{
public function behaviors() {
return array_merge(parent::behaviors(),[
'login' => [
'class' => LoginBehavior::className(),
'ignore' =>[
'api/auth/login',
'api/auth/login-by-mobile',
'api/auth/login-by-wxmp',
'api/auth/signup-by-mobile',
'api/auth/access-token',
'api/auth/reset-pwd',
'api/auth/wxmp-auth-mobile',
'api/auth/login-by-wxmp-mobile',
],
'bind_phone_ignore' =>[
'api/auth/bind-phone',
],
'bind_oauth_ignore' =>[
'api/auth/bind-wxmp',
]
]
]);
}
/**
* showdoc
* @catalog 登录注册
* @title 账号密码登录
* @description 本接口提供账号密码登录
* @method post
* @url /api/auth/login
* @param username 必选 string 用户名/手机号
* @param password 必选 string 密码
* @return {"code":0,"msg":"ok","data":{"access_token":"WXl0dXlzbDhxcTdTcExyQUVmVWRoLzV6cnJrK04rV1RocHVpTzdGTWxRajhLVzA3Vk9hWEhBPT0=","refresh_token":"aDcxQTc4ZDFtUGlWOEh6akUwaVVQQ2NuZGNxdTJhcDQ5cGxSZll6Mk9PWUJPdmhRQU1RMW5DWDl1SFllVFZrbWVvU1JCWEdOSUl2S29rU3I3NndobWVTN0kxNlhjSFhwODREYlRFUkFmZVhUNUpQZHkxd1oveWlnOVdQa2JzSHp6K1dZS1dtRVJBSzFwMnBnVnNmdncvRTgvWDRiSWhpUA==","access_token_expires":1592392165,"refresh_token_expires":1594976965}}
* @return_param access_token string 登录令牌
* @return_param access_token_expires int 登录令牌失效时间
* @return_param refresh_token string 刷新token令牌
* @return_param refresh_token_expires int 刷新token令牌失效时间
* @remark
*/
public function actionLogin()
{
if(!\Yii::$app->request->isPost){
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
$form = new LoginForm();
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$form->token_type = $this->_cx_token_type;
$data = $form->login();
return $this->responseHandler($data);
}
/**
* showdoc
* @catalog 登录注册
* @title 手机验证码登录
* @description 本接口提供手机验证码登录
* @method post
* @url /api/auth/login-by-mobile
* @param mobile 必选 string 手机号
* @param code 必选 string 验证码
* @param mobile_prefix 必选 string 手机号国家代码
* @return {"code":0,"msg":"ok","data":{"access_token":"WXl0dXlzbDhxcTdTcExyQUVmVWRoLzV6cnJrK04rV1RocHVpTzdGTWxRajhLVzA3Vk9hWEhBPT0=","refresh_token":"aDcxQTc4ZDFtUGlWOEh6akUwaVVQQ2NuZGNxdTJhcDQ5cGxSZll6Mk9PWUJPdmhRQU1RMW5DWDl1SFllVFZrbWVvU1JCWEdOSUl2S29rU3I3NndobWVTN0kxNlhjSFhwODREYlRFUkFmZVhUNUpQZHkxd1oveWlnOVdQa2JzSHp6K1dZS1dtRVJBSzFwMnBnVnNmdncvRTgvWDRiSWhpUA==","access_token_expires":1592392165,"refresh_token_expires":1594976965}}
* @return_param access_token string 登录令牌
* @return_param access_token_expires int 登录令牌失效时间
* @return_param refresh_token string 刷新token令牌
* @return_param refresh_token_expires int 刷新token令牌失效时间
* @remark
*/
public function actionLoginByMobile()
{
if(!\Yii::$app->request->isPost){
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
$form = new LoginByMobileForm();
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$form->token_type = $this->_cx_token_type;
$data = $form->login();
return $this->responseHandler($data);
}
/**
* showdoc
* @catalog 登录注册
* @title 微信小程序登录
* @description 本接口提供微信登录
* @method post
* @url /api/auth/login-by-wxmp
* @param code 必选 string wx.login() 获取 临时登录凭证code
* @param iv 必选 string 加密算法的初始向量
* @param encrypted_data 必选 string 包括敏感数据在内的完整用户信息的加密数据
* @return {"code":0,"msg":"ok","data":{"access_token":"WXl0dXlzbDhxcTdTcExyQUVmVWRoLzV6cnJrK04rV1RocHVpTzdGTWxRajhLVzA3Vk9hWEhBPT0=","refresh_token":"aDcxQTc4ZDFtUGlWOEh6akUwaVVQQ2NuZGNxdTJhcDQ5cGxSZll6Mk9PWUJPdmhRQU1RMW5DWDl1SFllVFZrbWVvU1JCWEdOSUl2S29rU3I3NndobWVTN0kxNlhjSFhwODREYlRFUkFmZVhUNUpQZHkxd1oveWlnOVdQa2JzSHp6K1dZS1dtRVJBSzFwMnBnVnNmdncvRTgvWDRiSWhpUA==","access_token_expires":1592392165,"refresh_token_expires":1594976965}}
* @return_param access_token string 登录令牌
* @return_param access_token_expires int 登录令牌失效时间
* @return_param refresh_token string 刷新token令牌
* @return_param refresh_token_expires int 刷新token令牌失效时间
* @remark
*/
public function actionLoginByWxmp()
{
if(!\Yii::$app->request->isPost){
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
if(!$this->lock->acquire()){
$data = ['code' => 1, 'msg' => '系统繁忙!稍后再试^v^!'];
return $this->responseHandler($data);
}
$form = new LoginByWxmpForm();
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$form->token_type = $this->_cx_token_type;
$form->wechat_mp = $this->wechat_mp;
$data = $form->login();
$this->lock->release();
return $this->responseHandler($data);
}
/**
* showdoc
* @catalog 登录注册
* @title 手机号注册
* @description 本接口提供手机号注册
* @method post
* @url /api/auth/signup-by-mobile
* @param mobile 必选 string 手机号
* @param code 必选 string 验证码
* @param password 必选 string 密码
* @param password_repeat 必选 string 确认密码
* @param mobile_prefix 非必选 string 手机号国家代码默认86
* @return {"code":0,"msg":"ok","data":{"access_token":"WXl0dXlzbDhxcTdTcExyQUVmVWRoLzV6cnJrK04rV1RocHVpTzdGTWxRajhLVzA3Vk9hWEhBPT0=","refresh_token":"aDcxQTc4ZDFtUGlWOEh6akUwaVVQQ2NuZGNxdTJhcDQ5cGxSZll6Mk9PWUJPdmhRQU1RMW5DWDl1SFllVFZrbWVvU1JCWEdOSUl2S29rU3I3NndobWVTN0kxNlhjSFhwODREYlRFUkFmZVhUNUpQZHkxd1oveWlnOVdQa2JzSHp6K1dZS1dtRVJBSzFwMnBnVnNmdncvRTgvWDRiSWhpUA==","access_token_expires":1592392165,"refresh_token_expires":1594976965}}
* @return_param access_token string 登录令牌
* @return_param access_token_expires int 登录令牌失效时间
* @return_param refresh_token string 刷新token令牌
* @return_param refresh_token_expires int 刷新token令牌失效时间
* @remark
*/
public function actionSignupByMobile()
{
if(!\Yii::$app->request->isPost){
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
if(!$this->lock->acquire()){
$data = ['code' => 1, 'msg' => '系统繁忙!稍后再试^v^!'];
return $this->responseHandler($data);
}
$form = new SignupByMobileForm();
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$form->token_type = $this->_cx_token_type;
$data = $form->signup();
$this->lock->release();
return $this->responseHandler($data);
}
/**
* showdoc
* @catalog 登录注册
* @title 账号退出
* @description 本接口提供账号退出
* @method post
* @url /api/auth/logout
* @return {"code":0,"msg":"ok","data":{}}
* @remark
*/
public function actionLogout()
{
if(!\Yii::$app->request->isPost){
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
$form = new LogoutForm();
$form->cx_mch_id = $this->cx_mch_id;
$form->user_id = \Yii::$app->user->isGuest ? 0 : \Yii::$app->user->identity->id;
$form->token_type = $this->_cx_token_type;
$data = $form->logout();
return $this->responseHandler($data);
}
/**
* showdoc
* @catalog 登录注册
* @title 刷新access_token
* @description 使用refresh_token换取access_token
* @method get
* @url /api/auth/access-token
* @param refresh_token 必选 string 刷新令牌token
* @return {"code":0,"msg":"ok","data":{"access_token":"VTQ2VTROQ2QvSGhwZmN3L1pOTWJiTmtpaXh5MVBwMlpRQXk3eDVtTUFWWS90YnJpTUVWampZdW90RldiRHN0N2drRGJ2Vms4MWp6S3l0Q3VRc3BYQUdzLzRneE9WbGZ0S0UrT3FsaDlRRmc9","access_token_expires":1592296898}}
* @return_param access_token string 登录令牌
* @return_param access_token_expires int 登录令牌失效时间
* @remark
*/
public function actionAccessToken()
{
$args = [];
$args['cx_mch_id'] = $this->cx_mch_id;
$args['token_type'] = $this->_cx_token_type;
$atoken = new AToken($args);
$data = $atoken->refresh_access_token();
return $this->responseHandler($data);
}
/**
* showdoc
* @catalog 登录注册
* @title 密码找回
* @description 本接口提供账号密码找回
* @method post
* @url /api/auth/reset-pwd
* @param mobile 必选 string 手机号
* @param code 必选 string 验证码
* @param mobile_prefix 必选 string 手机号国家代码
* @param password 必选 string 密码
* @param password_repeat 必选 string 重复密码
* @return {"code":0,"msg":"密码重置成功","data":{}}
* @remark
*/
public function actionResetPwd()
{
if(!\Yii::$app->request->isPost){
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
$form = new ResetPasswordForm();
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$data = $form->reset();
return $this->responseHandler($data);
}
/**
* showdoc
* @catalog 登录注册
* @title 手机号绑定
* @description 本接口提供手机号绑定
* @method post
* @url /api/auth/bind-phone
* @param type 必选 int 绑定类型0=微信授权绑定1=手机验证码验证绑定
* @param mobile 必选(使用手机验证码绑定) string 手机号
* @param mobile_prefix 必选 string 手机号国家代码
* @param code 必选 string type=1时验证码type=0时wx.login()获取临时登录凭证code
* @param iv 必选 string 加密算法的初始向量
* @param encrypted_data 必选 string 包括敏感数据在内的完整用户信息的加密数据
* @return {"code":0,"msg":"手机号绑定成功","data":{}}
* @remark
*/
public function actionBindPhone()
{
if(!\Yii::$app->request->isPost){
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
$type = \Yii::$app->request->post('type');
$form = new BindMobilePhoneForm();
if($type == 0){
$form->scenario = 'wxmp_auth_bind';
$form->wechat_mp = $this->wechat_mp;
}
if($type == 1){
$form->scenario = 'mobile_captcha';
}
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$form->user_id = \Yii::$app->user->identity->id;
$data = $form->bind();
return $this->responseHandler($data);
}
/**
* showdoc
* @catalog 登录注册
* @title 微信授权手机号登录
* @description 本接口提供微信授权手机号登录
* @method post
* @url /api/auth/login-by-wxmp-mobile
* @param code 必选 string wx.login()获取临时登录凭证code
* @param iv 必选 string 加密算法的初始向量
* @param encrypted_data 必选 string 包括敏感数据在内的完整用户信息的加密数据
* @param pid 非必选 int 上级id,分享链接上面的用户id
* @return {"code":0,"msg":"ok","data":{"access_token":"WXl0dXlzbDhxcTdTcExyQUVmVWRoLzV6cnJrK04rV1RocHVpTzdGTWxRajhLVzA3Vk9hWEhBPT0=","refresh_token":"aDcxQTc4ZDFtUGlWOEh6akUwaVVQQ2NuZGNxdTJhcDQ5cGxSZll6Mk9PWUJPdmhRQU1RMW5DWDl1SFllVFZrbWVvU1JCWEdOSUl2S29rU3I3NndobWVTN0kxNlhjSFhwODREYlRFUkFmZVhUNUpQZHkxd1oveWlnOVdQa2JzSHp6K1dZS1dtRVJBSzFwMnBnVnNmdncvRTgvWDRiSWhpUA==","access_token_expires":1592392165,"refresh_token_expires":1594976965}}
* @return_param access_token string 登录令牌
* @return_param access_token_expires int 登录令牌失效时间
* @return_param refresh_token string 刷新token令牌
* @return_param refresh_token_expires int 刷新token令牌失效时间
* @remark
*/
public function actionLoginByWxmpMobile()
{
if(!\Yii::$app->request->isPost){
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
$form = new LoginByWxmpMobileForm();
$form->scenario = 'wxmp_auth_bind';
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$form->token_type = $this->_cx_token_type;
$form->wechat_mp = $this->wechat_mp;
$data = $form->login();
return $this->responseHandler($data);
}
/**
* showdoc
* @catalog 登录注册
* @title 绑定微信小程序登录
* @description 本接口提供绑定微信小程序登录
* @method post
* @url /api/auth/bind-wxmp
* @param code 必选 string wx.login() 获取 临时登录凭证code
* @param iv 必选 string 加密算法的初始向量
* @param encrypted_data 必选 string 包括敏感数据在内的完整用户信息的加密数据
* @return {"code":0,"msg":"ok","data":{}}
* @remark
*/
public function actionBindWxmp()
{
if(!\Yii::$app->request->isPost){
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
$form = new BindOauthWxmpForm();
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$form->wechat_mp = $this->wechat_mp;
$form->user_id = \Yii::$app->user->identity->id;
$data = $form->bind();
return $this->responseHandler($data);
}
/**
* showdoc
* @catalog 登录注册
* @title 获取微信授权手机号
* @description 本接口提供获取微信授权手机号
* @method post
* @url /api/auth/wxmp-auth-mobile
* @param code 必选 string wx.login()获取临时登录凭证code
* @param iv 必选 string 加密算法的初始向量
* @param encrypted_data 必选 string 包括敏感数据在内的完整用户信息的加密数据
* @return {"code":0,"msg":"ok","data":{"mobile":18888888888,"mobile_prefix":86}}
* @remark
*/
public function actionWxmpAuthMobile()
{
if(!\Yii::$app->request->isPost){
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
$form = new WxmpAuthMobileForm();
$form->attributes = \Yii::$app->request->post();
$form->wechat_mp = $this->wechat_mp;
$form->cx_mch_id = $this->cx_mch_id;
$data = $form->auth();
return $this->responseHandler($data);
}
}