123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?php
- namespace app\common\library;
- use fast\Http;
- use think\Cache;
- use think\Session;
- class Wechat
- {
- private $app_id = '';
- private $app_secret = '';
- private $scope = 'snsapi_userinfo';
- public function __construct()
- {
- $wxConfig = config('user_wxMiniProgram');
- $this->app_id = $wxConfig['appid'];
- $this->app_secret = $wxConfig['secret'];
- }
-
-
-
-
- public function getOpenid($code = '')
- {
- $openid = Session::get('openid');
- if (!$openid) {
- $token = $this->getAccessToken($code);
- $openid = isset($token['openid']) ? $token['openid'] : '';
- if ($openid) {
- Session::set("openid", $openid);
- }
- }
- return $openid;
- }
-
- public function getwxuserinfo($code = '')
- {
- $wxuserinfo = Session::get('wxuserinfo');
- if (!$wxuserinfo) {
- $token = $this->getAccessToken($code);
- $openid = isset($token['openid']) ? $token['openid'] : '';
- $access_token = isset($token['access_token']) ? $token['access_token'] : '';
- $ret = Http::sendRequest('https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN');
- if ($ret['ret']) {
- $wxuserinfo = json_decode($ret['msg'], true);
- Session::set('wxuserinfo', $wxuserinfo);
- }else{
- $wxuserinfo = [];
- }
- }
- return $wxuserinfo;
- }
-
- public function getAccessToken($code = '')
- {
- $params = [
- 'appid' => $this->app_id,
- 'secret' => $this->app_secret,
- 'code' => $code,
- 'grant_type' => 'authorization_code'
- ];
- $ret = Http::sendRequest('https://api.weixin.qq.com/sns/oauth2/access_token', $params, 'GET');
- if ($ret['ret']) {
- $ar = json_decode($ret['msg'], true);
- return $ar;
- }
- return [];
- }
-
-
-
-
- }
|