接上一篇
众所周知,开发环境下所有的操作都是在每一个开发人员的预期之下的,但是对于小程序用户来说,万万不会按照开发者的意愿来执行,这个时候,我们作为开发人员,就要想办法去引导用户进行每一步的操作。
当客户端弹窗需要用户授权的时候,不一定每一个用户都会同意授权,但是为了防止他们不同意授权,我们就要采取以下其他措施。(特此声明:接下来的这个方法有点无赖式,但是对于让用户授权来说还是有一定作用的,可选可不选,仅作参考。)
home.js
import wxApi from '../../utils/wxApi.js';
Page({
data: {
scopeBoxanimation:'',
scopeShow:false,
},
onLoad (options) {
setTimeout(()=>{
that.scopeBox(that,'open')
},1000)
},
scopeBox(that, currentStatu){//蒙版层动画
var animation = wx.createAnimation({
duration: 400, //动画时长
timingFunction: "linear", //线性
delay: 100 //0则不延迟
});
this.animation = animation;
animation.opacity(0).step();
that.setData({
scopeBoxanimation: animation.export()
})
setTimeout(function () {
animation.opacity(1).step();
that.setData({
scopeBoxanimation: animation
})
if (currentStatu == "close") {//关闭
that.setData({
scopeShow: false
});
}
}.bind(this), 200)
if (currentStatu == "open") {//打开
that.setData({
scopeShow: true
});
}
},
onConfirm(e) {
let that = this;
that.scopeBox(that, 'close');
if (e.detail.UserInfo){
doSomeThing(e.detail)//就是进行用户绑定和注册,绑定注册函数自己应该都会写
}else{
that._getUserInfo();
}
},
_getUserInfo(){
let title = '警告',
content = '该小程序需要获取您的个人信息,请到设置中心打开授权!',
cancel = false;
wxApi._showMoudle(title, content , cancel ).then(res => {
if (res.confirm) {
wx.openSetting({
complete: data => {
console.log(data)
if (data.authSetting['scope.userInfo']) {
wx.getUserInfo({
success: res => {
doSomeThing(res)//就是进行用户绑定和注册,绑定注册函数自己应该都会写
}
})
} else {
that.scopeBox(that, 'open');
}
}
})
}
})
},
onCancel(){
let that = this;
that.scopeBox(that, 'close');
that._getUserInfo();
},
move(){},//解决事件穿透问题
})
wxApi.js
function _showMoudle(title, content, cancel) {
let promise = new Promise(function (resolve, reject) {
wx.showModal({
title: title,
content: content,
showCancel: cancel,
success: data => {
resolve(data)
},
fail: data => {
reject(data)
}
})
});
return promise
}
module.exports={
_showMoudle
}