WxUser.php 1.1 KB

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