import {MAIN_API} from "@/config"; const SHARE_USER_ID_KEY = "share_user_id" const normalizeStorageValue = (value) => { if (value === undefined || value === null) return "" const text = String(value).trim() if (!text || text === "undefined" || text === "null") return "" return text } const parsePathQuery = (path = "") => { const [pathname = "", queryString = ""] = String(path).split("?") const query = {} if (!queryString) return {pathname, query} queryString.split("&").forEach(segment => { if (!segment) return const [rawKey = "", rawValue = ""] = segment.split("=") const key = decodeURIComponent(rawKey) if (!key) return query[key] = decodeURIComponent(rawValue) }) return {pathname, query} } const buildPathQuery = (pathname = "", query = {}) => { const queryString = Object.keys(query) .filter(key => normalizeStorageValue(key) && normalizeStorageValue(query[key]) !== "") .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(query[key])}`) .join("&") return queryString ? `${pathname}?${queryString}` : pathname } export const setLocation = ({latitude, longitude, province_str, city_str, area_str, area_id, city_id, province_id}) => { if (latitude) uni.setStorageSync("latitude", latitude) if (longitude) uni.setStorageSync("longitude", longitude) uni.setStorageSync("province_str", province_str) uni.setStorageSync("city_str", city_str) uni.setStorageSync("area_str", area_str) uni.setStorageSync("area_id", area_id) uni.setStorageSync("city_id", city_id) uni.setStorageSync("province_id", province_id) } export const getLocation = () => { return { latitude: uni.getStorageSync("latitude"), longitude: uni.getStorageSync("longitude"), province_str: uni.getStorageSync("province_str"), city_str: uni.getStorageSync("city_str"), area_str: uni.getStorageSync("area_str"), area_id: uni.getStorageSync("area_id"), city_id: uni.getStorageSync("city_id"), province_id: uni.getStorageSync("province_id") } } export const setUserInfo = (user) => { if (user.uid) uni.setStorageSync("uid", user.uid) if (user.username) uni.setStorageSync("username", user.username) if (user.token) uni.setStorageSync("token", user.token) if (user.header) uni.setStorageSync("header", `${MAIN_API}${user.header}`) if (user.phone) uni.setStorageSync("phone", user.phone) } export const getUserInfo = () => { return { uid: uni.getStorageSync("uid"), username: uni.getStorageSync("username"), token: uni.getStorageSync("token"), header: uni.getStorageSync("header"), phone: uni.getStorageSync("phone"), } } export const clearUserInfo = () => { uni.removeStorageSync("uid"); uni.removeStorageSync("username"); uni.removeStorageSync("token"); uni.removeStorageSync("header"); uni.removeStorageSync("phone"); } export const getShareUserId = () => normalizeStorageValue(uni.getStorageSync(SHARE_USER_ID_KEY)) export const setShareUserId = (userId) => { const value = normalizeStorageValue(userId) if (!value) return "" uni.setStorageSync(SHARE_USER_ID_KEY, value) return value } export const clearShareUserId = () => { uni.removeStorageSync(SHARE_USER_ID_KEY) } export const saveShareUserIdFromQuery = (query = {}) => { const shareUserId = normalizeStorageValue( query.shareUserId ?? query.inviterUserId ?? query.userId ?? query.uid ) if (!shareUserId) return "" const currentUserId = normalizeStorageValue(uni.getStorageSync("uid")) if (currentUserId && currentUserId === shareUserId) return "" return setShareUserId(shareUserId) } export const appendPathParams = (path = "", params = {}) => { const {pathname, query} = parsePathQuery(path) Object.keys(params).forEach(key => { const value = normalizeStorageValue(params[key]) if (value === "") return query[key] = value }) return buildPathQuery(pathname, query) } export const getCurrentPagePath = () => { const pages = typeof getCurrentPages === "function" ? getCurrentPages() : [] const currentPage = pages[pages.length - 1] if (!currentPage || !currentPage.route) return "" return appendPathParams(`/${currentPage.route}`, currentPage.options || {}) } export const buildSharePath = (path = "") => { const shareUserId = normalizeStorageValue(uni.getStorageSync("uid")) const targetPath = normalizeStorageValue(path) || getCurrentPagePath() if (!targetPath || !shareUserId) return targetPath return appendPathParams(targetPath, {shareUserId}) }