Skip to content

Commit 4d0069b

Browse files
committed
demo: 新增支付宝demo
1 parent 5bcea67 commit 4d0069b

File tree

10 files changed

+237
-0
lines changed

10 files changed

+237
-0
lines changed

demo/alipay-demo/app.acss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
page {
2+
background: #f7f7f7;
3+
}

demo/alipay-demo/app.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
App({
2+
onLaunch(options) {
3+
// 第一次打开
4+
// options.query == {number:1}
5+
console.info('App onLaunch');
6+
},
7+
onShow(options) {
8+
// 从后台被 scheme 重新打开
9+
// options.query == {number:1}
10+
},
11+
});

demo/alipay-demo/app.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"pages": [
3+
"pages/index/index"
4+
],
5+
"window": {
6+
"defaultTitle": "Axios 能力测试"
7+
}
8+
}

demo/alipay-demo/package-lock.json

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/alipay-demo/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "alipay-axios-demo",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "app.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"dependencies": {
12+
"axios": "^0.19.2",
13+
"axios-miniprogram-adapter": "^0.3.0"
14+
}
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.btn {
2+
display: block;
3+
width: 80%;
4+
margin: 10px auto;
5+
border-radius: 8px;
6+
height: 40px;
7+
display: flex;
8+
align-items: center;
9+
justify-content: center;
10+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
<view>
3+
注意打开调试工具,查看console 和 network来测试
4+
<button type="primary" class="btn" onTap="handleRequestByGet">
5+
普通get请求
6+
</button>
7+
8+
<button type="primary" class="btn" onTap="handleRequestByPOST">
9+
普通POST请求
10+
</button>
11+
12+
<button type="primary" class="btn" onTap="handleRequestByGetParams">
13+
get请求带参数
14+
</button>
15+
16+
<button type="primary" class="btn" onTap="handleRequestByPOSTParams">
17+
普通POST请求带参数
18+
</button>
19+
20+
<button type="primary" class="btn" onTap="handleRequestByHeaders">
21+
自定义请求头参数
22+
</button>
23+
24+
<button type="primary" class="btn" onTap="handleRequestByContentType">
25+
content-type 修改
26+
</button>
27+
28+
29+
30+
31+
</view>
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import axios from 'axios'
2+
import mpAdapter from 'axios-miniprogram-adapter'
3+
axios.defaults.adapter = mpAdapter
4+
5+
// 创建实例 设置baseURL
6+
const instance = axios.create({
7+
baseURL: 'https://wx.jk724.com'
8+
})
9+
10+
Page({
11+
async handleRequestByGet() {
12+
my.showLoading();
13+
try {
14+
const resp = await instance.get('/api/HotSearchWords')
15+
console.log('GET请求成功:', resp)
16+
17+
} catch (error) {
18+
//
19+
} finally {
20+
my.hideLoading();
21+
}
22+
},
23+
async handleRequestByPOST() {
24+
my.showLoading();
25+
try {
26+
const resp = await instance.post('https://t.captcha.qq.com/cap_union_new_verify')
27+
console.log('POST请求成功:', resp)
28+
29+
} catch (error) {
30+
//
31+
} finally {
32+
my.hideLoading();
33+
}
34+
},
35+
// get请求带参数
36+
async handleRequestByGetParams() {
37+
my.showLoading();
38+
try {
39+
const resp = await instance({
40+
url: '/api/HotSearchWords',
41+
params: {
42+
name: '我是参数'
43+
}
44+
})
45+
console.log('get带参数请求成功:', resp)
46+
47+
} catch (error) {
48+
//
49+
} finally {
50+
my.hideLoading();
51+
}
52+
},
53+
// post请求带参数(注意查看请求头的 content-type: application/json 和 请求体的 Reuquest Payload)
54+
async handleRequestByPOSTParams() {
55+
my.showLoading();
56+
try {
57+
const resp = await instance({
58+
method: 'POST',
59+
url: 'https://t.captcha.qq.com/cap_union_new_verify',
60+
data: {
61+
name: '我是参数'
62+
}
63+
})
64+
console.log('post带参数请求成功:', resp)
65+
66+
} catch (error) {
67+
//
68+
} finally {
69+
my.hideLoading();
70+
}
71+
},
72+
// 自定义请求头参数
73+
async handleRequestByHeaders() {
74+
my.showLoading();
75+
try {
76+
const resp = await instance({
77+
method: 'GET',
78+
url: '/api/HotSearchWords',
79+
headers: {
80+
Authorization: 'im a token'
81+
}
82+
})
83+
console.log('post带参数请求成功:', resp)
84+
85+
} catch (error) {
86+
//
87+
} finally {
88+
my.hideLoading();
89+
}
90+
},
91+
// 修改content type (注意查看请求头的 content-type: application/x-www-form-urlencoded 和 请求体的 form data)
92+
async handleRequestByContentType() {
93+
my.showLoading();
94+
try {
95+
const resp = await instance({
96+
method: 'POST',
97+
url: 'https://t.captcha.qq.com/cap_union_new_verify',
98+
headers: {
99+
'content-type': 'application/x-www-form-urlencoded'
100+
},
101+
data: {
102+
name: '我是参数'
103+
}
104+
})
105+
console.log('post带参数请求成功:', resp)
106+
107+
} catch (error) {
108+
//
109+
} finally {
110+
my.hideLoading();
111+
}
112+
}
113+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

demo/alipay-demo/snapshot.png

15.6 KB
Loading

0 commit comments

Comments
 (0)