WxUser.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 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. /**
  15. * 微信小程序用户管理类
  16. * Class WxUser
  17. * @package app\common\library\wechat
  18. */
  19. class WxUser extends WxBase
  20. {
  21. /**
  22. * 获取session_key
  23. * @param string $code
  24. * @return array|mixed
  25. */
  26. public function jscode2session(string $code)
  27. {
  28. /**
  29. * code 换取 session_key
  30. * 这是一个 HTTPS 接口,开发者服务器使用登录凭证 code 获取 session_key 和 openid。
  31. * 其中 session_key 是对用户数据进行加密签名的密钥。为了自身应用安全,session_key 不应该在网络上传输。
  32. */
  33. $url = 'https://api.weixin.qq.com/sns/jscode2session';
  34. $result = helper::jsonDecode($this->get($url, [
  35. 'appid' => $this->appId,
  36. 'secret' => $this->appSecret,
  37. 'grant_type' => 'authorization_code',
  38. 'js_code' => $code
  39. ]));
  40. if (isset($result['errcode'])) {
  41. $this->error = $result['errmsg'];
  42. return false;
  43. }
  44. return $result;
  45. }
  46. }