npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

wx-auth-router

v1.0.5

Published

拓展微信小程序路由跳转,方便增加路由页面授权的检查(如:登录状态、用户权限等)

Downloads

1

Readme

一、npm包介绍

拓展微信小程序路由跳转,方便增加路由页面授权的检查(如:登录状态、用户权限等)

二、使用

1.安装

执行 npm i wx-auth-router,并构建 npm

2.初始化

在 app.js 文件中引入 wxAuthRouter,定义用于检查授权状态的方法(必须返回布尔值),定义用于授权检查未通过时执行的回调,在 onLaunch 生命周期函数内调用 wxAuthRouter.init(checkCallback, unAuthCallback)

// app.js
import wxAuthRouter from 'wx-auth-router'

App({
  onLaunch: function () {
    /**
     * @description: 挂载全局对象 wxAuth
     * @param {func} checkCallback 自定义的检查函数,返回 boolean
     * @param {func} unAuthCallback 自定义的检查未通过时的回调
     */
    wxAuthRouter.init(this.checkCallback, this.unAuthCallback)
  },

  /**
   * @description: 检查函数
   * @return {boolean} 检查是否通过
   */
  checkCallback () {
    // 仅为示例:当缓存中不存在openId时,判断未用户未登录,授权检查不通过
    const openId = wx.getStorageSync('openId')
    return Boolean(openId)
  },

  /**
   * @description: 检查未通过时的回调
   * @param {string} url 检查未通过时,会返回当前要跳转的 url
   */
  unAuthCallback (url) {
    // 仅为示例:除了首页,其它页面的访问都需要登录
    if (url !== '/pages/index/index') {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
  },
})

3.在页面内使用

wxAuth 全局对象提供两个API接口

(1) wxAuth.router( ) 页面跳转

// page.js

Page({
  // 仅为示例:跳转到未付款订单列表(需要登录)
  jumpOrderList () {
    // 使用授权路由跳转
    wxAuth.router({
      url: '/pages/orderList/orderList',
      type: 'navigateTo',
      params: {
        status: 'unpaid'
      }
    })
  }
})

| 参数 | 说明 | 类型 | 必填 | 默认值 | | :-------- | :--------| :------| :------: | :--: | url | 目标页面路径 | string | 是 |- | | type | 页面跳转的类型,与微信小程序原生一致,包括:navigateTo, redirectTo, switchTab, reLaunch | string | 否 | navigateTo | | params | 页面参数 | object | 否 | - | | success | 接口调用成功的回调函数,与微信小程序原生一致 | function | 否 | - | | fail | 接口调用失败的回调函数,与微信小程序原生一致 | function | 否 | - | | complete | 接口调用结束的回调函数,与微信小程序原生一致 | function | 否 | - |

(2) 授权检查 wxAuth.check( )
Promise 函数,检查通过时返回resolve(true);未通过时自动调用初始化时传入的回调,默认不返回reject(false)

// page.js

Page({
  // 仅为示例:商品详情页加入购物车(需要登录)
  async addToCart () {
    // 使用授权检查,通过时Promise状态为resolve
    const useReject = false
    await wxAuth.check(useReject)
    // 仅为示例:调用加入购物车接口
    await request({
      url: '/example/cart/add',
      data: ...
    })
  }
})

| 参数 | 说明 | 类型 | 必填 | 默认值 | | :-------- | :--------| :------| :------: | :--: | useReject | 检查授权未通过时,是否返回Promise.reject,可用try catch 捕获 | boolean | 否 | false |