Commit e9c0c452c87f53247da049f2627f383ea9a6fdb5

Authored by sfmind
1 parent 713817d0

请求响应错误统一处理

yudao-ui-app/pages.json
@@ -57,13 +57,13 @@ @@ -57,13 +57,13 @@
57 { 57 {
58 "path": "pages/address/list", 58 "path": "pages/address/list",
59 "style": { 59 "style": {
60 - "navigationBarTitleText": "收货列表" 60 + "navigationBarTitleText": "收货地址"
61 } 61 }
62 }, 62 },
63 { 63 {
64 "path": "pages/search/search", 64 "path": "pages/search/search",
65 "style": { 65 "style": {
66 - "navigationBarTitleText": "商品搜索" 66 + "navigationBarTitleText": "搜索"
67 } 67 }
68 }, 68 },
69 { 69 {
yudao-ui-app/pages/forgot/forgot.vue
@@ -121,7 +121,6 @@ export default { @@ -121,7 +121,6 @@ export default {
121 .then(res => { 121 .then(res => {
122 uni.$u.toast('点击了重置密码') 122 uni.$u.toast('点击了重置密码')
123 }) 123 })
124 - .catch(err => {})  
125 }, 124 },
126 navigateBack() { 125 navigateBack() {
127 uni.navigateBack() 126 uni.navigateBack()
yudao-ui-app/pages/index/index.vue
@@ -174,22 +174,14 @@ export default { @@ -174,22 +174,14 @@ export default {
174 }, 174 },
175 methods: { 175 methods: {
176 loadBannerData() { 176 loadBannerData() {
177 - getBannerData()  
178 - .then(res => {  
179 - this.bannerList = res.data  
180 - })  
181 - .catch(err => {  
182 - //console.log(err)  
183 - }) 177 + getBannerData().then(res => {
  178 + this.bannerList = res.data
  179 + })
184 }, 180 },
185 loadNoticeData() { 181 loadNoticeData() {
186 - getNoticeData()  
187 - .then(res => {  
188 - this.noticeList = res.data  
189 - })  
190 - .catch(err => {  
191 - //console.log(err)  
192 - }) 182 + getNoticeData().then(res => {
  183 + this.noticeList = res.data
  184 + })
193 }, 185 },
194 handleSearchClick(e) { 186 handleSearchClick(e) {
195 uni.$u.route('/pages/search/search') 187 uni.$u.route('/pages/search/search')
@@ -331,7 +323,6 @@ export default { @@ -331,7 +323,6 @@ export default {
331 } 323 }
332 324
333 .prod-list { 325 .prod-list {
334 -  
335 .prod-item { 326 .prod-item {
336 background: #ffffff; 327 background: #ffffff;
337 @include flex-space-between; 328 @include flex-space-between;
yudao-ui-app/pages/login/login.vue
@@ -129,22 +129,13 @@ export default { @@ -129,22 +129,13 @@ export default {
129 }) 129 })
130 130
131 //scene:1登陆获取验证码场景 131 //scene:1登陆获取验证码场景
132 - sendSmsCode({ mobile: mobile, scene: 1 })  
133 - .then(res => {  
134 - //console.log(res)  
135 - uni.hideLoading()  
136 - if (res.code === 0) {  
137 - // 这里此提示会被this.start()方法中的提示覆盖  
138 - uni.$u.toast('验证码已发送')  
139 - // 通知验证码组件内部开始倒计时  
140 - this.$refs.uCode.start()  
141 - } else {  
142 - uni.$u.toast(res.msg)  
143 - }  
144 - })  
145 - .catch(err => {  
146 - uni.$u.toast('服务器接口请求异常')  
147 - }) 132 + sendSmsCode({ mobile: mobile, scene: 1 }).then(res => {
  133 + //console.log(res)
  134 + uni.hideLoading()
  135 + uni.$u.toast('验证码已发送')
  136 + // 通知验证码组件内部开始倒计时
  137 + this.$refs.uCode.start()
  138 + })
148 } else { 139 } else {
149 uni.$u.toast('倒计时结束后再发送') 140 uni.$u.toast('倒计时结束后再发送')
150 } 141 }
@@ -159,21 +150,13 @@ export default { @@ -159,21 +150,13 @@ export default {
159 }) 150 })
160 }, 151 },
161 handleLoginPromise(promise) { 152 handleLoginPromise(promise) {
162 - promise  
163 - .then(res => {  
164 - if (res.code === 0) {  
165 - this.$store.commit('setToken', res.data)  
166 - uni.$u.toast('登录成功')  
167 - setTimeout(() => {  
168 - this.navigateBack()  
169 - }, 1000)  
170 - } else {  
171 - uni.$u.toast(res.msg)  
172 - }  
173 - })  
174 - .catch(err => {  
175 - uni.$u.toast('接口请求失败')  
176 - }) 153 + promise.then(res => {
  154 + this.$store.commit('setToken', res.data)
  155 + uni.$u.toast('登录成功')
  156 + setTimeout(() => {
  157 + this.navigateBack()
  158 + }, 1000)
  159 + })
177 }, 160 },
178 navigateBack() { 161 navigateBack() {
179 uni.navigateBack() 162 uni.navigateBack()
yudao-ui-app/pages/register/register.vue
@@ -78,12 +78,9 @@ export default { @@ -78,12 +78,9 @@ export default {
78 }) 78 })
79 }, 79 },
80 handleSubmit() { 80 handleSubmit() {
81 - this.$refs.form  
82 - .validate()  
83 - .then(res => {  
84 - uni.$u.toast('点击了注册账号')  
85 - })  
86 - .catch(err => {}) 81 + this.$refs.form.validate().then(res => {
  82 + uni.$u.toast('点击了注册账号')
  83 + })
87 }, 84 },
88 navigateBack() { 85 navigateBack() {
89 uni.navigateBack() 86 uni.navigateBack()
yudao-ui-app/pages/user/user.vue
@@ -72,7 +72,9 @@ export default { @@ -72,7 +72,9 @@ export default {
72 } 72 }
73 }, 73 },
74 onLoad() { 74 onLoad() {
75 - this.$store.dispatch('obtainUserInfo') 75 + if (this.hasLogin){
  76 + this.$store.dispatch('obtainUserInfo')
  77 + }
76 }, 78 },
77 methods: { 79 methods: {
78 loginOrJump(pageUrl) { 80 loginOrJump(pageUrl) {
yudao-ui-app/util/request/responseInterceptors.js
@@ -7,10 +7,19 @@ module.exports = vm => { @@ -7,10 +7,19 @@ module.exports = vm => {
7 res => { 7 res => {
8 //对响应成功做点什么 可使用async await 做异步操作 8 //对响应成功做点什么 可使用async await 做异步操作
9 //可以根据业务情况做相应的处理 9 //可以根据业务情况做相应的处理
10 - return res.data 10 + if (res.data.code === 0) {
  11 + return res.data
  12 + } else {
  13 + console.log(res)
  14 + //错误信息统一处理
  15 + uni.$u.toast(res.data.msg)
  16 + return Promise.reject(res)
  17 + }
11 }, 18 },
12 err => { 19 err => {
13 //对响应错误做点什么 (statusCode !== 200) 20 //对响应错误做点什么 (statusCode !== 200)
  21 + console.log(err)
  22 + uni.$u.toast('响应错误' + err.statusCode)
14 return Promise.reject(err) 23 return Promise.reject(err)
15 } 24 }
16 ) 25 )