Commit cb40959e146a8a485a1bf1bc7c19ceb2a22dc964

Authored by liuqimichale
1 parent cb1ec74e

添加mock 数据

config/dev.env.js
@@ -3,5 +3,7 @@ const merge = require('webpack-merge') @@ -3,5 +3,7 @@ const merge = require('webpack-merge')
3 const prodEnv = require('./prod.env') 3 const prodEnv = require('./prod.env')
4 4
5 module.exports = merge(prodEnv, { 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
1 'use strict' 1 'use strict'
2 module.exports = { 2 module.exports = {
3 - NODE_ENV: '"production"' 3 + NODE_ENV: '"production"',
  4 + ENV_CONFIG: '"prod"',
  5 + BASE_API: '"https://api-prod"'
4 } 6 }
package-lock.json
@@ -5554,6 +5554,15 @@ @@ -5554,6 +5554,15 @@
5554 "minimist": "0.0.8" 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 "move-concurrently": { 5566 "move-concurrently": {
5558 "version": "1.0.1", 5567 "version": "1.0.1",
5559 "resolved": "http://registry.npm.taobao.org/move-concurrently/download/move-concurrently-1.0.1.tgz", 5568 "resolved": "http://registry.npm.taobao.org/move-concurrently/download/move-concurrently-1.0.1.tgz",
package.json
@@ -31,6 +31,7 @@ @@ -31,6 +31,7 @@
31 "file-loader": "^1.1.4", 31 "file-loader": "^1.1.4",
32 "friendly-errors-webpack-plugin": "^1.6.1", 32 "friendly-errors-webpack-plugin": "^1.6.1",
33 "html-webpack-plugin": "^2.30.1", 33 "html-webpack-plugin": "^2.30.1",
  34 + "mockjs": "^1.0.1-beta3",
34 "node-notifier": "^5.1.2", 35 "node-notifier": "^5.1.2",
35 "optimize-css-assets-webpack-plugin": "^3.2.0", 36 "optimize-css-assets-webpack-plugin": "^3.2.0",
36 "ora": "^1.2.0", 37 "ora": "^1.2.0",
src/api/api.js
1 import request from '@/utils/request' 1 import request from '@/utils/request'
2 2
3 -export function fetchList(query) { 3 +export function fetchList() {
4 return request({ 4 return request({
5 url: '/article/list', 5 url: '/article/list',
6 method: 'get', 6 method: 'get',
7 - params: query  
8 }) 7 })
9 } 8 }
10 9
@@ -12,7 +11,7 @@ export function fetchArticle(id) { @@ -12,7 +11,7 @@ export function fetchArticle(id) {
12 return request({ 11 return request({
13 url: '/article/detail', 12 url: '/article/detail',
14 method: 'get', 13 method: 'get',
15 - params: { id } 14 + params: {id}
16 }) 15 })
17 } 16 }
18 17
@@ -20,7 +19,7 @@ export function fetchPv(pv) { @@ -20,7 +19,7 @@ export function fetchPv(pv) {
20 return request({ 19 return request({
21 url: '/article/pv', 20 url: '/article/pv',
22 method: 'get', 21 method: 'get',
23 - params: { pv } 22 + params: {pv}
24 }) 23 })
25 } 24 }
26 25
src/main.js
@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
2 // (runtime-only or standalone) has been set in webpack.base.conf with an alias. 2 // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
3 import Vue from 'vue' 3 import Vue from 'vue'
4 import App from './App' 4 import App from './App'
  5 +import './mock/mock'
5 import '@/styles/reset.css'/*引入重置样式*/ 6 import '@/styles/reset.css'/*引入重置样式*/
6 7
7 Vue.config.productionTip = false 8 Vue.config.productionTip = false
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 &#39;axios&#39; @@ -2,7 +2,7 @@ import axios from &#39;axios&#39;
2 2
3 // create an axios instance 3 // create an axios instance
4 const service = axios.create({ 4 const service = axios.create({
5 - baseURL: '', // base_url 5 + baseURL: process.env.BASE_API, // api 的 base_url
6 timeout: 5000 // request timeout 6 timeout: 5000 // request timeout
7 }) 7 })
8 8
@@ -25,20 +25,47 @@ service.interceptors.request.use( @@ -25,20 +25,47 @@ service.interceptors.request.use(
25 25
26 // response interceptor 26 // response interceptor
27 service.interceptors.response.use( 27 service.interceptors.response.use(
  28 + response => response,
28 /** 29 /**
29 * 下面的注释为通过在response里,自定义code来标示请求状态 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 error => { 62 error => {
40 console.log('err' + error) // for debug 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 return Promise.reject(error) 69 return Promise.reject(error)
43 } 70 }
44 ) 71 )
src/views/pdasection.vue
1 <template> 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 </template> 7 </template>
8 8
9 <script> 9 <script>
10 import titlesection from '../components/titlesection' 10 import titlesection from '../components/titlesection'
11 import totalsection from '../components/total' 11 import totalsection from '../components/total'
  12 +import {fetchList} from '../api/api'
  13 +
12 export default { 14 export default {
13 - name: 'pdasection', 15 + name: 'pdasection',
14 components: { 16 components: {
15 titlesection, 17 titlesection,
16 totalsection 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 </script> 33 </script>