163 lines
5.6 KiB
PHP
163 lines
5.6 KiB
PHP
<?php
|
||
|
||
/**
|
||
* @author Any
|
||
* @description KISS
|
||
* @date 2021年6月7日
|
||
* @version 1.0.0
|
||
*
|
||
* _____LOG_____
|
||
*
|
||
*/
|
||
namespace app\modules\api\models;
|
||
|
||
use app\models\UserOauth;
|
||
use app\components\wechat\aes\WxBizDataCrypt;
|
||
use app\components\SysConst;
|
||
use app\components\FlashStorage;
|
||
use app\components\EncryptHelper;
|
||
use app\components\Utils;
|
||
|
||
class BindOauthWxmpForm extends ApiModel
|
||
{
|
||
public $code;
|
||
public $encrypted_data;
|
||
public $iv;
|
||
|
||
public $code_expires;//code是否过期,0=否,1=是
|
||
|
||
public $cx_mch_id;
|
||
public $user_id;
|
||
public $wechat_mp;
|
||
|
||
public function rules()
|
||
{
|
||
return [
|
||
[['code', 'iv', 'encrypted_data', ], 'trim'],
|
||
[['code', 'iv', 'encrypted_data', ], 'string'],
|
||
[['code_expires', 'user_id'], 'integer'],
|
||
[['code', 'iv', 'encrypted_data', 'cx_mch_id', 'user_id', 'wechat_mp'], 'required'],
|
||
];
|
||
}
|
||
|
||
public function bind()
|
||
{
|
||
if(!$this->validate()){
|
||
return $this->getModelError();
|
||
}
|
||
$res = FlashStorage::getCache("m{$this->cx_mch_id}_c{$this->code}");
|
||
if($this->code_expires == 1 || $res === false){
|
||
$res = $this->code2session($this->code);
|
||
if($res['code'] != 0)
|
||
return $res;
|
||
FlashStorage::setCache("m{$this->cx_mch_id}_c{$this->code}",$res,864000);
|
||
}
|
||
|
||
$session_key = $res['data']['session_key'];
|
||
$openid = $res['data']['openid'];
|
||
$res = $this->decrypted_data($session_key);
|
||
if($res['code'] != 0){
|
||
return $res;
|
||
}
|
||
$decrypted_info = json_decode($res['data'], true);
|
||
$openid = $openid ? $openid : $decrypted_info['openId'];
|
||
$nickname = $decrypted_info['nickName'];
|
||
$avatar_url = empty($decrypted_info['avatarUrl']) ? User::DEFAULT_AVATAR_URL : $decrypted_info['avatarUrl'];
|
||
$gender = $decrypted_info['gender'];
|
||
$unionid = isset($decrypted_info['unionId']) ? $decrypted_info['unionId'] : '0';
|
||
|
||
//用户是否存在
|
||
$user_oauth = UserOauth::findOne([
|
||
'cx_mch_id' => $this->cx_mch_id,
|
||
'is_delete' => 0,
|
||
'type' => SysConst::$cxOauthProviderWxmp,
|
||
'openid' => $openid
|
||
]);
|
||
if($user_oauth != null){
|
||
/*if($user_oauth->user_id == $this->user_id){
|
||
return $this->apiReturnSuccess("绑定成功");
|
||
}
|
||
$mobile = $user_oauth->user && !empty($user_oauth->user->mobile_phone) ? EncryptHelper::decryptMobilePhone($user_oauth->user->mobile_phone) : null;
|
||
$account_name = $mobile ? $mobile : ($user_oauth->user ? $user_oauth->user->username : "");
|
||
if(strlen($account_name) != 0)
|
||
$account_name = Utils::stringDesensitization ($account_name,2,-2);
|
||
return $this->apiReturnError("绑定失败,此微信已绑定{$account_name}账号");*/
|
||
$user_oauth->user_id = $this->user_id;
|
||
$user_oauth->unionid = $unionid;
|
||
$user_oauth->created_at = time();
|
||
$user_oauth->nickname = $nickname;
|
||
$user_oauth->avatar_url = $avatar_url;
|
||
}else{
|
||
$user_oauth = new UserOauth();
|
||
$user_oauth->cx_mch_id = $this->cx_mch_id;
|
||
$user_oauth->type = SysConst::$cxOauthProviderWxmp;
|
||
$user_oauth->user_id = $this->user_id;
|
||
$user_oauth->openid = $openid;
|
||
$user_oauth->unionid = $unionid;
|
||
$user_oauth->is_delete = 0;
|
||
$user_oauth->created_at = time();
|
||
$user_oauth->nickname = $nickname;
|
||
$user_oauth->avatar_url = $avatar_url;
|
||
}
|
||
|
||
// 删除用户信息缓存
|
||
$obj = new WindowsApiForm();
|
||
$cache = $obj->getUserCacheName($this->user_id);
|
||
foreach ($cache as $key=>$val){
|
||
FlashStorage::deleteCache($val);
|
||
}
|
||
|
||
if(!$user_oauth->save()){
|
||
return $this->getModelError($user_oauth);
|
||
}
|
||
return $this->apiReturnSuccess("绑定成功");
|
||
}
|
||
|
||
|
||
/***
|
||
* 用户数据解密
|
||
*/
|
||
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
|
||
];
|
||
}
|
||
}
|
||
|