昨天做了一个获取小程序微信运动步数demo
获取微信用户信息的方法,详情https://developers.weixin.qq.com/community/develop/doc/0000a26e1aca6012e896a517556c01
获取小程序步数授权,当用户取消授权后我们需要去引导用户去开启服务,首先去wx.getSetting()然后在里面去wx.openSetting()设置需要调去开启服务的名称(当前用到的是scope.werun)
const appId = 'wxsfsfdsfds'; const secret = 'sdfsdfsdfsfsdfsdfs' const that = this wx.login({ success: function (res) { const { code } = res; wx.request({// 获取微信运动必须使用sessionKey 所以先从该接口拿到sessionKey,提示请求https://api.weixin.qq.com 不在以下 request 合法域名列表中需要去“设置”=="项目设置"==“不校验合法域名、web-view(业务域名)、TLS 版本以及 HTTPS 证书”勾选上就可以了 url: 'https://api.weixin.qq.com/sns/jscode2session', data: { appid: appId, secret, js_code: code, grant_type: 'authorization_code' }, method: 'GET', success: function (res) { const { session_key, openid } = res.data; that.setData({ session_key: session_key, openid: openid }) wx.getWeRunData({//拿取到微信步数的encryptedData和iv success: function (res) { const { encryptedData, iv } = res; var pc = new WXBizDataCrypt(AppId, session_key) //进行解码 wx.getUserInfo({ success: function (res) { const { userInfo } = res var data = pc.decryptData(encryptedData, iv) const { stepInfoList } = data let average let steps = [] stepInfoList.map((k, v) = { average = (average || 0) + k.step const date = k.timestamp = that.setTimeStamp(k.timestamp) steps.push(k.step) return { average, date, steps } }) steps.sort((a, b) = { return a - b }) that.setData({ userInfo, openid, session_key, encryptedData: encryptedData, iv: iv, average: Math.ceil(average / 30), stepList: data.stepInfoList.reverse(), steps: steps, isLoading: false }) } }) }, fail: function (res) { that.setData({ getWerun: false }) } }) } }) } }) wx.getSetting({//引导用户允许微信获得你的公共信息,重新唤起微信运动授权 success: function (res) { wx.openSetting({ data: { "scope.werun": true }, success: function (res) { that.setData({ getWerun: true }) wx.redirectTo({ url: '/pages/start/start?id=userConsole',//跳到中转页面再跳回当前页面 }) } }) } })














