-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
65f11aa
commit 64b23dc
Showing
10 changed files
with
143 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# 请求模版封装:微信版 | ||
|
||
## 安装方法 | ||
|
||
``` | ||
npm intall @request-template/wechat | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "../../api-extractor.json", | ||
"mainEntryPointFilePath": "./dist/packages/<unscopedPackageName>/src/index.d.ts", | ||
"dtsRollup": { | ||
"publicTrimmedFilePath": "./dist/request-template-vue3-hooks.d.ts" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"name": "@request-template/wechat", | ||
"version": "1.0.0", | ||
"description": "请求模板封装-微信版", | ||
"main": "dist/request-template-wechat.esm-bundler.js", | ||
"types": "dist/request-template-wechat.d.ts", | ||
"buildOptions": { | ||
"filename": "request-template-wechat", | ||
"formats": [ | ||
"esm-bundler", | ||
"cjs" | ||
] | ||
}, | ||
"sideEffects": false, | ||
"files": [ | ||
"dist/*", | ||
"CHANGELOG.md", | ||
"README.md" | ||
], | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"scripts": {}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/mengxinssfd/request-template.git" | ||
}, | ||
"author": "DYH", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/mengxinssfd/request-template/issues" | ||
}, | ||
"homepage": "https://github.com/mengxinssfd/request-template#readme", | ||
"devDependencies": { | ||
"axios": "^0.26.1", | ||
"request-template": "^2.0.0" | ||
}, | ||
"peerDependencies": { | ||
"request-template": "^2.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { Context, CustomConfig, RequestTemplate } from 'request-template'; | ||
|
||
export interface WechatCustomConfig extends CustomConfig { | ||
loading?: boolean; | ||
showSuccessMsg?: boolean; | ||
successMsg?: string; | ||
} | ||
|
||
export class WechatRequestTemplate< | ||
CC extends WechatCustomConfig = WechatCustomConfig, | ||
> extends RequestTemplate<CC> { | ||
protected isCancel(value: any) { | ||
return value?.errMsg === 'request:fail abort'; | ||
} | ||
|
||
protected fetch(ctx) { | ||
const baseConfig = this.globalConfigs.requestConfig; | ||
const config = ctx.requestConfig; | ||
|
||
const method = config.method || baseConfig.method; | ||
return new Promise((resolve, reject) => { | ||
const task = wx.request({ | ||
url: (config.baseURL || baseConfig.baseURL) + (config.url || baseConfig.url), | ||
method, | ||
data: | ||
method === 'get' | ||
? { ...baseConfig.params, ...config.params } | ||
: { ...baseConfig.data, ...config.data }, | ||
header: { ...baseConfig.headers, ...config.headers }, | ||
success: resolve, | ||
fail: reject, | ||
}); | ||
// 注册取消事件 | ||
this.registerCanceler(ctx, task.abort.bind(task)); | ||
}) as any; | ||
} | ||
|
||
// eslint-disable-next-line | ||
protected handleCanceler(_ctx) {} | ||
|
||
protected beforeRequest(ctx: Context<CC>) { | ||
// 复用基础模板逻辑 | ||
super.beforeRequest(ctx); | ||
|
||
// 未设置showSuccessMsg时,且非get请求则全部显示请求成功信息 | ||
if (ctx.requestConfig.method !== 'get' && ctx.customConfig.showSuccessMsg === undefined) { | ||
ctx.customConfig.showSuccessMsg = true; | ||
} | ||
|
||
// 全局开启loading | ||
if (ctx.customConfig.loading) { | ||
wx.showLoading({ title: 'LOADING...' }); | ||
} | ||
} | ||
|
||
// 关闭loading | ||
protected afterRequest(ctx) { | ||
super.afterRequest(ctx); // 复用基础模板逻辑 | ||
// 加个定时器避免请求太快,loading一闪而过 | ||
setTimeout(() => { | ||
wx.hideLoading(); | ||
}, 200); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './WechatRequestTemplate'; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters