Skip to content

Commit 717c2c2

Browse files
committed
Merge branch 'demo'
2 parents 5cfca40 + c32cc54 commit 717c2c2

19 files changed

+298
-46
lines changed

env/.env

+2
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ VITE_WX_APPID = 'wxa2abb91f64032a2b'
1616
VITE_FALLBACK_LOCALE = 'zh-Hans'
1717

1818
VITE_SERVER_BASEURL = 'https://ukw0y1.laf.run'
19+
20+
VITE_ENV_FILE = '.env'

env/.env.development

+2
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ NODE_ENV = 'development'
44
VITE_DELETE_CONSOLE = false
55

66
# VITE_SERVER_BASEURL = 'https://ukw0y1.laf.run'
7+
8+
VITE_ENV_FILE = '.env.development'

env/.env.production

+2
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ NODE_ENV = 'development'
44
VITE_DELETE_CONSOLE = true
55

66
# VITE_SERVER_BASEURL = 'https://xxx.com'
7+
8+
VITE_ENV_FILE = '.env.production'

env/.env.test

+2
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ NODE_ENV = 'development'
44
VITE_DELETE_CONSOLE = false
55

66
# VITE_SERVER_BASEURL = 'https://xxx.com'
7+
8+
VITE_ENV_FILE = '.env.test'

mock/test.ts

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// test.ts
2+
3+
import { MockMethod, MockConfig } from 'vite-plugin-mock'
4+
5+
export default [
6+
{
7+
url: '/api/get',
8+
method: 'get',
9+
response: ({ query }) => {
10+
return {
11+
code: 0,
12+
result: {
13+
name: '菲鸽',
14+
},
15+
}
16+
},
17+
},
18+
{
19+
url: '/api/post',
20+
method: 'post',
21+
timeout: 2000,
22+
response: {
23+
code: 0,
24+
result: {
25+
name: '菲鸽',
26+
},
27+
},
28+
},
29+
{
30+
url: '/api/text',
31+
method: 'post',
32+
rawResponse: async (req, res) => {
33+
let reqbody = ''
34+
await new Promise((resolve) => {
35+
req.on('data', (chunk) => {
36+
reqbody += chunk
37+
})
38+
req.on('end', () => resolve(undefined))
39+
})
40+
res.setHeader('Content-Type', 'text/plain')
41+
res.statusCode = 200
42+
res.end(`hello, ${reqbody}`)
43+
},
44+
},
45+
] as MockMethod[]

package.json

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
"dev:app-ios": "uni -p app-ios",
2929
"dev:custom": "uni -p",
3030
"dev": "uni",
31+
"dev-dev": "uni --mode development",
32+
"dev-test": "uni --mode test",
33+
"dev-prod": "uni --mode production",
3134
"dev:h5": "uni",
3235
"dev:h5:ssr": "uni --ssr",
3336
"dev:mp": "uni -p mp-weixin",
@@ -155,6 +158,7 @@
155158
"unplugin-auto-import": "^0.17.2",
156159
"vite": "4.3.5",
157160
"vite-plugin-imagemin": "^0.6.1",
161+
"vite-plugin-mock": "^3.0.1",
158162
"vite-plugin-restart": "^0.4.0",
159163
"vite-plugin-svg-icons": "^2.0.1",
160164
"vite-plugin-vue-setup-extend": "^0.4.0",

pnpm-lock.yaml

+99-34
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/hooks/useNavbarWeixin.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { onReady } from '@dcloudio/uni-app'
2-
import { getIsTabbar, getArrElementByIdx } from '@/utils/index'
2+
import { getIsTabbar, getLastItem } from '@/utils/index'
33

44
export default () => {
55
// 获取页面栈
@@ -15,7 +15,7 @@ export default () => {
1515
// 基于小程序的 Page 类型扩展 uni-app 的 Page
1616
type PageInstance = Page.PageInstance & WechatMiniprogram.Page.InstanceMethods<any>
1717
// 获取当前页面实例,数组最后一项
18-
const pageInstance = getArrElementByIdx(getCurrentPages(), -1) as PageInstance
18+
const pageInstance = getLastItem(getCurrentPages()) as PageInstance
1919

2020
// 页面渲染完毕,绑定动画效果
2121
onReady(() => {

src/interceptors/request.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ const httpInterceptor = {
2222
options.url += `?${queryStr}`
2323
}
2424
}
25-
26-
// 1. 非 http 开头需拼接地址
27-
if (!options.url.startsWith('http')) {
28-
options.url = baseURL + options.url
25+
// api 目前为 mock 接口,所以无需处理
26+
if (!options.url.startsWith('/api')) {
27+
if (!options.url.startsWith('http')) {
28+
// 1. 非 http 开头需拼接地址
29+
options.url = baseURL + options.url
30+
}
2931
}
3032
// 2. 请求超时
3133
options.timeout = 10000 // 10s

src/main.ts

+7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ import 'virtual:svg-icons-register'
88
import 'virtual:uno.css'
99
import '@/style/index.scss'
1010

11+
// production mock server
12+
if (process.env.NODE_ENV === 'production') {
13+
import('./mockProdServer').then(({ setupProdMockServer }) => {
14+
setupProdMockServer()
15+
})
16+
}
17+
1118
export function createApp() {
1219
const app = createSSRApp(App)
1320
app.use(store)

src/mockProdServer.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { createProdMockServer } from 'vite-plugin-mock/dist/client'
2+
3+
const modules = import.meta.glob('../mock/**/*.ts', { eager: true })
4+
5+
const mockModules: any[] = []
6+
Object.keys(modules).forEach((key) => {
7+
if (key.includes('/_')) {
8+
return
9+
}
10+
mockModules.push(...(modules as Recordable)[key].default)
11+
})
12+
13+
/**
14+
* Used in a production environment. Need to manually import all modules
15+
*/
16+
export function setupProdMockServer() {
17+
createProdMockServer(mockModules)
18+
}

0 commit comments

Comments
 (0)