index.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. let Parse = getApp().Parse;
  2. const company = getApp().globalData.company
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. scrollTops: 0, // 要滚动的高度
  9. tabCur: 0, // 当前项
  10. rightCur: 0, // 用于实现左边联动右边
  11. cates: [],
  12. countMap: {},
  13. totalPrice: 0,
  14. totalCount: 0,
  15. tradeNo: "",
  16. show: false
  17. },
  18. onLoad: async function (options) {
  19. console.log(options)
  20. let storeid = options.storeid
  21. let tableid = options.tableid
  22. let active = options.active
  23. let desc = options.desc
  24. this.setData({
  25. storeid: storeid,
  26. tableid: tableid,
  27. active: active,
  28. desc: desc
  29. })
  30. await this.getCategory(storeid)
  31. },
  32. async getCategory(storeid) {
  33. let Category = new Parse.Query('Category')
  34. Category.notEqualTo('isDeleted', "true")
  35. Category.equalTo('company', company)
  36. Category.equalTo('store', storeid)
  37. Category.equalTo('type', 'catering')
  38. Category.ascending('index')
  39. let cates = await Category.find()
  40. if (cates && cates.length > 0) {
  41. let cateJSON = []
  42. cates.forEach(c => {
  43. cateJSON.push(c.toJSON())
  44. });
  45. this.setData({
  46. cates: cateJSON
  47. })
  48. await this.getFoodSet(storeid)
  49. }
  50. },
  51. async getFoodSet(storeid) {
  52. let FoodSet = new Parse.Query('Food')
  53. FoodSet.equalTo('store', storeid)
  54. FoodSet.equalTo('company', company)
  55. FoodSet.ascending('price')
  56. FoodSet.limit(200)
  57. let foodList = await FoodSet.find()
  58. console.log(foodList, this.data.cates)
  59. let countMap = {}
  60. if (foodList && foodList.length > 0) {
  61. let cates = this.data.cates
  62. cates.forEach(c => {
  63. c.foodList = []
  64. foodList.forEach(f => {
  65. let fJSON = f.toJSON()
  66. if (fJSON.category.objectId == c.objectId) {
  67. c.foodList.push(fJSON)
  68. }
  69. })
  70. })
  71. console.log(cates, countMap)
  72. this.setData({
  73. cates: cates,
  74. })
  75. }
  76. },
  77. discount(e) {
  78. let food = e.currentTarget.dataset.food
  79. if (this.data.countMap[food.objectId] == 0) {
  80. return
  81. }
  82. let count = this.data.countMap[food.objectId] ? this.data.countMap[food.objectId] - 1 : 0
  83. let tem = `countMap.${food.objectId}`
  84. let price = food.price
  85. this.setData({
  86. [tem]: count,
  87. totalCount: this.data.totalCount - 1,
  88. totalPrice: Number((this.data.totalPrice - price).toFixed(2))
  89. })
  90. },
  91. addcount(e) {
  92. let food = e.currentTarget.dataset.food
  93. let count = this.data.countMap[food.objectId] ? this.data.countMap[food.objectId] + 1 : 1
  94. let price = food.price
  95. let tem = `countMap.${food.objectId}`
  96. this.setData({
  97. [tem]: count,
  98. totalCount: this.data.totalCount + 1,
  99. totalPrice: Number((this.data.totalPrice + price).toFixed(2))
  100. })
  101. console.log(this.data.countMap)
  102. },
  103. tabNav(e) {
  104. let index = e.currentTarget.dataset.index;
  105. console.log(index)
  106. this.setData({
  107. tabCur: index,
  108. rightCur: index,
  109. // 实现左边自动滑动到某个位置 4表示自动滑动到 第五项 (4为索引值)
  110. // scrollTops: (index - 4) * 50
  111. })
  112. },
  113. /**
  114. * 滑动右边对应左边菜单切换
  115. * 1、拿到该元素的高度,设定它的top和bottom
  116. * 2、判断滑动的距离是否大于 设定的top并小于设定的bottom,然后对应左边菜单的滑动
  117. */
  118. scrollLink(e) {
  119. let list = this.data.cates
  120. let itemHeight = 0;
  121. for (let i = 0; i < list.length; i++) {
  122. //拿到每个元素
  123. let els = wx.createSelectorQuery().select("#scroll-" + i);
  124. els.fields({
  125. size: true
  126. }, function (res) {
  127. if (res) {
  128. list[i].top = itemHeight;
  129. itemHeight += res.height;
  130. list[i].bottom = itemHeight
  131. }
  132. }).exec()
  133. }
  134. console.log(list)
  135. this.setData({
  136. list
  137. })
  138. // 拿到滚动的高度
  139. let scrollTop = e.detail.scrollTop;
  140. console.log(scrollTop)
  141. for (let i = 0; i < list.length; i++) {
  142. if (scrollTop > list[i].top && scrollTop + 675 < list[i].bottom) {
  143. this.setData({
  144. tabCur: i,
  145. scrollTops: (i - 4) * 50
  146. })
  147. return false
  148. }
  149. }
  150. },
  151. async toSubmit() {
  152. console.log(this.data.totalCount, this.data.totalPrice)
  153. if (!this.data.totalPrice || !this.data.totalCount) {
  154. return
  155. }
  156. let now = new Date()
  157. let user = Parse.User.current()
  158. let tradeNo = "C" +
  159. String(now.getFullYear()) +
  160. (now.getMonth() + 1) +
  161. now.getDate() +
  162. now.getHours() +
  163. now.getMinutes() +
  164. now.getSeconds() +
  165. Math.random().toString().slice(-6); //生成六位随机数
  166. let specMapArr = Object.keys(this.data.countMap)
  167. let foods = []
  168. specMapArr.forEach(id => {
  169. if (this.data.countMap[id]) {
  170. foods.push({
  171. __type: "Pointer",
  172. className: "Food",
  173. objectId: id
  174. })
  175. }
  176. })
  177. let FoodOrder = Parse.Object.extend('FoodOrder')
  178. let foodOrder = new FoodOrder()
  179. foodOrder.set('totalPrice', this.data.totalPrice)
  180. foodOrder.set('tradeNo', tradeNo)
  181. foodOrder.set('state', '100')
  182. foodOrder.set('type', "meal")
  183. foodOrder.set('isPay', false)
  184. foodOrder.set('merber', Number(this.data.active))
  185. foodOrder.set('foods', foods)
  186. foodOrder.set('specMap', this.data.countMap)
  187. foodOrder.set('user', {
  188. __type: "Pointer",
  189. className: "_User",
  190. objectId: user.id
  191. })
  192. foodOrder.set('company', {
  193. __type: "Pointer",
  194. className: "Company",
  195. objectId: company
  196. })
  197. foodOrder.set('store', {
  198. __type: "Pointer",
  199. className: "ShopStore",
  200. objectId: this.data.storeid
  201. })
  202. foodOrder.set('table', {
  203. __type: "Pointer",
  204. className: "TableNumber",
  205. objectId: this.data.tableid
  206. })
  207. console.log(1111)
  208. let order = await foodOrder.save()
  209. if (order && order.id) {
  210. this.setData({
  211. order: order,
  212. tradeNo: tradeNo,
  213. show: true
  214. })
  215. }
  216. },
  217. async acceptResult(e) {
  218. let { activeOrder } = this.data
  219. let that = this
  220. let {
  221. params,
  222. no
  223. } = e.detail;
  224. that.setData({
  225. show: false
  226. })
  227. try {
  228. if (params == "ok") {
  229. let order = this.data.order
  230. order.set('state', '400')
  231. order.set('isPay', true)
  232. await order.save()
  233. wx.showToast({
  234. title: '购买成功',
  235. icon: 'none',
  236. image: '',
  237. duration: 1500,
  238. mask: false,
  239. });
  240. wx.navigateBack({
  241. delta: 2,
  242. })
  243. } else {
  244. wx.showToast({
  245. title: '支付失败,取消订单',
  246. icon: 'none',
  247. image: '',
  248. duration: 1500,
  249. mask: false,
  250. });
  251. }
  252. } catch (error) {
  253. console.log(error)
  254. wx.showToast({
  255. title: "支付失败",
  256. icon: "error",
  257. duration: 1500,
  258. });
  259. wx.hideLoading()
  260. }
  261. },
  262. /**
  263. * 生命周期函数--监听页面加载
  264. */
  265. /**
  266. * 生命周期函数--监听页面初次渲染完成
  267. */
  268. onReady: function () {
  269. },
  270. /**
  271. * 生命周期函数--监听页面显示
  272. */
  273. onShow: function () {
  274. },
  275. /**
  276. * 生命周期函数--监听页面隐藏
  277. */
  278. onHide: function () {
  279. },
  280. /**
  281. * 生命周期函数--监听页面卸载
  282. */
  283. onUnload: function () {
  284. },
  285. /**
  286. * 页面相关事件处理函数--监听用户下拉动作
  287. */
  288. onPullDownRefresh: function () {
  289. },
  290. /**
  291. * 页面上拉触底事件的处理函数
  292. */
  293. onReachBottom: function () {
  294. },
  295. /**
  296. * 用户点击右上角分享
  297. */
  298. onShareAppMessage: function () {
  299. }
  300. })