cxfoot/modules/api/models/LoginByWxoaForm.php
2023-11-22 11:17:56 +08:00

146 lines
4.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
/**
* @author Any
* @description KISS
* @date 2020-12-3
* @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;
class LoginByWxoaForm extends ApiModel
{
public $code;
public $code_expires;//code是否过期0=否1=是
public $cx_mch_id;
public $token_type;
public $wechat_mp;
public function rules()
{
return [
[['code'], 'trim'],
[['code'], 'string'],
[['code_expires'], 'integer'],
[['code', 'token_type', 'wechat_mp'], 'required'],
];
}
public function login()
{
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);
}
if ($res['code'] != 0) {
return $res;
}
$access_token = $res['data']['access_token'];
$openid = $res['data']['openid']??'';
$user_oauth = UserOauth::findOne([
'cx_mch_id' => $this->cx_mch_id,
'is_delete' => 0,
'openid' => $openid
]);
if (empty($user_oauth)) {
$user_oauth = new UserOauth();
$user_oauth->cx_mch_id = 0;
$user_oauth->type = 'wxoa';
$user_oauth->openid = $openid;
$user_oauth->created_at = time();
$user_oauth->user_id = 0;
$user_oauth->is_delete = 0;
$res = $user_oauth->save();
if (!$res) {
return ['code' => 1, 'msg' => '登陆失败'];
}
}
$data = ['code' => 0, 'msg' => '登陆成功', 'openid' => $openid];
return $data;
}
/***
* 用户数据解密
*/
private function curl_wechat_user($access_token, $openid)
{
$api = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $access_token . "&openid=" . $openid . "&lang=zh_CN";
$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['nickname']) || !isset($res['headimgurl'])) {
return [
'code' => 1,
'msg' => isset($res['errmsg']) ? $res['errmsg'] : 'error'
];
}
return [
'code' => 0,
'msg' => 'ok',
'data' => $res
];
}
private function code2session($code)
{
$api = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->wechat_mp->appId}&secret={$this->wechat_mp->appSecret}&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['access_token']) || !isset($res['openid'])) {
return [
'code' => 1,
'msg' => isset($res['errmsg']) ? $res['errmsg'] : 'error'
];
}
return [
'code' => 0,
'msg' => 'ok',
'data' => $res
];
}
}