Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

施宇 / icsales

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Settings
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Find file
Normal viewHistoryPermalink
Switch branch/tag
  • icsales
  • ThinkPHP
  • Library
  • Vendor
  • Wechat
  • MiniProgram.php
MiniProgram.php 1.79 KB
Edit
施宇's avatar
init
14f95149
 
施宇 committed 5 years ago
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
<?php
/**
 * 微信小程序
 */
class MiniProgram
{
    const API_URL = 'https://api.weixin.qq.com/sns/jscode2session';
    static public $CODE = array(
        '11024' => '获取access_token失败',
        '11025' => '微信错误信息',
        '11028' => '获取用户微信信息失败',
    );

    /**
     * 返回固定信息
     * @param  integer $errcode [description]
     * @param  string  $err_msg [description]
     * @param  array   $data    [description]
     * @return [type]           [description]
     */
    public function returnMsg($errcode = 0, $err_msg = '', $data = array())
    {
        if (empty($err_msg) && !empty(self::$CODE[$errcode])) {
            $err_msg = self::$CODE[$errcode];
        }
        $res = array(
            'err_code' => $errcode,
            'err_msg' => $err_msg,
            'data' => $data,
        );
        return $res;
    }

    public function __construct($options = array())
    {
        $this->appid        = isset($options['appid']) ? $options['appid'] : C('MINI_PROGRAM_CONFIG.appid');
        $this->appsecret    = isset($options['appsecret']) ? $options['appsecret'] : C('MINI_PROGRAM_CONFIG.appsecret');
    }

    /**
     * 获取sessionkey(返回session_key,openid,unionid)
     * @param  [type] $code [description]
     * @return [type]       [description]
     */
    public function getSessionKey($code)
    {
        $url = self::API_URL;
        $param = array(
            'appid' => $this->appid,
            'secret' => $this->appsecret,
            'js_code' => $code,
            'grant_type' => 'authorization_code',
        );
        $res = get_curl($url, $param);
        $info = json_decode($res, true);
        if (empty($info)) {
            return $this->returnMsg(11024);
        }
        return $this->returnMsg(0, '', $info);
    }

}