WxUser.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\library\wechat;
  13. use app\common\library\helper;
  14. use cores\exception\BaseException;
  15. /**
  16. * 微信小程序用户管理类
  17. * Class WxUser
  18. * @package app\common\library\wechat
  19. */
  20. class WxUser extends WxBase
  21. {
  22. /**
  23. * 获取session_key
  24. * @param string $code
  25. * @return array|false
  26. * @throws BaseException
  27. */
  28. public function jscode2session(string $code)
  29. {
  30. /**
  31. * code 换取 session_key
  32. * 这是一个 HTTPS 接口,开发者服务器使用登录凭证 code 获取 session_key 和 openid。
  33. * 其中 session_key 是对用户数据进行加密签名的密钥。为了自身应用安全,session_key 不应该在网络上传输。
  34. */
  35. $url = 'https://api.weixin.qq.com/sns/jscode2session';
  36. $result = helper::jsonDecode($this->get($url, [
  37. 'appid' => $this->appId,
  38. 'secret' => $this->appSecret,
  39. 'grant_type' => 'authorization_code',
  40. 'js_code' => $code
  41. ]));
  42. if (isset($result['errcode'])) {
  43. $this->error = $result['errmsg'];
  44. return false;
  45. }
  46. return $result;
  47. }
  48. }