功能:新增微信小程序一键登录

This commit is contained in:
xiaowang
2026-04-02 13:20:28 +08:00
parent 8ac7deffca
commit 4f5ef4fedd
32 changed files with 1899 additions and 65 deletions
+15
View File
@@ -16,6 +16,10 @@ function getNeedSetPassword() {
return Boolean(getStorage(STORAGE_KEYS.NEED_SET_PASSWORD, false))
}
function getPreferredMiniLoginShopId() {
return getStorage(STORAGE_KEYS.PREFERRED_MINI_LOGIN_SHOP_ID, '')
}
function isLoggedIn() {
return Boolean(getAccessToken())
}
@@ -64,14 +68,25 @@ function clearSession() {
removeStorage(STORAGE_KEYS.NEED_SET_PASSWORD)
}
function setPreferredMiniLoginShopId(shopId) {
if (shopId === undefined || shopId === null || shopId === '') {
removeStorage(STORAGE_KEYS.PREFERRED_MINI_LOGIN_SHOP_ID)
return
}
setStorage(STORAGE_KEYS.PREFERRED_MINI_LOGIN_SHOP_ID, String(shopId))
}
module.exports = {
getAccessToken,
getRefreshToken,
getStoredUserInfo,
getNeedSetPassword,
getPreferredMiniLoginShopId,
isLoggedIn,
setSession,
updateStoredUserInfo,
setNeedSetPassword,
setPreferredMiniLoginShopId,
clearSession
}
+46 -16
View File
@@ -1,6 +1,8 @@
const auth = require('./auth')
const { TAB_PAGES } = require('./config')
let waitingForAutoLoginNavigation = false
function buildUrl(path, params) {
const query = Object.keys(params || {})
.filter(key => params[key] !== undefined && params[key] !== null && params[key] !== '')
@@ -131,27 +133,51 @@ function openAuth(redirect) {
})
}
function showLoginToast(actionText) {
wx.showToast({
title: `${actionText || '继续操作'}前请先登录`,
icon: 'none'
})
}
function scheduleOpenAuth(actionText) {
showLoginToast(actionText)
setTimeout(() => {
openAuth()
}, 120)
}
function ensureLogin(actionText) {
if (auth.isLoggedIn()) {
return true
}
wx.showToast({
title: `${actionText || '继续操作'}前请先登录`,
icon: 'none'
})
setTimeout(() => {
openAuth()
}, 120)
return false
}
function openProducts(filter) {
const app = getApp()
app.globalData.pendingProductFilter = filter || {}
return switchTab('/pages/products/index')
const hasAutoLoginHelpers = app
&& typeof app.isWechatMiniAutoLoginPending === 'function'
&& typeof app.waitForWechatMiniAutoLogin === 'function'
if (hasAutoLoginHelpers && app.isWechatMiniAutoLoginPending()) {
if (waitingForAutoLoginNavigation) {
return false
}
waitingForAutoLoginNavigation = true
Promise.resolve(app.waitForWechatMiniAutoLogin())
.catch(() => false)
.then(() => {
if (!auth.isLoggedIn()) {
scheduleOpenAuth(actionText)
}
})
.finally(() => {
waitingForAutoLoginNavigation = false
})
return false
}
scheduleOpenAuth(actionText)
return false
}
module.exports = {
@@ -164,5 +190,9 @@ module.exports = {
redirectAfterLogin,
openAuth,
ensureLogin,
openProducts
openProducts(filter) {
const app = getApp()
app.globalData.pendingProductFilter = filter || {}
return switchTab('/pages/products/index')
}
}
+1
View File
@@ -23,6 +23,7 @@ function isWhitelistedPath(url) {
'/user/refresh-token',
'/user/send-verification-code',
'/user/phone-login',
'/user/wechat-mini/',
'/user/reset-password-by-phone',
'/product/get/',
'/product/list/',
+2 -1
View File
@@ -2,7 +2,8 @@ const STORAGE_KEYS = {
ACCESS_TOKEN: 'accessToken',
REFRESH_TOKEN: 'refreshToken',
USER_INFO: 'userInfo',
NEED_SET_PASSWORD: 'needSetPassword'
NEED_SET_PASSWORD: 'needSetPassword',
PREFERRED_MINI_LOGIN_SHOP_ID: 'preferredMiniLoginShopId'
}
function getStorage(key, defaultValue) {