微信小程序非跳转式组件授权登录(2)

微信小程序非跳转式组件授权登录(2)

在第一篇文章中有一个app.Tips()的函数没有列出下面为此函数

 /**
   * opt  object | string
   * to_url object | string
   * 例:
   * this.Tips('/pages/test/test'); 跳转不提示
   * this.Tips({title:'提示'},'/pages/test/test'); 提示并跳转
   * this.Tips({title:'提示'},{tab:1,url:'/pages/index/index'}); 提示并跳转值table上
   * tab=1 一定时间后跳转至 table上
   * tab=2 一定时间后跳转至非 table上
   * tab=3 一定时间后返回上页面
   * tab=4 关闭所有页面跳转至非table上
   * tab=5 关闭当前页面跳转至table上
   */
const Tips= function (opt, to_url) {
  if (typeof opt == 'string') {
    to_url = opt;
    opt = {};
  }
  var title = opt.title || '', icon = opt.icon || 'none', endtime = opt.endtime || 2000;
  if (title) wx.showToast({ title: title, icon: icon, duration: endtime })
  if (to_url != undefined) {
    if (typeof to_url == 'object') {
      var tab = to_url.tab || 1, url = to_url.url || '';
      switch (tab) {
        case 1:
          //一定时间后跳转至 table
          setTimeout(function () {
            wx.switchTab({
              url: url
            })
          }, endtime);
          break;
        case 2:
          //跳转至非table页面
          setTimeout(function () {
            wx.navigateTo({
              url: url,
            })
          }, endtime);
          break;
        case 3:
          //返回上页面
          setTimeout(function () {
            wx.navigateBack({
              delta: parseInt(url),
            })
          }, endtime);
          break;
        case 4:
          //关闭当前所有页面跳转至非table页面
          setTimeout(function () {
            wx.reLaunch({
              url: url,
            })
          }, endtime);
          break;
        case 5:
          //关闭当前页面跳转至非table页面
          setTimeout(function () {
            wx.redirectTo({
              url: url,
            })
          }, endtime);
          break;
      }

    }else if(typeof to_url == 'function'){
      setTimeout(function () { 
        to_url && to_url();
      }, endtime);
    }else{
      //没有提示时跳转不延迟
      setTimeout(function () {
        wx.navigateTo({
          url: to_url,
        })
      }, title ? endtime : 0);
    }
  }
}

authorize.wxss页面代码

众说周知小程序第一次授权需要button 触发

<!--pages/authorize/authorize.wxml-->
<view class='Popup' hidden='{{iShidden}}'>
   <image src='{{url}}{{logo_url}}'></image>
   <view class='title'>授权提醒</view>
   <view class='tip'>请授权头像等信息,以便为您提供更好的服务</view>
   <view class='bottom flex'>
      <button class='item grant on'  type="primary" open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="setUserInfo">去授权</button>
   </view>
</view>
<view class='mask' hidden='{{iShidden}}' catchtouchmove="true" bindtap='close'></view>

整个授权的页面以及所有的具体方法全部完毕;

使用方法:

例1:情景为,需要自动授权登录


//index.js

Page({
	 /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  //这里为授权会掉函数,授权后会调用此方法,请求一些需要访问权限的借口请放在这个
  //函数里面执行
  onLoadFun:function (){
  }
  
})

//index.wxml
//在页面的任意位置加上这个组件并绑定 onLoadFun函数 和 isAuto 为授权不会自动登录默认值为:false
//iShidden=true 隐藏授权
<authorize bind:onLoadFun="onLoadFun" isAuto="{{isAuto}}" iShidden="{{iShidden}}"></authorize>

//index.json
//加载自定义组件 一定要记得加载组件 一定要记得,一定要记得 !!!
{
	"usingComponents":{
		"authorize": "/components/authorize/authorize"
	}
}

例2:情景为,自动加载授权
除了wxml内组件标签有所变化其他都一样这里只列出wxml内的标签写法

在页面任意地方添加标签 onLoadFun此函数为pages页面自定义函数;bind:onLoadFun 中的onLoadFun名称必须的和回调函数同名请注意
<authorize bind:onLoadFun="onLoadFun"></authorize>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值