- 小程序logo 在网上找的,如有侵权,联系删除
- 小程序名称
彬婧的月亮- 小程序开发配置
AppID(小程序ID):
原始ID:
AppSecret(小程序密钥):
{
"pages": [
"pages/index/index",
"pages/infos/infos",
"pages/my/my"
],
"window": {
"backgroundColor": "#F6F6F6",
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#F6F6F6",
"navigationBarTitleText": "首页",
"navigationBarTextStyle": "black"
},
"style": "v2",
"sitemapLocation": "sitemap.json",
"lazyCodeLoading": "requiredComponents",
"tabBar": {
"color": "#bfbfbf",
"selectedColor": "#ffbc0d",
"borderStyle": "white",
"list": [{
"pagePath": "pages/index/index",
"text": "首页",
"iconPath": "images/2.png",
"selectedIconPath": "images/2.png"
},
{
"pagePath": "pages/infos/infos",
"text": "日记",
"iconPath": "images/3.png",
"selectedIconPath": "images/3.png"
},
{
"pagePath": "pages/my/my",
"text": "我的",
"iconPath": "images/1.png",
"selectedIconPath": "images/1.png"
}
]
}
}
账号信息
账号:
如何访问内容管理平台
内容管理平台是独立于云控制台进行内容和权限的管理,可使用开通时注册的管理员账号登录。详情
访问地址:
开源地址
由腾讯云TCB提供:
模型
[
{
"fields": [
{
"dateFormatType": "timestamp-ms",
"description": "CMS 系统字段,请勿随意修改。通过 CMS 系统录入的数据会默认添加该字段",
"displayName": "创建时间",
"id": "_createTime",
"isHidden": true,
"isSystem": true,
"name": "_createTime",
"type": "DateTime"
},
{
"dateFormatType": "timestamp-ms",
"description": "CMS 系统字段,请勿随意修改。通过 CMS 系统录入的数据会默认添加该字段",
"displayName": "修改时间",
"id": "_updateTime",
"isHidden": true,
"isSystem": true,
"name": "_updateTime",
"type": "DateTime"
},
{
"description": "手机号码",
"displayName": "电话",
"id": "mhw2bsjof9shg15ae8t2bu7j96lqq0xi",
"name": "phone",
"order": 2,
"type": "Tel"
},
{
"copyable": true,
"description": "CMS 系统字段,请勿随意修改",
"displayName": "文档 ID",
"id": "_id",
"isHidden": true,
"isSystem": true,
"name": "_id",
"type": "String"
},
{
"displayName": "密码",
"id": "d72d6t14uz3592f5y55tbbxa5bg1mesj",
"name": "pw11d111",
"order": 4,
"type": "String"
},
{
"displayName": "邮箱",
"id": "m9hx8hd9iroi2x6lavnlob5kylzxd2st",
"name": "mail",
"order": 5,
"type": "Email"
},
{
"displayName": "openid",
"id": "x0o8rs0vf2ipo8siothcqb67y9ttg5p4",
"name": "_openid",
"order": 6,
"type": "String"
},
{
"defaultValue": true,
"displayName": "状态",
"id": "2ahcg52m7qtafuocpasr371oq7ty6nl5",
"name": "status",
"order": 7,
"type": "Boolean"
},
{
"displayName": "昵称",
"id": "1y6mtvc0ifr5bidr64m0mazs1sy37hl2",
"name": "nickName",
"order": 8,
"type": "String"
},
{
"displayName": "头像",
"id": "hoor4y975ob4g58oq13enftxgahhh4fm",
"name": "avatarUrl",
"order": 9,
"type": "String"
},
{
"dateFormatType": "string",
"displayName": "创建时间",
"id": "pfa4dvlnh3j5jzzci6fz8sk9fd0y6tww",
"name": "createTime",
"order": 10,
"type": "DateTime"
},
{
"dateFormatType": "string",
"displayName": "修改时间",
"id": "k5pvz13d2smqb44zg2attzdtd1xrc58u",
"name": "updateTime",
"order": 11,
"type": "DateTime"
}
],
"collectionName": "users",
"displayName": "用户表",
"description": "用户表",
"_id": "eed5cdec632c64ae0029ee832b38043c"
}
]
数据库
[
{
"_id": "e1afb303632c925c0034f028011033e7",
"avatarUrl": "https://thirdwx.qlogo.cn/mmopen/vi_32/HAKxmIGibkZgib3vcTg8eaAfUeOIc5InEZibmqEU7LJM8kOzRWGWOfo1Bsibib9fchq6Pv2WUBJsQnLjWIicXzjl9KqQ/132",
"nickName": "Ashes of Time",
"createTime": "2022/09/23 00:50:47",
"updateTime": "2022/09/23 01:17:36",
"status": true,
"_openid": "o1jgE5h7Pj-QCm9-NG0EqMqvemDE",
"mail": "",
"phone": "",
"pw11d111": ""
}
]
不知道明天的路该往哪里走,先做登录
// app.js
// App({
// onLaunch() {
// // 展示本地存储能力
// const logs = wx.getStorageSync('logs') || []
// logs.unshift(Date.now())
// wx.setStorageSync('logs', logs)
// // 登录
// wx.login({
// success: res => {
// // 发送 res.code 到后台换取 openId, sessionKey, unionId
// }
// })
// },
// globalData: {
// userInfo: null
// }
// })
// App({
// globalData: {
// userInfo: null
// },
// onLaunch: function () {
// // 初始化云开发环境
// wx.cloud.init({
// env: 'cloud-2g520387'
// })
// }
// })
// app.js
var util = require('./utils/util.js')
// let shangjiOpenId = null
App({
globalData: {
userInfo: null
},
onShow(options) {
},
onLaunch(options) {
// 初始化云开发环境
wx.cloud.init({
env: 'cloud-xxxxxxxxxxxxx'
})
// this.getGlobalJInbizhi()
// this.getSystemController()
// this.getGlobalShareMedia()
},
// 查询用户状态
selecteUser() {
return new Promise((resolve, reject) => {
wx.cloud.callFunction({
name: 'users',
data: {
tag: 'selecteUser',
updateTime: util.formatTime(new Date())
// updateTime: util.formatTime(new Date()).slice(0, 10)
}
})
.then(res => {
console.log(res,"users");
if (res.result.length!==0) {
// 1.用户已经注册,判断禁用状态 为true 则保存注册状态 为false 则叫他滚蛋
if (res.result[0].status) {
this.globalData.userInfo = res.result[0]
resolve(true)
} else {
resolve(false)
this.jingyongShow()
}
} else {
// 直接注册
resolve(this.getUserProfileModel())
}
}).catch(err=>{
console.log(err,"users");
})
})
},
// 授权弹窗
getUserProfileModel() {
// 获取授权
return new Promise((resolve, reject) => {
wx.showModal({
title: '提示',
content: '授权后才可以继续操作',
success:res=>{
if(res.confirm){
this.getUserProfile().then(res => {
resolve(res)
})
}else{
resolve(false)
}
}
})
})
},
// 获取登录授权
getUserProfile() {
return new Promise((resolve, reject) => {
wx.getUserProfile({
desc: '用于完善资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
success: (res) => {
this.globalData.userInfo = res.userInfo
this.zhuceUser(res.userInfo).then(res => {
resolve(res)
})
},
fail: (res) => {
resolve(false)
}
})
})
},
// 注册用户
zhuceUser(userInfo) {
return new Promise((resolve, reject) => {
wx.cloud.callFunction({
name: 'users',
data: {
tag: 'zhuceUser',
avatarUrl: userInfo.avatarUrl,
nickName: userInfo.nickName,
createTime: util.formatTime(new Date()),
updateTime: util.formatTime(new Date()),
// updateTime: util.formatTime(new Date()).slice(0, 10),
status: true,
mail:'',
phone:'',
pw111d:'',
}
})
.then(res => {
this.globalData.userInfo._openid = res.result
wx.showToast({
icon: 'none',
title: `注册成功!`
})
resolve(true)
})
})
},
// 异步获取全局媒体资源及分享转发资源图片及文字
getGlobalShareMedia() {
let that = this;
return new Promise((resolve, reject) => {
wx.cloud.database().collection('jinbi_page_top_show').get()
.then(res => {
that.globalData.getGlobalShareMedia = res.data[0]
resolve(that.globalData.getGlobalShareMedia)
})
})
},
// 异步获取系统控制变量数据查询
getSystemController() {
let that = this;
return new Promise((resolve, reject) => {
wx.cloud.database().collection('controller').get()
.then(res => {
that.globalData.controller = res.data[0]
resolve(that.globalData.controller)
})
})
},
// 定义检查用户禁用状态
jingyongShow() {
wx.showModal({
showCancel: false,
title: '账号禁用提示',
content: '请联系管理员处理!',
cancelColor: 'cancelColor',
complete: () => {
wx.exitMiniProgram()
}
})
},
// 访问次数方法
getFangwen() {
wx.cloud.callFunction({
name: 'fangwen',
data: {
tag: 'fangwencishu',
updateTime: util.formatTime(new Date())
// updateTime: util.formatTime(new Date()).slice(0, 10)
}
})
},
})
// 云端 遥远的云端,很迷茫,前途未知
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database()
const _ = db.command
// 云函数入口函数
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
if (event.tag == 'gengxin') {
return await db.collection('users')
.where({
_openid: wxContext.OPENID,
}).update({
data: {
updateTime: event.updateTime
}
}).then(res => {
return res.data
})
}
// 查询当日访问人数
if (event.tag == 'fangwenrenshu') {
return await db.collection('users')
.where({
updateTime: event.updateTime
})
.count()
.then(res => {
return res.total
})
.catch(err => {
return err.data
})
}
// 查询用户
if (event.tag == 'selecteUser') {
return await db.collection('users')
.where({
_openid: wxContext.OPENID,
})
.get()
.then(res => {
console.log(res,"yunhanshu");
if (res.data.length >0) {
// 注册过 去更新时间
db.collection('users')
.where({
_openid: wxContext.OPENID,
}).update({
data: {
updateTime: event.updateTime
}
})
}
console.log(res.data,"yunhanshu2222");
return res.data
})
.catch(err => {
return err
})
}
// 注册用户
if (event.tag == 'zhuceUser') {
return await db.collection('users')
.add({
data: {
avatarUrl: event.avatarUrl,
nickName: event.nickName,
createTime: event.createTime,
updateTime: event.updateTime,
status: true,
_openid: wxContext.OPENID,
mail:event.mail||'',
phone:event.phone||'',
p111wd:event.pw111d||'',
}
})
.then(res => {
return wxContext.OPENID
})
}
// 添加操作
if (event.tag == 'addJinbi') {
return await db.collection('user').where({
_openid: event.mubiao
})
.update({
data: {
jinbiNum: _.inc(event.zhi)
}
})
.then(res => {
return res
})
}
// 减操作
if (event.tag == 'subjinbi') {
return await db.collection('user').where({
_openid: wxContext.OPENID
})
.update({
data: {
jinbiNum: _.inc(event.zhi)
}
})
.then(res => {
return res
})
}
// return {
// event,
// openid: wxContext.OPENID,
// appid: wxContext.APPID,
// unionid: wxContext.UNIONID,
// }
}
npm init --y
npm install --production
npm install moment --save
24小时 是 大写的 HH 12小时 是 小写的 hh
// 背景切换,onload时调用
selectBg() {
let date = Number(moment().format('HH')) || 0
if (date >= 6 && date <= 18) {
this.setData({
bjShow: 'bg1'
})
} else {
this.setData({
bjShow: 'bg2'
})
}
},
<!--index.wxml-->
<view class="{{isDay?'bg1':'bg2'}}">
<button bindtap="selectBg">切换背景</button>
</view>
/**index.js**/
data: {
isDay:true, // 白天与黑夜状态
infoList: [],
},
// 背景切换
selectBg() {
let date = Number(moment().format('HH')) || 0
if (date >= 6 && date <= 18) {
this.setData({
isDay:true
})
} else {
this.setData({
isDay:false
})
}
},
/**index.wxss**/
.bg1{
position: relative;
z-index: 0;
width: 100%;
height: 100vh;
overflow: hidden;
background: url(https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpicnew13.photophoto.cn%2F20190114%2Fshiliangshouhuixiaoqingxinlvseziranfengjinghaibao-32414843_1.jpg&refer=http%3A%2F%2Fpicnew13.photophoto.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1666502292&t=26c415891be97e52c710d1e9e6977e60) no-repeat top center;
background-size: 100%;
}
.bg2{
position: relative;
z-index: 0;
width: 100%;
height: 100vh;
overflow: hidden;
background: url(https://img1.baidu.com/it/u=323926268,4194139776&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=749) no-repeat top center;
background-size: 100%;
}
模型
[
{
"fields": [
{
"dateFormatType": "timestamp-ms",
"description": "CMS 系统字段,请勿随意修改。通过 CMS 系统录入的数据会默认添加该字段",
"displayName": "创建时间",
"id": "_createTime",
"isSystem": true,
"name": "_createTime",
"type": "DateTime"
},
{
"dateFormatType": "timestamp-ms",
"description": "CMS 系统字段,请勿随意修改。通过 CMS 系统录入的数据会默认添加该字段",
"displayName": "修改时间",
"id": "_updateTime",
"isSystem": true,
"name": "_updateTime",
"type": "DateTime"
},
{
"description": "信息类型(1笑话,2天气,3问候,4段子,5守护者信息,6logs)",
"displayName": "信息类型",
"id": "zrz5xkoooudz7oy0v850liq704ypihje",
"name": "info_type",
"order": 2,
"type": "Number"
},
{
"copyable": true,
"description": "CMS 系统字段,请勿随意修改",
"displayName": "文档 ID",
"id": "_id",
"isHidden": true,
"isSystem": true,
"name": "_id",
"type": "String"
},
{
"displayName": "信息标题",
"id": "gx4qdk87g0qp1sl68d5ywoj35pnktm9s",
"name": "info_title",
"order": 4,
"type": "String"
},
{
"displayName": "信息内容",
"id": "gjqb7up9iapl54oe2d9tp6dky5bmbs6r",
"name": "info_content",
"order": 5,
"type": "MultiLineString"
},
{
"dateFormatType": "string",
"displayName": "创建时间",
"id": "ch1pjhxb8zkv2k921ocyy9q3iwdbcdk8",
"isOrderField": true,
"name": "createTime",
"order": 6,
"orderDirection": "desc",
"type": "DateTime"
},
{
"dateFormatType": "string",
"displayName": "修改时间",
"id": "ty88ckehyjfv9u6vanohxh808dr9sbek",
"name": "updateTime",
"order": 7,
"type": "DateTime"
},
{
"description": "推送状态 true false",
"displayName": "推送状态",
"id": "oe93iiq0cgeu61xw95dgpanl1eoyg4uq",
"name": "info_push_status",
"order": 8,
"type": "Boolean"
},
{
"description": "接受反馈(备用字段1,2,3,4)",
"displayName": "接受反馈",
"id": "5p9bmvqino8n4ez999ejv9j6reqcy8ma",
"name": "info_feedbacks",
"order": 9,
"type": "Number"
}
],
"collectionName": "infos",
"displayName": "信息表",
"_id": "c89957c7632df9ea002bc9b3357c141e"
}
]
数据
{
"_id": "14bec23b632dfdcc002e3a175b969988",
"createTime": "2022-09-24 02:41:07",
"info_content": "很久很久以前,有只猪,跑着跑着,就死了!13",
"info_feedbacks": 1,
"info_push_status": false,
"info_title": "一个小笑话13",
"info_type": 1,
"updateTime": "2022-09-24 02:41:13",
"_createTime": 1663658476429,
"_updateTime": 1663958476429
}
// 加载信息列表
loadinfoList() {
wx.showLoading({
title: 'Loading...',
})
wx.cloud.database().collection('infos').where({})
.orderBy('createTime', 'desc')
.limit(10)
.get()
.then(res => {
console.log(res);
if (res?.data?.length > 0) {
this.setData({
infoList: this.data.infoList.concat(res.data),
total: res.data.length
})
} else {
wx.showToast({
title: '没有数据,或者服务器欠费了!',
})
}
wx.stopPullDownRefresh()
wx.hideLoading()
}).catch(err => {
console.log(err);
wx.showToast({
title: '没有权限查看!',
icon:'error'
})
})
},
使用原生小程序开发
从现有项目开始
下载源码解压获得/demo,复制目录下的 /colorui 文件夹到你的项目根目录
App.wxss 引入关键Css main.wxss icon.wxss
@import “colorui/main.wxss”;
@import “colorui/icon.wxss”;
@import “app.css”; /* 你的项目css */…
从新项目开始
下载源码解压获得/template,复制/template并重命名为你的项目,导入到小程序开发工具既可以开始你的新项目了
使用自定义导航栏
导航栏作为常用组件有做简单封装,当然你也可以直接复制代码结构自己修改,达到个性化目的。
App.js 获得系统信息
onLaunch: function() {
wx.getSystemInfo({
success: e => {
this.globalData.StatusBar = e.statusBarHeight;
let custom = wx.getMenuButtonBoundingClientRect();
this.globalData.Custom = custom;
this.globalData.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
}
})
},
App.json 配置取消系统导航栏,并全局引入组件
“window”: {
“navigationStyle”: “custom”
},
“usingComponents”: {
“cu-custom”:“/colorui/components/cu-custom”
}
page.wxml 页面可以直接调用了
返回
导航栏
参数 作用 类型 默认值
bgColor 背景颜色类名 String ‘’
isBack 是否开启返回 Boolean false
isCustom 是否开启左侧胶囊 Boolean false
bgImage 背景图片路径 String ‘’
slot块 作用
backText 返回时的文字
content 中间区域
right 右侧区域(小程序端可使用范围很窄!)
<!--index.wxml-->
<!-- <view class="{{isDay?'bg1':'bg2'}}">
<block wx:for="{{infoList}}" wx:for-index="index" wx:for-item="item" wx:key="_id">
<view style="height: 100px;">
<view>{{item.info_title}}
</view>
<view>{{item.info_content}}
</view>
</view>
</block>
</view> -->
<!-- <view class="cover-box">
<block wx:for="{{infoList}}" wx:for-index="index" wx:for-item="item" wx:key="_id">
<view style="height: 100px;">
<view>{{item.info_title}}
</view>
<view>{{item.info_content}}
</view>
</view>
</block>
</view> -->
<!-- bgImage="{{bg[0]}}" -->
<!-- <cu-custom bgColor="bg-gradual-blue" isBack="{{true}}">
<view slot="backText">返回</view>
<view slot="content">导航栏</view>
</cu-custom> -->
<view class='set-background'>
<image class='background-image' src='{{isDay? bg[0]:bg[1]}}'></image>
<view class='background-content'>
<view class="set-background-avatar" background-size="cover">
<view class="page-section-spacing">
<scroll-view scroll-with-animation="true" scroll-y="true" style="height: 100%;" bindscrolltoupper="upper" bindscrolltolower="lower" bindscroll="scroll" scroll-into-view="{{toView}}" scroll-top="{{scrollTop}}">
<block wx:for="{{infoList}}" wx:for-index="index" wx:for-item="item" wx:key="_id">
<view class="scroll-view-item" id="{{item.info_type}}">
<!-- <view>{{item.info_title}}
</view>
<view>{{item.info_content}}
</view> -->
<view class="msg_box ">
<view class="msg_box_l cu-avatar round lg" style="background-image:url(/images/2.png);"></view>
<view class="msg_box_r tip">
<view class="tip-trangle-left">
</view>
<view class="msg_box_content">
<view style="border-bottom: 1px solid #ddd;font-weight: bold; margin-bottom: 10px;" class="content flex-sub ">
<view>{{item.info_title}}</view>
<view class="text-gray text-sm flex justify-between">
{{item.createTime}}
</view>
</view>
<view class=" text-content flex justify-between">
{{item.info_content}}
</view>
</view>
</view>
</view>
<!-- <view class="text-content">
content
</view>
<view class="grid flex-sub padding-lr {{isCard?'col-3 grid-square':'col-1'}}">
<view class="bg-img {{isCard?'':'only-img'}}" style="background-image:url({{item.avatar}});">
</view>
</view> -->
<!-- <view class="text-gray text-sm text-right padding">
<block wx:for="{{item.tags}}" wx:for-item="child">
<l-icon name="{{child.name}}" extraClass="margin-lr-xs"></l-icon>
{{child.num}}
</block>
</view> -->
<!-- <view class="cu-list menu-avatar comment solids-top" wx:for="{{item.message}}" wx:for-item="child">
<view class="cu-item">
<view class="cu-avatar round" style="background-image:url({{child.avatar}});"></view>
<view class="content">
<view class="text-grey">{{child.name}}</view>
<view class="text-gray text-content text-df">
{{child.comment}}
</view>
<view class="bg-grey padding-sm radius margin-top-sm text-sm">
<view class="flex">
<view>{{child.replyName}}:</view>
<view class="flex-sub">{{child.reply}}</view>
</view>
</view>
<view class="margin-top-sm flex justify-between">
<view class="text-gray text-df">{{child.time}}</view>
<view>
<block wx:for="{{child.tags}}" wx:for-item="childs">
<l-icon name="{{childs.name}}" textColor="gray" extraClass="margin-lr-xs"></l-icon>
{{childs.num}}
</block>
</view>
</view>
</view>
</view>
</view> -->
</view>
</block>
<view style="color:white" class="cu-load {{isLoad?'loading':'over'}}"></view>
</scroll-view>
</view>
</view>
</view>
</view>
/**index.js**/
var util = require('../../utils/util.js')
import moment from 'moment'
const app = getApp()
Page({
data: {
isLoad: true,
limit: 10, //每次获取数据条数
total: 10, // 返回数据条数
toView: 'pink', // 滚动到该元素
isDay: true, // 白天与黑夜状态
infoList: [],
bg: [
'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpicnew13.photophoto.cn%2F20190114%2Fshiliangshouhuixiaoqingxinlvseziranfengjinghaibao-32414843_1.jpg&refer=http%3A%2F%2Fpicnew13.photophoto.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1666502292&t=26c415891be97e52c710d1e9e6977e60',
'https://img1.baidu.com/it/u=323926268,4194139776&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=749'
]
},
// 可滚动视图区域
upper(e) {
console.log(e, "upper")
},
lower(e) {
console.log(e, "到底了")
console.log(this.data.infoList.length);
this.setData({
isLoad: true
})
this.loadinfoList()
},
scroll(e) {
console.log(e, "scroll")
},
scrollToTop() {
console.log("scrollToTop")
this.setAction({
scrollTop: 0
})
},
tap() {
console.log("tap")
for (let i = 0; i < order.length; ++i) {
if (order[i] === this.data.toView) {
this.setData({
toView: order[i + 1],
scrollTop: (i + 1) * 200
})
break
}
}
},
tapMove() {
console.log("tapMove")
this.setData({
scrollTop: this.data.scrollTop + 10
})
},
// 背景切换
selectBg() {
let date = Number(moment().format('HH')) || 0
if (date >= 6 && date <= 18) {
this.setData({
isDay: true
})
} else {
this.setData({
isDay: false
})
}
},
// 确定发布消息
fatie() {
wx.navigateToMiniProgram({
appId: 'wx83ed21db6ad34120',
path: `/pages/news/news`,
extraData: {
pingtai: keyWord
},
envVersion: 'release',
success(res) {
// 打开成功
}
})
},
onLoad: function () {
// 切换背景
this.selectBg()
// 获取信息列表
this.loadinfoList()
if (!app.globalData.isRegistered) {
app.selecteUser().then(res => {
app.globalData.isRegistered = res
})
}
},
//异步获取系统控制变量
getSystemController() {
if (app.globalData.controller == null) {
app.getSystemController().then(res => {
this.setData({
sendNews: res.sendNews
})
})
} else {
this.setData({
sendNews: app.globalData.controller.sendNews
})
}
},
// 加载信息列表
loadinfoList() {
if (this.data.total < this.data.limit) {
// wx.showToast({
// icon: 'none',
// title: '没有数据了',
// })
this.setData({
isLoad: false
})
return false
}
// wx.showLoading({
// title: 'Loading...',
// })
wx.cloud.database().collection('infos').where({})
.orderBy('createTime', 'desc')
.skip(this.data.infoList.length)
.limit(this.data.limit)
.get()
.then(res => {
console.log(res);
if (res?.data?.length > 0) {
this.setData({
infoList: this.data.infoList.concat(res.data),
total: res.data.length
})
} else {
// wx.showToast({
// title: '没有数据了!',
// icon: 'none'
// })
}
wx.stopPullDownRefresh()
// wx.hideLoading()
}).catch(err => {
console.log(err);
wx.showToast({
title: '没有权限查看!',
icon: 'none'
})
this.setData({
isLoad: false
})
})
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
this.getFangwen()
this.setData({
infoList: [],
TabCur: 0,
scrollLeft: 0
})
this.loadinfoList(0)
},
/**
* 页面上拉触底事件的处理函数
*/
handscrolltolower() {
this.setData({
daodile: true
})
this.onReachBottom()
},
onReachBottom() {
// if (resCount < total) {
// wx.showToast({
// icon: 'none',
// title: '没有数据了',
// })
// } else {
// this.loadinfoList(this.data.infoList.length)
// }
},
// 放大显示图片
onPreviewImage(e) {
wx.previewImage({
urls: e.target.dataset.imgs,
current: e.target.dataset.imgs[e.target.dataset.id]
})
},
/**
* 用户点击右上角分享
*/
onShareAppMessage(e) {
let title = `${app.globalData.userInfo.nickName}`
let imageUrl = app.globalData.userInfo.avatarUrl
if (e.from == 'button') {
title = e.target.dataset.news.content
imageUrl = e.target.dataset.news.img[0]
}
return {
title,
imageUrl,
path: `/pages/news/news?shangjiOpenId=${app.globalData.userInfo._openid}`,
}
},
})
/**index.wxss**/
/* .bg1{
position: relative;
z-index: 0;
width: 100%;
height: 100vh;
overflow: hidden;
background: url(https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpicnew13.photophoto.cn%2F20190114%2Fshiliangshouhuixiaoqingxinlvseziranfengjinghaibao-32414843_1.jpg&refer=http%3A%2F%2Fpicnew13.photophoto.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1666502292&t=26c415891be97e52c710d1e9e6977e60) no-repeat top center;
background-size: 100%;
}
.bg2{
position: relative;
z-index: 0;
width: 100%;
height: 100vh;
overflow: hidden;
background: url(https://img1.baidu.com/it/u=323926268,4194139776&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=749) no-repeat top center;
background-size: 100%;
}
.cover-box {
position: fixed;
background-size: 100% 100%;
height: 100%;
width: 100%;
background-image: url(https://img1.baidu.com/it/u=323926268,4194139776&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=749);
} */
.set-background {
display: flex;
flex-direction: column;
align-items: center;
height: 100vh;
}
.set-background-avatar {
width: 100%;
height: 100vh;
overflow: hidden;
}
.background-content {
position: absolute;
z-index: 1;
width: 100%;
padding: 0 20px;
}
.background-image {
width: 100%;
height: 100vh;
opacity: 0.8;
/* 通过滤镜增加毛玻璃效果 */
/* filter: blur(1rpx); */
}
.post-specific-image {
width: 215px;
height: 150px;
vertical-align: middle;
}
/* 滚动视图区域 */
.page-section-spacing {
height: 100vh;
width: 100%;
padding-bottom: 1px;
}
.scroll-view-item {
/* height: 100px; */
margin: 40px auto;
border-radius: 5px;
/* background-color: burlywood; */
}
/* 消息框样式 */
.msg_box {
/* height: 100px;
width: 100px;*/
height: 100%;
/* background: chartreuse; */
display: flex;
/* align-self: start; */
align-items: center;
/* justify-content: center; */
}
.msg_box_l {
/* height: 100px;
width: 100px;*/
/* background: chartreuse; */
margin-right: 20px;
/* flex: 1; */
}
.msg_box_r {
/* height: 100px;
width: 100px;*/
/* background: red; */
height: 100%;
width: 100%;
flex: 1;
}
/* 三角弹窗 */
/*提示框容器*/
.tip {
position: relative;
/* margin-left: 10px; */
/* margin-top: 20px; */
/* width: 200px; */
/* background: #fff; */
padding: 5px;
/*设置圆角*/
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
/*提示框-右三角*/
.tip-trangle-right {
position: absolute;
top: 15px;
right: -10px;
width: 0;
height: 0;
border-top: 15px solid transparent;
border-bottom: 15px solid transparent;
border-left: 15px solid #fff;
}
/*提示框-上三角*/
.tip-trangle-top {
position: absolute;
top: -10px;
left: 20px;
width: 0;
height: 0;
border-left: 15px solid transparent;
border-right: 15px solid transparent;
border-bottom: 15px solid #fff;
}
/*提示框-下三角*/
.tip-trangle-bottom {
position: absolute;
bottom: -10px;
left: 20px;
width: 0;
height: 0;
border-left: 15px solid transparent;
border-right: 15px solid transparent;
border-top: 15px solid #fff;
}
/*提示框-左三角*/
.tip-trangle-left {
position: absolute;
bottom: 40%;
left: -10px;
width: 0;
height: 0;
border-top: 15px solid transparent;
border-bottom: 15px solid transparent;
border-right: 15px solid #fff;
}
.msg_box_content {
height: 100%;
width: 100%;
background: white;
display: block;
border-radius: 5px;
padding: 10px;
}
订阅消息推送位置:服务通知
订阅消息下发条件:用户自主订阅
订阅消息卡片跳转能力:点击查看详情可跳转至该小程序的页面
一次性订阅消息用于解决用户使用小程序后,后续服务环节的通知问题。用户自主订阅后,开发者可不限时间地下发一条对应的服务消息;每条消息可单独订阅或退订。
用户勾选 “总是保持以上选择,不再询问” 之后,下次订阅调用 wx.requestSubscribeMessage 不会弹窗,保持之前的选择,修改选择需要打开小程序设置进行修改。
// sendmsg云函数
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
}) // 使用当前云环境
// 任务名称
// {{thing5.DATA}}
// 任务描述
// {{thing2.DATA}}
// 任务开始时间
// {{time3.DATA}}
// 任务结束时间
// {{time4.DATA}}
// 任务发布人
// {{thing1.DATA}}
// 云函数入口函数
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
console.log(wxContext.OPENID, "wxContext.OPENID");
try {
const result = await cloud.openapi.subscribeMessage.send({
touser: wxContext.OPENID, //要推送给那个用户
page: 'pages/index/index', //要跳转到那个小程序页面
data: { //推送的内容
thing5: {
value: '跳舞计划' //任务名称
},
thing2: {
value: '每天晚上跳一会儿舞,身材会更好!' //任务描述
},
time3: {
value: '2022年09月25日 21:00' //任务开始时间
},
time4: {
value: '2022年09月25日 21:30' //任务结束时间
},
thing1: {
value: '彬婧的月亮' //任务发布人
}
},
templateId: 'BhzlO_gmYn3qh37pHvNNuDMT7wg' //模板id
})
console.log(result)
return result
} catch (err) {
console.log(err)
return err
}
// return {
// event,
// openid: wxContext.OPENID,
// appid: wxContext.APPID,
// unionid: wxContext.UNIONID,
// }
}
const app = getApp();
Page({
data: {
},
//获取授权的点击事件
authorize: function() {
wx.requestSubscribeMessage({
tmplIds: ['BhzlO_gmYn3qh37pHvNL3MZOT7wg'], //这里填入我们生成的模板id
success(res) {
console.log('授权成功', res)
//发送消息到指定用户,推送之前要先获取用户的openid
wx.cloud.callFunction({
name: "sendmsg",
data: {
data:1
}
}).then(res => {
console.log("推送消息成功", res)
}).catch(res => {
console.log("推送消息失败", res)
})
},
fail(res) {
console.log('授权失败', res)
}
})
},
});
模板ID
BhzlO_gmYn3qh37pHvNL3MZOuZ7g
模板编号
23530
标题
健康任务提醒
类目
健康管理
操作人
y13****000 2022-09-24 添加
详细内容
任务名称
{{thing5.DATA}}
任务描述
{{thing2.DATA}}
任务开始时间
{{time3.DATA}}
任务结束时间
{{time4.DATA}}
任务发布人
{{thing1.DATA}}
场景说明
健康任务提醒
//获取授权的点击事件
authorize: function() {
wx.requestSubscribeMessage({
tmplIds: ['BhzlO_gmYn3qh37NNuDMT7wg'], //这里填入我们生成的模板id
success(res) {
console.log('授权成功', res)
//发送消息到指定用户,推送之前要先获取用户的openid
wx.cloud.callFunction({
name: "sendmsg",
data: {
data:1
}
}).then(res => {
console.log("推送消息成功", res)
}).catch(res => {
console.log("推送消息失败", res)
})
},
fail(res) {
console.log('授权失败', res)
}
})
},
判断通知权限使用的是wx.getSetting
请求一次性订阅使用的是wx.requestSubscribeMessage
// 检查订阅消息权限,未开启提示前往开启,已开启请求订阅消息
checkAndRequestSubscribeMessage() {
let that = this
wx.getSetting({
withSubscriptions: true,
success(res) {
console.log(res)
// console.log(res.subscriptionsSetting)
// 订阅消息总开关是否开启
if (!res?.subscriptionsSetting?.mainSwitch) {
that.subscriptionFailed()
wx.showModal({
title: '提示',
content: '当前暂未开启接消息提醒,是否前往设置页开启?',
success(res) {
if (res.confirm) {
wx.openSetting()
}
}
})
} else {
console.log("else-->");
let templateId = 'BhzlO_gmYn3qh37pHuDMT7wg' // 模板ID
wx.requestSubscribeMessage({
tmplIds: [templateId],
success(res) {
console.log(res)
// 申请订阅成功,将订阅信息调用云函数存入云开发数据
if (res.errMsg === 'requestSubscribeMessage:ok') {
// res[templateId]: 'accept'、'reject'、'ban'、'filter'
if (res[templateId] == 'accept') {
console.log("订阅时间存入数据库");
} else {
that.subscriptionFailed()
}
}
},
fail(err) {
console.log(err)
that.subscriptionFailed()
wx.showToast({
title: '订阅失败',
icon: 'none'
})
}
})
}
}
})
},
// 订阅失败
subscriptionFailed() {
this.setData({
["dict.isPush"]: false
})
},
推送使用的是服务端的subscribeMessage.send方法。
这里是的是云调用进行推送,这样可以使用云开发
subscribeMessage.send使用需要在云函数代码的config.json文件中配置 subscribeMessage.send API 的权限
"permissions": {
"openapi": ["subscribeMessage.send"]
},
// sendmsg云函数
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
}) // 使用当前云环境
// 任务名称
// {{thing5.DATA}}
// 任务描述
// {{thing2.DATA}}
// 任务开始时间
// {{time3.DATA}}
// 任务结束时间
// {{time4.DATA}}
// 任务发布人
// {{thing1.DATA}}
// 云函数入口函数
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
console.log(wxContext.OPENID, "wxContext.OPENID");
try {
const result = await cloud.openapi.subscribeMessage.send({
touser: wxContext.OPENID, //要推送给那个用户
page: 'pages/index/index', //要跳转到那个小程序页面
data: { //推送的内容
thing5: {
value: '跳舞计划' //任务名称
},
thing2: {
value: '每天晚上跳一会儿舞,身材会更好!' //任务描述
},
time3: {
value: '2022年09月25日 21:00' //任务开始时间
},
time4: {
value: '2022年09月25日 21:30' //任务结束时间
},
thing1: {
value: '彬婧的月亮' //任务发布人
}
},
templateId: 'BhzlO_gmYn3qh37pHvNL3DMT7wg', //模板id
"miniprogramState": 'developer'
})
console.log(result)
return result
} catch (err) {
console.log(err)
return err
}
// return {
// event,
// openid: wxContext.OPENID,
// appid: wxContext.APPID,
// unionid: wxContext.UNIONID,
// }
}
- 该功能需开发者工具 1.02.1811270 及以上版本方可使用 从开发者工具 1.02.1910182 开始,新上传的定时触发器内支持使用云调用
- 如果云函数需要定时 / 定期执行,也就是定时触发,我们可以使用云函数定时触发器。配置了定时触发器的云函数,会在相应时间点被自动触发,函数的返回结果不会返回给调用方。
- 在需要添加触发器的云函数目录下新建文件 config.json
{
// triggers 字段是触发器数组,目前仅支持一个触发器,即数组只能填写一个,不可添加多个
"triggers": [
{
// name: 触发器的名字,规则见下方说明
"name": "myTrigger",
// type: 触发器类型,目前仅支持 timer (即 定时触发器)
"type": "timer",
// config: 触发器配置,在定时触发器下,config 格式为 cron 表达式,规则见下方说明
"config": "0 0 2 1 * * *"
}
]
}
- 如果代码实现上传并部署云函数之后,左等右等在日志中看不到日志,因为少了一个步骤,在上传并部署云函数之后,需要右键云函数上传触发器,这样才生效,想关闭可以删除触发器
“config”: “*/50 * * * * * *” 50秒一次
*/5 * * * * * * 表示每5秒触发一次
0 0 2 1 * * * 表示在每月的1日的凌晨2点触发
0 15 10 * * MON-FRI * 表示在周一到周五每天上午10:15触发
0 0 10,14,16 * * * * 表示在每天上午10点,下午2点,4点触发
0 */30 9-17 * * * * 表示在每天上午9点到下午5点内每半小时触发
0 0 12 * * WED * 表示在每个星期三中午12点触发
triggers中的config字段可以控制触发的频率,具体开发测试时我使用的是50秒调用一次
// index.js
// 云函数入口文件
const cloud = require('wx-server-sdk')
// 初始化 cloud
cloud.init({
// API 调用都保持和云函数当前所在环境一致
env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database()
const _ = db.command
const $ = db.command.aggregate
const kTableName = '换成自己的表名'
// 云函数入口函数
exports.main = async (event, context) => {
try {
// 从云开发数据库中查询等待发送的消息列表
const msgArr = await db
.collection(kTableName)
// 查询条件,已开启推送,并且提醒时间为今天
.where({
A_IsPush: true,
A_PushTime: timeStampToTime(new Date().getTime(), '{y}/{m}/{d}')
})
.get()
// 循环消息列表
const sendPromises = msgArr.data.map(async msgData => {
try {
// 发送订阅消息
await cloud.openapi.subscribeMessage.send({
touser: msgData._openid, // 要发送用户的openid
page: 'pages/index/index', // 用户通过消息通知点击进入小程序的页面
lang: 'zh_CN',
templateId: 'BhzlO_gmYn3qh37NNuDMT7wg', // 订阅消息模板ID
// 跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
// miniprogramState: 'developer',
// 要发送的数据,要和模板一致
data: { //推送的内容
thing5: {
value: '跳舞计划' //任务名称
},
thing2: {
value: '每天晚上跳一会儿舞,身材会更好!' //任务描述
},
time3: {
value: '2022年09月25日 21:00' //任务开始时间
},
time4: {
value: '2022年09月25日 21:30' //任务结束时间
},
thing1: {
value: '彬婧的月亮' //任务发布人
}
}
})
// 发送成功后将数据状态重置
return db
.collection(kTableName)
.doc(msgData._id)
.update({
data: {
A_IsPush: false,
A_PushTime: '',
A_NextTime: '',
},
})
} catch (e) {
return e
}
})
return Promise.all(sendPromises)
} catch (err) {
console.log(err)
return err
}
}
function timeStampToTime(time, cFormat) {
if (arguments.length === 0) {
return null
}
const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
let date
if (typeof time === 'object') {} else {
if (('' + time).length === 10) time = parseInt(time) * 1000
date = new Date(time)
}
const formatObj = {
y: date.getFullYear(),
m: date.getMonth() + 1,
d: date.getDate(),
h: date.getHours(),
i: date.getMinutes(),
s: date.getSeconds(),
w: date.getDay()
}
const time_str = format.replace(/{(y|m|d|h|i|s|w)+}/g, (result, key) => {
let value = formatObj[key]
if (key === 'w') {
return ['日', '一', '二', '三', '四', '五', '六'][value]
}
if (result.length > 0 && value < 10) {
value = '0' + value
}
return value || 0
})
return time_str
// return {
// event,
// openid: wxContext.OPENID,
// appid: wxContext.APPID,
// unionid: wxContext.UNIONID,
// }
}
// 定时触发器
// https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/functions/triggers.html
// 50秒一次
// "config": "*/50 * * * * * *"
// 每天上午8点一次
// "config": "0 0 8 * * * *"
{
"permissions": {
"openapi": [
"subscribeMessage.send"
]
},
"triggers": [
{
"name": "myTrigger",
"type": "timer",
"config": "*/50 * * * * * *"
}
]
}
{
"name": "push",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"wx-server-sdk": "~2.6.3"
}
}
- 后端00:00点从数据库获取订阅消息发送时间及消息类型,设置定时任务为订阅发送时间
- 到时间后端调用api,获取一条api数据,存入数据库,推送消息给彬婧
- 彬婧收到订阅,进入小程序,点赞,进入数据库,该条消息标注喜欢状态,不喜欢,删除数据库该条消息
- 没有收到订阅,进入小程序,调用数据库所有类型消息,按时间降序排序,点过赞的消息不可以点赞,可以标注不喜欢,不喜欢的,删除该条消息
- 过一段时间做分析,喜欢的是什么类型的消息,更改API接口为她喜欢的类型
头像
昵称
创建时间
更新时间
发送时间
openid
订阅时间 (默认晚上22:00点)
发送状态
订阅消息类型(默认为名言1)
订阅状态[0,1,2,3 0为取消所有订阅] (默认为1)
[
{
"fields": [
{
"dateFormatType": "timestamp-ms",
"description": "CMS 系统字段,请勿随意修改。通过 CMS 系统录入的数据会默认添加该字段",
"displayName": "创建时间",
"id": "_createTime",
"isSystem": true,
"name": "_createTime",
"type": "DateTime"
},
{
"dateFormatType": "timestamp-ms",
"description": "CMS 系统字段,请勿随意修改。通过 CMS 系统录入的数据会默认添加该字段",
"displayName": "修改时间",
"id": "_updateTime",
"isSystem": true,
"name": "_updateTime",
"type": "DateTime"
},
{
"copyable": true,
"description": "CMS 系统字段,请勿随意修改",
"displayName": "文档 ID",
"id": "_id",
"isHidden": true,
"isSystem": true,
"name": "_id",
"type": "String"
},
{
"displayName": "openid",
"id": "91f3ilqb42rzbgwa8ai5jcue1jvdlen0",
"name": "_openid",
"order": 6,
"type": "String"
},
{
"dateFormatType": "string",
"displayName": "创建时间",
"id": "atoc1cwjoulguglsgru1dwxnx61yuaka",
"name": "createTime",
"order": 4,
"type": "DateTime"
},
{
"dateFormatType": "string",
"displayName": "更新时间",
"id": "hoctdb9lacscx6mgo81u9fp9nz79n29m",
"name": "updateTime",
"order": 5,
"type": "DateTime"
},
{
"dateFormatType": "string",
"displayName": "发送时间",
"id": "zka0chgukvgk2tng70xhih8yf5m9et8c",
"name": "pushTime",
"order": 6,
"type": "DateTime"
},
{
"dateFormatType": "string",
"displayName": "订阅时间",
"id": "um163t4qukxlrpyl6qjk08fjkfyxtnl6",
"name": "subTime",
"order": 7,
"type": "DateTime"
},
{
"displayName": "发送状态",
"id": "xg4h6xbaiqginsirzxwhcf8sx6x7p9f3",
"name": "status",
"order": 8,
"type": "Boolean"
},
{
"description": "订阅消息类型(1明言)",
"displayName": "订阅消息类型",
"id": "zdfqhsp0yfln1pgb1wcnlcevom88nway",
"name": "type",
"order": 9,
"type": "Number"
},
{
"displayName": "头像",
"id": "bj8qqmy875phayvuph39pc3hxgwwlor6",
"name": "avatarUrl",
"order": 10,
"type": "String"
},
{
"displayName": "昵称",
"id": "tmm1v5gizz7z5gj497s7bidvx6ht8m91",
"name": "nickName",
"order": 11,
"type": "String"
},
{
"description": "订阅状态[0,1,2,3 0为取消所有订阅]",
"displayName": "订阅状态",
"id": "w8oew76bugo0xxq7qxd9eijt0cetytoh",
"name": "sub_status",
"order": 12,
"type": "Array"
}
],
"collectionName": "subs",
"displayName": "订阅表",
"description": "记录订阅",
"_id": "e1afb303633071110045257611abe5ec"
}
]
[
{
"_id": "bd4da45863307337003a4d9360be2397",
"createTime": "2022-09-25 23:25:01",
"status": false,
"updateTime": "2022-09-25 23:25:04",
"_createTime": 1664119607725,
"_updateTime": 1664121401450,
"sub_status": [
"1",
"2",
"3"
],
"_openid": "o3pXW5Sx0Mi1qwi_KWJhcqzcoMsk",
"avatarUrl": "11",
"nickName": "11",
"pushTime": "2022-09-25 23:56:25",
"subTime": "2022-09-25 23:56:28",
"type": 1
}
//1. 删除type,用sub_status就够了
// //订阅状态[0,1,2,3 0为取消所有订阅] //订阅消息类型(1明言)
// 2.发送状态status改为数组
// 发送状态[{type:'1',status:true,creatTime:'2022-01-01'}]
]
ashes_logs
[
{
"fields": [
{
"dateFormatType": "timestamp-ms",
"description": "CMS 系统字段,请勿随意修改。通过 CMS 系统录入的数据会默认添加该字段",
"displayName": "创建时间",
"id": "_createTime",
"isSystem": true,
"name": "_createTime",
"type": "DateTime"
},
{
"dateFormatType": "timestamp-ms",
"description": "CMS 系统字段,请勿随意修改。通过 CMS 系统录入的数据会默认添加该字段",
"displayName": "修改时间",
"id": "_updateTime",
"isSystem": true,
"name": "_updateTime",
"type": "DateTime"
},
{
"displayName": "日记",
"id": "th45l3t35kz6ijrpc2jgmfen1l64v0yh",
"name": "logs",
"order": 2,
"type": "RichText"
},
{
"copyable": true,
"description": "CMS 系统字段,请勿随意修改",
"displayName": "文档 ID",
"id": "_id",
"isHidden": true,
"isSystem": true,
"name": "_id",
"type": "String"
}
],
"collectionName": "ashes_logs",
"displayName": "守护者日记",
"_id": "b3abc05c63307d33004979661553ddee"
}
]
// 往订阅信息记录表插入一条默认数据
insertSubInfos() {
// console.log(this.globalData.userInfo, "this.globalData.userInfo");
wx.cloud.database().collection('subs').add({
// data 字段表示需新增的 JSON 数据
data: {
createTime: util.formatTime(new Date()),
push_status: [], //发送状态[{type:'1',status:true,creatTime:'2022-01-01'}]
updateTime: util.formatTime(new Date()),
sub_status: ['1', '2', '3'], //订阅状态[0,1,2,3 0为取消所有订阅] //订阅消息类型(1明言)
openid: this.globalData.userInfo._openid, // 会自动创建一条openid ,先不管,先存着,以防万一
avatarUrl: this.globalData.userInfo.avatarUrl,
nickName: this.globalData.userInfo.nickName,
pushTime: '', //发送时间
subTime: moment().add(1, 'days').set({
'hour': 22,
'minute': 0,
'second': 0
}).format('yyyy-MM-DD HH:mm:ss'), //第二天 22:00 订阅时间
}
})
.then(res => {
console.log(res)
// let res = {
// errMsg: "collection.add:ok",
// _id: "4e3386e26331dab00039ee25784f14b5"
// }
})
.catch(console.error)
},
//数据库数据
[
{
"_id": "4e3386e26331dab00039ee25784f14b5",
"avatarUrl": "https://thirdwx.qlogo.cn/mmopen/vi_32/HAKxmIGibkZgib3vcTg8eaAfUeOIc5InEZibmqEU7LJM8kOzRWGWOfo1Bsibib9fchq6Pv2WUBJsQnLjWIicXzjl9KqQ/132",
"createTime": "2022/09/27 01:00:31",
"openid": "o1jgE5h7Pj-QC",
"subTime": "2022-09-28 22:00:00",
"sub_status": [
"1",
"2",
"3"
],
"_openid": "o1jgE5h7Pj-QCm9-N",
"nickName": "Ashes of Time",
"pushTime": "",
"push_status": [],
"updateTime": "2022/09/27 01:00:31"
}
]
-间隔一个小时查询订阅任务表,获取订阅任务发送时间,执行任务
// 云函数入口文件
const cloud = require('wx-server-sdk')
// 初始化 cloud
cloud.init({
// API 调用都保持和云函数当前所在环境一致
env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database()
const _ = db.command
const $ = db.command.aggregate
const kTableName = 'subs'
// 云函数入口函数
exports.main = async (event, context) => {
try {
// 从云开发数据库中查询等待发送的消息列表
const msgArr = await db
.collection(kTableName)
// 查询条件,已开启推送,并且提醒时间为今天
.where({
// A_IsPush: true,
// A_PushTime: timeStampToTime(new Date().getTime(), '{y}/{m}/{d}')
})
.get()
console.log(msgArr,"msgArr-->>");
return msgArr
// 循环消息列表
const sendPromises = msgArr.data.map(async msgData => {
try {
// 发送订阅消息
await cloud.openapi.subscribeMessage.send({
touser: msgData._openid, // 要发送用户的openid
page: 'pages/index/index', // 用户通过消息通知点击进入小程序的页面
lang: 'zh_CN',
templateId: 'BhzlO_gmYn3qh37pHvNL3MZOuZ7j-029YpNNuDMT7wg', // 订阅消息模板ID
// 跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
// miniprogramState: 'developer',
// 要发送的数据,要和模板一致
data: { //推送的内容
thing5: {
value: '跳舞计划' //任务名称
},
thing2: {
value: '每天晚上跳一会儿舞,身材会更好!' //任务描述
},
time3: {
value: '2022年09月25日 21:00' //任务开始时间
},
time4: {
value: '2022年09月25日 21:30' //任务结束时间
},
thing1: {
value: '彬婧的月亮' //任务发布人
}
}
})
// 发送成功后将数据状态重置
return db
.collection(kTableName)
.doc(msgData._id)
.update({
data: {
A_IsPush: false,
A_PushTime: '',
A_NextTime: '',
},
})
} catch (e) {
return e
}
})
return Promise.all(sendPromises)
} catch (err) {
console.log(err)
return err
}
}
function timeStampToTime(time, cFormat) {
if (arguments.length === 0) {
return null
}
const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
let date
if (typeof time === 'object') {} else {
if (('' + time).length === 10) time = parseInt(time) * 1000
date = new Date(time)
}
const formatObj = {
y: date.getFullYear(),
m: date.getMonth() + 1,
d: date.getDate(),
h: date.getHours(),
i: date.getMinutes(),
s: date.getSeconds(),
w: date.getDay()
}
const time_str = format.replace(/{(y|m|d|h|i|s|w)+}/g, (result, key) => {
let value = formatObj[key]
if (key === 'w') {
return ['日', '一', '二', '三', '四', '五', '六'][value]
}
if (result.length > 0 && value < 10) {
value = '0' + value
}
return value || 0
})
return time_str
// return {
// event,
// openid: wxContext.OPENID,
// appid: wxContext.APPID,
// unionid: wxContext.UNIONID,
// }
}
// 定时触发器
// https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/functions/triggers.html
// 50秒一次
// "config": "*/50 * * * * * *"
// 每天上午8点一次
// "config": "0 0 8 * * * *"
//index.js
// 时间处理模块
// const moment = require("moment");
const moment = require('moment-timezone');
// package.json
"dependencies": {
"wx-server-sdk": "~2.6.3",
"moment": "latest",
"moment-timezone": "latest"
}
出了点事,还有一个月软考,得给自己一个交代,所以先暂停了项目
小程序已经开发完毕,换了一个名字“愿望森林”,目前正在审核中。。。
原来的代码及方案全都抛弃了,工作辞了,有时间重新做了一个
vue2.0构建 uniapp
uniapp原生组件 uview2.0 colorUI
postgresql+ hasura+ graphql+ fastify+ nodejs+ docker
彬婧的月亮2