/** * 开发环境专用:可拖拽的 baseURL 切换悬浮按钮 * 正式环境(release)不显示 */ const PRODUCTION_URL = 'https://soulapi.quwanzhi.com' const STORAGE_KEY = 'apiBaseUrl' const POSITION_KEY = 'envSwitchPosition' const URL_OPTIONS = [ { label: '生产', url: PRODUCTION_URL }, { label: '本地', url: 'http://localhost:8080' }, { label: '测试', url: 'https://souldev.quwanzhi.com' }, ] Component({ data: { visible: false, x: 20, y: 120, currentLabel: '生产', areaWidth: 375, areaHeight: 812, }, lifetimes: { attached() { try { const accountInfo = wx.getAccountInfoSync?.() const envVersion = accountInfo?.miniProgram?.envVersion || 'release' if (envVersion === 'release') { return } const sys = wx.getSystemInfoSync?.() || {} const areaWidth = sys.windowWidth || 375 const areaHeight = sys.windowHeight || 812 const saved = wx.getStorageSync(POSITION_KEY) const pos = saved ? JSON.parse(saved) : { x: 20, y: 120 } // 与 app.js 一致:storage 优先,否则用 globalData(已按 env 自动切换) const current = wx.getStorageSync(STORAGE_KEY) || getApp().globalData?.baseUrl || PRODUCTION_URL const opt = URL_OPTIONS.find(o => o.url === current) || URL_OPTIONS[0] this.setData({ visible: true, x: pos.x ?? 20, y: pos.y ?? 120, currentLabel: opt.label, areaWidth, areaHeight, }) } catch (_) { this.setData({ visible: false }) } }, }, methods: { onTap() { const items = URL_OPTIONS.map(o => o.label) const current = wx.getStorageSync(STORAGE_KEY) || PRODUCTION_URL const idx = URL_OPTIONS.findIndex(o => o.url === current) wx.showActionSheet({ itemList: items, success: (res) => { const opt = URL_OPTIONS[res.tapIndex] wx.setStorageSync(STORAGE_KEY, opt.url) const app = getApp() if (app && app.globalData) { app.globalData.baseUrl = opt.url } this.setData({ currentLabel: opt.label }) wx.showToast({ title: `已切到${opt.label}`, icon: 'none', duration: 1500 }) }, }) }, onMovableChange(e) { const { x, y } = e.detail if (typeof x === 'number' && typeof y === 'number') { wx.setStorageSync(POSITION_KEY, JSON.stringify({ x, y })) } }, }, })