Skip to content

Commit da77fe6

Browse files
committed
改用callfunction调用云函数,不需要配置云函数绑定域名
1 parent fb3844f commit da77fe6

File tree

5 files changed

+98
-88
lines changed

5 files changed

+98
-88
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.hbuilderx
2+
.vscode
3+
node_modules
4+
unpackage

App.vue

+15-38
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,23 @@
11
<script>
22
/* 本项目为开源项目,作者微信:zheng593446899,如有问题可联系*/
3+
import { login, inviteTrack } from "./request"
4+
35
export default {
4-
onLaunch: function(e) {
6+
onLaunch: async function(e) {
57
console.log(e)
68
console.log('App Launch');
7-
var openid = wx.getStorageSync('openid');
9+
var openid = uni.getStorageSync('openid');
810
if (openid) {
911
this.globalData.openid = openid;
1012
this.inviteTrack(e.query.openid, openid, e.query.id)
1113
} else {
1214
// 登录
13-
wx.login({
14-
success: res => {
15-
// 发送 res.code 到后台换取 openId, sessionKey, unionId
16-
wx.request({
17-
url: this.globalData.api.login,
18-
data: {
19-
code: res.code
20-
},
21-
success: (res) => {
22-
// 存用户的openid
23-
this.globalData.openid = res.data.data.user.openid;
24-
wx.setStorageSync('openid', res.data.data.user.openid);
25-
this.inviteTrack(e.query.openid, this.globalData.openid, e.query.id)
26-
}
27-
});
28-
}
29-
});
15+
const [loginError, loginRes] = await uni.login();
16+
// 发送 res.code 到后台换取 openId, sessionKey, unionId
17+
const res = await login({code: loginRes.code})
18+
this.globalData.openid = res.result.data.user.openid;
19+
uni.setStorageSync('openid', res.result.data.user.openid);
20+
this.inviteTrack(e.query.openid, this.globalData.openid, e.query.id)
3021
}
3122
},
3223
onShow: function() {
@@ -36,13 +27,6 @@ export default {
3627
console.log('App Hide');
3728
},
3829
globalData: {
39-
api: {
40-
login: 'https://f1086865-c582-4811-a6f8-b2e5c20b4a5f.bspapp.com/http/api/login',
41-
cover: 'https://f1086865-c582-4811-a6f8-b2e5c20b4a5f.bspapp.com/http/api/cover',
42-
coverDetail: 'https://f1086865-c582-4811-a6f8-b2e5c20b4a5f.bspapp.com/http/api/cover/detail',
43-
lookVideo: 'https://f1086865-c582-4811-a6f8-b2e5c20b4a5f.bspapp.com/http/api/look/video',
44-
inviteTrack: 'https://f1086865-c582-4811-a6f8-b2e5c20b4a5f.bspapp.com/http/api/invite/track',
45-
},
4630
openid: '',
4731
},
4832
methods: {
@@ -59,18 +43,11 @@ export default {
5943
inviteTrack(inviteOpenid, openid, id){
6044
if(inviteOpenid && openid && inviteOpenid != openid){
6145
console.log(inviteOpenid, openid)
62-
wx.request({
63-
url: this.globalData.api.inviteTrack,
64-
method: "POST",
65-
data: {
66-
inviteOpenid,
67-
openid,
68-
id,
69-
},
70-
success: (res) => {
71-
72-
}
73-
});
46+
inviteTrack({
47+
inviteOpenid,
48+
openid,
49+
id,
50+
})
7451
}
7552
},
7653
}

pages/detail/detail.vue

+27-38
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
</template>
3737

3838
<script>
39+
import { coverDetail, lookVideo } from '../../request';
40+
3941
var rewardedVideoAd = null
4042
// 在页面中定义插屏广告
4143
var interstitialAd = null
@@ -74,34 +76,27 @@ export default {
7476
handle(){
7577
return
7678
},
77-
getCoverDetail(isFirst){
78-
uni.request({
79-
url: getApp().globalData.api.coverDetail,
80-
data: {
81-
id: this.id,
82-
openid: getApp().globalData.openid,
83-
},
84-
success: (res) => {
85-
this.coverDetail = res.data.data.coverDetail
86-
this.lockEdInfo = res.data.data.lockEdInfo
87-
this.ad = res.data.data.ad
88-
if(isFirst && this.ad){
89-
//激励视频和插屏广告
90-
if(this.ad.one){
91-
this.adinsertInit(this.ad.one)
92-
}
93-
if(this.ad.five){
94-
this.adInit(this.ad.five);
95-
}
96-
}
97-
if(this.lockEdInfo.isLocked){
98-
this.modalShow = true
99-
}
100-
},
101-
complete() {
102-
uni.hideLoading()
103-
}
79+
async getCoverDetail(isFirst){
80+
const res = await coverDetail({
81+
id: this.id,
82+
openid: getApp().globalData.openid,
10483
})
84+
this.coverDetail = res.result.data.coverDetail
85+
this.lockEdInfo = res.result.data.lockEdInfo
86+
this.ad = res.result.data.ad
87+
if(isFirst && this.ad){
88+
//激励视频和插屏广告
89+
if(this.ad.one){
90+
this.adinsertInit(this.ad.one)
91+
}
92+
if(this.ad.five){
93+
this.adInit(this.ad.five);
94+
}
95+
}
96+
if(this.lockEdInfo.isLocked){
97+
this.modalShow = true
98+
}
99+
uni.hideLoading()
105100
},
106101
lookAd() {
107102
rewardedVideoAd.show().catch(() => {
@@ -156,17 +151,11 @@ export default {
156151
}
157152
},
158153
//看视频上班
159-
trackLookVideo(isEnded){
160-
uni.request({
161-
url: getApp().globalData.api.lookVideo,
162-
data: {
163-
openid: getApp().globalData.openid,
164-
id: this.id,
165-
isEnded,
166-
},
167-
success: (res) => {
168-
169-
},
154+
async trackLookVideo(isEnded){
155+
await lookVideo({
156+
openid: getApp().globalData.openid,
157+
id: this.id,
158+
isEnded,
170159
})
171160
},
172161
openModal(){

pages/index/index.vue

+7-12
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
</template>
2929

3030
<script>
31+
import { cover } from '../../request'
3132
export default {
3233
data() {
3334
return {
@@ -48,21 +49,15 @@
4849
url: `/pages/detail/detail?id=${id}`
4950
});
5051
},
51-
getCoverList(){
52+
async getCoverList(){
5253
uni.showLoading({
5354
title: "获取封面中"
5455
})
55-
uni.request({
56-
url: getApp().globalData.api.cover,
57-
success: (res) => {
58-
this.couponList = res.data.data.couponList
59-
this.coverList = res.data.data.coverList
60-
this.tips = res.data.data.tips
61-
},
62-
complete() {
63-
uni.hideLoading()
64-
}
65-
})
56+
const res = await cover()
57+
this.couponList = res.result.data.couponList
58+
this.coverList = res.result.data.coverList
59+
this.tips = res.result.data.tips
60+
uni.hideLoading()
6661
},
6762
toCoupon(i){
6863
console.log(this.couponList[i])

request/index.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
3+
async function login(queryStringParameters = {}) {
4+
const res = await uniCloud.callFunction({
5+
name: "api",
6+
data: { path: '/login', queryStringParameters },
7+
});
8+
return res
9+
}
10+
11+
async function cover(queryStringParameters = {}) {
12+
const res = await uniCloud.callFunction({
13+
name: "api",
14+
data: { path: '/cover', queryStringParameters },
15+
});
16+
return res
17+
}
18+
19+
async function coverDetail(queryStringParameters = {}) {
20+
const res = await uniCloud.callFunction({
21+
name: "api",
22+
data: { path: '/cover/detail', queryStringParameters },
23+
});
24+
return res
25+
}
26+
27+
async function lookVideo(queryStringParameters = {}) {
28+
const res = await uniCloud.callFunction({
29+
name: "api",
30+
data: { path: '/look/video', queryStringParameters },
31+
});
32+
return res
33+
}
34+
35+
async function inviteTrack(queryStringParameters = {}) {
36+
const res = await uniCloud.callFunction({
37+
name: "api",
38+
data: { path: '/invite/track', queryStringParameters },
39+
});
40+
return res
41+
}
42+
43+
export {
44+
login, cover, coverDetail, lookVideo, inviteTrack
45+
}

0 commit comments

Comments
 (0)