赞
踩
-js
// index.js // 获取应用实例 const app = getApp() Page({ data: { motto: 'Hello World', userInfo: {}, hasUserInfo: false, canIUse: wx.canIUse('button.open-type.getUserInfo'), canIUseGetUserProfile: false, canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName'), // 如需尝试获取用户信息可改为false step: '9999', timestamp:'2022-8-3' }, //定义按钮的事件 bthtap(e){ console.log("进入了"); console.log(e.target.dataset.info); console.log(e); }, inputenevt(e){ console.log(e.detail.value); this.setData({ msg: e.datail.value }) }, // 事件处理函数 bindViewTap() { wx.navigateTo({ url: '../logs/logs' }) }, onLoad() { if (wx.getUserProfile) { this.setData({ canIUseGetUserProfile: true }) } }, getUserProfile(e) { // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗 wx.getUserProfile({ desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写 success: (res) => { console.log(res) this.setData({ userInfo: res.userInfo, hasUserInfo: true }) } }) }, getUserInfo(e) { // 不推荐使用getUserInfo获取用户信息,预计自2021年4月13日起,getUserInfo将不再弹出弹窗,并直接返回匿名的用户个人信息 console.log(e) this.setData({ userInfo: e.detail.userInfo, hasUserInfo: true }) }, getUserRun:function(res){ var that = this; var steps = ''; var timestamps = ''; //获取用户登录状态 wx.login({ success:(res)=>{ var logdata = res.code; console.log("进行登录..."); console.log(res.code); if(res.code){ wx.request({ url: 'http://localhost:9023/weixin/getUserinfo', data: { codestr: res.code }, success:(res)=>{ console.log(res.data); var sessionkey = res.data; console.log("登录成功"); //获取微信运动数据 wx.getWeRunData({ success:(res)=>{ console.log("获取微信运动数据"); //用户数据加密了 // 拿 encryptedData 到开发者后台解密开放数据 var teddate = res.encryptedData; var iv = res.iv; var cloudID = res.cloudID; console.log(teddate); //获取用户信息 // 必须是在用户已经授权的情况下调用 wx.showModal({ title:'温馨提示', content:'亲 请授权获取微信步数', success:(res)=>{ if(res.confirm){ wx.getUserProfile({ desc: '用来获取用户步数来颁发奖励', success(res){ console.log("用户信息"); var infos = res.userInfo; var rawdata = res.rawData; var signature = res.signature; var encryptedadata = res.encryptedData; var ivvv = res.iv; var cloudids = res.cloudID; console.log(infos); if(infos){ wx.request({ url: 'http://localhost:9023/weixin/getwerundata', data:{ nickName:infos.nickName, avatarUrl:infos.avatarUrl, gender:infos.gender, skey:sessionkey, iv:iv, getwerundata:teddate }, success: (res)=>{ console.log("输出用户步数了"); console.log(res); console.log("输出步数吧"); var st = res.data.step; var ti = res.data.timestamp; steps = res.data.step;; timestamps = res.data.timestamp; console.log(steps); console.log(timestamps); console.log("输出内容呀"+steps+timestamps); that.setData({ step: steps, timestamp:timestamps }) } }) } }, fail(){ console.log("获取用户信息失败") } }) }else if(res.cancel){ console.log('取消了'); wx.showToast({ title: '您拒绝了,无法通过步数获取积分' }) } } }) } }) } }) }else{ console.log() } }, }) }, alerts(){ wx.showToast({ title: '用户信息', }), wx.showModal({ cancelColor: '是否进入', content: '好棒' }) }, test1: function(steps,timestamps){ console.log("传过来的内容呀"+steps+timestamps); this.setData({ }) } })
-bushu.wxml
<!--index.wxml--> <view class="container"> <view class="userinfo"> <block wx:if="{{canIUseOpenData}}"> <view class="userinfo-avatar" bindtap="bindViewTap"> <open-data type="userAvatarUrl"></open-data> </view> <open-data type="userNickName"></open-data> </block> <block wx:elif="{{!hasUserInfo}}"> <button wx:if="{{canIUseGetUserProfile}}" bindtap="getUserProfile"> 获取头像昵称 </button> <button wx:elif="{{canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 获取头像昵称 </button> <view wx:else> 请使用1.4.4及以上版本基础库 </view> </block> <block wx:else> <image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image> <text class="userinfo-nickname">{{userInfo.nickName}}</text> </block> </view> <button type="primary" bindtap="getUserRun">获取运动步数</button> <view > 时间:{{timestamp}} </view> <view > 步数:{{step}} </view> <button type="primary" bindtap="alerts">弹框</button> </view>
– 后端代码
/** 获取微信运动接口 * */ @Controller @RequestMapping("/weixin") @RestController public class BuShuController { //获取微信用户信息用来登录 @RequestMapping("/getUserinfo") public String getUserinfo(String codestr){ System.out.println("传过来的值"+codestr); /** https://api.weixin.qq.com/sns/jscode2session? appid=APPID &secret=SECRET &js_code=JSCODE &grant_type=authorization_code 返回信息 session_key */ String getsessionurl = "https://api.weixin.qq.com/sns/jscode2session?appid=*********&secret=***********&js_code="+codestr+"&grant_type=authorization_code"; StringBuffer lines = null; String line = null; try { //创建接口地址对象 URL url = new URL(getsessionurl); //根据url对象来生成一个打开连接http请求 HttpURLConnection connection = (HttpURLConnection)url.openConnection(); //设置请求方式 connection.setRequestMethod("GET"); BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); while(null != (line = br.readLine())){ System.out.println(line); lines = new StringBuffer(line); } br.close(); System.out.println("结果"); System.out.println(lines); } catch (Exception e) { e.printStackTrace(); } JSONObject jsonObject = JSONObject.parseObject(lines.toString()); String session_key = jsonObject.getString("session_key"); System.out.println("session_key="+session_key); return session_key; } /*获取微信用户运动数据getWeRunData http://localhost:9023/weixin/getwerundata * */ @RequestMapping("/getwerundata") public String getWeRunData(String nickName,String avatarUrl,String gender,String skey,String iv,String getwerundata){ System.out.println("进入获取用户信息了"); System.out.println("nickName"+nickName); System.out.println("skey"+skey); System.out.println("iv"+iv); System.out.println("getwerundata"+getwerundata); System.out.println("输入完毕!"); String userInfo = WXDecryptUtil.getUserInfo(getwerundata,skey,iv); System.out.println(userInfo); JSONObject jsonObject = JSONObject.parseObject(userInfo); String stepInfoList = jsonObject.getString("stepInfoList"); System.out.println(stepInfoList); JSONArray objects = JSONArray.parseArray(stepInfoList); System.out.println(objects.size()); String step = JSONObject.parseObject(objects.get(objects.size()-1).toString()).getString("step"); String timestamp = JSONObject.parseObject(objects.get(objects.size()-1).toString()).getString("timestamp"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String format = sdf.format(new Date(Long.valueOf(timestamp + "000"))); System.out.println(step); System.out.println(format); JSONObject jsonObject1 = new JSONObject(); jsonObject1.put("step",step); jsonObject1.put("timestamp",format); return jsonObject1.toString(); } }
依赖
<!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->
<!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.59</version>
– 优化一下
// index.js // 获取应用实例 const app = getApp() Page({ data: { motto: 'Hello World', userInfo: {}, // hasUserInfo: false, // canIUse: wx.canIUse('button.open-type.getUserInfo'), canIUseGetUserProfile: true, // canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName'), // 如需尝试获取用户信息可改为false step: '9999', timestamp:'2022-8-3' }, //定义按钮的事件 bthtap(e){ console.log("进入了"); console.log(e.target.dataset.info); console.log(e); }, inputenevt(e){ console.log(e.detail.value); this.setData({ msg: e.datail.value }) }, // 事件处理函数 bindViewTap() { wx.navigateTo({ url: '../logs/logs' }) }, onLoad() { if (wx.getUserProfile) { this.setData({ canIUseGetUserProfile: true }) } }, getUserProfile(e) { // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗 wx.getUserProfile({ desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写 success: (res) => { console.log(res) this.setData({ userInfo: res.userInfo, hasUserInfo: true }) } }) }, // getUserInfo(e) { // // 不推荐使用getUserInfo获取用户信息,预计自2021年4月13日起,getUserInfo将不再弹出弹窗,并直接返回匿名的用户个人信息 // console.log("haha"); // alert("dada"); // this.setData({ // userInfo: e.detail.userInfo, // hasUserInfo: true // }) // }, // haha(){ // console.log("dddd"); // }, getUserRun:function(res){ console.log("传过来的内容呀"); var that = this; var steps = ''; var timestamps = ''; //获取用户登录状态 console.log("传过来的内容呀dddd"), wx.login({ success:(res)=>{ var logdata = res.code; console.log("进行登录..."); console.log(res.code); if(res.code){ wx.request({ //url: 'http://localhost:9023/weixin/getUserinfo', // url: 'http://weixin-dev3.klb.com.cn/klbwx/applets/getUserinfo.htm', url:'https://api.weixin.qq.com/sns/jscode2session?appid=wx66314a80dc7d2c46&secret=25d72907e21322a78bf64e01ccb5f445&grant_type=authorization_code&js_code='+res.code, // data: { // appid:'wx66314a80dc7d2c46', // secret:'25d72907e21322a78bf64e01ccb5f445', // grant_type:'authorization_code', // codestr: res.code // }, success:(res)=>{ console.log("返回的信息"); console.log(res.data); console.log("登录成功"); var sessionkey = res.data; console.log("sessionkey"+sessionkey); //获取微信运动数据 wx.getWeRunData({ success:(res)=>{ console.log("获取微信运动数据"); //用户数据加密了 // 拿 encryptedData 到开发者后台解密开放数据 var teddate = res.encryptedData; var iv = res.iv; var cloudID = res.cloudID; console.log(teddate); //获取用户信息 // 必须是在用户已经授权的情况下调用 wx.showModal({ title:'温馨提示', content:'亲 请授权获取微信步数', success:(res)=>{ if(res.confirm){ wx.getUserProfile({ desc: '用来获取用户步数来颁发奖励', success(res){ console.log("用户信息"); var infos = res.userInfo; var rawdata = res.rawData; var signature = res.signature; var encryptedadata = res.encryptedData; var ivvv = res.iv; var cloudids = res.cloudID; console.log(infos); if(infos){ wx.request({ url: 'http://weixin-dev3.klb.com.cn/klbwx/applets/getwerundata.htm', // url: 'http://localhost:9023/weixin/getwerundata', data:{ nickName:infos.nickName, avatarUrl:infos.avatarUrl, gender:infos.gender, skey:sessionkey, iv:iv, getwerundata:teddate }, success: (res)=>{ console.log("输出用户步数了"); console.log(res); console.log("输出步数吧"); console.log(res.data); let jsonitem = JSON.parse(res.data); console.log(jsonitem.step); console.log(jsonitem.timestamp); var st = jsonitem.step; var ti = jsonitem.timestamp; steps = jsonitem.step; timestamps = jsonitem.timestamp; console.log(steps); console.log(timestamps); console.log("输出内容呀"+steps+timestamps); that.setData({ step: steps, timestamp:timestamps }) } }) } }, fail(){ console.log("获取用户信息失败") } }) }else if(res.cancel){ console.log('取消了'); wx.showToast({ title: '您拒绝了' }) } } }) } }) } }) }else{ console.log() } }, }) }, alerts(){ wx.showToast({ title: '用户信息', }), wx.showModal({ cancelColor: '是否进入', content: '好棒' }) }, test1: function(steps,timestamps){ console.log("传过来的内容呀"+steps+timestamps); this.setData({ }) }, haha(){ console.log('dsfsdf'); console.log('取消了'); wx.showModal({ cancelColor: '是否进入', content: '好棒!' }) } })
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。