wxbizdatacrypt下载php,小程序登录

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;}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在uniapp中使用WXBizDataCrypt时,需要先将提供的代码复制到你的项目中。这段代码创建了一个名为WXBizDataCrypt的类,并定义了decryptData方法用于解密微信加密数据。该代码使用了crypto模块来实现AES-128-CBC对称解密算法。 要在uniapp中获取用户授权信息,可以将相关代码放在mounted生命周期函数中。首先,你需要调用uni.getProvider方法来检查是否支持微信登录授权。如果支持微信登录授权,你可以调用uni.login方法来获取用户的code,然后再调用uni.getUserInfo方法来获取用户的信息。最后,你可以使用uni.request方法来向微信服务器发送请求以获取用户的openid和session_key。 下面是一个示例代码,演示了如何在uniapp中使用WXBizDataCrypt和获取用户授权信息: ```javascript // 引入WXBizDataCrypt var WXBizDataCrypt = require('path/to/WXBizDataCrypt') export default { mounted() { // 检查是否支持微信登录授权 uni.getProvider({ service: 'oauth', success: function(res) { if (~res.provider.indexOf('weixin')) { // 获取用户的code uni.login({ provider: 'weixin', success: (res2) => { const code = res2.code // 获取用户的信息 uni.getUserInfo({ provider: 'weixin', success: (info) => { // 发送请求获取openid和session_key uni.request({ url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + that.appID + '&secret=' + that.secret + '&js_code=' + res2.code + '&grant_type=authorization_code', data: {}, method: 'GET', success(r) { console.log(r.data) that.openId = r.data.openid that.session_key = r.data.session_key } }) }, fail: () => { uni.showToast({ title: "微信登录授权失败", icon: "none" }) } }) }, fail: () => { uni.showToast({ title: "微信登录授权失败", icon: "none" }) } }) } else { uni.showToast({ title: '请先安装微信或升级版本', icon: "none" }) } } }) } } ``` 通过以上代码,你就可以在uniapp中使用WXBizDataCrypt进行数据解密,并且获取用户的授权信息了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值