| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
- namespace api\modules\v1\forms;
- use Yii;
- use yii\base\Model;
- /**
- * Class MiniProgramLoginForm
- * @package api\modules\v1\models
- */
- class MiniProgramLoginForm extends Model
- {
- public $iv;
- public $rawData;
- public $encryptedData;
- public $signature;
- public $code;
- public $auth;
- /**
- * @var string
- */
- protected $openid;
- /**
- * @var string
- */
- protected $unionid;
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['iv', 'rawData', 'encryptedData', 'signature', 'code'], 'required'],
- [['signature'], 'authVerify'],
- ];
- }
- public function attributeLabels()
- {
- return [
- 'iv' => '加密算法的初始向量',
- 'rawData' => '不包括敏感信息的原始数据字符串,用于计算签名',
- 'encryptedData' => '包括敏感数据在内的完整用户信息的加密数据',
- 'signature' => '签名',
- 'code' => 'code码',
- 'auth' => '授权秘钥',
- ];
- }
- /**
- * @param $attribute
- * @throws \EasyWeChat\Kernel\Exceptions\HttpException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
- * @throws \Psr\SimpleCache\InvalidArgumentException
- * @throws \yii\web\UnprocessableEntityHttpException
- */
- public function authVerify($attribute)
- {
- $auth = Yii::$app->wechat->miniProgram->auth->session($this->code);
- // 解析是否接口报错
- Yii::$app->services->base->getWechatError($auth);
- $sign = sha1(htmlspecialchars_decode($this->rawData . $auth['session_key']));
- if ($sign !== $this->signature) {
- $this->addError($attribute, '签名错误');
- return;
- }
- $this->auth = $auth;
- $this->openid = $auth['openid'] ?? '';
- $this->unionid = $auth['unionid'] ?? '';
- }
- /**
- * @return mixed
- */
- public function getOpenid()
- {
- return $this->openid;
- }
- /**
- * @return mixed
- */
- public function getUnionId()
- {
- return $this->unionid;
- }
- /**
- * @return array
- * @throws \EasyWeChat\Kernel\Exceptions\DecryptException
- */
- public function getUser()
- {
- $user = Yii::$app->wechat->miniProgram->encryptor->decryptData($this->auth['session_key'], $this->iv, $this->encryptedData);
- !empty($this->openid) && $user['openId'] = $this->openid;
- !empty($this->unionid) && $user['unionId'] = $this->unionid;
- return $user;
- }
- }
|