index.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // pages/Lucky-draw/index.js
  2. const Parse = getApp().Parse
  3. const company = getApp().globalData.company
  4. const rechText = require('../../../utils/rech-text')
  5. const dateF = require('../../../utils/date.js')
  6. const login = require("../../../utils/login");
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. prizeId: '', // 抽中结果id,通过属性方式传入组件
  13. config: { // 转盘配置,通过属性方式传入组件
  14. nameLength: 7
  15. },
  16. luckyDrawRules: null,
  17. giftModules: [],
  18. account: null,
  19. gifts: [],
  20. user: "",
  21. module: "",
  22. giftsList: [],
  23. twoModal: false,
  24. region: ['江西省', '南昌市', '东湖区'],
  25. name: "",
  26. address: "",
  27. mobile: "",
  28. area: "",
  29. value: '',
  30. enable: true
  31. },
  32. // 奖品表
  33. async getgiftModule() {
  34. let GiftModule = new Parse.Query("GiftModule");
  35. GiftModule.equalTo("company", company);
  36. let giftModules = await GiftModule.find();
  37. let giftModuleJSON = []
  38. if (giftModules && giftModules.length > 0) {
  39. giftModules.forEach(giftModule => {
  40. let b = giftModule.toJSON()
  41. giftModuleJSON.push(b)
  42. })
  43. this.setData({
  44. giftModules: giftModuleJSON
  45. })
  46. }
  47. },
  48. //积分表
  49. async getAccount() {
  50. let uid = Parse.User.current().id
  51. let queryAccount = new Parse.Query("Account");
  52. queryAccount.equalTo("user", uid);
  53. let account = await queryAccount.first();
  54. console.log(account)
  55. if (account && account.id) {
  56. this.setData({
  57. account: account.toJSON()
  58. });
  59. }
  60. },
  61. // 数据增减
  62. async subtractIntegral() {
  63. let Account = new Parse.Query('Account')
  64. let updateAccount = await Account.get(this.data.account.objectId)
  65. updateAccount.increment('credit', -this.data.luckyDrawRules.credit)
  66. let res = await updateAccount.save()
  67. console.log(res)
  68. if (res && res.id) {
  69. this.setData({
  70. account: res.toJSON()
  71. })
  72. }
  73. },
  74. onNotEnoughHandle(e) {
  75. wx.showToast({
  76. icon: 'none',
  77. title: e.detail
  78. })
  79. },
  80. // 点击函数
  81. onLuckDrawHandle() {
  82. console.log(this.data.luckyDrawRules.credit, this.data.account.credit)
  83. if(!this.data.account.credit || (this.data.luckyDrawRules.credit > this.data.account.credit))
  84. {
  85. wx.showToast({
  86. title: '积分不足!',
  87. icon: 'none'
  88. });
  89. this.setData({
  90. enable: true
  91. })
  92. return;
  93. }
  94. this.subtractIntegral() //修改数据库
  95. this.setData({
  96. prizeId: this.data.giftModules[Math.floor(Math.random() * 10 % this.data.giftModules.length)].objectId,
  97. });
  98. },
  99. /**
  100. * 动画旋转完成回调
  101. */
  102. async onLuckDrawFinishHandle() {
  103. let giftModules = this.data.giftModules;
  104. let data = giftModules.find((item) => {
  105. return item.objectId === this.data.prizeId;
  106. });
  107. wx.showToast({
  108. icon: 'none',
  109. title: `恭喜你抽中 ${data.name}`
  110. })
  111. await this.saveGift(this.data.prizeId)
  112. this.setData({
  113. prizeId: ''
  114. });
  115. },
  116. //添加数据
  117. async saveGift(prizeId) {
  118. let Gift = Parse.Object.extend('Gift')
  119. let gift = new Gift()
  120. let uid = Parse.User.current().id
  121. gift.set('user', {
  122. __type: 'Pointer',
  123. className: "_User",
  124. objectId: uid
  125. })
  126. gift.set('module', {
  127. __type: 'Pointer',
  128. className: "GiftModule",
  129. objectId: prizeId
  130. })
  131. gift.set('company', {
  132. __type: 'Pointer',
  133. className: "Company",
  134. objectId: company
  135. })
  136. let res = await gift.save()
  137. if (res && res.id) {
  138. await this.getGift()
  139. }
  140. },
  141. async getGift() {
  142. let uid = Parse.User.current().id
  143. let Gift = new Parse.Query("Gift");
  144. Gift.equalTo("company", company);
  145. Gift.descending("createdAt")
  146. Gift.include('module')
  147. Gift.equalTo("user", uid);
  148. let gifts = await Gift.find();
  149. let giftJSON = []
  150. if (gifts && gifts.length > 0) {
  151. gifts.forEach(gift => {
  152. let b = gift.toJSON()
  153. b.joinTime = dateF.formatTime("YYYY-mm-dd HH:MM:SS", b.createdAt)
  154. giftJSON.push(b)
  155. })
  156. this.setData({
  157. giftList: giftJSON
  158. })
  159. }
  160. },
  161. async getLuckyDrawRules() {
  162. let LuckyDrawRule = new Parse.Query("LuckyDrawRules");
  163. LuckyDrawRule.equalTo("company", company);
  164. LuckyDrawRule.equalTo("isOpen", true);
  165. let luckyDrawRules = await LuckyDrawRule.first();
  166. if (luckyDrawRules) {
  167. let rules = luckyDrawRules.toJSON()
  168. rules.content = rechText.formatRichText(rules.detail)
  169. this.setData({
  170. luckyDrawRules: rules,
  171. })
  172. }
  173. console.log(this.data.luckyDrawRules)
  174. },
  175. shopsubmit: function(e) {
  176. let item = e.currentTarget.dataset.item
  177. console.log(item)
  178. this.setData({
  179. twoModal: true,
  180. receiveId: item.objectId
  181. })
  182. },
  183. shoppreventTouchMove: function() {
  184. this.setData({
  185. twoModal: false
  186. })
  187. },
  188. bindRegionChange: function(e) { // picker值发生改变都会触发该方法
  189. console.log('picker发送选择改变,携带值为', e.detail.value)
  190. this.setData({
  191. region: e.detail.value
  192. })
  193. },
  194. hideRule: function() {
  195. this.setData({
  196. twoModal: false
  197. })
  198. },
  199. //提交按纽// 修改数据
  200. async submit() {
  201. let newGift = new Parse.Query('Gift')
  202. if (!this.data.mobile || !this.data.name || !this.data.address || this.data.region.length == 0) {
  203. wx.showToast({
  204. title: '请将信息填写完整',
  205. icon: 'none'
  206. })
  207. return
  208. }
  209. let area = ""
  210. this.data.region.forEach((item, index) => {
  211. if (index == (this.data.region.length - 1)) {
  212. area += item
  213. } else {
  214. area += item + '-'
  215. }
  216. })
  217. let res = await newGift.get(this.data.receiveId)
  218. if (res) {
  219. res.set("mobile", this.data.mobile)
  220. res.set("name", this.data.name)
  221. res.set("address", this.data.address)
  222. res.set("area", area)
  223. let updateuser = await res.save()
  224. if (updateuser) {
  225. console.log(updateuser)
  226. wx.showToast({
  227. title: '确认成功',
  228. icon: 'success',
  229. image: '',
  230. duration: 1000,
  231. mask: false,
  232. });
  233. this.setData({
  234. receiveId: "",
  235. name: "",
  236. mobile: "",
  237. address: "",
  238. twoModal: false
  239. })
  240. }
  241. }
  242. },
  243. /**
  244. * 生命周期函数--监听页面加载
  245. */
  246. onLoad: async function(options) {
  247. let userLogin = wx.getStorageSync("userLogin");
  248. if (userLogin == "") {
  249. login.loginNow();
  250. return;
  251. }
  252. await this.getLuckyDrawRules()
  253. await this.getgiftModule()
  254. await this.getGift()
  255. await this.getAccount()
  256. },
  257. /**
  258. * 生命周期函数--监听页面初次渲染完成
  259. */
  260. onReady: function(e) {
  261. },
  262. /**
  263. * 生命周期函数--监听页面显示
  264. */
  265. onShow: function() {
  266. },
  267. /**
  268. * 生命周期函数--监听页面隐藏
  269. */
  270. onHide: function() {
  271. },
  272. /**
  273. * 生命周期函数--监听页面卸载
  274. */
  275. onUnload: function() {
  276. },
  277. /**
  278. * 页面相关事件处理函数--监听用户下拉动作
  279. */
  280. onPullDownRefresh: function() {
  281. },
  282. /**
  283. * 页面上拉触底事件的处理函数
  284. */
  285. onReachBottom: function() {
  286. },
  287. /**
  288. * 用户点击右上角分享
  289. */
  290. onShareAppMessage: function() {
  291. }
  292. })