1、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,
- };
2、如何使用 在你的JS文頭,引用auth.js微信
- var auth = require('../../utils/auth.js');
- // 在調用通信地址判斷是否開啓受權,若是沒開啓就會自動調用受權
- auth.address({
- success:function(res){
- console.log('已受權');
- // 這兒寫你的業務邏輯
- },
- fail:function(res){
- console.log('未受權');
- wx.showToast({
- title:'未受權',
- duration:2000
- });
- }
- });
|