190 lines
5.5 KiB
PHP
190 lines
5.5 KiB
PHP
<?php
|
||
|
||
/**
|
||
* @author Any
|
||
* @description KISS
|
||
* @date 2021-5-17
|
||
* @version 1.0.0
|
||
*
|
||
* _____LOG_____
|
||
*
|
||
*/
|
||
namespace app\modules\api\models;
|
||
|
||
|
||
use app\models\User;
|
||
use app\components\auth\AToken;
|
||
use app\components\wechat\aes\WxBizDataCrypt;
|
||
use app\models\UserOauth;
|
||
use app\models\common\CommonUserEditForm;
|
||
use app\components\SysConst;
|
||
use app\components\FlashStorage;
|
||
use app\components\EncryptHelper;
|
||
use app\models\sms\SmsMsgHelper;
|
||
use app\models\sms\SmsTpl;
|
||
|
||
class BindMobilePhoneForm extends ApiModel
|
||
{
|
||
public $user_id;
|
||
|
||
public $iv;
|
||
public $code;
|
||
public $encrypted_data;
|
||
|
||
public $wechat_mp;
|
||
public $cx_mch_id;
|
||
|
||
public $mobile;
|
||
public $mobile_prefix;
|
||
|
||
public $type;//绑定类型,0=微信授权绑定,1=手机验证码
|
||
|
||
|
||
|
||
public function rules()
|
||
{
|
||
return [
|
||
[['type'], 'integer'],
|
||
[['iv', 'code', 'encrypted_data', 'mobile', 'mobile_prefix', 'code'], 'trim'],
|
||
[['iv', 'code', 'encrypted_data', 'mobile', 'mobile_prefix', 'code'], 'string'],
|
||
[['user_id', 'cx_mch_id', 'type'], 'required'],
|
||
[['mobile_prefix'], 'default', 'value' => 86],
|
||
[['mobile', 'mobile_prefix', 'code'], 'required', 'on' => 'mobile_captcha'],
|
||
[['iv', 'code', 'encrypted_data', 'wechat_mp',], 'required', 'on' => 'wxmp_auth_bind'],
|
||
];
|
||
}
|
||
|
||
public function bind()
|
||
{
|
||
if(!$this->validate()){
|
||
return $this->getModelError();
|
||
}
|
||
//用户是否绑定手机号
|
||
if(\Yii::$app->user->identity->isBindPhone){
|
||
return [
|
||
'code' => 1,
|
||
'msg' => '手机号已绑定,无需重复绑定'
|
||
];
|
||
}
|
||
if($this->type == 0)
|
||
return $this->bind_by_wxmp_auth();
|
||
if($this->type == 1)
|
||
return $this->bind_by_mobile_captcha();
|
||
return [
|
||
'code' => 1,
|
||
'msg' => '无效绑定类型'
|
||
];
|
||
}
|
||
|
||
|
||
//微信授权绑定
|
||
public function bind_by_wxmp_auth()
|
||
{
|
||
$res = $this->code2session($this->code);
|
||
if($res['code'] != 0)
|
||
return $res;
|
||
$session_key = $res['data']['session_key'];
|
||
$res = $this->decrypted_data($session_key);
|
||
if($res['code'] != 0){
|
||
return $res;
|
||
}
|
||
$res['data'] = json_decode($res['data'],true);
|
||
if(!isset($res['data']['purePhoneNumber'])){
|
||
return [
|
||
'code' => 1,
|
||
'msg' => '数据解析失败',
|
||
'data' => $res['data']
|
||
];
|
||
}
|
||
$this->mobile = $res['data']['purePhoneNumber'];//不带区号的手机号
|
||
$this->mobile_prefix = $res['data']['countryCode'];
|
||
return $this->bind_mobile();
|
||
}
|
||
|
||
//手机短信验证码绑定
|
||
public function bind_by_mobile_captcha()
|
||
{
|
||
//验证码校验
|
||
$sms_sender = new SmsMsgHelper($this->cx_mch_id);
|
||
$res = $sms_sender->validate($this->mobile, $this->code, SmsTpl::TYPE_ALTER_INFO, $this->mobile_prefix, $this->user_id);
|
||
if($res['code'] != 0){
|
||
return $res;
|
||
}
|
||
return $this->bind_mobile();
|
||
}
|
||
|
||
private function bind_mobile()
|
||
{
|
||
$user = User::findOne([
|
||
'id' => $this->user_id,
|
||
'is_delete' => 0,
|
||
'status' => User::STATUS_NORMAL,
|
||
'cx_mch_id' => $this->cx_mch_id,
|
||
]);
|
||
if($user == null){
|
||
return [
|
||
'code' => 1,
|
||
'msg' => '用户不存在'
|
||
];
|
||
}
|
||
$form = new CommonUserEditForm();
|
||
$form->scenario = 'bind_mobile';
|
||
$form->model = $user;
|
||
$form->cx_mch_id = $this->cx_mch_id;
|
||
$form->mobile_phone = $this->mobile;
|
||
$form->mobile_prefix = $this->mobile_prefix;
|
||
$res = $form->save();
|
||
if($res['code'] != 0)
|
||
return $res;
|
||
return [
|
||
'code' => 0,
|
||
'msg' => '手机号绑定成功'
|
||
];
|
||
}
|
||
|
||
|
||
/***
|
||
* 用户数据解密
|
||
*/
|
||
private function decrypted_data($session_key){
|
||
$pc = new WxBizDataCrypt($this->wechat_mp->appId, $session_key);
|
||
$errCode = $pc->decryptData($this->encrypted_data, $this->iv, $data );
|
||
if ($errCode == 0) {
|
||
return [
|
||
'code' => 0,
|
||
'msg' => 'success',
|
||
'data' => $data
|
||
];
|
||
} else {
|
||
return [
|
||
'code' => 1,
|
||
'msg' => $errCode,
|
||
];
|
||
}
|
||
}
|
||
|
||
private function code2session($code)
|
||
{
|
||
$api = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->wechat_mp->appId}&secret={$this->wechat_mp->appSecret}&js_code={$code}&grant_type=authorization_code";
|
||
$this->wechat_mp->curl->get($api);
|
||
if($this->wechat_mp->curl->error_code != 0){
|
||
return [
|
||
'code' => 1,
|
||
'msg' => "err_code:{$this->wechat_mp->curl->error_code}err_msg:{$this->wechat_mp->curl->error_msg}"
|
||
];
|
||
}
|
||
$resp = $this->wechat_mp->curl->response;
|
||
$res = json_decode($resp, true);
|
||
if(!isset($res['openid'])){
|
||
return [
|
||
'code' => 1,
|
||
'msg' => isset($res['errmsg']) ? $res['errmsg'] : 'error'
|
||
];
|
||
}
|
||
return [
|
||
'code' => 0,
|
||
'msg' => 'ok',
|
||
'data' => $res
|
||
];
|
||
}
|
||
} |