MiniProgramDecodeForm.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace api\modules\v1\forms;
  3. use Yii;
  4. use yii\base\Model;
  5. /**
  6. * Class MiniProgramLoginForm
  7. * @package api\modules\v1\models
  8. */
  9. class MiniProgramDecodeForm extends Model
  10. {
  11. public $iv;
  12. public $encryptedData;
  13. public $code;
  14. public $auth;
  15. /**
  16. * @inheritdoc
  17. */
  18. public function rules()
  19. {
  20. return [
  21. [['iv', 'encryptedData', 'code'], 'required'],
  22. [['code'], 'authVerify'],
  23. ];
  24. }
  25. public function attributeLabels()
  26. {
  27. return [
  28. 'iv' => '加密算法的初始向量',
  29. 'encryptedData' => '包括敏感数据在内的完整用户信息的加密数据',
  30. 'code' => 'code码',
  31. ];
  32. }
  33. /**
  34. * @param $attribute
  35. * @throws \EasyWeChat\Kernel\Exceptions\HttpException
  36. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  37. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  38. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  39. * @throws \Psr\SimpleCache\InvalidArgumentException
  40. * @throws \yii\web\UnprocessableEntityHttpException
  41. */
  42. public function authVerify($attribute)
  43. {
  44. $auth = Yii::$app->wechat->miniProgram->auth->session($this->code);
  45. // 解析是否接口报错
  46. Yii::$app->services->base->getWechatError($auth);
  47. $this->auth = $auth;
  48. }
  49. /**
  50. * @return array
  51. * @throws \EasyWeChat\Kernel\Exceptions\DecryptException
  52. */
  53. public function getUser()
  54. {
  55. return Yii::$app->wechat->miniProgram->encryptor->decryptData($this->auth['session_key'], $this->iv, $this->encryptedData);
  56. }
  57. }
粤ICP备19079148号