一、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:'未授权',
  • });
  • }
  • });
 

小程序判断是否授权源码 auth.js的更多相关文章

  1. 基于olami开放语义平台的微信小程序遥知之源码实现

    概述 实现一个智能生活信息查询的小秘书功能,支持查天气.新闻.日历.汇率.笑话.故事.百科.诗词.邮编.区号.菜谱.股票.节目预告,还支持闲聊.算24点.数学计算.单位换算.购物.搜索等功能. 使用方 ...

  2. 微信小程序支付C#后端源码

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  3. 微信商城小程序 带java后台源码

    微信小程序商城(Java版) 技术选型 1 后端使用技术 1.1 spring-web-4.0.2.RELEASE 1.2 mybatis3.2.8 1.3 shiro1.2.3 1.4 servle ...

  4. ESA2GJK1DH1K微信小程序篇: 小程序实现MQTT封包源码使用说明

    说明 我为了后期能够快速的让小程序实现MQTT,我做了一个MQTT的封装. 功能的封装有助于后期快速的开发,还方便咱维护. 我后期的所有代码皆使用此封装库, 这一节,我就详细的介绍我封装的MQTT.j ...

  5. 微信小程序——智能小秘“遥知之”源码分享(语义理解基于olami)

    微信小程序智能生活小秘书开发详解 >>>>>>>>>>>>>>>>>>>>> ...

  6. 微信小程序条码、二维码生成模块

    代码地址如下:http://www.demodashi.com/demo/13994.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...

  7. 微信小程序新版用户授权方式处理

    最新更新(2018-12-27): 最近做了改版,做成默认进来就是首页,然后去判断有没有用户信息,没有的话再去判断用没授权过,如果授权过直接自动去获取,没有的话再跳转到授权页面.因为用户授权主要就是针 ...

  8. 微信小程序获取地理位置授权

    微信小程序获取地理位置授权,首先需要在app.json中添加配置: "permission": { "scope.userLocation": { " ...

  9. 微信小程序开发 - 用户授权登陆

    准备:微信开发者工具下载地址:https://developers.weixin.qq.com/miniprogram/dev/devtools/download.html 微信小程序开发文档:htt ...

随机推荐

  1. HTML5 3D Google搜索 小盒子 大世界

    HTML5真是能让人想象万千,居然动起了Google搜索的主意,它利用HTML5技术将Google搜索放到了一个小盒子里,弄起了3D搜索.随着鼠标移动,HTML5 3D搜索盒子也就转动,非常立体.点击 ...

  2. 精巧好用的DelayQueue

    我们谈一下实际的场景吧.我们在开发中,有如下场景 a) 关闭空闲连接.服务器中,有很多客户端的连接,空闲一段时间之后需要关闭之.b) 缓存.缓存中的对象,超过了空闲时间,需要从缓存中移出.c) 任务超 ...

  3. Java设计模式之十一种行为型模式(附实例和详解)

    Java经典设计模式共有21中,分为三大类:创建型模式(5种).结构型模式(7种)和行为型模式(11种). 本文主要讲行为型模式,创建型模式和结构型模式可以看博主的另外两篇文章:J设计模式之五大创建型 ...

  4. 安卓开发笔记——关于Handler的一些总结(上)

    接上篇文章<安卓开发笔记——关于AsyncTask的使用>,今天来讲下在安卓开发里"重中之重"的另一个异步操作类Handler. 今天打算先讲下关于Handler的一些 ...

  5. 你可能不知道UED和UCD

    我们都知道UI是User Interface,即它的本意是用户界面,从字面上看是用户和界面组成,实际上还包括用户与界面之间的交互关系.UI最初对大家来说只是一个名词,它代表一些界面.当然重点还是是UI ...

  6. 114自定义UITableViewCell(扩展知识:为UITableViewCell添加动画效果)

    关键操作: 效果如下: ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UITableViewCo ...

  7. Canvas控件之CanvasGroup

    Canvas Group可以用来控制一组不需要个别控制的UI元素的某些方面,CanvasGroup的属性会影响他所有children的GameObject 其中有四个选项: -Alpha:这个选项很多 ...

  8. git链接github仓库

    配置Git 我们先在电脑硬盘里找一块地方存放本地仓库,比如我们把本地仓库建立在C:\MyRepository\1ke_test文件夹下 进入1ke_test文件夹 鼠标右键操作如下步骤: 1)在本地仓 ...

  9. python装饰器、继承、元类、mixin,四种給类动态添加类属性和方法的方式(一)

    介绍装饰器.继承.元类.mixin,四种給类动态添加类属性和方法的方式 有时候需要給类添加额外的东西,有些东西很频繁,每个类都需要,如果不想反复的复制粘贴到每个类,可以动态添加. # coding=u ...

  10. Connect to a ROS Network---2

    原创博文:转载请标明出处(周学伟):http://www.cnblogs.com/zxouxuewei/tag/ 一.Introduction ROS网络由单个ROS主机和多个ROS节点组成. ROS ...