php(ThinkPHP)实现微信小程序的登录过程

源码也在我的github中给出

https://github.com/wulongtao/think-wxminihelper

下面结合thinkPHP框架来实现以下微信小程序的登录流程,这些流程是结合了官网和github的一个网站综合实现的

https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-login.html?t=2017112#wxloginobject

https://github.com/cantoo/learning-wxapp/blob/master/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F%E7%99%BB%E5%BD%95%E6%80%81%E9%AA%8C%E8%AF%81%E6%B5%81%E7%A8%8B.md

我已经把登录流程做了一下简单的封装,你也可以直接使用composer下载直接使用:

composer require xxh/think-wxminihelper
  • 1

登录流程图参考了如下两个图:

这里写图片描述

这里写图片描述

按照上面的步骤,代码实现如下:

/**
 * 登录
 */
function wxLogin(func) {
    //调用登录接口
    //1.小程序调用wx.login得到code.
    wx.login({
      success: function (res) {
        var code = res['code'];
        //2.小程序调用wx.getUserInfo得到rawData, signatrue, encryptData.
        wx.getUserInfo({
          success: function (info) {
            console.log(info);
            var rawData = info['rawData'];
            var signature = info['signature'];
            var encryptData = info['encryptData'];
            var encryptedData = info['encryptedData']; //注意是encryptedData不是encryptData...坑啊
            var iv = info['iv'];

            //3.小程序调用server获取token接口, 传入code, rawData, signature, encryptData.
            wx.request({
              url: constants.LOGIN_URL,
              data: {
                "code" : code,
                "rawData" : rawData,
                "signature" : signature,
                "encryptData" : encryptData,
                'iv' : iv,
                'encryptedData': encryptedData
              },
              success: function(res) {
                if(res.statusCode != 200) {
                    wx.showModal({
                        title: '登录失败'
                    });
                }
                typeof func == "function" && func(res.data);
              }
            });
          }
        });

      }
    });


}
  • 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
public function wxLogin() {
    /**
     * 3.小程序调用server获取token接口, 传入code, rawData, signature, encryptData.
     */
    $code = input("code", '', 'htmlspecialchars_decode');
    $rawData = input("rawData", '', 'htmlspecialchars_decode');
    $signature = input("signature", '', 'htmlspecialchars_decode');
    $encryptedData = input("encryptedData", '', 'htmlspecialchars_decode');
    $iv = input("iv", '', 'htmlspecialchars_decode');

    /**
     * 4.server调用微信提供的jsoncode2session接口获取openid, session_key, 调用失败应给予客户端反馈
     * , 微信侧返回错误则可判断为恶意请求, 可以不返回. 微信文档链接
     * 这是一个 HTTP 接口,开发者服务器使用登录凭证 code 获取 session_key 和 openid。其中 session_key 是对用户数据进行加密签名的密钥。
     * 为了自身应用安全,session_key 不应该在网络上传输。
     * 接口地址:"https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code"
     */
    $params = [
        'appid' => $this->appid,
        'secret' => $this->secret,
        'js_code' => $code,
        'grant_type' => $this->grant_type
    ];
    $res = makeRequest($this->url, $params);

    if ($res['code'] !== 200 || !isset($res['result']) || !isset($res['result'])) {
        return json(ret_message('requestTokenFailed'));
    }
    $reqData = json_decode($res['result'], true);
    if (!isset($reqData['session_key'])) {
        return json(ret_message('requestTokenFailed'));
    }
    $sessionKey = $reqData['session_key'];

    /**
     * 5.server计算signature, 并与小程序传入的signature比较, 校验signature的合法性, 不匹配则返回signature不匹配的错误. 不匹配的场景可判断为恶意请求, 可以不返回.
     * 通过调用接口(如 wx.getUserInfo)获取敏感数据时,接口会同时返回 rawData、signature,其中 signature = sha1( rawData + session_key )
     *
     * 将 signature、rawData、以及用户登录态发送给开发者服务器,开发者在数据库中找到该用户对应的 session-key
     * ,使用相同的算法计算出签名 signature2 ,比对 signature 与 signature2 即可校验数据的可信度。
     */
    $signature2 = sha1($rawData . $sessionKey);

    if ($signature2 !== $signature) return ret_message("signNotMatch");

    /**
     *
     * 6.使用第4步返回的session_key解密encryptData, 将解得的信息与rawData中信息进行比较, 需要完全匹配,
     * 解得的信息中也包括openid, 也需要与第4步返回的openid匹配. 解密失败或不匹配应该返回客户相应错误.
     * (使用官方提供的方法即可)
     */
    $pc = new WXBizDataCrypt($this->appid, $sessionKey);
    $errCode = $pc->decryptData($encryptedData, $iv, $data );

    if ($errCode !== 0) {
        return json(ret_message("encryptDataNotMatch"));
    }


    /**
     * 7.生成第三方3rd_session,用于第三方服务器和小程序之间做登录态校验。为了保证安全性,3rd_session应该满足:
     * a.长度足够长。建议有2^128种组合,即长度为16B
     * b.避免使用srand(当前时间)然后rand()的方法,而是采用操作系统提供的真正随机数机制,比如Linux下面读取/dev/urandom设备
     * c.设置一定有效时间,对于过期的3rd_session视为不合法
     *
     * 以 $session3rd 为key,sessionKey+openId为value,写入memcached
     */
    $data = json_decode($data, true);
    $session3rd = randomFromDev(16);

    $data['session3rd'] = $session3rd;
    cache($session3rd, $data['openId'] . $sessionKey);



    return json($data);
}
  • 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
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77

一些用到的工具函数


/**
 * 返回信息
 * @param $message
 * @return array
 */
function ret_message($message = "") {
    if ($message == "") return ['result'=>0, 'message'=>''];
    $ret = lang($message);

    if (count($ret) != 2) {
        return ['result'=>-1,'message'=>'未知错误'];
    }
    return array(
        'result'  => $ret[0],
        'message' => $ret[1]
    );
}

/**
 * 发起http请求
 * @param string $url 访问路径
 * @param array $params 参数,该数组多于1个,表示为POST
 * @param int $expire 请求超时时间
 * @param array $extend 请求伪造包头参数
 * @param string $hostIp HOST的地址
 * @return array    返回的为一个请求状态,一个内容
 */
function makeRequest($url, $params = array(), $expire = 0, $extend = array(), $hostIp = '')
{
    if (empty($url)) {
        return array('code' => '100');
    }

    $_curl = curl_init();
    $_header = array(
        'Accept-Language: zh-CN',
        'Connection: Keep-Alive',
        'Cache-Control: no-cache'
    );
    // 方便直接访问要设置host的地址
    if (!empty($hostIp)) {
        $urlInfo = parse_url($url);
        if (empty($urlInfo['host'])) {
            $urlInfo['host'] = substr(DOMAIN, 7, -1);
            $url = "http://{$hostIp}{$url}";
        } else {
            $url = str_replace($urlInfo['host'], $hostIp, $url);
        }
        $_header[] = "Host: {$urlInfo['host']}";
    }

    // 只要第二个参数传了值之后,就是POST的
    if (!empty($params)) {
        curl_setopt($_curl, CURLOPT_POSTFIELDS, http_build_query($params));
        curl_setopt($_curl, CURLOPT_POST, true);
    }

    if (substr($url, 0, 8) == 'https://') {
        curl_setopt($_curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($_curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    }
    curl_setopt($_curl, CURLOPT_URL, $url);
    curl_setopt($_curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($_curl, CURLOPT_USERAGENT, 'API PHP CURL');
    curl_setopt($_curl, CURLOPT_HTTPHEADER, $_header);

    if ($expire > 0) {
        curl_setopt($_curl, CURLOPT_TIMEOUT, $expire); // 处理超时时间
        curl_setopt($_curl, CURLOPT_CONNECTTIMEOUT, $expire); // 建立连接超时时间
    }

    // 额外的配置
    if (!empty($extend)) {
        curl_setopt_array($_curl, $extend);
    }

    $result['result'] = curl_exec($_curl);
    $result['code'] = curl_getinfo($_curl, CURLINFO_HTTP_CODE);
    $result['info'] = curl_getinfo($_curl);
    if ($result['result'] === false) {
        $result['result'] = curl_error($_curl);
        $result['code'] = -curl_errno($_curl);
    }

    curl_close($_curl);
    return $result;
}

/**
 * 读取/dev/urandom获取随机数
 * @param $len
 * @return mixed|string
 */
function randomFromDev($len) {
    $fp = @fopen('/dev/urandom','rb');
    $result = '';
    if ($fp !== FALSE) {
        $result .= @fread($fp, $len);
        @fclose($fp);
    }
    else
    {
        trigger_error('Can not open /dev/urandom.');
    }
    // convert from binary to string
    $result = base64_encode($result);
    // remove none url chars
    $result = strtr($result, '+/', '-_');

    return substr($result, 0, $len);
}
  • 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
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112

猜你喜欢

转载自blog.csdn.net/wangmj518/article/details/79192333