0.0.12 • Published 7 months ago
@dinofe/xt-core v0.0.12
xt-core
业务中常用工具方法的核心库,包括:微信网页授权链接构建、dexie 数据库操作等。
安装
npm i -S @dinofe/xt-core
使用
es6
import { genOAuthUrl } from '@dinofe/xt-core'
const wechatOptions = {
wx_appid: '123456',
wx_component_appid: '1123456',
wx_scope: 'snsapi_base',
redirect_url: 'https://www.baidu.com',
state: '0'
}
const url = genOAuthUrl(wechatOptions)
es5
const { genOAuthUrl } = require('@dinofe/xt-core')
const wechatOptions = {
wx_appid: '123456',
wx_component_appid: '1123456',
wx_scope: 'snsapi_base',
redirect_url: 'https://www.baidu.com',
state: '0'
}
const url = genOAuthUrl(wechatOptions)
特性
微信网页授权链接处理
微信网页授权过程中需要按照微信文档要求生成一个授权跳转的 URL 链接,形如:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx520c15f417810387&redirect_uri=https%3A%2F%2Fchong.qq.com%2Fphp%2Findex.php%3Fd%3D%26c%3DwxAdapter%26m%3DmobileDeal%26showwxpaytitle%3D1%26vb2ctag%3D4_2030_5_1194_60&response_type=code&scope=snsapi_base&state=123#wechat_redirect
将这个参数拼接的过程进行了封装,同时可以对封装好的授权链接进行解析得出原始参数。
微信网页授权官方资料请参考:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html。
方法列表
- 根据 appid、scope、redirectUrl 等参数构建构建跳转链接
import { genOAuthUrl } from '@dinofe/xt-core'
const wechatOptions = {
wx_appid: '123456',
wx_component_appid: '1123456',
wx_scope: 'snsapi_base',
redirect_url: 'https://www.baidu.com',
state: '0'
}
const url = genOAuthUrl(wechatOptions)
- 从拼接好的 oAuth 认证中方向解析参数
import { decodeOAuthUrl } from '@dinofe/xt-core'
const wechatOptions = decodeOAuthUrl(url) // wechatOptions是个对象