141 lines
4.7 KiB
PHP
141 lines
4.7 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @author Any
|
|
* @description KISS
|
|
* @date 2021-1-26
|
|
* @version 1.0.0
|
|
*
|
|
* _____LOG_____
|
|
*
|
|
*/
|
|
namespace app\modules\api\models;
|
|
|
|
|
|
use app\models\common\CommonUserEditForm;
|
|
use app\models\User;
|
|
use app\components\Validation;
|
|
use app\components\auth\AToken;
|
|
use app\models\sms\SmsMsgHelper;
|
|
use app\models\sms\SmsTpl;
|
|
use app\components\EncryptHelper;
|
|
use app\components\SysErrCode;
|
|
|
|
|
|
class SignupByMobileForm extends ApiModel
|
|
{
|
|
public $mobile;
|
|
public $mobile_prefix;
|
|
public $code;
|
|
public $password;
|
|
public $password_repeat;
|
|
|
|
|
|
public $cx_mch_id;
|
|
public $token_type;
|
|
public $skip_code_verify; # 跳过验证码
|
|
|
|
public function rules()
|
|
{
|
|
return [
|
|
[['mobile', 'mobile_prefix', 'code', 'password', 'password_repeat'], 'trim'],
|
|
[['mobile', 'mobile_prefix', 'code', 'password', 'password_repeat'], 'string'],
|
|
[['cx_mch_id', 'token_type'], 'integer'],
|
|
[['mobile_prefix'], 'default', 'value' => 86],
|
|
[['mobile', 'code', 'cx_mch_id', 'token_type', 'password', 'password_repeat'], 'required'],
|
|
[['password', 'password_repeat'], 'string', 'min' => 6],
|
|
['password', 'compare', 'message' => '两次密码必须一致'],
|
|
];
|
|
}
|
|
|
|
public function attributeLabels() {
|
|
return [
|
|
'mobile' => '手机号',
|
|
'mobile_prefix' => '手机号国家代码',
|
|
'code' => '验证码',
|
|
'password' => '密码',
|
|
'password_repeat' => '确认密码',
|
|
];
|
|
}
|
|
|
|
public function signup()
|
|
{
|
|
if(!$this->validate()){
|
|
return $this->getModelError();
|
|
}
|
|
if(!Validation::is_mobile($this->mobile, $this->mobile_prefix)){
|
|
return [
|
|
'code' => 1,
|
|
'msg' => '手机号格式不正确'
|
|
];
|
|
}
|
|
$encrypted_mobile = EncryptHelper::encryptMobilePhone($this->mobile);
|
|
if(!$encrypted_mobile){
|
|
return [
|
|
'code' => 1,
|
|
'msg' => '系统内部错误,错误代码:'.SysErrCode::$encryptedMobilePhoneFailed
|
|
];
|
|
}
|
|
//手机号是否被注册
|
|
$is_exists = User::find()->where(['mobile_phone' => $encrypted_mobile, 'mobile_prefix' => $this->mobile_prefix, 'is_delete' => 0])->exists();
|
|
if($is_exists){
|
|
return [
|
|
'code' => 1,
|
|
'msg' => '手机号已被注册'
|
|
];
|
|
}
|
|
|
|
//验证码校验
|
|
$is_verify_pass = false;
|
|
$sms_sender = new SmsMsgHelper($this->cx_mch_id);
|
|
$res = $sms_sender->validate($this->mobile, $this->code, SmsTpl::TYPE_USER_SIGNUP, $this->mobile_prefix);
|
|
$res1 = $sms_sender->validate($this->mobile, $this->code, SmsTpl::TYPE_LOGIN_CONFIRM, $this->mobile_prefix);
|
|
if($res['code'] == 0 || $res1['code'] == 0){
|
|
$is_verify_pass = true;
|
|
}
|
|
if(!$is_verify_pass && empty($this->skip_code_verify)){
|
|
return $res;
|
|
}
|
|
|
|
|
|
$form = new CommonUserEditForm();
|
|
$form->scenario = 'mobile_signup';
|
|
$form->model = new User();
|
|
$form->cx_mch_id = $this->cx_mch_id;
|
|
$form->mobile_phone = $this->mobile;
|
|
$form->mobile_prefix = $this->mobile_prefix;
|
|
$form->username = User::generateUsername(3, "Hw", 6);
|
|
$form->nickname = User::generateNickname(3, "HY_", 8);
|
|
$form->password = \Yii::$app->security->generatePasswordHash($this->password);
|
|
$form->avatar_url = User::DEFAULT_AVATAR_URL;
|
|
$form->access_token = \Yii::$app->security->generateRandomString();
|
|
// $form->real_name = "微信用户".$form->username;
|
|
$form->is_modify_un = 1;
|
|
$res = $form->save();
|
|
if($res['code'] != 0)
|
|
return $res;
|
|
|
|
$user = $form->model;
|
|
|
|
if(\Yii::$app->user->login($user,\Yii::$app->params['loginAuthExpireTime'])){
|
|
$args = [];
|
|
$args['cx_mch_id'] = $this->cx_mch_id;
|
|
$args['token_type'] = $this->token_type;
|
|
$atoken = new AToken($args);
|
|
$data = $atoken->generate_access_token();
|
|
if($data['code'] == 0){
|
|
$err_msg = "登录成功";
|
|
User::lastLogin($user->id, 1, $err_msg, $this->cx_mch_id);
|
|
//是否需要绑定
|
|
$data = User::bindQuery($user, $data);
|
|
}
|
|
} else {
|
|
$data = [
|
|
'code' => 0,
|
|
'msg' => '注册成功'
|
|
];
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
} |