WxUser.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace app\common\library\wechat;
  3. /**
  4. * 微信小程序用户管理类
  5. * Class WxUser
  6. * @package app\common\library\wechat
  7. */
  8. class WxUser extends WxBase
  9. {
  10. /**
  11. * 获取session_key
  12. * @param $code
  13. * @return array|mixed
  14. */
  15. public function sessionKey($code)
  16. {
  17. /**
  18. * code 换取 session_key
  19. * ​这是一个 HTTPS 接口,开发者服务器使用登录凭证 code 获取 session_key 和 openid。
  20. * 其中 session_key 是对用户数据进行加密签名的密钥。为了自身应用安全,session_key 不应该在网络上传输。
  21. */
  22. $url = 'https://api.weixin.qq.com/sns/jscode2session';
  23. $result = json_decode(curl($url, [
  24. 'appid' => $this->appId,
  25. 'secret' => $this->appSecret,
  26. 'grant_type' => 'authorization_code',
  27. 'js_code' => $code
  28. ]), true);
  29. if (isset($result['errcode'])) {
  30. $this->error = $result['errmsg'];
  31. return false;
  32. }
  33. return $result;
  34. }
  35. }