Houjie
2 天以前 51c7896fd8e45085dd5cdfff11e79a00ee0a7379
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import Vue from 'vue'
import Vuex from 'vuex'
import api from "@/api/api"
import MinCache from '@/common/util/MinCache.js'
import {
  ACCESS_TOKEN,
  USER_NAME,
  USER_INFO,
  X_TENANT_ID
} from "@/common/util/constants"
 
Vue.use(Vuex)
 
export default new Vuex.Store({
  state: {
    auth: [],
    token: '',
    userid: '',
    username: '',
    realname: '',
    welcome: '',
    avatar: '',
    user: {}, // 补充用户信息对象
    tenantId: '', // 补充租户ID
    // 产线管理相关状态
    lineList: [], // 产线列表
    currentLineId: uni.getStorageSync('currentLineId') || null, // 当前选中的产线ID
    currentLineName: uni.getStorageSync('currentLineName') || '', // 当前选中的产线名称
    currentLineInfo: null, // 当前选中的产线详细信息
    currentLineType: uni.getStorageSync('currentLineType') || '' // 新增:当前选中的产线类型
  },
  mutations: {
    SET_AUTH(state, auth) {
      state.auth = auth
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, {
      username,
      realname,
      welcome
    }) => {
      state.username = username
      state.realname = realname
      state.welcome = welcome
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ID: (state, id) => {
      state.userid = id
    },
    SET_INFO: (state, userInfo) => {
      state.user = userInfo
    },
    SET_TENANTID: (state, tenantId) => {
      state.tenantId = tenantId
    },
    // 产线相关mutations
    SET_LINE_LIST(state, list) {
      state.lineList = list
      console.log('Vuex SET_LINE_LIST: 产线列表已更新', list);
    },
    SET_CURRENT_LINE_ID(state, lineId) {
      state.currentLineId = lineId
      uni.setStorageSync('currentLineId', lineId)
 
      // 同步更新当前产线信息、名称和类型
      if (lineId && state.lineList.length > 0) {
        state.currentLineInfo = state.lineList.find(line => line.id === lineId) || null
        // 从产线信息中提取名称并保存
        state.currentLineName = state.currentLineInfo ? state.currentLineInfo.name : ''
        // 新增:从产线信息中提取类型并保存
        state.currentLineType = state.currentLineInfo ? state.currentLineInfo.type : ''
        
        uni.setStorageSync('currentLineName', state.currentLineName)
        // 新增:将类型同步保存到本地存储
        uni.setStorageSync('currentLineType', state.currentLineType)
      } else {
        state.currentLineInfo = null
        state.currentLineName = ''
        state.currentLineType = '' // 新增:清空类型
        
        uni.removeStorageSync('currentLineName')
        uni.removeStorageSync('currentLineType') // 新增:清除本地存储的类型
      }
      console.log('Vuex SET_CURRENT_LINE_ID: 当前产线ID已更新', lineId);
      console.log('Vuex SET_CURRENT_LINE_NAME: 当前产线名称已更新', state.currentLineName);
      console.log('Vuex SET_CURRENT_LINE_TYPE: 当前产线类型已更新', state.currentLineType); // 新增:打印类型信息
    }
  },
  actions: {
    // 账号登录
    mLogin({
      commit
    }, userInfo) {
      return new Promise((resolve, reject) => {
        api.login(userInfo).then(response => {
          if (response.data.code == 200) {
            const result = response.data.result
            const userInfo = result.userInfo
            uni.setStorageSync(ACCESS_TOKEN, result.token);
            uni.setStorageSync("isLogin", true);
            uni.setStorageSync("userId", userInfo.id);
            uni.setStorageSync(USER_INFO, userInfo);
            commit('SET_TOKEN', result.token)
            commit('SET_AVATAR', userInfo.avatar)
            commit('SET_NAME', {
              username: userInfo.username,
              realname: userInfo.realname
            })
            commit('SET_ID', userInfo.id)
            commit('SET_INFO', userInfo)
            resolve(response)
          } else {
            resolve(response)
          }
        }).catch(error => {
          console.log("登录失败:", error)
          reject(error)
        })
      })
    },
    // 手机号登录
    PhoneLogin({
      commit
    }, userInfo) {
      return new Promise((resolve, reject) => {
        api.phoneNoLogin(userInfo).then(response => {
          if (response.data.code == 200) {
            const result = response.data.result
            const userInfo = result.userInfo
            uni.setStorageSync(ACCESS_TOKEN, result.token);
            uni.setStorageSync(USER_INFO, userInfo);
            commit('SET_TOKEN', result.token)
            commit('SET_NAME', {
              username: userInfo.username,
              realname: userInfo.realname
            })
            commit('SET_AVATAR', userInfo.avatar)
            commit('SET_ID', userInfo.id)
            commit('SET_INFO', userInfo)
            resolve(response)
          } else {
            reject(response)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 第三方登录
    ThirdLogin({
      commit
    }, param) {
      return new Promise((resolve, reject) => {
        api.thirdLogin(param.token, param.thirdType).then(response => {
          if (response.data.code == '200') {
            const result = response.data.result
            const userInfo = result.userInfo
            uni.setStorageSync(ACCESS_TOKEN, result.token);
            uni.setStorageSync(USER_INFO, userInfo);
            uni.setStorageSync("userId", userInfo.id);
            uni.setStorageSync(X_TENANT_ID, userInfo.loginTenantId);
 
            commit('SET_TOKEN', result.token)
            commit('SET_AVATAR', userInfo.avatar)
            commit('SET_NAME', {
              username: userInfo.username,
              realname: userInfo.realname
            })
            commit('SET_ID', userInfo.id)
            commit('SET_INFO', userInfo)
            commit('SET_TENANTID', userInfo.loginTenantId)
            resolve(response)
          } else {
            reject(response)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 登出
    Logout({
      commit,
      state
    }) {
      return new Promise((resolve) => {
        let logoutToken = state.token;
        commit('SET_TOKEN', '')
        uni.removeStorageSync(ACCESS_TOKEN)
        uni.removeStorageSync("isLogin")
        uni.removeStorageSync("userId")
        // 清除产线选择状态
        commit('SET_CURRENT_LINE_ID', null)
        api.logout(logoutToken).then(() => {
          resolve()
        }).catch(() => {
          resolve()
        })
      })
    },
    saveAuth({
      commit
    }, auth) {
      commit('SET_AUTH', auth)
    },
 
    // 获取产线列表(已包含type字段)
    fetchLineList({ commit, state }) {
      return new Promise((resolve, reject) => {
        api.getLineList({
          headers: {
            'Authorization': `Bearer ${state.token}`
          }
        }).then(response => {
          console.log('fetchLineList接口返回原始数据:', response);
          if (response.data.code === 200) {
            const lineList = (response.data.result || []).map(line => ({
              id: line.value,
              name: line.text,
              type: line.type // 确保映射type字段
            }));
            commit('SET_LINE_LIST', lineList);
            resolve(lineList);
          } else {
            reject(new Error(`获取产线列表失败: ${response.data.message || '未知错误'}`));
          }
        }).catch(error => {
          console.error('获取产线列表出错:', error);
          reject(error);
        });
      });
    },
 
    // 设置当前产线(保持不变,类型通过mutation自动同步)
    setCurrentLine({ commit }, lineId) {
      return new Promise((resolve) => {
        commit('SET_CURRENT_LINE_ID', lineId)
        resolve()
      })
    }
  },
  getters: {
    getAuth: state => state.auth,
    token: state => state.token,
    username: state => state.username || uni.getStorageSync(USER_INFO)?.username || '',
    nickname: state => state.realname || uni.getStorageSync(USER_INFO)?.realname || '',
    avatar: state => state.avatar || uni.getStorageSync(USER_INFO)?.avatar || '',
    userid: state => state.userid || uni.getStorageSync(USER_INFO)?.id || '',
    // 产线相关getters
    lineList: state => state.lineList,
    currentLineId: state => state.currentLineId,
    currentLineName: state => state.currentLineName,
    currentLineInfo: state => state.currentLineInfo,
    currentLineType: state => state.currentLineType, // 新增:获取当前产线类型的getter
    hasSelectedLine: state => !!state.currentLineId
  }
})