index.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // pages/reserve/index.js
  2. import moment, { parseTwoDigitYear } from '../../utils/moment.min';
  3. moment.locale('en', {
  4. longDateFormat: {
  5. l: 'YYYY-MM-DD',
  6. L: 'YYYY-MM-DD HH:mm:ss',
  7. },
  8. });
  9. const app = getApp();
  10. Page({
  11. /**
  12. * 页面的初始数据
  13. */
  14. data: {
  15. height: app.globalData.height * 2 + 25,
  16. windowHeight: app.globalData.windowHeight,
  17. navbarData: {
  18. name: '报餐',
  19. },
  20. tenant: '',
  21. logo: '',
  22. today: '',
  23. // 点餐
  24. currentTab: 0,
  25. // 早餐,午餐,晚餐
  26. infoTab: '0',
  27. // 餐列表
  28. breakfastList: [],
  29. lunchList: [],
  30. dinnerList: [],
  31. // 餐数量
  32. oneStepper: 0,
  33. twoStepper: 0,
  34. thrStepper: 0,
  35. // 选餐表单
  36. form: {
  37. breakfast: { reserve: 0, list: [] },
  38. lunch: { reserve: 0, list: [] },
  39. dinner: { reserve: 0, list: [] },
  40. },
  41. // 时间选择范围
  42. picker: {},
  43. },
  44. // 选择日期
  45. bindDateChange: async function (e) {
  46. this.setData({ today: e.detail.value });
  47. await this.searchDate();
  48. await this.searchOrder();
  49. },
  50. // 禁止左右滑动
  51. stopTab: function (e) {
  52. return false;
  53. },
  54. //点击切换
  55. clickTab: function (e) {
  56. var that = this;
  57. if (this.data.currentTab === e.target.dataset.current) {
  58. return false;
  59. } else {
  60. that.setData({
  61. currentTab: e.target.dataset.current,
  62. });
  63. }
  64. },
  65. // 点击选择餐食
  66. infoClickTab: function (e) {
  67. var that = this;
  68. let data = e.target.dataset.current;
  69. if (this.data.infoTab === data) return false;
  70. else that.setData({ infoTab: data });
  71. },
  72. // 选择餐数量
  73. // 早餐,午餐,晚餐
  74. oneChange: function (e) {
  75. const { data, type } = e.target.dataset;
  76. const { detail } = e;
  77. this.menuNumOpera(type, detail, data._id);
  78. },
  79. // 增加
  80. onePlus: function (e) {
  81. let data = e.target.dataset.data;
  82. let type = this.data.infoTab;
  83. let initMealData = { list: [], reserve: 0 };
  84. let meal;
  85. let mw;
  86. switch (type) {
  87. case '0':
  88. mw = 'breakfast';
  89. meal = this.data.form.breakfast;
  90. break;
  91. case '1':
  92. mw = 'lunch';
  93. meal = this.data.form.lunch;
  94. break;
  95. case '2':
  96. mw = 'dinner';
  97. meal = this.data.form.dinner;
  98. break;
  99. default:
  100. break;
  101. }
  102. if (!meal) meal = initMealData;
  103. // 计算卡路里
  104. meal.reserve = meal.reserve + data.reserve;
  105. // 查询下标
  106. let res = meal.list.findIndex((i) => i._id === data._id);
  107. // 查询数据
  108. let arr = meal.list.find((i) => i._id === data._id);
  109. let onum = 0;
  110. if (arr) {
  111. //已有值
  112. onum = arr.num + 1;
  113. let qwe = { ...arr, num: onum }; //id: arr.id, title: arr.title
  114. meal.list.splice(res, 1, qwe);
  115. } else {
  116. //没有值
  117. let onum = 1;
  118. let arr = { ...data, num: onum }; //id: data.id, title: data.title
  119. meal.list.push(arr);
  120. }
  121. const key = `form.${mw}`;
  122. this.setData({ [key]: meal });
  123. this.computedTotalReserve();
  124. },
  125. /**
  126. * 更新菜单的数量
  127. * @param {String} type 三餐的类型
  128. * @param {Number} num 当前的数量
  129. * @param {String} id 指定菜品
  130. */
  131. menuNumOpera(type, num, id) {
  132. let list = this.data[`${type}List`];
  133. const li = list.findIndex((f) => f._id === id);
  134. const ld = list.find((f) => f._id === id);
  135. if (ld) {
  136. ld.num = num;
  137. list.splice(li, 1, ld);
  138. this.setData({
  139. [`${type}List`]: list,
  140. });
  141. }
  142. },
  143. // 减少
  144. oneMinus: function (e) {
  145. let data = e.target.dataset.data;
  146. let type = this.data.infoTab;
  147. let initMealData = { list: [], reserve: 0 };
  148. let meal;
  149. let mw;
  150. switch (type) {
  151. case '0':
  152. mw = 'breakfast';
  153. meal = this.data.form.breakfast;
  154. break;
  155. case '1':
  156. mw = 'lunch';
  157. meal = this.data.form.lunch;
  158. break;
  159. case '2':
  160. mw = 'dinner';
  161. meal = this.data.form.dinner;
  162. break;
  163. default:
  164. break;
  165. }
  166. if (!meal) meal = initMealData;
  167. // 计算卡路里
  168. meal.reserve = meal.reserve - data.reserve;
  169. // 查询下标
  170. let res = meal.list.findIndex((i) => i._id === data._id);
  171. // 查询数据
  172. let arr = meal.list.find((i) => i._id === data._id);
  173. if (arr) {
  174. if (arr.num - 1 <= 0) meal.list.splice(res, 1);
  175. else {
  176. let qwe = { ...arr, num: arr.num - 1 };
  177. meal.list.splice(res, 1, qwe);
  178. }
  179. }
  180. const key = `form.${mw}`;
  181. this.setData({ [key]: meal });
  182. this.computedTotalReserve();
  183. },
  184. computedTotalReserve() {
  185. const form = this.data.form;
  186. const br = form.breakfast && form.breakfast.reserve ? form.breakfast.reserve : 0;
  187. const lr = form.lunch && form.lunch.reserve ? form.lunch.reserve : 0;
  188. const dr = form.dinner && form.dinner.reserve ? form.dinner.reserve : 0;
  189. const total = br + lr + dr;
  190. this.setData({ totalReserve: total });
  191. },
  192. // 提交
  193. onSubmit: async function () {
  194. const data = JSON.parse(JSON.stringify(this.data.form));
  195. if (app.globalData.wxInfo) data.openid = app.globalData.wxInfo.openid;
  196. if (this.data.today) data.date = this.data.today;
  197. let url;
  198. if (data._id) url = `/order/update/${data._id}`;
  199. else url = `/order`;
  200. const res = await app.$post(url, data);
  201. if (res.errcode === 0) wx.showToast({ title: '点餐成功', icon: 'success' });
  202. },
  203. /**
  204. * 生命周期函数--监听页面加载
  205. */
  206. onLoad: async function (options) {
  207. this.searchST();
  208. let today = moment().add(1, 'days').format('YYYY-MM-DD');
  209. let endday = moment().add(1, 'months').format('YYYY-MM-DD');
  210. this.setData({ today: today, picker: { start: today, end: endday } });
  211. await this.searchDate();
  212. await this.searchOrder();
  213. },
  214. // 查询时间
  215. searchDate: async function () {
  216. let today = this.data.today;
  217. const res = await app.$get(`/arrange/getByDate?date=${today}`);
  218. const { arrange } = res.data;
  219. if (!arrange) return;
  220. let { breakfast, lunch, dinner } = arrange;
  221. breakfast = this.dealImg(breakfast);
  222. lunch = this.dealImg(lunch);
  223. dinner = this.dealImg(dinner);
  224. this.setData({
  225. breakfastList: breakfast,
  226. lunchList: lunch,
  227. dinnerList: dinner,
  228. });
  229. },
  230. dealImg(list) {
  231. for (let i of list) {
  232. if (i.img && i.img.length > 0 && i.img[0]) i.url = `${i.img[0].url}`;
  233. else i.url = this.data.logo;
  234. }
  235. return list;
  236. },
  237. searchST: async function () {
  238. const res = await app.$get('/config');
  239. const logo = `${res.data.logo[0].url || ''}`;
  240. this.setData({ logo });
  241. // wx.request({
  242. // url: `${app.globalData.publicUrl}/api/st/system/tenant/getTenant/${app.globalData.tenant}`,
  243. // method: "get",
  244. // data: {},
  245. // success: res => {
  246. // const { data } = res.data;
  247. // this.setData({ tenant: data.name });
  248. // this.setData({ logo: `${app.globalData.fileUrl}` + data.img.logo })
  249. // },
  250. // error: err => {
  251. // wx.showToast({
  252. // title: err.msg,
  253. // icon: 'error'
  254. // })
  255. // }
  256. // })
  257. },
  258. // 查订单
  259. async searchOrder() {
  260. let today = this.data.today;
  261. let openid = app.globalData.wxInfo.openid;
  262. const res = await app.$get(`/order/getByOpenid?date=${today}&openid=${openid}`);
  263. const { data } = res;
  264. if (!data) return;
  265. this.setData({
  266. form: res.data,
  267. });
  268. const robj = res.data;
  269. if (robj.breakfast && robj.breakfast.list && robj.breakfast.list.length > 0) this.dealOrderToMenu('breakfast', robj.breakfast.list);
  270. if (robj.lunch && robj.lunch.list && robj.lunch.list.length > 0) this.dealOrderToMenu('lunch', robj.lunch.list);
  271. if (robj.dinner && robj.dinner.list && robj.dinner.list.length > 0) this.dealOrderToMenu('dinner', robj.dinner.list);
  272. this.computedTotalReserve();
  273. },
  274. /**
  275. * 将点过的单还原,继续修改
  276. * @param {String} type 三餐类型
  277. * @param {Array} list 某餐的内容
  278. */
  279. dealOrderToMenu(type, list) {
  280. if (list.length <= 0) return;
  281. const menu = this.data[`${type}List`];
  282. if (!menu) {
  283. console.error('没找到菜单');
  284. return;
  285. }
  286. for (const o of list) {
  287. const { num, _id } = o;
  288. this.menuNumOpera(type, num, _id);
  289. }
  290. },
  291. /**
  292. * 生命周期函数--监听页面初次渲染完成
  293. */
  294. onReady: function () {},
  295. /**
  296. * 生命周期函数--监听页面显示
  297. */
  298. onShow: function () {
  299. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  300. this.getTabBar().setData({
  301. selected: 1,
  302. });
  303. }
  304. },
  305. /**
  306. * 生命周期函数--监听页面隐藏
  307. */
  308. onHide: function () {},
  309. /**
  310. * 生命周期函数--监听页面卸载
  311. */
  312. onUnload: function () {},
  313. /**
  314. * 页面相关事件处理函数--监听用户下拉动作
  315. */
  316. onPullDownRefresh: function () {},
  317. /**
  318. * 页面上拉触底事件的处理函数
  319. */
  320. onReachBottom: function () {},
  321. /**
  322. * 用户点击右上角分享
  323. */
  324. onShareAppMessage: function () {},
  325. });