ByteDanceMicroLoginForm.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace api\modules\v1\forms;
  3. use Yii;
  4. use yii\base\Model;
  5. /**
  6. * Class ByteDanceMicroLoginForm
  7. * @package api\modules\v1\forms
  8. * @author jianyan74 <751393839@qq.com>
  9. */
  10. class ByteDanceMicroLoginForm extends Model
  11. {
  12. public $iv;
  13. public $rawData;
  14. public $encryptedData;
  15. public $signature;
  16. public $code;
  17. public $auth;
  18. /**
  19. * @var string
  20. */
  21. protected $openid;
  22. /**
  23. * 匿名用户在当前小程序的 ID,如果请求时有 anonymous_code 参数才会返回
  24. *
  25. * @var string
  26. */
  27. protected $anonymous_openid;
  28. /**
  29. * @var
  30. */
  31. protected $unionid;
  32. /**
  33. * @inheritdoc
  34. */
  35. public function rules()
  36. {
  37. return [
  38. [['iv', 'rawData', 'encryptedData', 'signature', 'code'], 'required'],
  39. [['signature'], 'authVerify'],
  40. ];
  41. }
  42. public function attributeLabels()
  43. {
  44. return [
  45. 'iv' => '加密算法的初始向量',
  46. 'rawData' => '不包括敏感信息的原始数据字符串,用于计算签名',
  47. 'encryptedData' => '包括敏感数据在内的完整用户信息的加密数据',
  48. 'signature' => '签名',
  49. 'code' => 'code码',
  50. 'auth' => '授权秘钥',
  51. ];
  52. }
  53. /**
  54. * @param $attribute
  55. * @throws \EasyWeChat\Kernel\Exceptions\HttpException
  56. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  57. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  58. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  59. * @throws \Psr\SimpleCache\InvalidArgumentException
  60. * @throws \yii\web\UnprocessableEntityHttpException
  61. */
  62. public function authVerify($attribute)
  63. {
  64. $auth = Yii::$app->byteDance->miniProgram->auth->session($this->code);
  65. // 解析是否接口报错
  66. Yii::$app->services->base->getWechatError($auth);
  67. // $sign = hash_hmac("sha256", $this->rawData, $auth['session_key']);
  68. // if ($sign !== $this->signature) {
  69. // $this->addError($attribute, '签名错误');
  70. // return;
  71. // }
  72. $this->auth = $auth;
  73. $this->openid = $auth['openid'] ?? '';
  74. $this->anonymous_openid = $auth['anonymous_openid'] ?? '';
  75. $this->unionid = $auth['unionid'] ?? '';
  76. }
  77. /**
  78. * @return mixed
  79. */
  80. public function getOpenid()
  81. {
  82. return $this->openid;
  83. }
  84. /**
  85. * @return mixed
  86. */
  87. public function getAnonymousOpenid()
  88. {
  89. return $this->anonymous_openid;
  90. }
  91. /**
  92. * @return mixed
  93. */
  94. public function getUnionId()
  95. {
  96. return $this->unionid;
  97. }
  98. /**
  99. * @return array
  100. * @throws \EasyWeChat\Kernel\Exceptions\DecryptException
  101. */
  102. public function getUser()
  103. {
  104. $user = Yii::$app->wechat->miniProgram->encryptor->decryptData(
  105. $this->auth['session_key'],
  106. $this->iv,
  107. $this->encryptedData
  108. );
  109. !empty($this->openid) && $user['openid'] = $this->openid;
  110. !empty($this->unionid) && $user['unionid'] = $this->unionid;
  111. !empty($this->anonymous_openid) && $user['anonymous_openid'] = $this->anonymous_openid;
  112. return $user;
  113. }
  114. }
粤ICP备19079148号