微信小程序发布试用版本
This commit is contained in:
@@ -22,8 +22,8 @@ export function checkTenantIdInCode() {
|
||||
console.log(' - POST/PUT/DELETE 请求:tenantId 作为 body 字段')
|
||||
console.log('')
|
||||
console.log('📝 需要迁移的文件(使用 uni.request 的地方):')
|
||||
console.log(' 1. pages/reception/reception.vue')
|
||||
console.log(' 2. pages/customer/customer.vue')
|
||||
console.log(' 1. pages-subpackage/reception/reception.vue')
|
||||
console.log(' 2. pages-subpackage/customer/customer.vue')
|
||||
console.log(' 3. uni_modules/uni-id-pages/common/store.js (登出接口)')
|
||||
console.log('')
|
||||
console.log('💡 迁移方法:')
|
||||
|
||||
@@ -27,7 +27,9 @@ function resolveApiEnv() {
|
||||
if (fileEnv) {
|
||||
const mapped = ENV_ALIAS[fileEnv.toLowerCase()]
|
||||
if (mapped) {
|
||||
// #ifdef APP-PLUS || H5 || MP-ALIPAY
|
||||
console.log('[API Config] 使用 env.js 配置:', fileEnv, '=>', mapped)
|
||||
// #endif
|
||||
return mapped
|
||||
}
|
||||
}
|
||||
@@ -39,7 +41,9 @@ function resolveApiEnv() {
|
||||
: ''
|
||||
const aliasKey = runtimeEnv ? runtimeEnv.toLowerCase() : ''
|
||||
const env = ENV_ALIAS[aliasKey] || 'prod'
|
||||
// #ifdef APP-PLUS || H5 || MP-ALIPAY
|
||||
console.log('[API Config] 使用环境变量:', runtimeEnv, '=>', env)
|
||||
// #endif
|
||||
return env
|
||||
}
|
||||
|
||||
@@ -49,8 +53,10 @@ const API_ENV = resolveApiEnv()
|
||||
export const API_BASE_URL = normalizeBaseUrl(API_TARGETS[API_ENV] || API_TARGETS.prod)
|
||||
|
||||
// 输出当前配置信息(便于调试)
|
||||
// #ifdef APP-PLUS || H5 || MP-ALIPAY
|
||||
console.log('[API Config] 当前环境:', API_ENV)
|
||||
console.log('[API Config] API_BASE_URL:', API_BASE_URL)
|
||||
// #endif
|
||||
|
||||
// 当前 API 环境(local / prod)
|
||||
export function getApiEnv() {
|
||||
@@ -66,14 +72,18 @@ export function getApiUrl(path = '') {
|
||||
// H5环境下,local / prod 都返回完整URL,确保 prod 时直接请求远程服务器
|
||||
const fullPath = apiPath.startsWith('/') ? apiPath.slice(1) : apiPath
|
||||
const fullUrl = `${API_BASE_URL}${fullPath}`
|
||||
// #ifdef APP-PLUS || H5 || MP-ALIPAY
|
||||
console.log(`[API Config] H5环境(${API_ENV})直接使用完整URL:`, fullUrl)
|
||||
// #endif
|
||||
return fullUrl
|
||||
// #endif
|
||||
// #ifndef H5
|
||||
// 其他环境使用完整URL
|
||||
const fullPath = apiPath.startsWith('/') ? apiPath.slice(1) : apiPath
|
||||
const fullUrl = `${API_BASE_URL}${fullPath}`
|
||||
// #ifdef APP-PLUS || H5 || MP-ALIPAY
|
||||
console.log('[API Config] 非H5环境使用完整URL:', fullUrl)
|
||||
// #endif
|
||||
return fullUrl
|
||||
// #endif
|
||||
}
|
||||
|
||||
@@ -3,6 +3,6 @@
|
||||
* apiEnv 可选:'local' | 'prod'
|
||||
*/
|
||||
export default {
|
||||
apiEnv: 'local'
|
||||
apiEnv: 'prod'
|
||||
}
|
||||
|
||||
|
||||
198
common/store.js
Normal file
198
common/store.js
Normal file
@@ -0,0 +1,198 @@
|
||||
import pagesJson from '@/pages.json'
|
||||
import { getApiUrl } from '@/common/config.js'
|
||||
|
||||
// 由于 store.js 在主包中,无法直接访问子包中的 config.js
|
||||
// 使用默认配置值
|
||||
const config = {
|
||||
setPasswordAfterLogin: false // 默认值,如果需要修改,可以在这里调整
|
||||
}
|
||||
|
||||
// 获取用户信息,避免在模块加载时就执行 uni-app API
|
||||
let hostUserInfo = null
|
||||
|
||||
function getHostUserInfo() {
|
||||
if (hostUserInfo === null) {
|
||||
// 检查 uni 是否可用
|
||||
if (typeof uni !== 'undefined' && uni.getStorageSync) {
|
||||
hostUserInfo = uni.getStorageSync('uni-id-pages-userInfo') || {}
|
||||
} else {
|
||||
hostUserInfo = {}
|
||||
}
|
||||
}
|
||||
return hostUserInfo
|
||||
}
|
||||
|
||||
const data = {
|
||||
userInfo: getHostUserInfo(),
|
||||
hasLogin: Object.keys(getHostUserInfo()).length != 0
|
||||
}
|
||||
|
||||
// 定义 mutations, 修改属性
|
||||
export const mutations = {
|
||||
// 更新用户信息(简化版,不使用数据库)
|
||||
async updateUserInfo(data = false) {
|
||||
if (data) {
|
||||
// 直接更新本地用户信息
|
||||
this.setUserInfo(data)
|
||||
if (typeof uni !== 'undefined' && uni.showToast) {
|
||||
uni.showToast({
|
||||
title: "更新成功",
|
||||
icon: 'none',
|
||||
duration: 3000
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// 如果没有传入数据,不做任何操作(不再从数据库获取)
|
||||
// #ifdef APP-PLUS || H5 || MP-ALIPAY
|
||||
console.log('updateUserInfo called without data, skipping database operation')
|
||||
// #endif
|
||||
}
|
||||
},
|
||||
setUserInfo(data, {cover}={cover:false}) {
|
||||
// console.log('set-userInfo', data);
|
||||
let userInfo = cover?data:Object.assign(store.userInfo,data)
|
||||
store.userInfo = Object.assign({},userInfo)
|
||||
store.hasLogin = Object.keys(store.userInfo).length != 0
|
||||
// console.log('store.userInfo', store.userInfo);
|
||||
if (typeof uni !== 'undefined' && uni.setStorageSync) {
|
||||
uni.setStorageSync('uni-id-pages-userInfo', store.userInfo)
|
||||
}
|
||||
return data
|
||||
},
|
||||
async logout() {
|
||||
// 检查 uni 是否可用
|
||||
if (typeof uni === 'undefined') {
|
||||
console.warn('uni API not available for logout')
|
||||
return
|
||||
}
|
||||
|
||||
// 优先调用后端自定义登出接口(/api/sys/auth/logout),失败也不影响本地清理
|
||||
const backendUserId = uni.getStorageSync('backend-user-id')
|
||||
if (backendUserId) {
|
||||
try {
|
||||
await uni.request({
|
||||
url: getApiUrl('/api/sys/auth/logout'),
|
||||
method: 'POST',
|
||||
header: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
},
|
||||
data: {
|
||||
userId: backendUserId
|
||||
}
|
||||
})
|
||||
} catch (e) {
|
||||
console.error('后端登出接口调用失败:', e)
|
||||
}
|
||||
}
|
||||
|
||||
// 清理本地登录状态(后端 token + 旧 uni-id token + tenantId)
|
||||
uni.removeStorageSync('backend-token')
|
||||
uni.removeStorageSync('backend-login-response')
|
||||
uni.removeStorageSync('backend-user-id')
|
||||
uni.removeStorageSync('backend-tenant-id') // 清除 tenantId
|
||||
uni.removeStorageSync('uni_id_token');
|
||||
uni.setStorageSync('uni_id_token_expired', 0)
|
||||
this.setUserInfo({},{cover:true})
|
||||
uni.$emit('uni-id-pages-logout')
|
||||
uni.redirectTo({
|
||||
url: `/${pagesJson.uniIdRouter && pagesJson.uniIdRouter.loginPage ? pagesJson.uniIdRouter.loginPage: 'uni_modules/uni-id-pages/pages/login/login-withoutpwd'}`,
|
||||
});
|
||||
},
|
||||
loginBack (e = {}) {
|
||||
// 检查 uni 是否可用
|
||||
if (typeof uni === 'undefined') {
|
||||
console.warn('uni API not available for loginBack')
|
||||
return
|
||||
}
|
||||
|
||||
const {uniIdRedirectUrl = ''} = e
|
||||
let delta = 0; //判断需要返回几层
|
||||
let pages = getCurrentPages();
|
||||
// console.log(pages);
|
||||
pages.forEach((page, index) => {
|
||||
if (pages[pages.length - index - 1].route.split('/')[3] == 'login') {
|
||||
delta++
|
||||
}
|
||||
})
|
||||
// console.log('判断需要返回几层:', delta);
|
||||
if (uniIdRedirectUrl) {
|
||||
return uni.redirectTo({
|
||||
url: uniIdRedirectUrl,
|
||||
fail: (err1) => {
|
||||
uni.switchTab({
|
||||
url:uniIdRedirectUrl,
|
||||
fail: (err2) => {
|
||||
console.log(err1,err2)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
// #ifdef H5
|
||||
if (e.loginType == 'weixin') {
|
||||
// console.log('window.history', window.history);
|
||||
return window.history.go(-3)
|
||||
}
|
||||
// #endif
|
||||
|
||||
if (delta) {
|
||||
const page = pagesJson.pages[0]
|
||||
return uni.reLaunch({
|
||||
url: `/${page.path}`
|
||||
})
|
||||
}
|
||||
|
||||
uni.navigateBack({
|
||||
delta
|
||||
})
|
||||
},
|
||||
loginSuccess(e = {}){
|
||||
const {
|
||||
showToast = true, toastText = '登录成功', autoBack = true, uniIdRedirectUrl = '', passwordConfirmed
|
||||
} = e
|
||||
// console.log({toastText,autoBack});
|
||||
|
||||
// 检查 uni 是否可用
|
||||
if (typeof uni === 'undefined') {
|
||||
console.warn('uni API not available for loginSuccess')
|
||||
return
|
||||
}
|
||||
|
||||
if (showToast) {
|
||||
uni.showToast({
|
||||
title: toastText,
|
||||
icon: 'none',
|
||||
duration: 3000
|
||||
});
|
||||
}
|
||||
// 异步调用(更新用户信息)防止获取头像等操作阻塞页面返回
|
||||
this.updateUserInfo()
|
||||
|
||||
uni.$emit('uni-id-pages-login-success')
|
||||
|
||||
if (config.setPasswordAfterLogin && !passwordConfirmed) {
|
||||
return uni.redirectTo({
|
||||
url: uniIdRedirectUrl ? `/uni_modules/uni-id-pages/pages/userinfo/set-pwd/set-pwd?uniIdRedirectUrl=${uniIdRedirectUrl}&loginType=${e.loginType}`: `/uni_modules/uni-id-pages/pages/userinfo/set-pwd/set-pwd?loginType=${e.loginType}`,
|
||||
fail: (err) => {
|
||||
console.log(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (autoBack) {
|
||||
this.loginBack({uniIdRedirectUrl})
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// #ifdef VUE2
|
||||
import Vue from 'vue'
|
||||
// 通过Vue.observable创建一个可响应的对象
|
||||
export const store = Vue.observable(data)
|
||||
// #endif
|
||||
|
||||
// #ifdef VUE3
|
||||
// 在小程序环境中,可能 reactive 不可用,使用简单的对象
|
||||
export const store = data
|
||||
// #endif
|
||||
22
common/uni-id-pages-init.js
Normal file
22
common/uni-id-pages-init.js
Normal file
@@ -0,0 +1,22 @@
|
||||
// 简化版的 uni-id-pages 初始化
|
||||
// 由于项目不使用 uniCloud,移除了所有 uniCloud 相关功能
|
||||
|
||||
export default async function () {
|
||||
// #ifdef APP-PLUS
|
||||
// 如果配置的登录功能有一键登录,执行预登录(异步)
|
||||
// 注意:这里需要从配置中读取,但由于配置在子包中,暂时注释
|
||||
// 如果需要一键登录功能,可以在这里添加配置
|
||||
// if (loginTypes.includes('univerify')) {
|
||||
// uni.preLogin({
|
||||
// provider: 'univerify',
|
||||
// complete: e => {
|
||||
// // console.log(e);
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
// #endif
|
||||
|
||||
// 其他初始化逻辑已移除(因为不需要 uniCloud)
|
||||
console.log('[uni-id-pages] 初始化完成(简化版,无 uniCloud)')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user