一、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,
  • confirmText:'前去设置',
  • confirmColor:'#004b97',
  • success:function(res){
  • if(res.confirm){
  • wx.openSetting({
  • success:(res)=>{
  • res.authSetting ={
  • 'scope.address':true,
  • };
  • },
  • complete:function(res){
  • openSetting_address(authHandler);
  • },
  • })
  • }
  • if(res.cancel){
  • authHandler.fail();
  • // 注释上一行,启用下面这一行,就是强制用户授权
  • // openSetting_address(authHandler); //强制授权
  • }
  • if(!res.confirm &&!res.cancel){
  • openSetting_address(authHandler);
  • }
  • }
  • });
  • }else{
  • address(authHandler);
  • }
  • }
  • })
  • };
  • /**
  • * 授权--发票抬头 wx.chooseInvoiceTitle
  • * @param  {[type]} authHandler [description]
  • * @return {[type]}             [description]
  • */
  • function invoiceTitle(authHandler){
  • wx.getSetting({
  • success(res){
  • if(!res.authSetting['scope.invoiceTitle']){
  • wx.authorize({
  • scope:'scope.invoiceTitle',
  • success(){},
  • complete(){
  • openSetting_invoiceTitle(authHandler)
  • }
  • })
  • }else{
  • authHandler.success();
  • }
  • }
  • })
  • };
  • function openSetting_invoiceTitle(authHandler){
  • wx.getSetting({
  • success(res){
  • if(!res.authSetting['scope.invoiceTitle']){
  • wx.showModal({
  • title:'',
  • content:'请先完成授权!在设置页面中勾选“发票抬头”选项,否则部分功能将受限。',
  • showCancel:true,
  • confirmText:'前去设置',
  • confirmColor:'#004b97',
  • success:function(res){
  • if(res.confirm){
  • wx.openSetting({
  • success:(res)=>{
  • res.authSetting ={
  • 'scope.invoiceTitle':true,
  • };
  • },
  • complete:function(res){
  • openSetting_invoiceTitle(authHandler);
  • },
  • })
  • }
  • if(res.cancel){
  • authHandler.fail();
  • // 注释上一行,启用下面这一行,就是强制用户授权
  • // openSetting_invoiceTitle(authHandler); //强制授权
  • }
  • if(!res.confirm &&!res.cancel){
  • openSetting_invoiceTitle(authHandler);
  • }
  • }
  • });
  • }else{
  • invoiceTitle(authHandler);
  • }
  • }
  • })
  • };
  • /**
  • * 授权--微信运动步数 wx.getWeRunData
  • * @param  {[type]} authHandler [description]
  • * @return {[type]}             [description]
  • */
  • function werun(authHandler){
  • wx.getSetting({
  • success(res){
  • if(!res.authSetting['scope.werun']){
  • wx.authorize({
  • scope:'scope.werun',
  • success(){},
  • complete(){
  • openSetting_werun(authHandler)
  • }
  • })
  • }else{
  • authHandler.success();
  • }
  • }
  • })
  • };
  • function openSetting_werun(authHandler){
  • wx.getSetting({
  • success(res){
  • if(!res.authSetting['scope.werun']){
  • wx.showModal({
  • title:'',
  • content:'请先完成授权!在设置页面中勾选“微信运动步数”选项,否则部分功能将受限。',
  • showCancel:true,
  • confirmText:'前去设置',
  • confirmColor:'#004b97',
  • success:function(res){
  • if(res.confirm){
  • wx.openSetting({
  • success:(res)=>{
  • res.authSetting ={
  • 'scope.werun':true,
  • };
  • },
  • complete:function(res){
  • openSetting_werun(authHandler);
  • },
  • })
  • }
  • if(res.cancel){
  • authHandler.fail();
  • // 注释上一行,启用下面这一行,就是强制用户授权
  • // openSetting_werun(authHandler); //强制授权
  • }
  • if(!res.confirm &&!res.cancel){
  • openSetting_werun(authHandler);
  • }
  • }
  • });
  • }else{
  • werun(authHandler);
  • }
  • }
  • })
  • };
  • /**
  • * 授权--录音功能 wx.startRecord
  • * @param  {[type]} authHandler [description]
  • * @return {[type]}             [description]
  • */
  • function record(authHandler){
  • wx.getSetting({
  • success(res){
  • if(!res.authSetting['scope.record']){
  • wx.authorize({
  • scope:'scope.record',
  • success(){},
  • complete(){
  • openSetting_record(authHandler)
  • }
  • })
  • }else{
  • authHandler.success();
  • }
  • }
  • })
  • };
  • function openSetting_record(authHandler){
  • wx.getSetting({
  • success(res){
  • if(!res.authSetting['scope.record']){
  • wx.showModal({
  • title:'',
  • content:'请先完成授权!在设置页面中勾选“录音功能”选项,否则部分功能将受限。',
  • showCancel:true,
  • confirmText:'前去设置',
  • confirmColor:'#004b97',
  • success:function(res){
  • if(res.confirm){
  • wx.openSetting({
  • success:(res)=>{
  • res.authSetting ={
  • 'scope.record':true,
  • };
  • },
  • complete:function(res){
  • openSetting_record(authHandler);
  • },
  • })
  • }
  • if(res.cancel){
  • authHandler.fail();
  • // 注释上一行,启用下面这一行,就是强制用户授权
  • // openSetting_record(authHandler); //强制授权
  • }
  • if(!res.confirm &&!res.cancel){
  • openSetting_record(authHandler);
  • }
  • }
  • });
  • }else{
  • record(authHandler);
  • }
  • }
  • })
  • };
  • /**
  • * 授权--保存到相册 wx.saveImageToPhotosAlbum, wx.saveVideoToPhotosAlbum
  • * @param  {[type]} authHandler [description]
  • * @return {[type]}             [description]
  • */
  • function writePhotosAlbum(authHandler){
  • wx.getSetting({
  • success(res){
  • if(!res.authSetting['scope.writePhotosAlbum']){
  • wx.authorize({
  • scope:'scope.writePhotosAlbum',
  • success(){},
  • complete(){
  • openSetting_writePhotosAlbum(authHandler)
  • }
  • })
  • }else{
  • authHandler.success();
  • }
  • }
  • })
  • };
  • function openSetting_writePhotosAlbum(authHandler){
  • wx.getSetting({
  • success(res){
  • if(!res.authSetting['scope.writePhotosAlbum']){
  • wx.showModal({
  • title:'',
  • content:'请先完成授权!在设置页面中勾选“保存到相册”选项,否则部分功能将受限。',
  • showCancel:true,
  • confirmText:'前去设置',
  • confirmColor:'#004b97',
  • success:function(res){
  • if(res.confirm){
  • wx.openSetting({
  • success:(res)=>{
  • res.authSetting ={
  • 'scope.writePhotosAlbum':true,
  • };
  • },
  • complete:function(res){
  • openSetting_writePhotosAlbum(authHandler);
  • },
  • })
  • }
  • if(res.cancel){
  • authHandler.fail();
  • // 注释上一行,启用下面这一行,就是强制用户授权
  • // openSetting_writePhotosAlbum(authHandler); //强制授权
  • }
  • if(!res.confirm &&!res.cancel){
  • openSetting_writePhotosAlbum(authHandler);
  • }
  • }
  • });
  • }else{
  • writePhotosAlbum(authHandler);
  • }
  • }
  • })
  • };
  • module.exports ={
  • userInfo: userInfo,
  • userLocation: userLocation,
  • address: address,
  • invoiceTitle: invoiceTitle,
  • werun: werun,
  • record: record,
  • writePhotosAlbum: writePhotosAlbum,
  • putLoginLog: putLoginLog,
  • getSession: getSession,
  • };

二、如何使用

在你的JS文头,引用auth.js

  • var auth = require('../../utils/auth.js');
  • // 在调用通讯地址判断是否开启授权,如果没开启就会自动调用授权
  • auth.address({
  • success:function(res){
  • console.log('已授权');
  • // 这儿写你的业务逻辑
  • },
  • fail:function(res){
  • console.log('未授权');
  • wx.showToast({
  • title:'未授权',
  • });
  • }
  • });
 

最新文章

  1. 【Tips】史上最全H1B问题合辑——保持H1B身份终级篇
  2. Linux & Systemd 挂载问题解决
  3. Ad hoc sql
  4. 构造 & 析构 & 匿名对象‍
  5. CSS选择器以及优先级与匹配原理
  6. 简单JS实现对表的行的增删
  7. volley框架 出现at com.android.volley.Request.<init>
  8. 项目开发-->一键登录功能汇总
  9. POJ 1989
  10. erlang web socket参考。
  11. 【Linux/Ubuntu学习 13】ubuntu上好用的pdf软件okular
  12. Key Task
  13. jboss加密敏感信息
  14. iOS自动布局之autoresizingi
  15. ios7高级
  16. Swift迁入第三方库时的版本错误解决
  17. OC的特有语法-分类Category、 类的本质、description方法、SEL、NSLog输出增强、点语法、变量作用域、@property @synthesize关键字、Id、OC语言构造方法
  18. mysql数据库的基本操作:创建数据库、查看数据库、修改数据库、删除数据库
  19. 枚举 enum 成员变量初始化
  20. 几张图看明白VAO、VBO、EBO的关系和代码顺序

热门文章

  1. tp5数据输出
  2. 查看Ubuntu的版本和系统版本
  3. utf8 ucs4
  4. UIStatusBarStyle PreferredStatusBarStyle does not work on iOS 7
  5. JSP内置对象—session
  6. js eventLoop (使用chunk 同步变异步)
  7. SVN自动生成版本号信息
  8. koa2使用注意点总结
  9. Lua之转义字符
  10. iscroll5实现下拉加载更多