// index.js
// 获取应用实例
const app = getApp()
const time = require('../../utils/util')
Page({
data:{
hasUserInfo:false,
index: 0 , // 默认为0,日报
logArray:['日报','周报','月报'],
admin:'', // 操作员
lTitle:'未提交日志提醒', // 提醒标题
date: time.formatTime(new Date()), // 日期(格式化)
remind:'', // 提醒人,默认admin
tip:'尚未提交日志', // 温馨提示内容
},
// 获取提醒标题
changeTitle(e){
this.setData({ index : e.detail.value })
},
// 获取日期选择
bindDateChange(e){
this.setData({ date : e.detail.value })
},
// 获取提醒人
bindRemindPer(e){
this.setData({ remind : e.detail.value })
},
// 获取提示内容
bindTipCont(e){
console.log(e)
this.setData({ tip : e.detail.value })
},
// 获取用户信息
getUserProfile(e) {
// 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
wx.getUserProfile({
desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
success: (res) => {
this.setData({
admin: res.userInfo.nickName,
hasUserInfo: true
})
}
})
},
// 获取消息订阅权限
allowSubscribeMessage(){
// hasSubscribeMessage 授权状态
if (!app.globalData.hasSubscribeMessage) {
wx.requestSubscribeMessage({
tmplIds: [tempid], // 在此处填写模板id
success(res) {
console.log('获取权限:',res)
app.globalData.hasSubscribeMessage = res.errMsg.split(':')[1]
}
})
}
},
// 表单提交
formSubmit(e){
// e.detail.value 可以拿到用户提交表单的数据
// console.log(e.detail.value)
let formInfo = e.detail.value
formInfo.title = this.data.logArray[formInfo.title]
wx.login({
timeout: 2000,
// 成功后会返回code,将code提交给服务器
success: res =>{
// 获取到code
console.log('获取到code:' + res.code)
console.log(res)
// 提交服务器
server.post({code : res.code , formInfo},()=>{
wx.showToast({
title: '发送成功',
icon: 'success',
duration: 2000
})
// 成功提交后,由服务器来发起订阅消息
server.sendTempMsg(formInfo,res =>{
console.log('订阅消息发送结果:', res.data)
})
})
}
})
}
})
const tempid = '' // 填写上你的订阅模板的id
// 模拟服务器端
var server = {
appid: '', // 填写上你的appid
secret:'', // 填写上你的appidSecret
user:{
openid: '' // 待接收
},
// Post 表单提交处理
post(data, success) {
// 收到来自客户端提交的数据
console.log('客户端提交内容:',data)
// 获取Openid - 通过code,appid,secret
this.getOpenid(data,(res)=>{
// 获取到code保存
console.log('用户OpenId:',res.data.openid)
this.user.openid = res.data.openid
success()
})
},
// 获取openid
getOpenid(res,success){
// 通过wx.login获得code,再根据code拿到openid
wx.request({
url: 'https://api.weixin.qq.com/sns/jscode2session',
data:{ // 请求参数
appid: this.appid,
secret: this.secret,
js_code: res.code,
grant_type: 'authorization_code'
},
success: success
})
},
// 发送订阅消息
sendTempMsg(formInfo,success){
var that = this.user
var temp = {
touser: that.openid,
template_id: tempid, // 订阅模板id
data:{
// phrase1: { value: '日报' },
// date2: { value: '2021-12-10' },
// name3: {value: '老师'},
// thing4: { value: '未提交日志' }
phrase1: { value: formInfo.title },
date2: { value: formInfo.date },
name3: {value: formInfo.remind },
thing4: { value: formInfo.tip }
},
miniprogram_state: 'developer',
lang: 'zh_CN'
}
this.getAccessToken(res=>{
var token = res.data.access_token
console.log('服务器access_token:' + token)
var url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' + token
wx.request({
url,
method: 'post',
data: temp,
success
})
})
},
// 获取access_token
getAccessToken(success){
var url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' + this.appid + '&secret=' + this.secret
wx.request({
url: url,
success: success
})
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
微信小程序实现订阅消息源码,该资源是在客户端模拟的服务器,同时在资源还有使用Node.js搭载服务器的微信小程序实现订阅消息源码,可自行前往下载; 在微信小程序实现订阅消息发送的功能,通过订阅消息的发送,可以在微信上的微信服务通知上看到订阅发送消息的内容,订阅消息是小程序向用户发送消息的方式,小程序本身是不具备接收消息功能,而是通过微信的服务通知功能将消息转达给用户的,感兴趣的读者可以下载一下,感谢支持! 原文章地址:https://blog.csdn.net/weixin_52203618/article/details/130236946
资源推荐
资源详情
资源评论
收起资源包目录
subscribe订阅消息(模拟服务器端)源码.rar (16个子文件)
subscribe
sitemap.json 191B
pages
index
index.wxml 1KB
index.js 5KB
index.json 27B
index.wxss 3KB
logs
logs.json 77B
logs.js 305B
logs.wxml 197B
logs.wxss 106B
.eslintrc.js 587B
app.json 295B
project.config.json 2KB
app.js 87B
utils
util.js 318B
app.wxss 194B
解读指南.md 1KB
共 16 条
- 1
资源评论
灵魂学者
- 粉丝: 3094
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功