4 Star 2 Fork 3

梦之狼 / a repository store a wechat miniprogram code

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
app.js 1.94 KB
一键复制 编辑 原始数据 按行查看 历史
//app.js
//云开发
import formatTime from "/utils/util.js"
wx.cloud.init({ env: 'pethub-database' });
const user = wx.cloud.database().collection("user")
App({
toDays: function (date) {
var list = date.split("-")
var second = new Date(Date.UTC(parseInt(list[0]), parseInt(list[1]), parseInt(list[2])))
return second.getTime() / (1000 * 60 * 60 * 24)
},
onLaunch: function () {
wx.showToast({
title: '正努力为主人加载数据,请稍等',
icon: "none",
duration:2000
})
// 每次登录小程序都要更新计算宠物年龄
if(wx.getStorageSync('_id')&& wx.getStorageSync("lastTime")){
user.doc(wx.getStorageSync("_id")).update({
data:{
// 就是原来的年龄加上上次登录小程序的时间减去本次登录小程序的时间之差,都是以天数为单位
age: wx.cloud.database().command.inc(
this.toDays(formatTime.formatTime(new Date()).split(" ")[0]) - this.toDays(wx.getStorageSync("lastTime"))
)
}
})
.then(
wx.setStorageSync("lastTime", formatTime.formatTime(new Date()).split(" ")[0])
)
}
// 首次登录或清理了缓存
else{
wx.setStorageSync("lastTime", formatTime.formatTime(new Date()).split(" ")[0])
}
},
getOpenid:function(){
if (!wx.getStorageSync('openid')) {
console.log("app.js没有了缓存")
wx.cloud.callFunction({
name: "login",
success(res){
wx.setStorageSync("openid", res.result.openid)
return res.result.openid
},
fail(err){
wx.showToast({
title: '对不起出错了,请关闭小程序重新登录试试',
icon:"none"
})
console.log("云服务器忙或调用达到上限!")
console.log("错误原因:"+err)
}
})
}
else{
return wx.getStorageSync('openid')
}
},
globalData: {
},
})
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mengzhilang/pethub.git
git@gitee.com:mengzhilang/pethub.git
mengzhilang
pethub
a repository store a wechat miniprogram code
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891