微信小程序的授权实现过程解析


Posted in Javascript onAugust 02, 2019

自从小程序文档更新后,自动授权已不存在啦

目前的授权都是通过button来实现的,具体知识点可参考小程序的官方文档,以下是我做的一个小demo(进入首页,跳出一个登录弹出框,弹出框是自己写的一个UI组件),废话不多说,直接上代码

UI组件部分(modal)

modal.wxml

<view class='modal-mask' wx:if='{{show}}' bindtap='clickMask'>
 <view class='modal-content'>
  <scroll-view scroll-y class='main-content'>
   <slot></slot>
  </scroll-view>
 </view>
</view>

modal.wxss

n: fixed;
 left: 0;
 right: 0;
 top: 0;
 bottom: 0;
 background-color: rgba(0,0,0,0.5);
 z-index: 999;
}
/*遮罩内容*/
.modal-content{
 display: flex;
 flex-direction: column;
 width: 65%;
 background-color: #fff;
 border-radius: 10rpx;
 padding: 20rpx;
 text-align: center;
}
/*中间内容*/
.main-content{
 flex: 1;
 height: 100%;
 overflow-y: hidden; 
 max-height: 80vh; /* 内容高度最高80vh 以免内容太多溢出*/
}
.bottom {
  border-radius: 80rpx;
  margin: 70rpx 50rpx;
  font-size: 35rpx;
}

modal.js

Component({
 /**
  * 组件的属性列表
  */
 properties: {
  //是否显示modal弹窗
  show: {
   type: Boolean,
   value: false
  },
  //控制底部是一个按钮还是两个按钮,默认两个
  single: {
   type: Boolean,
   value: false
  }
 },

 /**
  * 组件的初始数据
  */
 data: {

 },

 /**
  * 组件的方法列表
  */
 methods: {
  // 点击modal的回调函数
  clickMask() {
   // 点击modal背景关闭遮罩层,如果不需要注释掉即可
   this.setData({ show: false })
  },
  // 点击取消按钮的回调函数
  cancel() {
   this.setData({ show: false })
   this.triggerEvent('cancel') //triggerEvent触发事件
  },
  // 点击确定按钮的回调函数
  confirm() {
   this.setData({ show: false })
   this.triggerEvent('confirm')
  }
 }
})

modal.json

{
 "component": true,
 "usingComponents": {}
}

pages页面

home.wxml(这个是弹框,home页面内容直接在下面加一个<view>这里写home页面的内容</view>)

<!-- modal弹窗-->
 <modalView show="{{showModal}}" bindcancel="modalCancel" bindconfirm='modalConfirm' single='{{single}}'>
  <view class='modal-content'>
   <scroll-view scroll-y class='main-content'>
  <view wx:if="{{canIUse}}" >
  <view class='header'>
      <text>提示</text>
    </view>
    <view class='content'>
     <image src="/images/goods_img2.png"></image>
     <text>是否登录并继续使用该程序</text>
    </view>
    <view class="header_title">
     <text class="dian">•</text>
     <text>登录程序需进行微信授权</text>
    </view>
    <view class="modal_footer">
    <view class="bottom">
     <button class='bottom_a'>
      拒绝
     </button>
     <button class='bottom_b' open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="bindGetUserInfo">
       去登录
     </button>
    </view>
    </view>
</view>
   </scroll-view>
  </view>
 </modalView>

home.wxss

.header {
  text-align: start;
  height: 100rpx;
  line-height: 100rpx;
}
 
.header image {
  width: 200rpx;
  height: 200rpx;
}
 
.content {
 display: flex;
 margin-left: 50rpx;
 height: 100rpx;
 line-height: 100rpx;
}

.content image{
 width: 100rpx;
 height: 100rpx;
}
 
.content text {
 font-size: 24rpx;
 margin-left: 20rpx;
}
 
.header_title{
 margin-left: 50rpx;
 text-align: start;
 font-size: 24rpx;
 color: #ccc;
 line-height: 100rpx;
 display: flex;
}

.dian{
 margin-right: 6rpx;
 font-size: 36rpx;
}

.modal_footer{
  display: flex;
  justify-content: flex-end;
}
.bottom {
 display: flex;
  color: #ccc;
 font-size: 24rpx;
 width: 280rpx;
}

button::after {
 border: none;
}

.bottom button{
 background-color: #fff;
 height: 50rpx;
 line-height: 50rpx;
}

.bottom_a{
 font-size: 24rpx;
}
.bottom_b{
 font-size: 28rpx;
 color: #0db95a;
}

home.js

//home.js
//获取应用实例
const app = getApp()

Page({
 data: {
  canIUse: wx.canIUse('button.open-type.getUserInfo'),
  showModal: true, 
  single: false
 },
 onLoad:function(){
  var that = this;
  // 查看是否授权
  wx.getSetting({
   success: function (res) {
    if (res.authSetting['scope.userInfo']) {
     wx.getUserInfo({
      success: function (res) {
       wx.login({
        success: res => {
         console.log("用户的code:" + res.code);
        }
       });
      }
     });
    } else {
     that.setData({
      showModal: true
     });
    }
   }
  });
 },
 bindGetUserInfo: function (e) {
  if (e.detail.userInfo) {
   //用户按了允许授权按钮
   var that = this;
   // 获取到用户的信息了,打印到控制台上看下
   console.log("用户的信息如下:");
   console.log(e.detail.userInfo);
   //授权成功后,通过改变 showModal的值,让实现页面显示出来,把授权页面隐藏起来
   that.setData({
    showModal: false
   });
  } else {
   var that = this;
   //用户按了拒绝按钮
   wx.showModal({
    title: '警告',
    content: '您点击了拒绝授权,将无法获取你的信息!!!',
    showCancel: false,
    confirmText: '返回授权',
    success: function (res) {
     // 用户没有授权成功,不需要改变 isHide 的值
     if (res.confirm) {
      that.setData({
       showModal: true
      });
     }
    }
   });
  }
 }
})

home.json

{
 "usingComponents": {
  "modalView": "../../components/modal/modal"
 }
}

好啦~这是全部代码,效果如下(点击登录可进行授权)

微信小程序的授权实现过程解析

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
JQuery EasyUI 加载两次url的原因分析及解决方案
Aug 18 Javascript
浅析javascript的间隔调用和延时调用
Nov 12 Javascript
jQuery中复合属性选择器用法实例
Dec 31 Javascript
jQuery对象和DOM对象之间相互转换的方法介绍
Feb 28 Javascript
jQuery遍历json的方法(推荐)
Jun 12 Javascript
聊聊JavaScript如何实现继承及特点
Apr 07 Javascript
jQuery开源组件BootstrapValidator使用详解
Jun 29 jQuery
浅谈对Angular中的生命周期钩子的理解
Jul 31 Javascript
VUE兄弟组件传值操作实例分析
Oct 26 Javascript
Vue封装Axios请求和拦截器的步骤
Sep 16 Javascript
浅谈Web Storage API的使用
Jun 23 Javascript
关于对TypeScript泛型参数的默认值理解
Jul 15 Javascript
jQuery实现input[type=file]多图预览上传删除等功能
Aug 02 #jQuery
用 js 写一个 js 解释器过程详解
Aug 02 #Javascript
vue实现登录页面的验证码以及验证过程解析(面向新手)
Aug 02 #Javascript
详解element-ui中el-select的默认选择项问题
Aug 02 #Javascript
jQuery实现checkbox全选、反选及删除等操作的方法详解
Aug 02 #jQuery
150行Node.js实现的dns代理工具
Aug 02 #Javascript
el-select 下拉框多选实现全选的实现
Aug 02 #Javascript
You might like
php的memcached客户端memcached
2011/06/14 PHP
PHP关联链接常用代码
2012/11/05 PHP
使用openssl实现rsa非对称加密算法示例
2014/01/24 PHP
[原创]php token使用与验证示例【测试可用】
2017/08/30 PHP
PHP中PCRE正则解析代码详解
2019/04/26 PHP
php数组遍历类与用法示例
2019/05/24 PHP
Jquery中LigerUi的弹出编辑框(实现方法)
2013/07/09 Javascript
公共js在页面底部加载的注意事项介绍
2013/07/18 Javascript
JS实现超简单的仿QQ折叠菜单效果
2015/09/21 Javascript
谈一谈JS消息机制和事件机制的理解
2016/04/14 Javascript
jQuery实现移动端Tab选项卡效果
2017/03/15 Javascript
基于jQuery实现的设置文本区域的光标位置
2018/06/15 jQuery
浅谈javascript中的prototype和__proto__的理解
2019/04/07 Javascript
关于layui的下拉搜索框异步加载数据的解决方法
2019/09/28 Javascript
vue指令v-html使用过滤器filters功能实例
2019/10/25 Javascript
Python中条件选择和循环语句使用方法介绍
2013/03/13 Python
Python之PyUnit单元测试实例
2014/10/11 Python
使用Python编写一个在Linux下实现截图分享的脚本的教程
2015/04/24 Python
python实现马耳可夫链算法实例分析
2015/05/20 Python
Python使用matplotlib实现在坐标系中画一个矩形的方法
2015/05/20 Python
windows及linux环境下永久修改pip镜像源的方法
2016/11/28 Python
python中in在list和dict中查找效率的对比分析
2018/05/04 Python
Pycharm更换python解释器的方法
2018/10/29 Python
Django 查询数据库并返回页面的例子
2019/08/12 Python
Django后台管理系统的图文使用教学
2020/01/20 Python
python GUI库图形界面开发之PyQt5浏览器控件QWebEngineView详细使用方法
2020/02/26 Python
MATLAB数学建模之画图汇总
2020/07/16 Python
Tirendo比利时:在线购买轮胎
2018/10/22 全球购物
GC是什么?为什么要有GC?
2013/12/08 面试题
写给女朋友的道歉信
2014/01/12 职场文书
学生党支部先进事迹
2014/02/04 职场文书
公司会计岗位职责
2014/02/13 职场文书
优秀的2014年两会精神解读
2014/03/17 职场文书
饭店服务员岗位职责
2015/02/09 职场文书
python for循环赋值问题
2021/06/03 Python
Python matplotlib安装以及实现简单曲线的绘制
2022/04/26 Python