1. 小程序端代码
- var App = getApp();
- var url = 'http://shzujune.com/mianya/public/index/index/wxlogin';
- var login = function (code, encrypteData, iv) {
- var that = this
- // 创建一个 dialog 提示
- wx.showToast({
- title: '正在登录...',
- icon: 'loading',
- duration: 5000
- });
- wx.request({
- url: url,
- method: 'get',
- data: {
- code: code,
- encrypteData: encrypteData,
- iv: iv
- },
- header: {
- 'Content-Type': 'application/json'
- },
- success: function (res) {
- wx.hideToast()
- //console.log('服务器返回' + res.data)
- App.globalData.userInfo = res.data
- },
- fail: function () {
- wx.showToast({
- title: '网络错误!',
- duration: 2000
- })
- },
- complete: function () {
- }
- })
- }
- Page({
- data: {
- // 判断小程序的 API, 回调, 参数, 组件等是否在当前版本可用.
- canIUse: wx.canIUse('button.open-type.getUserInfo')
- },
- onLoad: function () {
- var that = this
- wx.login({
- success: function (res) { // 登录成功
- //console.log(res)
- if (res.code) {
- var code = res.code
- wx.getUserInfo({ //getUserInfo 流程
- success: function (data) { //getUserInfo 获取用户信息成功
- //console.log(data)
- //encrypteData 加密密文, iv 偏移向量, encodeURIComponent 把加密字符串解密成 URI 字符串
- var encryptedData = encodeURIComponent(data.encryptedData);
- var iv = data.iv;
- // 请求自己的服务器
- login(code, encryptedData, iv);
- // 已经授权的用户
- wx.switchTab({
- url: '../rec/rec',
- })
- }
- })
- } else {
- console.log('用户没有进行授权!' + res.errMsg)
- }
- }
- });
- },
- bindGetUserInfo: function (e) {
- //console.log(e)
- if (e.detail.userInfo) {
- // 用户按了允许授权的按钮
- var that = this
- wx.login({
- success: function (res) {
- if (res.code) {
- var code = res.code
- wx.getUserInfo({
- success: function (data) {
- var encryptedData = encodeURIComponent(data.encryptedData);
- var iv = data.iv;
- // 请求自己的服务器
- login(code, encryptedData, iv);
- }
- })
- }
- }
- })
- // 授权成功后, 跳转进入小程序首页
- wx.switchTab({
- url: '../rec/rec'
- })
- } else {
- // 用户按了拒绝按钮
- wx.showModal({
- title: '警告',
- content: '您点击了拒绝授权, 将无法进入小程序, 请授权之后再进入!!!',
- showCancel: false,
- confirmText: '返回授权',
- success: function (res) {
- if (res.confirm) {
- console.log('用户点击了" 返回授权 "')
- }
- }
- })
- }
- },
- })
2.PHP 端代码
- <?PHP
- namespace App\teacherapi\controller;
- use think\Controller;
- /**
- * @date: 2018-12
- * 微信操作类
- */
- class WxDecode extends Controller
- {
- public function httpGet($url) {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_TIMEOUT, 500);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($curl, CURLOPT_URL, $url);
- $res = curl_exec($curl);
- curl_close($curl);
- return $res;
- }
- /**
- * @author: zxf
- * @date: 2018-12-08
- * @description: 解密微信用户敏感数据
- * @return array
- */
- public function WxDecode()
- {
- // 接收参数
- $data = request() -> param();
- // 引入解密文件 在微信小程序开发文档下载
- vendor('wx.WXBizDataCrypt');
- vendor('wx.ErrorCode');
- $appid = config('TESTPPID');
- $appsecret = config('TESTSECREET');
- $grant_type = "authorization_code"; // 授权 (必填)
- $code = $data['code']; // 有效期 5 分钟 登录会话
- $encryptedData=$data['encryptedData'];
- $iv = $data['iv'];
- $signature = $data['signature'];
- $rawData = $data['rawData'];
- // 拼接 url
- $url = "https://api.weixin.qq.com/sns/jscode2session?"."appid=".$appid."&secret=".$appsecret."&js_code=".$code."&grant_type=".$grant_type;
- $res = json_decode($this->httpGet($url),true);
- $sessionKey = $res['session_key']; // 取出 JSON 里对应的值
- $signature2 = sha1(htmlspecialchars_decode($rawData).$sessionKey);
- // 验证签名
- if ($signature2 !== $signature){
- return JSON("验签失败");
- }
- // 获取解密后的数据
- $pc = new \WXBizDataCrypt($appid, $sessionKey);
- $errCode = $pc->decryptData($encryptedData, $iv, $data );
- if ($errCode == 0) {
- return return_succ($data);
- } else {
- return return_error($errCode);
- }
- }
- }
来源: http://www.bubuko.com/infodetail-3497629.html