detail.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // pages/market_buy/detail.js
  2. import WxValidate from '../../utils/wxValidate';
  3. const app = getApp();
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. frameStyle: { useTop: true, name: '信息管理', leftArrow: true, useBar: false },
  10. // 主体高度
  11. infoHeight: '',
  12. userInfo: {},
  13. // 上架的商品
  14. marketList: [],
  15. // 类型
  16. typeList: [],
  17. // 详细信息
  18. info: {},
  19. // 弹框
  20. dialog: { title: '详细信息', show: false, type: '1' },
  21. // 选购
  22. formList: [],
  23. list: [],
  24. // 收货信息
  25. receiveForm: {}
  26. },
  27. initValidate() {
  28. const rules = { receive_user_name: { required: true, }, receive_user_phone: { required: true, }, receive_address: { required: true, } }
  29. // 验证字段的提示信息,若不传则调用默认的信息
  30. const messages = { receive_user_name: { required: '请输入收货人', }, receive_user_phone: { required: '请输入收货电话', }, receive_address: { required: '请输入收货地址', } };
  31. this.WxValidate = new WxValidate(rules, messages)
  32. },
  33. back: function () {
  34. wx.navigateBack({ url: '/pages/home/index' })
  35. },
  36. // 详细信息
  37. toView: function (e) {
  38. let item = e.currentTarget.dataset.item;
  39. this.setData({ info: item })
  40. this.setData({ dialog: { title: '详细信息', show: true, type: '1' } })
  41. },
  42. // 添加
  43. toPlus: function (e) {
  44. let p1 = this.data.formList;
  45. // 商品
  46. let { item } = e.currentTarget.dataset;
  47. let data = { user_id: this.data.userInfo.id, user_name: this.data.userInfo.name, user_phone: this.data.userInfo.phone, order: [] };
  48. // 查询是否已有订单
  49. if (p1.length > 0) {
  50. // 查询供货单位是否已经加入商品
  51. let p2 = p1.find(i => i.mech_id == item.mech_id);
  52. if (p2) {
  53. // 查找该商品是否已经加入订单
  54. let p3 = p2.order.find(i => i.id == item.id);
  55. if (p3) {
  56. p3.num = p3.num + 1;
  57. p2.money = Number(p2.money) + Number(p3.money);
  58. }
  59. else {
  60. item.num = 1;
  61. p2.money = Number(p2.money) + Number(item.money); p2.order.push(item);
  62. }
  63. } else {
  64. item.num = 1; data.mech_id = item.mech_id; data.mech_name = item.mech_name; data.money = item.money * item.num; data.order.push(item); p1.push(data);
  65. }
  66. } else {
  67. item.num = 1; data.mech_id = item.mech_id; data.mech_name = item.mech_name; data.money = item.money * item.num; data.order.push(item); p1.push(data);
  68. }
  69. },
  70. // 減少
  71. toMinus: function (e) {
  72. let p1 = this.data.formList;
  73. // 商品
  74. let { item } = e.currentTarget.dataset;
  75. let p2 = p1.find(i => i.mech_id == item.mech_id);
  76. if (p2) {
  77. p2.money = p2.money - item.money;
  78. if (p2 && p2.money === 0) {
  79. // 获取索引值,删除指定无商品商家信息
  80. var key = p1.indexOf(p2.mech_id);
  81. p1.splice(key, 1);
  82. } else {
  83. let p3 = p2.order.find(i => i.id == item.id);
  84. if (p3) p3.num = p3.num - 1;
  85. if (p3 && p3.num === 0) p3 = {};
  86. }
  87. }
  88. },
  89. // 查看选购
  90. toBuy: function () {
  91. this.setData({ list: this.data.formList })
  92. this.setData({ dialog: { title: '查看选购', show: true, type: '2' } })
  93. },
  94. // 确认选购
  95. toConfirm: function () {
  96. this.setData({ dialog: { title: '收货信息', show: true, type: '3' } })
  97. },
  98. // 提交订单
  99. onSubmit: function (e) {
  100. const params = e.detail.value;
  101. if (!this.WxValidate.checkForm(params)) {
  102. const error = this.WxValidate.errorList[0];
  103. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  104. return false
  105. } else {
  106. wx.showModal({
  107. title: '是否确认提交订单',
  108. success(res) {
  109. if (res.confirm) {
  110. wx.showToast({ title: `提交订单成功`, icon: 'success', duration: 2000 });
  111. } else if (res.cancel) {
  112. }
  113. }
  114. })
  115. }
  116. },
  117. /**
  118. * 生命周期函数--监听页面加载
  119. */
  120. onLoad: function (options) {
  121. //验证规则函数
  122. this.initValidate();
  123. this.watchLogin();
  124. // 计算高度
  125. this.searchHeight();
  126. },
  127. // 监听用户是否登录
  128. watchLogin: function () {
  129. let data = [
  130. { id: '001', name: '喜羊羊', money: '20', type_name: '羊肉', type_id: '111', brief: '一份', mech_id: '000000001', mech_name: '羊村', img_one: '', img_url: [], status: '1' },
  131. { id: '002', name: '美羊羊', money: '30', type_name: '羊肉', type_id: '222', brief: '一份', mech_id: '000000001', mech_name: '羊村' },
  132. { id: '003', name: '懒羊羊', money: '15', type_name: '羊肉', type_id: '333', brief: '一份', mech_id: '000000001', mech_name: '羊村' },
  133. { id: '004', name: '沸羊羊', money: '25', type_name: '羊肉', type_id: '444', brief: '一份', mech_id: '000000001', mech_name: '羊村' },
  134. { id: '005', name: '慢羊羊', money: '26', type_name: '羊肉', type_id: '555', brief: '一份', mech_id: '000000001', mech_name: '羊村' },
  135. { id: '006', name: '暖羊羊', money: '27', type_name: '羊肉', type_id: '666', brief: '一份', mech_id: '000000001', mech_name: '羊村' }
  136. ]
  137. let info = { id: '00038494480001', user_id: '00038494480001', name: '法外狂徒张三', phone: '1239999999' };
  138. this.setData({ marketList: data })
  139. this.setData({ userInfo: info })
  140. this.setData({ 'receiveForm.receive_user_name': info.name, 'receiveForm.receive_user_id': info.user_id, 'receiveForm.receive_user_phone': info.phone, })
  141. // wx.getStorage({
  142. // key: 'user',
  143. // success: res => {
  144. // if (res.data) {
  145. // // 查询菜单
  146. // if (res.data) this.searchRouter(res.data);
  147. // res.data.type = type.find((i) => i.value == res.data.type).label;
  148. // if (res.data) this.setData({ userInfo: res.data });
  149. // if (res.data && res.data.avatarUrl) this.setData({ avatarUrl: res.data.avatarUrl });
  150. // } else {
  151. // wx.redirectTo({ url: '/pages/login/index', })
  152. // }
  153. // }
  154. // })
  155. },
  156. // 计算高度
  157. searchHeight: function () {
  158. let frameStyle = this.data.frameStyle;
  159. let client = app.globalData.client;
  160. // 减去状态栏
  161. let infoHeight = client.windowHeight - (client.statusBarHeight + client.getMenu.height + (client.getMenu.top - client.statusBarHeight) * 2);
  162. // 是否减去底部菜单
  163. if (frameStyle.useBar) infoHeight = infoHeight - 50;
  164. if (infoHeight) this.setData({ infoHeight: infoHeight })
  165. },
  166. /**
  167. * 生命周期函数--监听页面初次渲染完成
  168. */
  169. onReady: function () {
  170. },
  171. /**
  172. * 生命周期函数--监听页面显示
  173. */
  174. onShow: function () {
  175. },
  176. /**
  177. * 生命周期函数--监听页面隐藏
  178. */
  179. onHide: function () {
  180. },
  181. /**
  182. * 生命周期函数--监听页面卸载
  183. */
  184. onUnload: function () {
  185. },
  186. /**
  187. * 页面相关事件处理函数--监听用户下拉动作
  188. */
  189. onPullDownRefresh: function () {
  190. },
  191. /**
  192. * 页面上拉触底事件的处理函数
  193. */
  194. onReachBottom: function () {
  195. },
  196. /**
  197. * 用户点击右上角分享
  198. */
  199. onShareAppMessage: function () {
  200. }
  201. })