Просмотр исходного кода

应用市场错误响应拦截与错误页制作

lixp11 лет назад: 4
Родитель
Сommit
61126b762e

+ 0 - 54
2020/appstore/index.js

@ -1,54 +0,0 @@
1
import Vue from 'vue'
2
import axios from 'axios'
3
import router from '../router'
4
import VueAxios from 'vue-axios'
5
import store from '../store'
6
import {Toast, Dialog} from 'cube-ui'
7

8
Vue.use(VueAxios, axios);
9

10

11
axios.interceptors.request.use(function (config) {
12
  // 在发送请求之前做些什么,添加公共前缀,自动添加sessionid
13
  let sessionId = store.state.sessionId;
14
  if (config.method == 'post') {
15
    if (sessionId) {
16
      config.data.sessionId = sessionId;
17
    }
18
    config.url = "/attendance" + config.url
19
  } else if (config.method == 'get') {
20
    if (sessionId) {
21
      config.url = "/attendance" + config.url + '?sessionId=' + sessionId
22
    }
23
  }
24

25
  // 添加loading
26
  return config;
27
}, function (error) { // 对请求错误做些什么
28
  console.error(error)
29
  return Promise.reject(error);
30
});
31

32
axios.interceptors.response.use(function (response) {  // 在接收请求之前做些什么
33
    // 这里就是status == 200
34
    let data = response.data
35

36
    // 未登录,登录
37
    if (data.respCode != '0000') {
38
      if (data.respCode == '0001') {  // 权限失败
39
        router.push({name: "Error"})  // 往错误页面跳转
40
      } else if (!response.config.headers.errorHandler) {  // 可在请求时在header里面设置参数表示自己处理异常
41
        Dialog.$create({type: 'alert', title: '提示', content: response.data.respMsg || '系统异常,请稍后重试'}).show()
42
      }
43
      return Promise.reject(response)
44
    }
45
    return data
46
  },
47
  function (error) {    // 对响应错误做些什么
48
    Toast.$create({
49
      type: 'text',
50
      txt: '服务器开小差了,请稍后重试',
51
      time: 2000
52
    }).show()
53
    return Promise.reject(error);
54
  })

+ 45 - 0
2020/appstore/src/assets/css/index-mobile.css

@ -498,6 +498,11 @@
498 498
    text-align: left;
499 499
  }
500 500
501
  .app-search-history-item,
502
  .app-search-history-delete {
503
    cursor: pointer;
504
  }
505
501 506
  .app-search-history-item {
502 507
    display: inline-block;
503 508
    width: auto;
@ -538,4 +543,44 @@
538 543
  .app-search-item .app-download {
539 544
    margin-top: .42rem;
540 545
  }
546
547
  .no-data-img {
548
    width: 3.2rem;
549
    height: 2.8rem;
550
    background: url(../img/no-data-mobile.png) center no-repeat;
551
    margin: 0 auto;
552
  }
553
  
554
  .no-data-text {
555
    font-size: .16rem;
556
    color: #232D34;
557
    line-height: .24rem;
558
  }
559
560
  /* 错误页 */
561
  .error-page {
562
    margin-left: -1.25rem;
563
    margin-top: -1.675rem;
564
  }
565
566
  .error-img {
567
    width: 2.5rem;
568
    height: 2.19rem;
569
    background: url(../img/error500-mobile.png) center no-repeat;
570
  }
571
572
  .error-text {
573
    text-align: center;
574
    font-size: .16rem;
575
    line-height: .24rem;
576
    margin-top: .2rem;
577
  }
578
579
  .back-home {
580
    width: .88rem;
581
    height: .32rem;
582
    font-size: .14rem;
583
    line-height: .32rem;
584
    margin: .4rem auto 0;
585
  }
541 586
}

+ 4 - 0
2020/appstore/src/assets/css/index.css

@ -19,6 +19,10 @@ body {
19 19
  text-align: center;
20 20
}
21 21
22
a {
23
  text-decoration: none;
24
}
25
22 26
.main-content {
23 27
  width: 960px;
24 28
  margin: 0 auto;

BIN
2020/appstore/src/assets/img/error500-mobile.png


BIN
2020/appstore/src/assets/img/error500-pc.png


BIN
2020/appstore/src/assets/img/no-data-mobile.png


BIN
2020/appstore/src/assets/img/no-data-pc.png


+ 27 - 0
2020/appstore/src/axios.js

@ -0,0 +1,27 @@
1
import axios from 'axios';
2
import router from './router/index';
3
4
// axios.interceptors.request.use(
5
//   (config) => {
6
//     // 在发送请求之前做些什么,添加公共前缀,自动添加sessionid
7
//   },
8
//   (error) => {
9
//     // 对请求错误做些什么
10
//     return Promise.reject(error);
11
// });
12
13
axios.interceptors.response.use(
14
  response => { // status == 200 接收请求之前判断
15
    let data = response.data;
16
17
    if (data.X_RESULTCODE !== '0') {
18
      return Promise.reject(response);
19
    }
20
    return data;
21
  },
22
  error => { // 处理响应错误
23
    router.push({ name: 'Error' });
24
  },
25
);
26
27
export default axios;

+ 8 - 6
2020/appstore/src/main.js

@ -1,11 +1,5 @@
1 1
import Vue from 'vue';
2
import App from './App.vue';
3
import axios from 'axios';
4
import router from './router';
5
import store from './store';
6 2
7
import './assets/css/index.css';
8
import './assets/css/index-mobile.css';
9 3
import {
10 4
  Input,
11 5
  Row,
@ -13,6 +7,14 @@ import {
13 7
  Loading,
14 8
} from 'element-ui';
15 9
10
import App from './App.vue';
11
import store from './store';
12
import router from './router';
13
import axios from './axios';
14
15
import './assets/css/index.css';
16
import './assets/css/index-mobile.css';
17
16 18
Vue.config.productionTip = false;
17 19
Vue.prototype.$http = axios;
18 20

+ 0 - 44
2020/appstore/src/store/index.js

@ -8,51 +8,10 @@ export default new Vuex.Store({
8 8
  state: {
9 9
    appTypeId: null,
10 10
    appData: [], // APP_TYPE []
11
    appId: null,
12 11
  },
13 12
  getters: {
14 13
    appData: (state) => state.appData, // 获取接口下 app 数据字段
15
    // appLists: (state) => { // 搜索也获取所有分类下的 app,从中筛选
16
    //   let allApps = [];
17
    //   let appData = state.appData;
18
    //   if (appData.length !== 0) {
19
    //     appData.forEach((e) => {
20
    //       allApps = allApps.concat(e.APP_LIST);
21
    //     });
22
    //     return allApps;
23
    //   }
24
    //   return [];
25
    // },
26
    appList: (state) => { // APP_LIST [] 获取当前分类下的 app 组
27
      let appData = state.appData;
28
      let appTypeId = state.appTypeId;
29
      if (appData.length !== 0 && appTypeId) {
30
        let list = appData.find((e) => e.APP_CATALOG_ID == state.appTypeId).APP_LIST; // 忽略字符串与数字
31
        return list;
32
      }
33
      return [];
34
    },
35 14
    appTypeId: (state) => state.appTypeId, // 获取当前配置的 类别
36
    appTypeName: (state) => { // 获取当前配置的 类别
37
      let appData = state.appData;
38
      let appTypeId = state.appTypeId;
39
      if (appData.length !== 0 && appTypeId) {
40
        let name = appData.find((e) => e.APP_CATALOG_ID == state.appTypeId).CATALOG_NAME;
41
        return name;
42
      }
43
      return '';
44
    },
45
    app: (state) => { // 获取当前 app
46
      let appData = state.appData;
47
      let appTypeId = state.appTypeId;
48
      let appId = state.appId;
49
      if (appData.length !== 0 && appTypeId && appId) {
50
        let list = appData.find((e) => e.APP_CATALOG_ID == appTypeId).APP_LIST;
51
        let app = list.find((e) => e.APP_ID == appId);
52
        return app;
53
      }
54
      return {};
55
    },
56 15
  },
57 16
  mutations: {
58 17
    setAppData(state, data) { // 设置接口 app 数据
@ -64,9 +23,6 @@ export default new Vuex.Store({
64 23
    setAppTypeId(state, typeId) { // 设置 appTypeId
65 24
      state.appTypeId = typeId;
66 25
    },
67
    setAppId(state, appId) { // 设置 appId
68
      state.appId = appId;
69
    },
70 26
  },
71 27
  actions: {},
72 28
});

+ 18 - 14
2020/appstore/src/views/Detail.vue

@ -1,5 +1,5 @@
1 1
<template>
2
  <div v-loading="showLoading" class="app-detail">
2
  <div class="app-detail">
3 3
    <div class="catalog">
4 4
      <div class="main-content">
5 5
        <div class="front-catalog">{{appTypeName}}<span>></span></div>
@ -93,19 +93,32 @@ export default {
93 93
  },
94 94
  data() {
95 95
    return {
96
      showLoading: true,
97 96
      showDownCard: false,
98 97
      appDownLeft: '',
99 98
      appDownTop: '',
100
      judgeUrl: null,
99
      judgeUrl: null, // 设备对应的 apk 链接
101 100
    };
102 101
  },
103 102
  computed: {
104 103
    appTypeName() {
105
      return this.$store.getters.appTypeName;
104
      let appData = this.$store.getters.appData;
105
      let appTypeId = this.$store.getters.appTypeId;
106
      if (appData.length !== 0 && appTypeId) {
107
        let name = appData.find((e) => e.APP_CATALOG_ID == appTypeId).CATALOG_NAME;
108
        return name;
109
      }
110
      return '';
106 111
    },
107 112
    app() {
108
      return this.$store.getters.app;
113
      let appData = this.$store.getters.appData;
114
      let appTypeId = this.$route.query.appTypeId;
115
      let appId = this.$route.query.appId;
116
      if (appData.length !== 0 && appTypeId && appId) {
117
        let list = appData.find((e) => e.APP_CATALOG_ID == appTypeId).APP_LIST;
118
        let app = list.find((e) => e.APP_ID == appId);
119
        return app;
120
      }
121
      return {};
109 122
    },
110 123
    apkType() {
111 124
      if (this.app !== undefined && Object.keys(this.app).length !== 0) {
@ -115,17 +128,9 @@ export default {
115 128
      return [];
116 129
    },
117 130
  },
118
  created() {
119
    // 设置当前页的 app 信息
120
    let appTypeId = this.$route.query.appTypeId;
121
    let appId = this.$route.query.appId;
122
    this.$store.commit('setAppTypeId', appTypeId);
123
    this.$store.commit('setAppId', appId);
124
  },
125 131
  methods: {
126 132
    showPreview(app) { // 是否展示 app 预览图
127 133
      if (Object.keys(app).length !== 0) {
128
        this.showLoading = false;
129 134
        if (this.getAppPreview(app).length !== 0) {
130 135
          return true;
131 136
        }
@ -142,7 +147,6 @@ export default {
142 147
        }
143 148
      });
144 149
      appPreview.sort();
145
      this.appPreviewArr = appPreview;
146 150
      return appPreview;
147 151
    },
148 152
    sortApkType(type) { // app 版本排序

+ 42 - 2
2020/appstore/src/views/Error.vue

@ -1,17 +1,57 @@
1 1
<template>
2
  <div class="error-page">请求不到数据</div>
2
  <div class="error-page">
3
    <div class="error-img"></div>
4
    <div class="error-text">{{warningText}}</div>
5
    <div class="back-home" @click="backHome">返回首页</div>
6
  </div>
3 7
</template>
4 8
5 9
<script>
6 10
export default {
7 11
  data() {
8 12
    return {
9
      text: '数据请求错误',
13
      warningText: '对不起,服务器连接失败…',
10 14
    };
11 15
  },
16
  methods: {
17
    backHome() {
18
      this.$router.push({ name: 'Home' });
19
    },
20
  },
12 21
};
13 22
</script>
14 23
15 24
<style>
25
.error-page {
26
  position: fixed;
27
  left: 50%;
28
  top: 50%;
29
  margin-left: -160px;
30
  margin-top: -183px;
31
  font-weight: 400;
32
}
33
34
.error-img {
35
  width: 320px;
36
  height: 280px;
37
  background: url(../assets/img/error500-pc.png) center no-repeat;
38
}
39
40
.error-text {
41
  font-size: 16px;
42
  color: #232D34;
43
  line-height: 24px;
44
}
16 45
46
.back-home {
47
  width: 88px;
48
  height: 32px;
49
  background: #19BB20;
50
  border-radius: 4px;
51
  font-size: 14px;
52
  color: #FFFFFF;
53
  line-height: 32px;
54
  margin: 30px auto 0;
55
  cursor: pointer;
56
}
17 57
</style>

+ 9 - 11
2020/appstore/src/views/Frame.vue

@ -54,7 +54,7 @@
54 54
      </div>
55 55
    </transition>
56 56
57
    <router-view/>
57
    <router-view v-loading="showLoading" />
58 58
  </div>
59 59
</template>
60 60
@ -76,6 +76,9 @@ export default {
76 76
    showNav() { // 是否展示
77 77
      return !(window.innerWidth < 980 && this.$route.path !== '/');
78 78
    },
79
    showLoading() {
80
      return this.appTypes.length === 0;
81
    },
79 82
  },
80 83
  created() {
81 84
    // 请求数据
@ -87,26 +90,21 @@ export default {
87 90
    // 测试环境 /display/getAppType  /ipu-home/wlwdata?action=appStore.getAppType
88 91
    this.$http.get('/ipu-home/wlwdata?action=appStore.getAppType')
89 92
      .then((res) => {
90
        this.appTypes = res.data.APP_TYPE;
91
        this.$store.commit('setAppData', res.data.APP_TYPE);
92
      })
93
      .catch((err) => {
94
        this.$router.push({ name: 'Error' });
93
        this.appTypes = res.APP_TYPE;
94
        this.$store.commit('setAppData', res.APP_TYPE);
95 95
      });
96 96
  },
97
  beforeRouteUpdate(to, from, next) {
98
    // 进入之前,更新state信息
97
  beforeRouteUpdate(to, from, next) { // 进入之前,更新state信息
99 98
    if (to.query.appTypeId) {
100 99
      this.$store.commit('setAppTypeId', to.query.appTypeId);
100
    } else {
101
      this.$store.commit('setAppTypeId', this.appTypes[0].APP_CATALOG_ID);
101 102
    }
102 103
    next();
103 104
  },
104 105
  methods: {
105 106
    isActive(typeId) {
106 107
      if (this.$route.name === 'Search') return false;
107
      if (this.$route.name === 'Home' && Object.keys(this.$route.query).length === 0) {
108
        this.$store.commit('setAppTypeId', this.appTypes[0].APP_CATALOG_ID);
109
      }
110 108
      let appTypeId = this.$store.getters.appTypeId;
111 109
      return appTypeId == typeId; // 忽略类型
112 110
    },

+ 15 - 14
2020/appstore/src/views/Home.vue

@ -1,8 +1,8 @@
1 1
<template>
2 2
  <div class="app-home">
3
    <div v-loading="showLoading" class="app-list">
3
    <div class="app-list">
4 4
      <el-row :gutter="49.5">
5
        <template v-for="(app, index) in getAppList()">
5
        <template v-for="(app, index) in appList">
6 6
          <el-col :span="8" :key="index">
7 7
            <div class="app-item" @click="toAppDetail(app.APP_ID)">
8 8
              <div class="main-content">
@ -62,21 +62,23 @@ export default {
62 62
  },
63 63
  data() {
64 64
    return {
65
      showLoading: true,
66 65
      showDownload: false,
67 66
      apkType: [],
68
      judgeUrl: null,
67
      judgeUrl: null, // 设备对应的 apk 链接
69 68
    };
70 69
  },
71
  methods: {
72
    getAppList() {
73
      let appList = this.$store.getters.appList;
74
      this.showLoading = false;
75
      if (appList.length !== 0) {
76
        return appList;
70
  computed: {
71
    appList() {
72
      let appData = this.$store.getters.appData;
73
      let appTypeId = this.$store.getters.appTypeId;
74
      if (appData.length !== 0 && appTypeId) {
75
        let list = appData.find((e) => e.APP_CATALOG_ID == appTypeId).APP_LIST; // 忽略字符串与数字
76
        return list;
77 77
      }
78 78
      return [];
79 79
    },
80
  },
81
  methods: {
80 82
    sortApkType(type) { // app 版本排序
81 83
      let apkType = [...type]; // 不能直接对 $store 内的数据进行倒转操作,先复制
82 84
      if (apkType.length === 1 || apkType[0].APK_TYPE.toLowerCase() === 'ios') {
@ -86,7 +88,6 @@ export default {
86 88
    },
87 89
    toAppDetail(appId) { // 进入 app 详情页
88 90
      let appTypeId = this.$store.getters.appTypeId;
89
      this.$store.commit('setAppId', appId);
90 91
      this.$router.push({
91 92
        name: 'Detail',
92 93
        query: {
@ -95,12 +96,12 @@ export default {
95 96
        },
96 97
      });
97 98
    },
98
    toDownload(app) {
99
    toDownload(app) { // 下载按钮
99 100
      let apk = {};
100 101
      this.apkType = this.sortApkType(app.APP_INFO);
101
      if (window.innerWidth >= 980) {
102
      if (window.innerWidth >= 980) { // pc 弹出二维码
102 103
        this.showDownload = true;
103
      } else {
104
      } else { // 辨别设备 跳转下载链接
104 105
        const UA = navigator.userAgent.toLowerCase();
105 106
        if (UA.indexOf('micromessenger') !== -1) return alert('请点击右上角前往浏览器下载');
106 107
        if (UA.indexOf('iphone') !== -1) {

+ 33 - 25
2020/appstore/src/views/Search.vue

@ -48,13 +48,17 @@
48 48
              </div>
49 49
              <div class="app-desc">{{app.APP_BRIEF}}</div>
50 50
            </div>
51
            <a class="app-download" @click.stop="toDownload(app)">下载</a>
51
            <a class="app-download" :href="judgeUrl"
52
              @click.stop="toDownload(app)">下载</a>
52 53
          </div>
53 54
        </div>
54 55
      </template>
55 56
    </div>
56 57
57
    <div v-else class="no-data">未查询到资源,请重新搜索</div>
58
    <div v-else class="no-data">
59
      <div class="no-data-img"></div>
60
      <div class="no-data-text">{{warningText}}</div>
61
    </div>
58 62
59 63
    <div v-if="showDownload">
60 64
      <div class="black-sheild">
@ -87,10 +91,12 @@ export default {
87 91
  },
88 92
  data() {
89 93
    return {
90
      showDownload: false,
91
      apkType: [],
94
      showDownload: false, // 展示下载二维码
95
      apkType: [], // app 版本
92 96
      value: '',
93 97
      hisArr: [], // 搜索历史记录
98
      judgeUrl: null, // 设备对应的 apk 链接
99
      warningText: '对不起,没有找到您搜索的结果',
94 100
    };
95 101
  },
96 102
  computed: {
@ -99,28 +105,18 @@ export default {
99 105
      let appData = this.$store.getters.appData;
100 106
      if (appData.length !== 0) {
101 107
        appData.forEach((e) => {
102
          allApps = allApps.concat(e.APP_LIST);
108
          allApps = allApps.concat(e.APP_LIST); // 将所有分类下 app 合并在一起
103 109
        });
104
      }
105
      if (allApps.length !== 0) {
106 110
        let resultApps = this.querySearch(allApps);
107
        if (resultApps.length !== 0) {
108
          return resultApps;
109
        }
111
        return resultApps;
110 112
      }
111 113
      return [];
112 114
    },
113
    showResults() { // 展示搜索结果 如无则显示未查询到页面
114
      if (this.resultApps.length !== 0 && this.$route.query.key) { // 有结果且有查询词
115
        return true;
116
      }
117
      if (window.innerWidth < 980 && this.resultApps.length === 0 && !this.$route.query.key) { // 移动端无查询
118
        return true;
119
      }
120
      return false;
115
    showResults() { // 展示搜索结果 如无则显示未查询到
116
      return !(this.resultApps.length === 0 && this.$route.query.key); // 有查询词且返回结果为空
121 117
    },
122 118
    showHistory() { // 展示搜索历史记录
123
      return this.resultApps.length === 0 && !this.$route.query.key // 移动端无查询时
119
      return (this.resultApps.length === 0 && !this.$route.query.key); // 移动端无查询
124 120
    },
125 121
  },
126 122
  created() {
@ -138,11 +134,10 @@ export default {
138 134
  },
139 135
  methods: {
140 136
    querySearch(allApps) { // 搜索
141
      this.showLoading = false;
142
      let value = this.$route.query.key;
143
      if (value) {
137
      let val = this.$route.query.key;
138
      if (val) {
144 139
        let results = allApps.filter((e) => {
145
          return e.APP_NAME.toLowerCase().indexOf(value.toLowerCase()) !== -1;
140
          return e.APP_NAME.toLowerCase().indexOf(val.toLowerCase()) !== -1;
146 141
        });
147 142
        return results; // 搜索词有效时匹配数据(可能为空)
148 143
      }
@ -231,8 +226,8 @@ export default {
231 226
    toDownload(app) { // 下载
232 227
      let apk = {};
233 228
      this.apkType = this.sortApkType(app.APP_INFO);
234
      if (window.innerWidth >= 980) {
235
        this.showDownload = true; // pc 端启用二维码,移动端识别设备挂载链接
229
      if (window.innerWidth >= 980) { // pc 端启用二维码,移动端识别设备挂载链接
230
        this.showDownload = true;
236 231
      } else {
237 232
        const UA = navigator.userAgent.toLowerCase();
238 233
        if (UA.indexOf('micromessenger') !== -1) return alert('请点击右上角前往浏览器下载');
@ -296,4 +291,17 @@ export default {
296 291
.no-data {
297 292
  margin-top: 50px;
298 293
}
294
295
.no-data-img {
296
  width: 320px;
297
  height: 280px;
298
  background: url(../assets/img/no-data-pc.png) center no-repeat;
299
  margin: 0 auto;
300
}
301
302
.no-data-text {
303
  font-size: 16px;
304
  color: #232D34;
305
  line-height: 24px;
306
}
299 307
</style>