import {accountManager} from "../../js/utils"; const app = getApp(); Page({ data: { version: global.config.version, imgDir: global.config.imgDir, eduImgDir: global.config.eduImgDir, attachDir:global.config.attachDir, user: {}, showModal: false, auto_attendance: false }, enterPage({ target: { dataset: { path } } }) { if (!path) return; let { id } = this.data.user; if (!id || id == 2) return wx.showToast({ title: "请先登录哦", icon: "none" }); app.navigateTo({ url: `{${path}}` }); }, onTapVersion() { let { envVersion, env } = global.config; if (envVersion && envVersion != "release") { wx.showActionSheet({ itemList: [env != "release" ? "切换为发布环境" : "切换为开发环境", "查看页面访问记录"], success: res => { if(res.tapIndex==0){ global.config.switchEnv(env != "release" ? "release" : "trial"); wx.showToast({ title: '切换成功' }); this.logout({ showToast: 0 }); let { version, eduImgDir } = global.config; this.setData({ version, eduImgDir }); }else if(res.tapIndex==1){ app.navigateTo({url:"{page_history}"}) } } }) return; } const updateManager = wx.getUpdateManager() wx.showToast({ title: '检查更新中...', icon: "none" }); updateManager.onCheckForUpdate(res => { if (res.hasUpdate) { wx.hideToast(); wx.showModal({ title: '更新提示', content: "(●'◡'●)\n有新版本哦,是否使用最新的小程序呢?", success: function (res) { if (res.confirm) { wx.showLoading({ title: '准备中' }); updateManager.onUpdateReady(() => { wx.hideLoading(); updateManager.applyUpdate() }) updateManager.onUpdateFailed(() => { wx.hideLoading(); wx.showToast({ title: '>︿<\n更新失败', icon: "none" }) }) } } }) } else { wx.showToast({ title: '你用的是最新版本哦', icon: "none" }) } }) }, onTapAvatar: function ({ currentTarget: { dataset } }) { wx.showActionSheet({ itemList: ["查看图片", "更改头像"], success: res => { switch (res.tapIndex) { case 0: wx.previewImage({ urls: [global.config.eduImgDir + dataset.url], }); break; case 1: wx.navigateTo({ url: '/setting/pages/image_crop/image_crop' }); break; } } }) }, logout: function ({ showToast = 1 }) { app.callApi({ name: "accounts.logout", success: res => { if (showToast) app.showMsg(res); this.onShow(); }, fail: e=> { if (showToast) app.showError(e); } }) }, enter_login: function (event) { wx.navigateTo({ url: '/account/pages/account/account?action=login' }) }, showModal: function (event) { let {id} = this.data.user; if (!id || id == 2) return wx.showToast({ title: "请先登录哦", icon: "none" }); this.setData({ showModal: true }); }, onLoad() { let user_id = app.user().user_id; if(user_id!=2&&user_id){ let key = `cache-users.homepage_info-${app.user().user_id}`; let cache_homepage = wx.getStorageSync(key); if(cache_homepage) this.setData({user: cache_homepage}); } /* let data = wx.getStorageSync("auto-attendence"); if (data) this.setData({ auto_attendance: true }); */ }, onShow() { app.syncUser().then(res => { if (res.user.user_id != 2){ this.refresh(); }else this.setData({ user: {}, currentLogin:"" }) }); }, refresh: function () { app.api("users.homepage_info")() .then(res => { let { name: show_name, avatar_url: image_url} = res; if(show_name&&image_url) app.updateUserInfo({ show_name, image_url}); this.setData({ user: res }); let key = `cache-users.homepage_info-${app.user().user_id}`; wx.setStorage({ data: res, key }) // 自动签到 if (!res.attendance_signed) this.attendance({ show: 0 }) }); let account = accountManager.getCurrentAccount(); if (account && account.login && account.active) { var currentLogin = account.login; /*if(account.login.indexOf("@")!=-1) currentLogin = currentLogin.replace(/.{4}()/) else currentLogin = currentLogin.replace(/./,""); */ } else { var currentLogin = ""; } this.setData({ currentLogin, profile_completed: app.user().profile_completed}); }, tapAutoAttendence() { this.setData({ auto_attendance: !this.data.auto_attendance }); if (!this.data.user.attendance_signed && this.data.auto_attendance) this.attendance({ show: 1 }) wx.setStorage({ key: 'auto-attendence', data: this.data.auto_attendance, }); }, updateGold({gold, duration=2500}){ let {grade} = this.data.user; if(grade >= gold) return console.log(new Date().getTime()) else{ let time = 1; this.setData({"user.grade": grade+1}, ()=>{ //this.updateGold(gold); var id = setTimeout(()=>{ clearTimeout(id); this.updateGold({gold,duration}); }, time); } ); } }, attendance({ show = 1 }={}) { if(!app.checkLogin({content:"您需要登陆后才能签到获取金币"})) return; if(this.data.user.attendance_signed){ if(show) wx.showToast({ title: '您已经签到过了',icon:"none" }); return; } app.api("users.attendance")() .then(res => { res.message = "签到成功"; if (show) app.showMsg(res); let {grade} = res; var db = wx.cloud.database(); db.collection("res").add({ data:{ ...res, name: "users.attendance", createdAt: db.serverDate(), createAt: db.serverDate() //@deprecated } }) this.setData({"user.attendance_signed": true}); this.updateGold({gold: grade}) }).catch(e=>{ if(show) app.showError(e); this.refresh(); }) }, enterGitrepo(){ app.navigateTo({url:"{git_repo}"}); }, enterUserinfo(){ app.navigateTo({url:"{user_info}"}); }, onShareAppMessage: function () { return app.shareApp({ imageUrl:global.config.attachDir + "944406" }) } })