1:小程序授权
Page({ data: { //判断小程序的API,回调,参数,组件等是否在当前版本可用。 canIUse: wx.canIUse('button.open-type.getUserInfo') }, onLoad: function () { var that = this; // 查看是否授权 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 encryptedData = info['encryptedData']; var iv = info['iv']; console.log(rawData); console.log(signature); console.log(encryptedData); console.log(code); console.log(iv); //3.小程序调用server获取token接口, 传入code, rawData, signature, encryptData. wx.request({ url: '接口地址', data: { "code": code, "rawData": rawData, "signature": signature, 'iv': iv, 'encryptedData': encryptedData }, method: 'POST', header: { 'content-type': 'application/x-www-form-urlencoded' // 默认值 }, success: function (res) { if (res.statusCode != 200) { wx.showModal({ title: '登录失败' }); }else{ console.log(res); } typeof func == "function" && func(res.data); } }); } }); } }) }, bindGetUserInfo: function (e) { if (e.detail.userInfo) { //用户按了允许授权按钮 var that = this; //授权成功后,跳转进入小程序首页 wx.switchTab({ url: '/pages/examination/examination' }) } else { //用户按了拒绝按钮 wx.showModal({ title: '警告', content: '您点击了拒绝授权,将无法进入小程序,请授权之后再进入!', showCancel: false, confirmText: '返回授权', success: function (res) { if (res.confirm) { console.log('用户点击了“返回授权”') } } }) } },})
2:后台php代码
public function wxlogin(){$this-load-model('ser_wx_user');$APPID = "自己申请的id";$AppSecret = "生成的AppSecret";$code = $this-input-post("code", true);//获取code请求接口$url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $APPID . "&secret=" . $AppSecret . "&js_code=" . $code . "&grant_type=authorization_code";$arr = $this-vget($url); // 一个使用curl实现的get方法请求$arr = json_decode($arr, true);$openid = $arr['openid'];$session_key = $arr['session_key'];// 数据签名校验$signature = $this-input-post("signature", true);$rawData =$this-input-post("rawData", true);$signature2 = sha1($rawData . $session_key);if ($signature != $signature2) {echo json_encode(['code' = 500, 'msg' = '数据签名验证失败!']);}else{//$this-load-library('wxBizDataCrypt');//加载解密文件,在官方有下载//Vendor("PHP.wxBizDataCrypt"); //加载解密文件,在官方有下载//引入文件require_once APPPATH . 'libraries/wxBizDataCrypt.php';$encryptedData = $this-input-post("encryptedData", true);$iv = $this-input-post("iv", true);$pc = new WXBizDataCrypt($APPID, $session_key); $errCode = $pc-decryptData($encryptedData, $iv, $data); //其中$data包含用户的所有数据$data = json_decode($data);if ($errCode == 0) {$data = $this-objectarray($data);$time_out = time();$query = $this-ser_wx_user-getone($data['openId']);if($query){$where = array(//'openid' = $data['openId'],'session_key' = $session_key,'avatar' = $data['avatarUrl'],'nickname' = $data['nickName'],'gender' = $data['gender'],'language' = $data['language'],//'token' = $data['openId'],'time_out' = $time_out,);//修改数据库数据$res = $this-ser_wx_user-update($query['id'],$where);}else{$where = array('openid' = $data['openId'],'session_key' = $session_key,'avatar' = $data['avatarUrl'],'nickname' = $data['nickName'],'gender' = $data['gender'],'language' = $data['language'],'token' = $data['openId'],'time_out' = $time_out,'status' = 0,);//存储数据$res = $this-ser_wx_user-create($where);}if($res){$data = array('token' = $data['openId'],);echo json_encode(['code' = 0, 'msg' = '登录成功!', 'data' = $data ]);}else{echo json_encode(['code' = 1, 'msg' = '登录失败!']);}} else {//echo $errCode;//打印失败信息}}}//请求public function vget($url){$info=curl_init();curl_setopt($info,CURLOPT_RETURNTRANSFER,true);curl_setopt($info,CURLOPT_HEADER,0);curl_setopt($info,CURLOPT_NOBODY,0);curl_setopt($info,CURLOPT_SSL_VERIFYPEER, false);curl_setopt($info,CURLOPT_SSL_VERIFYHOST, false);curl_setopt($info,CURLOPT_URL,$url);$output= curl_exec($info);curl_close($info);return $output;}//转换对象为数组function objectarray($object) {if (is_object($object)) {foreach ($object as $key = $value) {$array[$key] = $value;}}else {$array = $object;}return $array;}