```
// template/authorize/authorize.js
/**授权组件 */
const appinit = getApp();
import request from ‘…/…/utils/network.js’;
var postNumber = 20;
var issquan = true;
Component({
//组件的属性列表
properties: {
pageCode: String,
isshow: {
type: Boolean,
value: false
},
},
//组件的初始数据
data: {
imgshow: false
},
//组件的方法列表
methods: {
//体验
nologin: function() {
appinit.appData.isty = true;
this.setData({
isshow: false
})
if (wx.showTabBar) {
wx.showTabBar({})
}
},
//判断是否授权
getauthorizestatus() {
var than = this;
var flg = false;
//逻辑处理
wx.getSetting({
success(res) {
// console.log(‘授权信息’,res)
if (!res.authSetting[‘scope.userInfo’]) {
var imgshow = false;
if (wx.hideTabBar) {
wx.hideTabBar({});
imgshow = true;
}
than.setData({
isshow: true,
imgshow: imgshow
});
} else {
console.log(‘已授权,查询是否存在opendId’);
//已授权,查询是否存在opendId
var openId = wx.getStorageSync(“openId”);
if (openId && openId != ‘’) {
//判断是否存在用户信息缓存
var userInfoSession = wx.getStorageSync(“userInfoSession”);
if (userInfoSession && userInfoSession.id){
appinit.appData.userInfoStatus = true;
appinit.appData.userInfo = userInfoSession;
if (userInfoSession.orderInfo){
appinit.appData.orderInfo = userInfoSession.orderInfo;
}
appinit.appData.openId = userInfoSession.openId;
appinit.appData.openIdStatus = true;
appinit.appData.loginStatus = true;
}else{
//重新获取用户信息
appinit.getAppUserInfo(openId, than);
}
} else {
//,未存在,从新走登录
console.log(‘openId本地缓存不存在,重新登录’);
than.login(postNumber);
}
}
},
fail(err) {
console.log(‘出错’, err)
}
})
},
//用户信息授权回调
getuserinfo: function(options) {
//授权点击
issquan = true;
console.log(“用户信息授权回调”, options);
if (options.detail.errMsg == “getUserInfo:fail auth deny”) {
wx.showModal({
title: ‘温馨提示’,
content: ‘您点击了拒绝授权,将使无法使用小程序,点击确定重新获取授权。’,
success: function(res) {
if (res.confirm) {
wx.openSetting({
success: (res) => {
if (res.authSetting[“scope.getUserInfo”]) { //如果用户重新同意了授权登录
this.getuserinfo();
}
},
fail: function(res) {
}
})
}
}
})
} else {
console.log(‘已授权’);
//登录
this.login(postNumber);
}
},
login: function(num) {
var than = this;
console.log(‘已授权’);
if (num > 0) {
num–;
postNumber = num;
wx.login({
success(res) {
if (res.code) {
console.log(‘微信登录成功’, res);
//登录成功,获取微信用户信息
var rescode = res.code;
wx.getUserInfo({
success: function(res) {
console.log(‘获取微信用户信息成功’, res);
var userInfo = res.userInfo
var nickName = userInfo.nickName; //昵称
var avatarUrl = userInfo.avatarUrl; //头像
if (userInfo && !userInfo.avatarUrl){
avatarUrl = ‘http://w.taojianlou.com/image/default/head.jpg’;
}
//生成平台账号,并返回用户信息
var obj = {
nickname: nickName,
headimg: avatarUrl,
code: rescode,
}
if (appinit.appData.oneShareAgentId || (appinit.appData.shareInfo && appinit.appData.shareInfo.shareAgentId)) {
obj.agent = appinit.appData.oneShareAgentId || appinit.appData.shareInfo.shareAgentId;
}
than.getOpendId(obj);
},
fail: function(userError) {
console.log(‘获取微信用户信息失败’, res);
},
})
} else {
setTimeout(() => {
console.log(‘微信登录失败,2秒后重新请求!剩余请求次数’ + num, err);
than.login(postNumber);
}, 2000)
}
},
fail(err) {
setTimeout(() => {
console.log(‘微信登录失败,2秒后重新请求!剩余请求次数’ + num, err);
than.login(postNumber);
}, 2000)
}
})
} else {
wx.showToast({
title: ‘登录超时’,
icon: ‘none’,
mask: true,
duration: 3000
})
}
},
//获取openId
getOpendId: function(obj) {
var than = this;
request({
url: appinit.appData.port.queryopenid,
method: ‘post’,
data: obj,
log: true,
// isshow:true,
}).then(res => { //成功
if (res.data.result && res.data.result.success && res.data.info) {
if (appinit.appData.shareInfo && appinit.appData.shareInfo.channelType && res.data.info.newUser){
//用户记录-分享新用户
appinit.actionlog({
userAccountId: appinit.appData.shareInfo.shareAgentId,//‘用户标识’,
actionType: 5,//‘动作类型 1-点击 2-收藏 3-付款 4-分享 5-分享新用户’,
goodsId: appinit.appData.shareInfo.shareGoodsId ? appinit.appData.shareInfo.shareGoodsId : ‘’,//‘商品标识’,
status: 1,//‘1-非取消动作取1 -1-类似取消这种动作用-1’
shopUri: appinit.appData.shareInfo.shareShopUri ? appinit.appData.shareInfo.shareShopUri : ‘’,// ‘店铺ID’,
channelType: appinit.appData.shareInfo.channelType
})
}
appinit.appData.isty = false;
//用戶生成成功存儲openId
wx.setStorageSync(“openId”, res.data.info.openId)
appinit.appData.loginStatus = true;
appinit.appData.userInfoStatus = true; //用户信息获取状态
appinit.appData.openIdStatus = true;
appinit.appData.userInfo = res.data.info;
appinit.appData.openId = res.data.info.openId;
appinit.appData.orderInfo = res.data.orderInfo;
appinit.appData.userInfo.gdheadimg = res.data.info.headimg; //用于用户头像修改
if (res.data.info.shopUri) { //为商家
appinit.appData.userInfo.goodsManagerNum = res.data.supplierInfo.goodsManagerNum;
appinit.appData.userInfo.fansNum = res.data.supplierInfo.fansNum;
appinit.appData.userInfo.exclusiveNum = res.data.supplierInfo.exclusiveNum;
appinit.appData.userInfo.qrcode = res.data.supplierInfo.qrcode;
appinit.appData.userInfo.supplierScore = res.data.supplierInfo.supplierScore;
appinit.appData.userInfo.supplierLevel = res.data.supplierInfo.supplierLevel;
}
wx.setStorageSync(“userInfoSession”, appinit.appData.userInfo);
if (wx.showTabBar) {
wx.showTabBar({});
}
than.setData({
isshow: false
});
var shareInfo = appinit.appData.shareInfo;
var url = ‘’;
console.log(“than.data.pageCode == ‘merchantApplymessage’-----”, than.data.pageCode == ‘merchantApplymessage’, issquan)
if (than.data.pageCode == ‘index’ && issquan) {
wx.reLaunch({
url: ‘/pages/indexTwo/index’,
})
} else if (than.data.pageCode == ‘personal’) {
wx.reLaunch({
url: ‘/pages/personal/personal/personal’,
})
} else if (than.data.pageCode == ‘productdetail’ && issquan) {
if (shareInfo) {
url = ‘/pages/products/productdetail/productdetail?sharePage=productdetail&shareGoodsId=’ + shareInfo.shareGoodsId + ‘&shareShopUri=’ + shareInfo.shareShopUri + ‘&shareAgentId=’ + shareInfo.shareAgentId
} else {
url = ‘/pages/personal/personal/personal’;
}
if (url != ‘’) {
wx.reLaunch({
url: url,
})
}
} else if (than.data.pageCode == ‘merchantindex’ && issquan) {
if (shareInfo) {
if (shareInfo.sharePage2 == ‘productdetail’) {
url = ‘/pages/products/productdetail/productdetail?sharePage=merchantIndex&sharePage2=productdetail&shareGoodsId=’ + shareInfo.shareGoodsId + ‘&shareShopUri=’ + shareInfo.shareShopUri + ‘&shareAgentId=’ + shareInfo.shareAgentId;
} else if (shareInfo.sharePage2 == ‘index’) {
url = ‘/pages/merchant/index/index?sharePage=merchantIndex&sharePage2=index&shopUri=’ + shareInfo.shopUri + ‘&shareAgentId=’ + shareInfo.shareAgentId;
}
} else {
url = ‘/pages/personal/personal/personal’;
}
if (url != ‘’) {
wx.reLaunch({
url: url,
})
}
} else if (than.data.pageCode == ‘lotteryDetail’ && issquan) {
if (shareInfo) {
url = ‘/pages/package/activity/lotteryDetail/lotteryDetail?sharePage=lotteryDetail&&id=’ + shareInfo.id + ‘&shopUri=’ + shareInfo.shopUri + ‘&goodsId=’ + shareInfo.goodsId + ‘&shareAgentId=’ + shareInfo.shareAgentId;
} else {
url = ‘/pages/personal/personal/personal’;
}
if (url != ‘’) {
wx.reLaunch({
url: url,
})
}
} else if (than.data.pageCode == ‘lottery’ && issquan) {
if (shareInfo) {
url = ‘/pages/lottery/lottery?sharePage=lottery&shareAgentId=’ + shareInfo.shareAgentId;
wx.switchTab({
url: url
})
} else {
url = ‘/pages/personal/personal/personal’;
if (url != ‘’) {
wx.reLaunch({
url: url,
})
}
}
} else if (than.data.pageCode == ‘shopping’ && issquan) {
if (shareInfo) {
url = ‘/pages/shopping/shopping?sharePage=shopping&shareAgentId=’ + shareInfo.shareAgentId;
wx.switchTab({
url: url
})
} else {
url = ‘/pages/personal/personal/personal’;
if (url != ‘’) {
wx.reLaunch({
url: url,
})
}
}
} else if (than.data.pageCode == ‘popularity’ && issquan) {
if (shareInfo) {
url = ‘/pages/package/activity/popularity/popularity?sharePage=popularity&shareAgentId=’ + shareInfo.shareAgentId;
wx.reLaunch({
url: url
})
} else {
url = ‘/pages/personal/personal/personal’;
if (url != ‘’) {
wx.reLaunch({
url: url,
})
}
}
}
} else if (than.data.pageCode == ‘good’ && issquan) {
if (shareInfo) {
url = ‘/pages/package/activity/good/good?sharePage=good&shareAgentId=’ + shareInfo.shareAgentId;
wx.reLaunch({
url: url
})
} else {
url = ‘/pages/personal/personal/personal’;
if (url != ‘’) {
wx.reLaunch({
url: url,
})
}
}
} else if (than.data.pageCode == ‘orderdetailmessage’ && issquan) {
var options = wx.getStorageSync(“messageOrderdetail”);
if (options && options.orderId){
wx.removeStorageSync(“messageOrderdetail”);
wx.reLaunch({
url: ‘/pages/orders/orderdetail/orderdetail?orderId=’ + options.orderId + ‘&looktype=’ + options.looktype
})
}
} else if (than.data.pageCode == ‘merchantApplymessage’ && issquan) {
console.log(‘2222222222222’)
wx.reLaunch({
url: ‘/pages/merchantApply/merchantApply’
})
} else {
if (res.data.result && res.data.result.message.indexOf(‘可能包含’)!=-1){
wx.showToast({
title: ‘您的微信昵称可能包含特殊字符’,
icon:‘none’,
duration:2000
})
}else{
setTimeout(() => {
console.log(‘1用户信息不存在,重新走登录流程,剩余次数’ + postNumber, res);
than.login(postNumber);
}, 2000)
}
}
}).catch(err => {
//失败
setTimeout(() => {
console.log(‘2用户信息不存在,重新走登录流程,剩余次数’ + postNumber, err);
than.login(postNumber);
}, 2000)
})
},
},
//组件生命周期函数-在组件实例进入页面节点树时执行)
attached: function(e) {
if (this.data.pageCode != ‘personal’ && !appinit.appData.isty && !appinit.appData.userInfoStatus) {
this.getauthorizestatus();
}
},
})
```