wbc
6 天以前 983dddd66ab8a999544272089e8c0fc12370f356
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import Vue from 'vue'
import Vuex from 'vuex'
 
//引入vuex插件 进行状态管理
Vue.use(Vuex) 
 
// 定义服务器预设配置
const serverPresets = [
    {
        name: '内网',
        url: 'http://192.168.1.22:10055/api'
    },
    {
        name: '外网',
        url: 'http://36.26.21.214:10055/api'
    },
    {
        name: '本地调试',
        url: 'http://localhost:5184/api'
    }
    // 可以根据需要添加更多预设
];
 
// 从本地存储获取已保存的服务器地址,如果没有则使用预设中的第一个(内网)
const getSavedServerAPI = () => {
    try {
        const savedAPI = uni.getStorageSync('server_api_address');
        return savedAPI || serverPresets[0].url; // 默认返回内网地址
    } catch (e) {
        return serverPresets[0].url; // 默认返回内网地址
    }
};
 
const store = new Vuex.Store({
    state: { 
        id: 'id',
        serverInfo:{//服务信息
            networkFlag:'内网', 
            serverURLInt:'http://192.168.11.251:10055',//服务器体检 10.0.1.104:10054
            serverURL:'http://localhost:10055',//本地调试地址
            //serverAPI:'http://localhost:5184/api',//当前正在使用的服务器,默认为外网  localhost
            serverAPI: getSavedServerAPI(),//当前正在使用的服务器,从本地存储获取或使用默认值
            ftpServer:'ftp://36.26.21.214',//FTP服务器地址
            
            // 服务器预设配置,包括显示名称和完整API地址
            serverPresets: serverPresets
        }
    },
    mutations: {
        test(state,id){
            state.id = id;
        },
        // 更新服务器API地址
        updateServerAPI(state, url) {
            state.serverInfo.serverAPI = url;
            // 将更新后的地址保存到本地存储
            try {
                uni.setStorageSync('server_api_address', url);
            } catch (e) {
                console.error('保存服务器地址失败:', e);
            }
        },
        // 添加自定义服务器预设
        addServerPreset(state, preset) {
            // 检查是否已存在相同URL的预设
            const exists = state.serverInfo.serverPresets.some(item => item.url === preset.url);
            if (!exists) {
                state.serverInfo.serverPresets.push(preset);
            }
        }
    },
    getters:{
        currentColor(state){
            return state.colorList[state.colorIndex]
        }
    },
    actions: {
        // lazy loading openid
    }
})
 
export default store