一、auth.js
-
const configGlobal = require('../config/config_global.js');
-
var util = require('function.js');
-
var http = require('http.js');
-
var app = getApp();
-
var authHandler ={
-
success:function(res){},
-
fail:function(res){},
-
complete:function(res){},
- };
-
- /*
- * 得到保存的SESSION
- */
-
function getSession(){
-
var session =null;
-
try{
-
session = wx.getStorageSync('session');
-
if(session){
-
wx.checkSession({
-
success:function(res){
-
setAppGlobalData(session);
-
},
-
fail:function(res){
-
session =null;
-
setAppGlobalData(session);
-
}
-
});
-
}else{
-
session =null;
-
setAppGlobalData(session);
-
}
-
}catch(e){}
-
-
return session;
- };
-
-
- /**
- * [putLoginLog description]--记录登录日志
- * @return {[type]} [description]
- */
-
function putLoginLog(){
-
http.POST({
-
url:'/_WxaappApiServer/putLoginLog',
-
data:{
-
openId: app.globalData.openId,
-
},
-
success:function(res){
-
-
if(res.data.code =='0'){}else{}
-
},
-
fail:function(res){ console.log('request失败,res:', res);},
-
complete:function(res){}
-
});
- };
-
-
function setAppGlobalData(session){
-
app.globalData.openId = session.openId;
-
app.globalData.unionid = session.unionid;
-
app.globalData._3rd_session = session._3rd_session;
-
app.globalData.userInfo = session.userInfo;
- };
-
-
function getUserInfo(authHandler){
-
// 调用登录接口
-
wx.login({
-
success:function(res){
-
var code = res.code;
-
-
wx.getUserInfo({
-
lang:'zh_CN',
-
success:function(res){
-
-
http.POST({
-
url:'/_WxaappApiServer/getUserInfo',
-
data:{
-
code: code,
-
iv: res.iv,
-
encryptedData: res.encryptedData,
-
},
-
success:function(res){
-
//对userInfo重新赋值
-
if(res.data.code =="0"){
-
var session = res.data.data;
-
try{
-
wx.setStorageSync('session', session);
-
setAppGlobalData(session);
-
authHandler.success();
-
}catch(e){}
-
}else{}
-
},
-
fail:function(res){ console.log('request失败,res:', res);},
-
complete:function(res){}
-
});
-
-
},
-
fail:function(res){
-
openSettingUserInfo(authHandler);
-
},
-
complete:function(res){},
-
})
-
},
-
fail:function(res){
-
console.log("登录失败!");
-
},
-
complete:function(res){},
-
});
- };
-
-
function openSettingUserInfo(authHandler){
-
wx.getSetting({
-
success(res){
-
if(!res.authSetting['scope.userInfo']){
-
wx.showModal({
-
title:'',
-
content:'请先完成授权!在设置页面中勾选“用户信息”选项,否则部分功能将受限。',
-
showCancel:true,
-
confirmText:'前去设置',
-
confirmColor:'#004b97',
-
success:function(res){
-
-
if(res.confirm){
-
wx.openSetting({
-
success:(res)=>{
-
res.authSetting ={
-
'scope.userInfo':true,
-
};
-
// authHandler.success();
-
},
-
complete:function(res){
-
openSettingUserInfo(authHandler);
-
},
-
})
-
}
-
-
if(res.cancel){
-
authHandler.fail();
-
// 注释上一行,启用下面这一行,就是强制用户授权
-
// openSettingUserInfo(authHandler); //强制授权
-
}
-
-
if(!res.confirm &&!res.cancel){
-
openSettingUserInfo(authHandler);
-
}
-
-
}
-
});
-
-
}else{
-
getUserInfo(authHandler);
-
}
-
}
-
})
- };
-
-
-
- /**
- * 授权--用户登录
- * @param {[type]} authHandler [description]
- * @return {[type]} [description]
- */
-
function userInfo(authHandler){
-
var session =null;
-
try{
-
session = wx.getStorageSync('session');
-
if(session){
-
wx.checkSession({
-
success:function(){
-
setAppGlobalData(session);
-
authHandler.success();
-
},
-
fail:function(){
-
session =null;
-
getUserInfo(authHandler);
-
-
}
-
});
-
}else{
-
session =null;
-
getUserInfo(authHandler);
-
}
-
}catch(e){
-
authHandler.fail();
-
}
-
-
return session;
- }
-
- /**
- * 授权--地理位置 wx.getLocation, wx.chooseLocation
- * @param {[type]} authHandler [description]
- * @return {[type]} [description]
- */
-
function userLocation(authHandler){
-
wx.getSetting({
-
success(res){
-
if(!res.authSetting['scope.userLocation']){
-
wx.authorize({
-
scope:'scope.userLocation',
-
success(){},
-
complete(){
-
openSetting_userLocation(authHandler)
-
}
-
})
-
}else{
-
authHandler.success();
-
}
-
}
-
})
- };
-
-
function openSetting_userLocation(authHandler){
-
wx.getSetting({
-
success(res){
-
if(!res.authSetting['scope.userLocation']){
-
wx.showModal({
-
title:'',
-
content:'请先完成授权!在设置页面中勾选“地理位置”选项,否则部分功能将受限。',
-
showCancel:true,
-
confirmText:'前去设置',
-
confirmColor:'#004b97',
-
success:function(res){
-
-
if(res.confirm){
-
wx.openSetting({
-
success:(res)=>{
-
res.authSetting ={
-
'scope.userLocation':true,
-
};
-
},
-
complete:function(res){
-
openSetting_userLocation(authHandler);
-
},
-
})
-
}
-
-
if(res.cancel){
-
authHandler.fail();
-
// 注释上一行,启用下面这一行,就是强制用户授权
-
// openSetting_userLocation(authHandler); //强制授权
-
}
-
-
if(!res.confirm &&!res.cancel){
-
openSetting_userLocation(authHandler);
-
}
-
-
}
-
});
-
}else{
-
userLocation(authHandler);
-
}
-
}
-
})
- };
-
-
- /**
- * 授权--通讯地址 wx.chooseAddress
- * @param {[type]} authHandler [description]
- * @return {[type]} [description]
- */
-
function address(authHandler){
-
wx.getSetting({
-
success(res){
-
if(!res.authSetting['scope.address']){
-
wx.authorize({
-
scope:'scope.address',
-
success(){},
-
complete(){
-
openSetting_address(authHandler)
-
}
-
})
-
}else{
-
authHandler.success();
-
}
-
}
-
})
- };
-
-
function openSetting_address(authHandler){
-
wx.getSetting({
-
success(res){
-
if(!res.authSetting['scope.address']){
-
wx.showModal({
-
title:'',
-
content:'请先完成授权!在设置页面中勾选“通讯地址”选项,否则部分功能将受限。',
-
showCancel:true
-
今天小编告诉大家如何对win10系统删除桌面回收站图标进行设置,可能很多用户都不知道
阅读:8317|2022-11-06
|
|
请发表评论