// pages/Lucky-draw/index.js const Parse = getApp().Parse const company = getApp().globalData.company const rechText = require('../../../utils/rech-text') const dateF = require('../../../utils/date.js') const login = require("../../../utils/login"); Page({ /** * 页面的初始数据 */ data: { prizeId: '', // 抽中结果id,通过属性方式传入组件 config: { // 转盘配置,通过属性方式传入组件 nameLength: 7 }, luckyDrawRules: null, giftModules: [], account: null, gifts: [], user: "", module: "", giftsList: [], twoModal: false, region: ['江西省', '南昌市', '东湖区'], name: "", address: "", mobile: "", area: "", value: '', enable: true }, // 奖品表 async getgiftModule() { let GiftModule = new Parse.Query("GiftModule"); GiftModule.equalTo("company", company); let giftModules = await GiftModule.find(); let giftModuleJSON = [] if (giftModules && giftModules.length > 0) { giftModules.forEach(giftModule => { let b = giftModule.toJSON() giftModuleJSON.push(b) }) this.setData({ giftModules: giftModuleJSON }) } }, //积分表 async getAccount() { let uid = Parse.User.current().id let queryAccount = new Parse.Query("Account"); queryAccount.equalTo("user", uid); let account = await queryAccount.first(); console.log(account) if (account && account.id) { this.setData({ account: account.toJSON() }); } }, // 数据增减 async subtractIntegral() { let Account = new Parse.Query('Account') let updateAccount = await Account.get(this.data.account.objectId) updateAccount.increment('credit', -this.data.luckyDrawRules.credit) let res = await updateAccount.save() console.log(res) if (res && res.id) { this.setData({ account: res.toJSON() }) } }, onNotEnoughHandle(e) { wx.showToast({ icon: 'none', title: e.detail }) }, // 点击函数 onLuckDrawHandle() { console.log(this.data.luckyDrawRules.credit, this.data.account.credit) if(!this.data.account.credit || (this.data.luckyDrawRules.credit > this.data.account.credit)) { wx.showToast({ title: '积分不足!', icon: 'none' }); this.setData({ enable: true }) return; } this.subtractIntegral() //修改数据库 this.setData({ prizeId: this.data.giftModules[Math.floor(Math.random() * 10 % this.data.giftModules.length)].objectId, }); }, /** * 动画旋转完成回调 */ async onLuckDrawFinishHandle() { let giftModules = this.data.giftModules; let data = giftModules.find((item) => { return item.objectId === this.data.prizeId; }); wx.showToast({ icon: 'none', title: `恭喜你抽中 ${data.name}` }) await this.saveGift(this.data.prizeId) this.setData({ prizeId: '' }); }, //添加数据 async saveGift(prizeId) { let Gift = Parse.Object.extend('Gift') let gift = new Gift() let uid = Parse.User.current().id gift.set('user', { __type: 'Pointer', className: "_User", objectId: uid }) gift.set('module', { __type: 'Pointer', className: "GiftModule", objectId: prizeId }) gift.set('company', { __type: 'Pointer', className: "Company", objectId: company }) let res = await gift.save() if (res && res.id) { await this.getGift() } }, async getGift() { let uid = Parse.User.current().id let Gift = new Parse.Query("Gift"); Gift.equalTo("company", company); Gift.descending("createdAt") Gift.include('module') Gift.equalTo("user", uid); let gifts = await Gift.find(); let giftJSON = [] if (gifts && gifts.length > 0) { gifts.forEach(gift => { let b = gift.toJSON() b.joinTime = dateF.formatTime("YYYY-mm-dd HH:MM:SS", b.createdAt) giftJSON.push(b) }) this.setData({ giftList: giftJSON }) } }, async getLuckyDrawRules() { let LuckyDrawRule = new Parse.Query("LuckyDrawRules"); LuckyDrawRule.equalTo("company", company); LuckyDrawRule.equalTo("isOpen", true); let luckyDrawRules = await LuckyDrawRule.first(); if (luckyDrawRules) { let rules = luckyDrawRules.toJSON() rules.content = rechText.formatRichText(rules.detail) this.setData({ luckyDrawRules: rules, }) } console.log(this.data.luckyDrawRules) }, shopsubmit: function(e) { let item = e.currentTarget.dataset.item console.log(item) this.setData({ twoModal: true, receiveId: item.objectId }) }, shoppreventTouchMove: function() { this.setData({ twoModal: false }) }, bindRegionChange: function(e) { // picker值发生改变都会触发该方法 console.log('picker发送选择改变,携带值为', e.detail.value) this.setData({ region: e.detail.value }) }, hideRule: function() { this.setData({ twoModal: false }) }, //提交按纽// 修改数据 async submit() { let newGift = new Parse.Query('Gift') if (!this.data.mobile || !this.data.name || !this.data.address || this.data.region.length == 0) { wx.showToast({ title: '请将信息填写完整', icon: 'none' }) return } let area = "" this.data.region.forEach((item, index) => { if (index == (this.data.region.length - 1)) { area += item } else { area += item + '-' } }) let res = await newGift.get(this.data.receiveId) if (res) { res.set("mobile", this.data.mobile) res.set("name", this.data.name) res.set("address", this.data.address) res.set("area", area) let updateuser = await res.save() if (updateuser) { console.log(updateuser) wx.showToast({ title: '确认成功', icon: 'success', image: '', duration: 1000, mask: false, }); this.setData({ receiveId: "", name: "", mobile: "", address: "", twoModal: false }) } } }, /** * 生命周期函数--监听页面加载 */ onLoad: async function(options) { let userLogin = wx.getStorageSync("userLogin"); if (userLogin == "") { login.loginNow(); return; } await this.getLuckyDrawRules() await this.getgiftModule() await this.getGift() await this.getAccount() }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function(e) { }, /** * 生命周期函数--监听页面显示 */ onShow: function() { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function() { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function() { }, /** * 用户点击右上角分享 */ onShareAppMessage: function() { } })