Skip to content

Commit a8c51ce

Browse files
committed
New: v2 版本
1 parent 0354b39 commit a8c51ce

39 files changed

+1512
-0
lines changed

admin/.env.development

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# just a flag
2+
ENV = 'development'
3+
4+
# base api
5+
VUE_APP_BASE_API = '/dev-api'
6+
7+
# vue-cli uses the VUE_CLI_BABEL_TRANSPILE_MODULES environment variable,
8+
# to control whether the babel-plugin-dynamic-import-node plugin is enabled.
9+
# It only does one thing by converting all import() to require().
10+
# This configuration can significantly increase the speed of hot updates,
11+
# when you have a large number of pages.
12+
# Detail: https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/babel-preset-app/index.js
13+
14+
VUE_CLI_BABEL_TRANSPILE_MODULES = true

admin/.env.production

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# just a flag
2+
ENV = 'production'
3+
4+
# base api
5+
VUE_APP_BASE_API = '/prod-api'
6+

admin/.env.staging

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
NODE_ENV = production
2+
3+
# just a flag
4+
ENV = 'staging'
5+
6+
# base api
7+
VUE_APP_BASE_API = '/stage-api'
8+

admin/babel.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
presets: [
3+
'@vue/app'
4+
]
5+
}

admin/build/index.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const { run } = require('runjs')
2+
const chalk = require('chalk')
3+
const config = require('../vue.config.js')
4+
const rawArgv = process.argv.slice(2)
5+
const args = rawArgv.join(' ')
6+
7+
if (process.env.npm_config_preview || rawArgv.includes('--preview')) {
8+
const report = rawArgv.includes('--report')
9+
10+
run(`vue-cli-service build ${args}`)
11+
12+
const port = 9526
13+
const publicPath = config.publicPath
14+
15+
const connect = require('connect')
16+
const serveStatic = require('serve-static')
17+
const app = connect()
18+
19+
app.use(
20+
publicPath,
21+
serveStatic('./dist', {
22+
index: ['index.html', '/']
23+
})
24+
)
25+
26+
app.listen(port, function () {
27+
console.log(chalk.green(`> Preview at http://localhost:${port}${publicPath}`))
28+
if (report) {
29+
console.log(chalk.green(`> Report at http://localhost:${port}${publicPath}report.html`))
30+
}
31+
32+
})
33+
} else {
34+
run(`vue-cli-service build ${args}`)
35+
}

admin/jest.config.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module.exports = {
2+
moduleFileExtensions: ['js', 'jsx', 'json', 'vue'],
3+
transform: {
4+
'^.+\\.vue$': 'vue-jest',
5+
'.+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$':
6+
'jest-transform-stub',
7+
'^.+\\.jsx?$': 'babel-jest'
8+
},
9+
moduleNameMapper: {
10+
'^@/(.*)$': '<rootDir>/src/$1'
11+
},
12+
snapshotSerializers: ['jest-serializer-vue'],
13+
testMatch: [
14+
'**/tests/unit/**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)'
15+
],
16+
collectCoverageFrom: ['src/utils/**/*.{js,vue}', '!src/utils/auth.js', '!src/utils/request.js', 'src/components/**/*.{js,vue}'],
17+
coverageDirectory: '<rootDir>/tests/unit/coverage',
18+
// 'collectCoverage': true,
19+
'coverageReporters': [
20+
'lcov',
21+
'text-summary'
22+
],
23+
testURL: 'http://localhost/'
24+
}

admin/mock/index.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import Mock from 'mockjs'
2+
import { param2Obj } from '../src/utils'
3+
4+
import user from './user'
5+
import table from './table'
6+
7+
const mocks = [
8+
...user,
9+
...table
10+
]
11+
12+
// for front mock
13+
// please use it cautiously, it will redefine XMLHttpRequest,
14+
// which will cause many of your third-party libraries to be invalidated(like progress event).
15+
export function mockXHR() {
16+
// mock patch
17+
// https://github.com/nuysoft/Mock/issues/300
18+
Mock.XHR.prototype.proxy_send = Mock.XHR.prototype.send
19+
Mock.XHR.prototype.send = function() {
20+
if (this.custom.xhr) {
21+
this.custom.xhr.withCredentials = this.withCredentials || false
22+
23+
if (this.responseType) {
24+
this.custom.xhr.responseType = this.responseType
25+
}
26+
}
27+
this.proxy_send(...arguments)
28+
}
29+
30+
function XHR2ExpressReqWrap(respond) {
31+
return function(options) {
32+
let result = null
33+
if (respond instanceof Function) {
34+
const { body, type, url } = options
35+
// https://expressjs.com/en/4x/api.html#req
36+
result = respond({
37+
method: type,
38+
body: JSON.parse(body),
39+
query: param2Obj(url)
40+
})
41+
} else {
42+
result = respond
43+
}
44+
return Mock.mock(result)
45+
}
46+
}
47+
48+
for (const i of mocks) {
49+
Mock.mock(new RegExp(i.url), i.type || 'get', XHR2ExpressReqWrap(i.response))
50+
}
51+
}
52+
53+
// for mock server
54+
const responseFake = (url, type, respond) => {
55+
return {
56+
url: new RegExp(`/mock${url}`),
57+
type: type || 'get',
58+
response(req, res) {
59+
res.json(Mock.mock(respond instanceof Function ? respond(req, res) : respond))
60+
}
61+
}
62+
}
63+
64+
export default mocks.map(route => {
65+
return responseFake(route.url, route.type, route.response)
66+
})

admin/mock/mock-server.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const chokidar = require('chokidar')
2+
const bodyParser = require('body-parser')
3+
const chalk = require('chalk')
4+
const path = require('path')
5+
6+
const mockDir = path.join(process.cwd(), 'mock')
7+
8+
function registerRoutes(app) {
9+
let mockLastIndex
10+
const { default: mocks } = require('./index.js')
11+
for (const mock of mocks) {
12+
app[mock.type](mock.url, mock.response)
13+
mockLastIndex = app._router.stack.length
14+
}
15+
const mockRoutesLength = Object.keys(mocks).length
16+
return {
17+
mockRoutesLength: mockRoutesLength,
18+
mockStartIndex: mockLastIndex - mockRoutesLength
19+
}
20+
}
21+
22+
function unregisterRoutes() {
23+
Object.keys(require.cache).forEach(i => {
24+
if (i.includes(mockDir)) {
25+
delete require.cache[require.resolve(i)]
26+
}
27+
})
28+
}
29+
30+
module.exports = app => {
31+
// es6 polyfill
32+
require('@babel/register')
33+
34+
// parse app.body
35+
// https://expressjs.com/en/4x/api.html#req.body
36+
app.use(bodyParser.json())
37+
app.use(bodyParser.urlencoded({
38+
extended: true
39+
}))
40+
41+
const mockRoutes = registerRoutes(app)
42+
var mockRoutesLength = mockRoutes.mockRoutesLength
43+
var mockStartIndex = mockRoutes.mockStartIndex
44+
45+
// watch files, hot reload mock server
46+
chokidar.watch(mockDir, {
47+
ignored: /mock-server/,
48+
ignoreInitial: true
49+
}).on('all', (event, path) => {
50+
if (event === 'change' || event === 'add') {
51+
try {
52+
// remove mock routes stack
53+
app._router.stack.splice(mockStartIndex, mockRoutesLength)
54+
55+
// clear routes cache
56+
unregisterRoutes()
57+
58+
const mockRoutes = registerRoutes(app)
59+
mockRoutesLength = mockRoutes.mockRoutesLength
60+
mockStartIndex = mockRoutes.mockStartIndex
61+
62+
console.log(chalk.magentaBright(`\n > Mock Server hot reload success! changed ${path}`))
63+
} catch (error) {
64+
console.log(chalk.redBright(error))
65+
}
66+
}
67+
})
68+
}

admin/mock/table.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import Mock from 'mockjs'
2+
3+
const data = Mock.mock({
4+
'items|30': [{
5+
id: '@id',
6+
title: '@sentence(10, 20)',
7+
'status|1': ['published', 'draft', 'deleted'],
8+
author: 'name',
9+
display_time: '@datetime',
10+
pageviews: '@integer(300, 5000)'
11+
}]
12+
})
13+
14+
export default [
15+
{
16+
url: '/table/list',
17+
type: 'get',
18+
response: config => {
19+
const items = data.items
20+
return {
21+
code: 20000,
22+
data: {
23+
total: items.length,
24+
items: items
25+
}
26+
}
27+
}
28+
}
29+
]

admin/mock/user.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
2+
const tokens = {
3+
admin: {
4+
token: 'admin-token'
5+
},
6+
editor: {
7+
token: 'editor-token'
8+
}
9+
}
10+
11+
const users = {
12+
'admin-token': {
13+
roles: ['admin'],
14+
introduction: 'I am a super administrator',
15+
avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
16+
name: 'Super Admin'
17+
},
18+
'editor-token': {
19+
roles: ['editor'],
20+
introduction: 'I am an editor',
21+
avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
22+
name: 'Normal Editor'
23+
}
24+
}
25+
26+
export default [
27+
// user login
28+
{
29+
url: '/user/login',
30+
type: 'post',
31+
response: config => {
32+
const { username } = config.body
33+
const token = tokens[username]
34+
35+
// mock error
36+
if (!token) {
37+
return {
38+
code: 60204,
39+
message: 'Account and password are incorrect.'
40+
}
41+
}
42+
43+
return {
44+
code: 20000,
45+
data: token
46+
}
47+
}
48+
},
49+
50+
// get user info
51+
{
52+
url: '/user/info\.*',
53+
type: 'get',
54+
response: config => {
55+
const { token } = config.query
56+
const info = users[token]
57+
58+
// mock error
59+
if (!info) {
60+
return {
61+
code: 50008,
62+
message: 'Login failed, unable to get user details.'
63+
}
64+
}
65+
66+
return {
67+
code: 20000,
68+
data: info
69+
}
70+
}
71+
},
72+
73+
// user logout
74+
{
75+
url: '/user/logout',
76+
type: 'post',
77+
response: _ => {
78+
return {
79+
code: 20000,
80+
data: 'success'
81+
}
82+
}
83+
}
84+
]

0 commit comments

Comments
 (0)