1. 获取手机号
<button open-type="getPhoneNumber" @getphonenumber="getphone" >验证手机号</button>
点击按钮可以获取手机号
getphone(e){
// 发送请求像后台传递参数
obj = {
encryptedData: e.detail.encryptedData,
iv:e.detail.encryptedData,
sessionKey: this.sessionKey, // 登录时后台返回的参数
openid:this.openid // 登录时后台返回的参数
}
}
但是由于后台还需要传递 rawData
,signature
参数来进一步校验, 以保证数据安全,而这两个参数来自于
<button open-type='getUserInfo' @getuserinfo="login" >微信登录</button>
的返回值, 并且signature
是变化的
解决: signature = sha1(rawData + sessionKey)
https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/signature.html
// 安装: sha1
npm install sha1 --save
// 组件内引用
<script>let sha1 = require("@/node_modules/sha1/sha1.js")<script>
所以最后传递的参数为
getphone(e){
// 发送请求像后台传递参数
obj = {
rawData: this.rawData, // 点击按钮获取用户时候保存下来
signature: sha1(this.rawData + this.sessionKey)
encryptedData: e.detail.encryptedData,
iv:e.detail.encryptedData,
sessionKey: this.sessionKey, // 登录时后台返回的参数
openid:this.openid // 登录时后台返回的参数
}
}
2. 接口地址
调取异步请求的时候 url后面多了一个空格, 例如: url: "login "
现象: 安卓上面可以正常访问调用接口
ios上面访问接口时 , 地址后面会自动添加 %… 的内容
3. 微信小程序提交代码, 版本更新问题
onLaunch: function() {
console.log('App Launch')
this.update()
},
// 版本更新提示
update(){
const updateManager = uni.getUpdateManager();
updateManager.onCheckForUpdate(function (res) {
// 请求完新版本信息的回调
console.log(res,res.hasUpdate,"请求完新版本信息");
if(res.hasUpdate){
updateManager.onUpdateReady(function (res) {
uni.showModal({
title: '更新提示',
content: '新版本已经准备好,为了获得更好的体验,建议立即更新',
showCancel: false,
success(res) {
if (res.confirm) {
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate();
}
}
});
});
updateManager.onUpdateFailed(function () {
// 新的版本下载失败
uni.showModal({
title: '已经有新版本了哟~',
content: '新版本更新失败,为了获得更好的体验,请您前往发现--小程序中删除当前小程序,重新搜索打开哟~',
showCancel: false,
})
})
}
});
}
可以在微信开发者工具中测试