diff --git a/src/actions/WaiAction.js b/src/actions/WaiAction.js index 181b62c..2e81ccc 100644 --- a/src/actions/WaiAction.js +++ b/src/actions/WaiAction.js @@ -1,7 +1,11 @@ import { fetchJSON, postForm, postJSON } from '@/utils/request'; import { API_HOST, WAI_HOST } from '@/config'; +import { usingStorage } from '@/utils/usingStorage'; + +const WAI_SERVER_KEY = 'G-STR:WAI_SERVER' export const postSendMsg = async (body) => { + const { waiServer } = usingStorage(WAI_SERVER_KEY) const { attaList=[], atta, content, ...bodyData } = body; const formData = new FormData(); Object.keys(bodyData).forEach(function (key) { @@ -10,12 +14,13 @@ export const postSendMsg = async (body) => { attaList.forEach(function (item) { formData.append('attachment', item); }); - const { result } = await postJSON(`${WAI_HOST}/messages/text`, body); + const { result } = await postJSON(`${waiServer}/messages/text`, body); return result; }; export const fetchQRCode = (phone) => { + const { waiServer } = usingStorage(WAI_SERVER_KEY) return fetchJSON( - `${WAI_HOST}/channels/qrcode`, + `${waiServer}/channels/qrcode`, { phone }) } diff --git a/src/config.js b/src/config.js index fe8ae0a..43fee66 100644 --- a/src/config.js +++ b/src/config.js @@ -11,6 +11,8 @@ // export const EMAIL_HOST = 'http://202.103.68.231:888/service-mail'; // export const WAI_HOST = 'http://47.83.248.120/api/v1'; // 香港服务器 export const WAI_HOST = 'http://47.254.53.81/api/v1'; // 美国服务器 +// export const WAI_HOST = 'http://localhost:3031/api/v1'; // 美国服务器 +export const WAI_SERVER_KEY = 'G-STR:WAI_SERVER' export const EMAIL_ATTA_HOST = 'https://p9axztuwd7x8a7.mycht.cn/attatchment'; // 邮件附件 // prod: diff --git a/src/stores/AuthStore.js b/src/stores/AuthStore.js index b73a106..4459321 100644 --- a/src/stores/AuthStore.js +++ b/src/stores/AuthStore.js @@ -3,6 +3,7 @@ import { devtools } from 'zustand/middleware' import { fetchJSON } from '@/utils/request' import { isEmpty, isNotEmpty } from '@/utils/commons' import { API_HOST, BUILD_VERSION } from '@/config' +import { usingStorage } from '@/utils/usingStorage'; export const PERM_MERGE_CONVERSATION = 'merge-conversation' export const PERM_ASSIGN_NEW_CONVERSATION = 'assign-new-conversation' @@ -55,6 +56,7 @@ const useAuthStore = create(devtools((set, get) => ({ }, login: async (authCode) => { + const { setStorage } = usingStorage() const { saveUserSession, setLoginStatus } = get() setLoginStatus(200) @@ -65,6 +67,8 @@ const useAuthStore = create(devtools((set, get) => ({ ) if (json.errcode === 0 && isNotEmpty(json.result.opisn)) { + // TODO:保存个人 WhatsApp 服务器地址 + // setStorage('G-STR:WAI_SERVER', ) set(() => ({ loginUser: { userId: json.result.opisn, diff --git a/src/utils/usingStorage.js b/src/utils/usingStorage.js new file mode 100644 index 0000000..1292d1a --- /dev/null +++ b/src/utils/usingStorage.js @@ -0,0 +1,86 @@ +const persistObject = {} + +/** + * G-INT:USER_ID -> userId = 456 + * G-STR:LOGIN_TOKEN -> loginToken = 'E6779386E7D64DF0ADD0F97767E00D8B' + * G-JSON:LOGIN_USER -> loginUser = { username: 'test-username' } + */ +export function usingStorage() { + + const getStorage = () => { + if (import.meta.env.DEV && window.localStorage) { + return window.localStorage + } else if (window.sessionStorage) { + return window.sessionStorage + } else { + console.error('browser not support localStorage and sessionStorage.') + } + } + + const setProperty = (key, value) => { + const webStorage = getStorage() + const typeAndKey = key.split(':') + if (typeAndKey.length === 2) { + const propName = camelCasedWords(typeAndKey[1]) + persistObject[propName] = value + if (typeAndKey[0] === 'G-JSON') { + webStorage.setItem(key, JSON.stringify(value)) + } else { + webStorage.setItem(key, value) + } + } + } + + // USER_ID -> userId + const camelCasedWords = (string) => { + if (typeof string !== 'string' || string.length === 0) { + return string; + } + return string.split('_').map((word, index) => { + if (index === 0) { + return word.toLowerCase() + } else { + return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase() + } + }).join('') + } + + if (Object.keys(persistObject).length == 0) { + + const webStorage = getStorage() + + for (let i = 0; i < webStorage.length; i++) { + const key = webStorage.key(i) + const typeAndKey = key.split(':') + + if (typeAndKey.length === 2) { + const value = webStorage.getItem(key) + const propName = camelCasedWords(typeAndKey[1]) + if (typeAndKey[0] === 'G-INT') { + persistObject[propName] = parseInt(value, 10) + } else if (typeAndKey[0] === 'G-JSON') { + try { + persistObject[propName] = JSON.parse(value) + } catch (e) { + // 如果解析失败,保留原始字符串值 + persistObject[propName] = value + console.error('解析 JSON 失败。') + } + } else { + persistObject[propName] = value + } + } + } + } + + return { + ...persistObject, + setStorage: (key, value) => { + setProperty(key, value) + }, + clearStorage: () => { + getStorage().clear() + Object.assign(persistObject, {}) + } + } +} diff --git a/src/views/accounts/Profile.jsx b/src/views/accounts/Profile.jsx index 1e73a12..1e15f3e 100644 --- a/src/views/accounts/Profile.jsx +++ b/src/views/accounts/Profile.jsx @@ -2,14 +2,15 @@ import { useEffect, useCallback, useState } from 'react' import { Row, Col, Space, Input, Descriptions, Avatar, Tag, Divider, List, App, Button, Flex, Select, Spin, Form, Typography, QRCode, Tooltip } from 'antd' import { UserOutlined, InfoCircleOutlined, ReloadOutlined, CheckCircleFilled } from '@ant-design/icons' import useAuthStore from '@/stores/AuthStore' -import { fetchJSON } from '@/utils/request' import { Conditional } from '@/components/Conditional' import { PERM_USE_WHATSAPP } from '@/stores/AuthStore' -import { WAI_HOST } from '@/config' import { fetchQRCode } from '@/actions/WaiAction' +import { usingStorage } from '@/utils/usingStorage'; +import { WAI_SERVER_KEY } from '@/config'; function Profile() { + const { waiServer } = usingStorage(WAI_SERVER_KEY) const { notification } = App.useApp() const [wabaForm] = Form.useForm() @@ -213,6 +214,10 @@ function Profile() {