export const saveImage = (url) => { uni.showLoading({ title: '正在保存图片...' }); uni.getSetting({ success: async (res) => { if (!res.authSetting["scope.writePhotosAlbum"]) { uni.authorize({ scope: "scope.writePhotosAlbum", success: async () => { const res = await uni.getImageInfo({ src: url }); uni.saveImageToPhotosAlbum({ filePath: res.path, success: () => { uni.hideLoading(); uni.showToast({ title: "保存成功!" }); }, fail: () => { uni.showToast({ title: "保存失败,请重试!", icon: 'error' }); } }); }, fail: () => { uni.hideLoading(); uni.showModal({ title: "权限未授权", content: "请前往设置中开启相册权限", success: (res) => { if (res.confirm) { uni.openSetting(); } } }); } }); } else { const res = await uni.getImageInfo({ src: url }); uni.saveImageToPhotosAlbum({ filePath: res.path, success: () => { uni.hideLoading(); uni.showToast({ title: "保存成功!" }); }, fail: () => { uni.showToast({ title: "保存失败,请重试!", icon: 'error' }); } }); } }, fail: () => { uni.hideLoading(); uni.showToast({ title: "获取权限失败!", icon: 'error' }); } }); }; /** * 将可选文本归一化为空字符串。 */ export const normalizeOptionalText = (value) => { return typeof value === 'string' ? value.trim() : '' } const APP_INVITE_CODE_STORAGE_KEY = 'appAutoInviteCode' /** * 缓存自动识别的邀请码,供注册页优先读取。 */ export const cacheAppInviteCode = (inviteCode) => { const normalizedInviteCode = normalizeOptionalText(inviteCode).toUpperCase() if (!/^[A-Z0-9]{6}$/.test(normalizedInviteCode)) { return } uni.setStorageSync(APP_INVITE_CODE_STORAGE_KEY, normalizedInviteCode) } /** * 读取并消费自动识别的邀请码。 */ export const consumeAppInviteCode = () => { const inviteCode = normalizeOptionalText(uni.getStorageSync(APP_INVITE_CODE_STORAGE_KEY)).toUpperCase() if (!/^[A-Z0-9]{6}$/.test(inviteCode)) { return '' } uni.removeStorageSync(APP_INVITE_CODE_STORAGE_KEY) return inviteCode }