初始化
This commit is contained in:
15
pages/openapi/callback/callback.js
Normal file
15
pages/openapi/callback/callback.js
Normal file
@@ -0,0 +1,15 @@
|
||||
// miniprogram/pages/openapi/callback/callback.js
|
||||
Page({
|
||||
|
||||
data: {
|
||||
|
||||
},
|
||||
|
||||
onLoad: function (options) {
|
||||
|
||||
},
|
||||
|
||||
onCustomerServiceButtonClick(e) {
|
||||
console.log(e)
|
||||
},
|
||||
})
|
||||
3
pages/openapi/callback/callback.json
Normal file
3
pages/openapi/callback/callback.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"usingComponents": {}
|
||||
}
|
||||
16
pages/openapi/callback/callback.wxml
Normal file
16
pages/openapi/callback/callback.wxml
Normal file
@@ -0,0 +1,16 @@
|
||||
<view class="container">
|
||||
<view class="list">
|
||||
<button open-type="contact" bindcontact="onCustomerServiceButtonClick">
|
||||
进入客服消息
|
||||
</button>
|
||||
</view>
|
||||
|
||||
<view class="guide">
|
||||
<text class="headline">测试须知</text>
|
||||
<text class="p">1. 进入云开发控制台“设置-全局设置”,选择添加消息推送配置</text>
|
||||
<text class="p">2. 消息类型选择 text(文本客户消息)</text>
|
||||
<text class="p">3. 事件类型选择空</text>
|
||||
<text class="p">4. 选择需要推送到哪个环境的哪个云函数</text>
|
||||
<text class="p">5. 在手机上测试</text>
|
||||
</view>
|
||||
</view>
|
||||
3
pages/openapi/callback/callback.wxss
Normal file
3
pages/openapi/callback/callback.wxss
Normal file
@@ -0,0 +1,3 @@
|
||||
/* miniprogram/pages/openapi/callback/callback.wxss */
|
||||
|
||||
@import "../../../style/guide.wxss";
|
||||
60
pages/openapi/cloudid/cloudid.js
Normal file
60
pages/openapi/cloudid/cloudid.js
Normal file
@@ -0,0 +1,60 @@
|
||||
// miniprogram/pages/openapi/cloudid/cloudid.js
|
||||
Page({
|
||||
|
||||
data: {
|
||||
weRunResult: '',
|
||||
userInfoResult: '',
|
||||
},
|
||||
|
||||
onGetWeRunData() {
|
||||
wx.getWeRunData({
|
||||
success: res => {
|
||||
wx.cloud.callFunction({
|
||||
name: 'echo',
|
||||
data: {
|
||||
// info 字段在云函数 event 对象中会被自动替换为相应的敏感数据
|
||||
info: wx.cloud.CloudID(res.cloudID),
|
||||
},
|
||||
}).then(res => {
|
||||
console.log('[onGetWeRunData] 收到 echo 回包:', res)
|
||||
|
||||
this.setData({
|
||||
weRunResult: JSON.stringify(res.result),
|
||||
})
|
||||
|
||||
wx.showToast({
|
||||
title: '敏感数据获取成功',
|
||||
})
|
||||
}).catch(err => {
|
||||
console.log('[onGetWeRunData] 失败:', err)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
},
|
||||
|
||||
onGetUserInfo(e) {
|
||||
console.log(e)
|
||||
wx.cloud.callFunction({
|
||||
name: 'openapi',
|
||||
data: {
|
||||
action: 'getOpenData',
|
||||
openData: {
|
||||
list: [
|
||||
e.detail.cloudID,
|
||||
]
|
||||
}
|
||||
}
|
||||
}).then(res => {
|
||||
console.log('[onGetUserInfo] 调用成功:', res)
|
||||
|
||||
this.setData({
|
||||
userInfoResult: JSON.stringify(res.result),
|
||||
})
|
||||
|
||||
wx.showToast({
|
||||
title: '敏感数据获取成功',
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
3
pages/openapi/cloudid/cloudid.json
Normal file
3
pages/openapi/cloudid/cloudid.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"usingComponents": {}
|
||||
}
|
||||
41
pages/openapi/cloudid/cloudid.wxml
Normal file
41
pages/openapi/cloudid/cloudid.wxml
Normal file
@@ -0,0 +1,41 @@
|
||||
<!--index.wxml-->
|
||||
<view class="container">
|
||||
|
||||
<view class="guide">
|
||||
<text class="headline">开放数据调用</text>
|
||||
<text class="p">通过 cloudID 获取敏感开放数据有以下两种方式</text>
|
||||
<text class="p">1. 小程序端 callFunction 自动获取</text>
|
||||
<text class="p">2. 通过 wx-server-sdk 获取</text>
|
||||
<text class="p">以下分别先后展示这两种获取方式</text>
|
||||
</view>
|
||||
|
||||
<view class="uploader">
|
||||
<button class="uploader-text" bindtap="onGetWeRunData">getWeRunData 敏感数据获取</button>
|
||||
</view>
|
||||
|
||||
<view class="guide">
|
||||
<text class="headline">测试须知</text>
|
||||
<text class="p">1. 公共库版本需大于 2.7.0</text>
|
||||
<text class="p">2. 请确保 echo 函数已上传</text>
|
||||
</view>
|
||||
|
||||
<view class="guide" style="word-break: break-all">
|
||||
{{weRunResult}}
|
||||
</view>
|
||||
|
||||
|
||||
<view class="uploader">
|
||||
<button class="uploader-text" open-type="getUserInfo" bindgetuserinfo="onGetUserInfo">getUserInfo 敏感数据获取</button>
|
||||
</view>
|
||||
|
||||
<view class="guide">
|
||||
<text class="headline">测试须知</text>
|
||||
<text class="p">1. 公共库版本需大于 2.7.0</text>
|
||||
<text class="p">2. 请确保 openapi 函数已上传</text>
|
||||
</view>
|
||||
|
||||
<view class="guide" style="word-break: break-all">
|
||||
{{userInfoResult}}
|
||||
</view>
|
||||
|
||||
</view>
|
||||
1
pages/openapi/cloudid/cloudid.wxss
Normal file
1
pages/openapi/cloudid/cloudid.wxss
Normal file
@@ -0,0 +1 @@
|
||||
@import "../../../style/guide.wxss";
|
||||
109
pages/openapi/openapi.js
Normal file
109
pages/openapi/openapi.js
Normal file
@@ -0,0 +1,109 @@
|
||||
Page({
|
||||
|
||||
data: {
|
||||
templateMessageResult: '',
|
||||
wxacodeSrc: '',
|
||||
wxacodeResult: '',
|
||||
showClearWXACodeCache: false,
|
||||
},
|
||||
|
||||
submitTemplateMessageForm(e) {
|
||||
this.setData({
|
||||
templateMessageResult: '',
|
||||
})
|
||||
|
||||
wx.cloud.callFunction({
|
||||
name: 'openapi',
|
||||
data: {
|
||||
action: 'sendTemplateMessage',
|
||||
formId: e.detail.formId,
|
||||
},
|
||||
success: res => {
|
||||
console.warn('[云函数] [openapi] templateMessage.send 调用成功:', res)
|
||||
wx.showModal({
|
||||
title: '发送成功',
|
||||
content: '请返回微信主界面查看',
|
||||
showCancel: false,
|
||||
})
|
||||
wx.showToast({
|
||||
title: '发送成功,请返回微信主界面查看',
|
||||
})
|
||||
this.setData({
|
||||
templateMessageResult: JSON.stringify(res.result)
|
||||
})
|
||||
},
|
||||
fail: err => {
|
||||
wx.showToast({
|
||||
icon: 'none',
|
||||
title: '调用失败',
|
||||
})
|
||||
console.error('[云函数] [openapi] templateMessage.send 调用失败:', err)
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
onGetWXACode() {
|
||||
|
||||
this.setData({
|
||||
wxacodeSrc: '',
|
||||
wxacodeResult: '',
|
||||
showClearWXACodeCache: false,
|
||||
})
|
||||
|
||||
// 此处为演示,将使用 localStorage 缓存,正常开发中文件 ID 应存在数据库中
|
||||
const fileID = wx.getStorageSync('wxacodeCloudID')
|
||||
|
||||
if (fileID) {
|
||||
// 有云文件 ID 缓存,直接使用该 ID
|
||||
// 如需清除缓存,选择菜单栏中的 “工具 -> 清除缓存 -> 清除数据缓存”,或在 Storage 面板中删掉相应的 key
|
||||
this.setData({
|
||||
wxacodeSrc: fileID,
|
||||
wxacodeResult: `从本地缓存中取得了小程序码的云文件 ID`,
|
||||
showClearWXACodeCache: true,
|
||||
})
|
||||
console.log(`从本地缓存中取得了小程序码的云文件 ID:${fileID}`)
|
||||
} else {
|
||||
wx.cloud.callFunction({
|
||||
name: 'openapi',
|
||||
data: {
|
||||
action: 'getWXACode',
|
||||
},
|
||||
success: res => {
|
||||
console.warn('[云函数] [openapi] wxacode.get 调用成功:', res)
|
||||
wx.showToast({
|
||||
title: '调用成功',
|
||||
})
|
||||
this.setData({
|
||||
wxacodeSrc: res.result,
|
||||
wxacodeResult: `云函数获取二维码成功`,
|
||||
showClearWXACodeCache: true,
|
||||
})
|
||||
wx.setStorageSync('wxacodeCloudID', res.result)
|
||||
},
|
||||
fail: err => {
|
||||
wx.showToast({
|
||||
icon: 'none',
|
||||
title: '调用失败',
|
||||
})
|
||||
console.error('[云函数] [openapi] wxacode.get 调用失败:', err)
|
||||
}
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
clearWXACodeCache() {
|
||||
wx.removeStorageSync('wxacodeCloudID')
|
||||
|
||||
this.setData({
|
||||
wxacodeSrc: '',
|
||||
wxacodeResult: '',
|
||||
showClearWXACodeCache: false,
|
||||
})
|
||||
|
||||
wx.showToast({
|
||||
title: '清除成功',
|
||||
})
|
||||
},
|
||||
|
||||
})
|
||||
|
||||
3
pages/openapi/openapi.json
Normal file
3
pages/openapi/openapi.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"usingComponents": {}
|
||||
}
|
||||
22
pages/openapi/openapi.wxml
Normal file
22
pages/openapi/openapi.wxml
Normal file
@@ -0,0 +1,22 @@
|
||||
<!--index.wxml-->
|
||||
<view class="container">
|
||||
|
||||
<view class="uploader">
|
||||
<navigator url="./serverapi/serverapi" open-type="navigate" class="uploader-text">
|
||||
<text>服务端调用</text>
|
||||
</navigator>
|
||||
</view>
|
||||
|
||||
<view class="uploader">
|
||||
<navigator url="./cloudid/cloudid" open-type="navigate" class="uploader-text">
|
||||
<text>开放数据调用</text>
|
||||
</navigator>
|
||||
</view>
|
||||
|
||||
<view class="uploader">
|
||||
<navigator url="./callback/callback" open-type="navigate" class="uploader-text">
|
||||
<text>消息推送</text>
|
||||
</navigator>
|
||||
</view>
|
||||
|
||||
</view>
|
||||
7
pages/openapi/openapi.wxss
Normal file
7
pages/openapi/openapi.wxss
Normal file
@@ -0,0 +1,7 @@
|
||||
/* miniprogram/pages/openapi/openapi.wxss */
|
||||
|
||||
@import "../../style/guide.wxss";
|
||||
|
||||
.black {
|
||||
color: black;
|
||||
}
|
||||
202
pages/openapi/serverapi/serverapi.js
Normal file
202
pages/openapi/serverapi/serverapi.js
Normal file
@@ -0,0 +1,202 @@
|
||||
Page({
|
||||
|
||||
data: {
|
||||
templateId: '',
|
||||
subscribeMessageResult: '',
|
||||
requestSubscribeMessageResult: '',
|
||||
wxacodeSrc: '',
|
||||
wxacodeResult: '',
|
||||
showClearWXACodeCache: false,
|
||||
},
|
||||
|
||||
async getSubscribeMessageTemplate() {
|
||||
try {
|
||||
const { result } = await wx.cloud.callFunction({
|
||||
name: 'openapi',
|
||||
data: {
|
||||
action: 'requestSubscribeMessage',
|
||||
},
|
||||
})
|
||||
|
||||
const templateId = result
|
||||
|
||||
console.warn('[云函数] [openapi] 获取订阅消息模板 调用成功:', templateId)
|
||||
this.setData({
|
||||
templateId,
|
||||
})
|
||||
} catch (err) {
|
||||
wx.showToast({
|
||||
icon: 'none',
|
||||
title: '调用失败',
|
||||
})
|
||||
console.error('[云函数] [openapi] 获取订阅消息模板 调用失败:', err)
|
||||
}
|
||||
},
|
||||
|
||||
async requestSubscribeMessage() {
|
||||
const templateId = this.data.templateId
|
||||
|
||||
if (!templateId) {
|
||||
wx.showModal({
|
||||
title: '发送失败',
|
||||
content: '请先获取模板 ID',
|
||||
showCancel: false,
|
||||
})
|
||||
}
|
||||
|
||||
wx.requestSubscribeMessage({
|
||||
tmplIds: [templateId],
|
||||
success: (res) => {
|
||||
if (res[templateId] === 'accept') {
|
||||
this.setData({
|
||||
requestSubscribeMessageResult: '成功',
|
||||
})
|
||||
} else {
|
||||
this.setData({
|
||||
requestSubscribeMessageResult: `失败(${res[templateId]})`,
|
||||
})
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
this.setData({
|
||||
requestSubscribeMessageResult: `失败(${JSON.stringify(err)})`,
|
||||
})
|
||||
},
|
||||
})
|
||||
},
|
||||
|
||||
sendSubscribeMessage(e) {
|
||||
this.setData({
|
||||
subscribeMessageResult: '',
|
||||
})
|
||||
|
||||
wx.cloud.callFunction({
|
||||
name: 'openapi',
|
||||
data: {
|
||||
action: 'sendSubscribeMessage',
|
||||
templateId: this.data.templateId,
|
||||
},
|
||||
success: res => {
|
||||
console.warn('[云函数] [openapi] subscribeMessage.send 调用成功:', res)
|
||||
wx.showModal({
|
||||
title: '发送成功',
|
||||
content: '请返回微信主界面查看',
|
||||
showCancel: false,
|
||||
})
|
||||
wx.showToast({
|
||||
title: '发送成功,请返回微信主界面查看',
|
||||
})
|
||||
this.setData({
|
||||
subscribeMessageResult: JSON.stringify(res.result)
|
||||
})
|
||||
},
|
||||
fail: err => {
|
||||
wx.showToast({
|
||||
icon: 'none',
|
||||
title: '调用失败',
|
||||
})
|
||||
console.error('[云函数] [openapi] subscribeMessage.send 调用失败:', err)
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
submitSubscribeMessageForm(e) {
|
||||
this.setData({
|
||||
subscribeMessageResult: '',
|
||||
})
|
||||
|
||||
wx.cloud.callFunction({
|
||||
name: 'openapi',
|
||||
data: {
|
||||
action: 'sendSubscribeMessage',
|
||||
formId: e.detail.formId,
|
||||
},
|
||||
success: res => {
|
||||
console.warn('[云函数] [openapi] subscribeMessage.send 调用成功:', res)
|
||||
wx.showModal({
|
||||
title: '发送成功',
|
||||
content: '请返回微信主界面查看',
|
||||
showCancel: false,
|
||||
})
|
||||
wx.showToast({
|
||||
title: '发送成功,请返回微信主界面查看',
|
||||
})
|
||||
this.setData({
|
||||
templateMessageResult: JSON.stringify(res.result)
|
||||
})
|
||||
},
|
||||
fail: err => {
|
||||
wx.showToast({
|
||||
icon: 'none',
|
||||
title: '调用失败',
|
||||
})
|
||||
console.error('[云函数] [openapi] templateMessage.send 调用失败:', err)
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
onGetWXACode() {
|
||||
|
||||
this.setData({
|
||||
wxacodeSrc: '',
|
||||
wxacodeResult: '',
|
||||
showClearWXACodeCache: false,
|
||||
})
|
||||
|
||||
// 此处为演示,将使用 localStorage 缓存,正常开发中文件 ID 应存在数据库中
|
||||
const fileID = wx.getStorageSync('wxacodeCloudID')
|
||||
|
||||
if (fileID) {
|
||||
// 有云文件 ID 缓存,直接使用该 ID
|
||||
// 如需清除缓存,选择菜单栏中的 “工具 -> 清除缓存 -> 清除数据缓存”,或在 Storage 面板中删掉相应的 key
|
||||
this.setData({
|
||||
wxacodeSrc: fileID,
|
||||
wxacodeResult: `从本地缓存中取得了小程序码的云文件 ID`,
|
||||
showClearWXACodeCache: true,
|
||||
})
|
||||
console.log(`从本地缓存中取得了小程序码的云文件 ID:${fileID}`)
|
||||
} else {
|
||||
wx.cloud.callFunction({
|
||||
name: 'openapi',
|
||||
data: {
|
||||
action: 'getWXACode',
|
||||
},
|
||||
success: res => {
|
||||
console.warn('[云函数] [openapi] wxacode.get 调用成功:', res)
|
||||
wx.showToast({
|
||||
title: '调用成功',
|
||||
})
|
||||
this.setData({
|
||||
wxacodeSrc: res.result,
|
||||
wxacodeResult: `云函数获取二维码成功`,
|
||||
showClearWXACodeCache: true,
|
||||
})
|
||||
wx.setStorageSync('wxacodeCloudID', res.result)
|
||||
},
|
||||
fail: err => {
|
||||
wx.showToast({
|
||||
icon: 'none',
|
||||
title: '调用失败',
|
||||
})
|
||||
console.error('[云函数] [openapi] wxacode.get 调用失败:', err)
|
||||
}
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
clearWXACodeCache() {
|
||||
wx.removeStorageSync('wxacodeCloudID')
|
||||
|
||||
this.setData({
|
||||
wxacodeSrc: '',
|
||||
wxacodeResult: '',
|
||||
showClearWXACodeCache: false,
|
||||
})
|
||||
|
||||
wx.showToast({
|
||||
title: '清除成功',
|
||||
})
|
||||
},
|
||||
|
||||
})
|
||||
|
||||
3
pages/openapi/serverapi/serverapi.json
Normal file
3
pages/openapi/serverapi/serverapi.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"usingComponents": {}
|
||||
}
|
||||
63
pages/openapi/serverapi/serverapi.wxml
Normal file
63
pages/openapi/serverapi/serverapi.wxml
Normal file
@@ -0,0 +1,63 @@
|
||||
<view class="container">
|
||||
|
||||
<view class="list">
|
||||
<view class="list-item" bindtap="getSubscribeMessageTemplate">
|
||||
<text>获取订阅消息模板 ID</text>
|
||||
</view>
|
||||
<view class="list-item" wx:if="{{templateId}}">
|
||||
<text class="request-text">模板 ID:{{templateId}}</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view class="list">
|
||||
<view class="list-item" bindtap="requestSubscribeMessage">
|
||||
<text>获取下发权限</text>
|
||||
</view>
|
||||
<view class="list-item" wx:if="{{requestSubscribeMessageResult}}">
|
||||
<text class="request-text">获取权限结果:{{requestSubscribeMessageResult}}</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view class="list">
|
||||
<view class="list-item" bindtap="sendSubscribeMessage">
|
||||
<text>发送订阅消息</text>
|
||||
</view>
|
||||
<view class="list-item" wx:if="{{subscribeMessageResult}}">
|
||||
<text class="request-text">调用结果:{{subscribeMessageResult}}</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view class="guide">
|
||||
<text class="headline">测试须知</text>
|
||||
<text class="p">1. 需先到小程序管理后台,进入订阅消息管理</text>
|
||||
<text class="p">2. 在订阅消息管理、公共模板库中添加一个模板</text>
|
||||
<text class="p">3. 添加完成后在我的模板中点开模板详情</text>
|
||||
<text class="p">4. 根据模板详情修改 openapi 云函数 index.js 中的相应位置</text>
|
||||
<text class="p">5. 上传 cloudfunctions 目录下的 openapi 云函数</text>
|
||||
<text class="p">6. 需在手机上预览测试,工具中无效</text>
|
||||
<text class="p">7. 依次点击获取模板、获取下发权限、发送订阅消息</text>
|
||||
<text class="p">8. 调用成功后返回到微信主界面查看收到的模板消息</text>
|
||||
</view>
|
||||
|
||||
<view class="list">
|
||||
<view class="list-item" bindtap="onGetWXACode">
|
||||
<text>获取小程序码</text>
|
||||
</view>
|
||||
<view class="list-item" wx:if="{{wxacodeResult}}">
|
||||
<text class="request-text">{{wxacodeResult}}</text>
|
||||
<text class="request-text" wx:if="{{showClearWXACodeCache}}" bindtap="clearWXACodeCache">清除缓存</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view class="guide">
|
||||
<text class="headline">测试须知</text>
|
||||
<text class="p">1. 需上传 cloudfunctions 目录下的 openapi 云函数</text>
|
||||
<text class="p">2. 云函数中获取图片后会上传至存储空间并返回至小程序使用和缓存</text>
|
||||
<text class="p">3. 云存储需设置为公有读</text>
|
||||
</view>
|
||||
|
||||
<view class="guide">
|
||||
<image src="{{wxacodeSrc}}" mode="aspectFit"></image>
|
||||
</view>
|
||||
|
||||
</view>
|
||||
7
pages/openapi/serverapi/serverapi.wxss
Normal file
7
pages/openapi/serverapi/serverapi.wxss
Normal file
@@ -0,0 +1,7 @@
|
||||
/* miniprogram/pages/openapi/openapi.wxss */
|
||||
|
||||
@import "../../../style/guide.wxss";
|
||||
|
||||
.black {
|
||||
color: black;
|
||||
}
|
||||
Reference in New Issue
Block a user