Commit cb40959e146a8a485a1bf1bc7c19ceb2a22dc964
1 parent
cb1ec74e
添加mock 数据
Showing
10 changed files
with
115 additions
and
22 deletions
config/dev.env.js
... | ... | @@ -3,5 +3,7 @@ const merge = require('webpack-merge') |
3 | 3 | const prodEnv = require('./prod.env') |
4 | 4 | |
5 | 5 | module.exports = merge(prodEnv, { |
6 | - NODE_ENV: '"development"' | |
6 | + NODE_ENV: '"development"', | |
7 | + ENV_CONFIG: '"dev"', | |
8 | + BASE_API: '"http://192.168.101.26:8089/"' | |
7 | 9 | }) | ... | ... |
config/prod.env.js
package-lock.json
... | ... | @@ -5554,6 +5554,15 @@ |
5554 | 5554 | "minimist": "0.0.8" |
5555 | 5555 | } |
5556 | 5556 | }, |
5557 | + "mockjs": { | |
5558 | + "version": "1.0.1-beta3", | |
5559 | + "resolved": "http://registry.npm.taobao.org/mockjs/download/mockjs-1.0.1-beta3.tgz", | |
5560 | + "integrity": "sha1-0jTzwnJWOXVk8slVFC6JGQlTcgk=", | |
5561 | + "dev": true, | |
5562 | + "requires": { | |
5563 | + "commander": "*" | |
5564 | + } | |
5565 | + }, | |
5557 | 5566 | "move-concurrently": { |
5558 | 5567 | "version": "1.0.1", |
5559 | 5568 | "resolved": "http://registry.npm.taobao.org/move-concurrently/download/move-concurrently-1.0.1.tgz", | ... | ... |
package.json
src/api/api.js
1 | 1 | import request from '@/utils/request' |
2 | 2 | |
3 | -export function fetchList(query) { | |
3 | +export function fetchList() { | |
4 | 4 | return request({ |
5 | 5 | url: '/article/list', |
6 | 6 | method: 'get', |
7 | - params: query | |
8 | 7 | }) |
9 | 8 | } |
10 | 9 | |
... | ... | @@ -12,7 +11,7 @@ export function fetchArticle(id) { |
12 | 11 | return request({ |
13 | 12 | url: '/article/detail', |
14 | 13 | method: 'get', |
15 | - params: { id } | |
14 | + params: {id} | |
16 | 15 | }) |
17 | 16 | } |
18 | 17 | |
... | ... | @@ -20,7 +19,7 @@ export function fetchPv(pv) { |
20 | 19 | return request({ |
21 | 20 | url: '/article/pv', |
22 | 21 | method: 'get', |
23 | - params: { pv } | |
22 | + params: {pv} | |
24 | 23 | }) |
25 | 24 | } |
26 | 25 | ... | ... |
src/main.js
src/mock/mock.js
0 → 100644
1 | +import Mock from 'mockjs' | |
2 | +import pdaApi from './pda' | |
3 | + | |
4 | +// 修复在使用 MockJS 情况下,设置 withCredentials = true,且未被拦截的跨域请求丢失 Cookies 的问题 | |
5 | +// https://github.com/nuysoft/Mock/issues/300 | |
6 | +Mock.XHR.prototype.proxy_send = Mock.XHR.prototype.send | |
7 | +Mock.XHR.prototype.send = function () { | |
8 | + if (this.custom.xhr) { | |
9 | + this.custom.xhr.withCredentials = this.withCredentials || false | |
10 | + } | |
11 | + this.proxy_send(...arguments) | |
12 | +} | |
13 | + | |
14 | +Mock.mock('/article/list', 'get', pdaApi.getList); | |
15 | + | |
16 | +export default Mock | ... | ... |
src/mock/pda.js
0 → 100644
1 | +import Mock from 'mockjs' | |
2 | + | |
3 | +const Random = Mock.Random; | |
4 | +const list = []; | |
5 | + | |
6 | +for (let i = 0; i < 10; i++) { | |
7 | + let newList = { | |
8 | + title: Random.csentence(5, 30), | |
9 | + date: Random.date() + ' ' + Random.time() | |
10 | + } | |
11 | + list.push(newList) | |
12 | +} | |
13 | + | |
14 | +export default { | |
15 | + getList: () => { | |
16 | + return { | |
17 | + total: list.length, | |
18 | + items: list | |
19 | + } | |
20 | + } | |
21 | +} | |
22 | + | ... | ... |
src/utils/request.js
... | ... | @@ -2,7 +2,7 @@ import axios from 'axios' |
2 | 2 | |
3 | 3 | // create an axios instance |
4 | 4 | const service = axios.create({ |
5 | - baseURL: '', // base_url | |
5 | + baseURL: process.env.BASE_API, // api 的 base_url | |
6 | 6 | timeout: 5000 // request timeout |
7 | 7 | }) |
8 | 8 | |
... | ... | @@ -25,20 +25,47 @@ service.interceptors.request.use( |
25 | 25 | |
26 | 26 | // response interceptor |
27 | 27 | service.interceptors.response.use( |
28 | + response => response, | |
28 | 29 | /** |
29 | 30 | * 下面的注释为通过在response里,自定义code来标示请求状态 |
31 | + * 当code返回如下情况则说明权限有问题,登出并返回到登录页 | |
32 | + * 如想通过 xmlhttprequest 来状态码标识 逻辑可写在下面error中 | |
33 | + * 以下代码均为样例,请结合自生需求加以修改,若不需要,则可删除 | |
30 | 34 | */ |
31 | - response => { | |
32 | - const res = response.data | |
33 | - if (res.code !== 200) { | |
34 | - alert( res.message) | |
35 | - } else { | |
36 | - return response.data | |
37 | - } | |
38 | - }, | |
35 | + // response => { | |
36 | + // const res = response.data | |
37 | + // if (res.code !== 20000) { | |
38 | + // Message({ | |
39 | + // message: res.message, | |
40 | + // type: 'error', | |
41 | + // duration: 5 * 1000 | |
42 | + // }) | |
43 | + // // 50008:非法的token; 50012:其他客户端登录了; 50014:Token 过期了; | |
44 | + // if (res.code === 50008 || res.code === 50012 || res.code === 50014) { | |
45 | + // // 请自行在引入 MessageBox | |
46 | + // // import { Message, MessageBox } from 'element-ui' | |
47 | + // MessageBox.confirm('你已被登出,可以取消继续留在该页面,或者重新登录', '确定登出', { | |
48 | + // confirmButtonText: '重新登录', | |
49 | + // cancelButtonText: '取消', | |
50 | + // type: 'warning' | |
51 | + // }).then(() => { | |
52 | + // store.dispatch('FedLogOut').then(() => { | |
53 | + // location.reload() // 为了重新实例化vue-router对象 避免bug | |
54 | + // }) | |
55 | + // }) | |
56 | + // } | |
57 | + // return Promise.reject('error') | |
58 | + // } else { | |
59 | + // return response.data | |
60 | + // } | |
61 | + // }, | |
39 | 62 | error => { |
40 | 63 | console.log('err' + error) // for debug |
41 | - alert(error.message) | |
64 | + // Message({ | |
65 | + // message: error.message, | |
66 | + // type: 'error', | |
67 | + // duration: 5 * 1000 | |
68 | + // }) | |
42 | 69 | return Promise.reject(error) |
43 | 70 | } |
44 | 71 | ) | ... | ... |
src/views/pdasection.vue
1 | 1 | <template> |
2 | - <div> | |
3 | - <titlesection></titlesection> | |
4 | - <totalsection></totalsection> | |
5 | - <div class="flexfm"></div> | |
6 | - </div> | |
2 | + <div> | |
3 | + <titlesection></titlesection> | |
4 | + <totalsection></totalsection> | |
5 | + <div class="flexfm"></div> | |
6 | + </div> | |
7 | 7 | </template> |
8 | 8 | |
9 | 9 | <script> |
10 | 10 | import titlesection from '../components/titlesection' |
11 | 11 | import totalsection from '../components/total' |
12 | +import {fetchList} from '../api/api' | |
13 | + | |
12 | 14 | export default { |
13 | - name: 'pdasection', | |
15 | + name: 'pdasection', | |
14 | 16 | components: { |
15 | 17 | titlesection, |
16 | 18 | totalsection |
19 | + }, | |
20 | + created() { | |
21 | + this.getList() | |
22 | + }, | |
23 | + methods: { | |
24 | + getList() { | |
25 | + fetchList() | |
26 | + .then(res => { | |
27 | + console.log(res); | |
28 | + | |
29 | + }); | |
30 | + }, | |
17 | 31 | } |
18 | 32 | } |
19 | 33 | </script> | ... | ... |