fix
songhuangfeng123
2022-04-07 02409ed8fe01eb40b40b87ca872dd4b07d3b633b
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
 
function modalInstance(modal) {
  const vm = modal.vm
  const type = modal.type
  const title = modal.title
  const content = '<p>' + modal.message + '</p>'
 
  switch (type) {
    case 'info':
      vm.$alert(content, title || '提示', {
        confirmButtonText: '确定',
        type: 'info',
        dangerouslyUseHTMLString: true,
        callback: action => {
        }
      })
      break
    case 'success':
      vm.$alert(content, title || '成功', {
        confirmButtonText: '确定',
        dangerouslyUseHTMLString: true,
        type: 'success',
        callback: action => {
        }
      })
      break
    case 'warning':
      vm.$alert(content, title || '警告', {
        confirmButtonText: '确定',
        dangerouslyUseHTMLString: true,
        type: 'warning',
        callback: action => {
        }
      })
 
      break
    case 'error':
      vm.$alert(content, title || '错误', {
        confirmButtonText: '确定',
        dangerouslyUseHTMLString: true,
        type: 'error',
        callback: action => {
 
        }
      })
      break
  }
}
 
function noticeInstance(notice) {
  const vm = notice.vm
  const type = notice.type
  const title = notice.title
  const content = notice.message
  switch (type) {
    case 'info':
      vm.$notify.info({
        title: title || '提示',
        message: content
      })
      break
    case 'success':
      vm.$notify.success({
        title: title || '成功',
        message: content
      })
      break
    case 'warning':
      vm.$notify.warning({
        title: title || '警告',
        message: content
      })
      break
    case 'error':
      vm.$notify.error({
        title: title || '错误',
        message: content,
        duration: 40
      })
      break
  }
}
 
function confirmInstance(modal) {
  const vm = modal.vm
  const title = modal.title
  const content = modal.message
  const okAction = modal.okAction
  const cancelAction = modal.cancelAction
  const okText = modal.okText
  const cancelText = modal.cancelText
  vm.$confirm('<p>' + content + '</p>', title || '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    dangerouslyUseHTMLString: true,
    type: 'warning'
  }).then(() => {
    vm.$message({
      type: 'success',
      message: '删除成功!'
    })
  }).catch(() => {
    vm.$message({
      type: 'info',
      message: '已取消删除'
    })
  })
}
 
export function parseError(parseParam) {
  const vm = parseParam.vm
  const error = parseParam.error
  const timeoutMessage = parseParam.timeoutMessage
  const defaultMessage = parseParam.defaultMessage
  let type = parseParam.type
  if (!type) {
    type = 'modalInstance'
  }
  if (error) {
    const errorStr = error + ''
    if (errorStr.search('timeout') != -1) {
      vm.$message.error({
        content: timeoutMessage || error,
        duration: 10
      })
    } else if (errorStr.search('token is expired') != -1 || errorStr.search('token is invalid') != -1) {
      const modal = {
        'vm': vm,
        'message': 'Token已失效,请重新登录...',
        'okText': '重新登录',
        'okAction': function() {
          vm.$router.push({
            name: 'logout'
          })
        },
        'cancelAction': function() {
          vm.$message.info('Clicked cancel')
        }
      }
      confirmInstance(modal)
    } else if (error.response) {
      const status = error.response.status
      if (status == 403) {
        const modal = {
          'vm': vm,
          'message': 'Token已失效,请重新登录...',
          'okText': '重新登录',
          'okAction': function() {
            vm.$router.push({
              name: 'logout'
            })
          },
          'cancelAction': function() {
            vm.$message.info('Clicked cancel')
          }
        }
        confirmInstance(modal)
      } else if (status == 401) {
        modalInstance({ type: 'error', message: '没有权限操作', vm: vm })
      } else {
        modalInstance({ type: 'error', message: defaultMessage || error, vm: vm })
      }
    } else {
      if (type == 'modalInstance') {
        modalInstance({ type: 'error', message: error, vm: vm })
      } else {
        noticeInstance({ type: 'error', message: error, vm: vm })
      }
    }
  } else {
    modalInstance({ type: 'error', message: String(error), vm: vm })
  }
}