auth-manager-usage-example.js
8.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
* @Description: 通用授权管理器使用示例
* @Date: 2025-01-25
*/
import { UniversalAuthManager, TaroAdapters } from './universal-auth-manager.js'
import request from '@/utils/request'
import { getMyFamiliesAPI } from '@/api/family'
/**
* 老来赛项目的授权管理器配置
*/
const laolaisaiAuthConfig = {
// 页面路径配置
authPage: '/pages/auth/index',
defaultPage: '/pages/Dashboard/index',
// 测试环境配置
testOpenIds: ['h-008', 'h-009', 'h-010', 'h-011', 'h-012', 'h-013'],
// 适配器配置
...TaroAdapters.getAdapters(request),
// 业务逻辑:检查用户是否已加入家庭
async checkUserStatus() {
try {
const { code, data } = await getMyFamiliesAPI()
return code && data && data.length > 0
} catch (error) {
console.error('检查用户家庭状态失败:', error)
return false
}
},
// 业务逻辑:根据用户状态决定重定向路径
async getRedirectPath(savedPath, defaultPath) {
const hasFamily = await this.checkUserStatus()
if (!hasFamily) {
return '/pages/Welcome/index'
}
return savedPath || defaultPath
},
// 授权成功回调
onAuthSuccess: () => {
// 授权成功处理逻辑
},
// 授权失败回调
onAuthError: () => {
// 授权失败处理逻辑
}
}
// 创建授权管理器实例
export const authManager = new UniversalAuthManager(laolaisaiAuthConfig)
/**
* 在页面中使用授权管理器的示例
*/
// 1. 在页面加载时进行静默授权
export async function handlePageLoad() {
try {
await authManager.silentAuth()
// 页面授权检查完成
} catch (error) {
// 页面授权失败,跳转到授权页面
await authManager.navigateToAuth()
}
}
// 2. 处理分享页面的授权
export async function handleSharePage(options) {
const isAuthorized = await authManager.handleSharePageAuth(options, () => {
// 分享页面授权成功,可以继续执行业务逻辑
// 这里执行授权成功后的业务逻辑
})
if (!isAuthorized) {
// 需要授权,已跳转到授权页面
}
}
// 3. 在授权页面完成授权后的跳转
export async function handleAuthComplete() {
await authManager.returnToOriginalPage()
}
// 4. 检查是否需要授权
export function checkAuthStatus() {
return !authManager.needAuth()
}
// 5. 手动触发授权流程
export async function manualAuth() {
const result = await authManager.silentAuth()
// 手动授权成功
return result
}
/**
* 其他项目使用示例
*/
// 示例1:简单的电商小程序
export function createEcommerceAuthManager() {
return new UniversalAuthManager({
// 基础配置
authPage: '/pages/login/index',
defaultPage: '/pages/home/index',
// 适配器
...TaroAdapters.getAdapters(yourHttpClient),
// 简单的用户状态检查
async checkUserStatus() {
try {
const userInfo = await getUserInfo()
return userInfo && userInfo.isActive
} catch (error) {
return false
}
},
// 简单的重定向逻辑
async getRedirectPath(savedPath, defaultPath) {
return savedPath || defaultPath
}
})
}
// 示例2:社交类小程序
export function createSocialAuthManager() {
return new UniversalAuthManager({
authPage: '/pages/auth/index',
defaultPage: '/pages/timeline/index',
...TaroAdapters.getAdapters(yourHttpClient),
// 检查用户是否完成了个人资料设置
async checkUserStatus() {
try {
const profile = await getUserProfile()
return profile && profile.isProfileComplete
} catch (error) {
return false
}
},
// 根据用户状态决定跳转
async getRedirectPath(savedPath, defaultPath) {
const hasCompleteProfile = await this.checkUserStatus()
if (!hasCompleteProfile) {
return '/pages/profile-setup/index'
}
return savedPath || defaultPath
}
})
}
// 示例3:企业应用小程序
export function createEnterpriseAuthManager() {
return new UniversalAuthManager({
authPage: '/pages/login/index',
defaultPage: '/pages/dashboard/index',
...TaroAdapters.getAdapters(yourHttpClient),
// 检查用户权限和部门信息
async checkUserStatus() {
try {
const userAuth = await getUserAuthInfo()
return userAuth && userAuth.hasValidRole
} catch (error) {
return false
}
},
// 根据用户角色决定跳转
async getRedirectPath(savedPath, defaultPath) {
const userAuth = await getUserAuthInfo()
if (!userAuth.hasValidRole) {
return '/pages/no-permission/index'
}
// 根据用户角色跳转到不同的首页
if (userAuth.role === 'admin') {
return savedPath || '/pages/admin-dashboard/index'
} else if (userAuth.role === 'manager') {
return savedPath || '/pages/manager-dashboard/index'
}
return savedPath || defaultPath
}
})
}
/**
* 迁移指南:如何从现有代码迁移到通用授权管理器
*/
// 步骤1:替换现有的授权函数调用
// 原来的代码:
// import { silentAuth, needAuth, returnToOriginalPage } from '@/utils/authRedirect'
// 新的代码:
// import { authManager } from './auth-manager-usage-example'
// 步骤2:更新函数调用
// 原来:silentAuth(onSuccess, onError)
// 现在:authManager.silentAuth(onSuccess, onError)
// 原来:needAuth()
// 现在:authManager.needAuth()
// 原来:returnToOriginalPage(defaultPath)
// 现在:authManager.returnToOriginalPage(defaultPath)
// 步骤3:更新页面中的授权逻辑
// 原来在页面的onLoad中:
/*
onLoad(options) {
if (needAuth()) {
silentAuth(
() => {
// 授权成功,继续执行业务逻辑
this.loadPageData()
},
(error) => {
// 授权失败,跳转到授权页面
navigateToAuth()
}
)
} else {
// 已授权,直接执行业务逻辑
this.loadPageData()
}
}
*/
// 现在:
/*
async onLoad(options) {
try {
await authManager.silentAuth()
// 授权成功,继续执行业务逻辑
this.loadPageData()
} catch (error) {
// 授权失败,跳转到授权页面
await authManager.navigateToAuth()
}
}
*/
/**
* 打包为npm包的建议结构
*/
// package.json
/*
{
"name": "@your-org/universal-auth-manager",
"version": "1.0.0",
"description": "通用的小程序静默授权管理器",
"main": "dist/index.js",
"module": "dist/index.esm.js",
"types": "dist/index.d.ts",
"files": [
"dist",
"adapters"
],
"scripts": {
"build": "rollup -c",
"dev": "rollup -c -w"
},
"peerDependencies": {
"@tarojs/taro": ">=3.0.0"
},
"keywords": [
"taro",
"miniprogram",
"auth",
"wechat"
]
}
*/
// 目录结构
/*
universal-auth-manager/
├── src/
│ ├── index.js # 主入口文件
│ ├── auth-manager.js # 核心授权管理器
│ └── adapters/
│ ├── taro.js # Taro框架适配器
│ ├── uni-app.js # uni-app框架适配器
│ └── native.js # 原生小程序适配器
├── dist/ # 构建输出目录
├── examples/ # 使用示例
├── docs/ # 文档
├── package.json
├── rollup.config.js
└── README.md
*/
export default authManager