index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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: function (e) {
  46. console.log('picker发送选择改变,携带值为', e.detail.value)
  47. this.setData({ today: e.detail.value })
  48. this.searchDate();
  49. this.searchOrder();
  50. },
  51. // 禁止左右滑动
  52. stopTab: function (e) {
  53. return false
  54. },
  55. //点击切换
  56. clickTab: function (e) {
  57. var that = this;
  58. if (this.data.currentTab === e.target.dataset.current) {
  59. return false;
  60. } else {
  61. that.setData({
  62. currentTab: e.target.dataset.current
  63. })
  64. }
  65. },
  66. // 点击选择餐食
  67. infoClickTab: function (e) {
  68. var that = this;
  69. let data = e.target.dataset.current;
  70. if (this.data.infoTab === data) return false;
  71. else that.setData({ infoTab: data });
  72. },
  73. // 选择餐数量
  74. // 早餐,午餐,晚餐
  75. oneChange: function (e) {
  76. console.log(e);
  77. const { data, type } = e.target.dataset
  78. const { detail } = e;
  79. this.menuNumOpera(type, detail, data._id)
  80. },
  81. // 增加
  82. onePlus: function (e) {
  83. console.log('增加');
  84. let data = e.target.dataset.data;
  85. let type = this.data.infoTab;
  86. let meal;
  87. let mw;
  88. if (type == 0) {//早餐
  89. mw = 'breakfast';
  90. meal = this.data.form.breakfast;
  91. // 计算卡路里
  92. let reserve = meal.reserve + data.reserve;
  93. this.setData({ 'form.breakfast.reserve': reserve })
  94. } else if (type == 1) {//午餐
  95. mw = 'lunch';
  96. meal = this.data.form.lunch;
  97. // 计算卡路里
  98. let reserve = meal.reserve + data.reserve;
  99. this.setData({ 'form.lunch.reserve': reserve })
  100. } else if (type == 2) {//晚餐
  101. mw = 'dinner';
  102. meal = this.data.form.dinner;
  103. // 计算卡路里
  104. let reserve = meal.reserve + data.reserve;
  105. this.setData({ 'form.dinner.reserve': reserve })
  106. }
  107. // 查询下标
  108. let res = meal.list.findIndex(i => i.id === data.id);
  109. // 查询数据
  110. let arr = meal.list.find(i => i.id === data.id);
  111. let onum = 0;
  112. if (arr) {//已有值
  113. onum = arr.num + 1;
  114. let qwe = { ...arr, num: onum } //id: arr.id, title: arr.title
  115. meal.list.splice(res, 1, qwe)
  116. } else {//没有值
  117. console.log(data)
  118. let onum = 1;
  119. let arr = { ...data, num: onum } //id: data.id, title: data.title
  120. meal.list.push(arr)
  121. }
  122. },
  123. /**
  124. * 更新菜单的数量
  125. * @param {String} type 三餐的类型
  126. * @param {Number} num 当前的数量
  127. * @param {String} id 指定菜品
  128. */
  129. menuNumOpera(type, num, id) {
  130. let list = this.data[`${type}List`];
  131. const li = list.findIndex(f => f._id === id)
  132. const ld = list.find(f => f._id === id)
  133. if (ld) {
  134. ld.num = num;
  135. list.splice(li, 1, ld)
  136. this.setData({
  137. [`${type}List`]: list
  138. })
  139. }
  140. },
  141. // 减少
  142. oneMinus: function (e) {
  143. console.log('减少');
  144. let data = e.target.dataset.data;
  145. let type = this.data.infoTab;
  146. let meal;
  147. let mw;
  148. if (type == 0) {//早餐
  149. mw = 'breakfast';
  150. meal = this.data.form.breakfast;
  151. // 计算减少卡路里
  152. let reserve = meal.reserve - data.reserve;
  153. this.setData({ 'form.breakfast.reserve': reserve })
  154. } else if (type == 1) {//午餐
  155. mw = 'lunch';
  156. meal = this.data.form.lunch;
  157. // 计算减少卡路里
  158. let reserve = meal.reserve - data.reserve;
  159. this.setData({ 'form.lunch.reserve': reserve })
  160. } else if (type == 2) {//晚餐
  161. mw = 'dinner';
  162. meal = this.data.form.dinner;
  163. // 计算减少卡路里
  164. let reserve = meal.reserve - data.reserve;
  165. this.setData({ 'form.dinner.reserve': reserve })
  166. }
  167. // 查询下标
  168. let res = meal.list.findIndex(i => i.id === data.id);
  169. // 查询数据
  170. let arr = meal.list.find(i => i.id === data.id);
  171. if (arr) {
  172. if (arr.num - 1 <= 0) meal.list.splice(res, 1)
  173. else {
  174. let qwe = { ...arr, num: arr.num - 1 }
  175. meal.list.splice(res, 1, qwe)
  176. }
  177. }
  178. // this.menuNumOpera(mw, onum, data._id)
  179. },
  180. // 提交
  181. onSubmit: async function () {
  182. const data = JSON.parse(JSON.stringify(this.data.form))
  183. if (app.globalData.wxInfo) data.openid = app.globalData.wxInfo.openid;
  184. if (this.data.today) data.date = this.data.today;
  185. let url;
  186. if (data._id) url = `${app.globalData.publicUrl}/api/st/dining/order/update/${data._id}`
  187. else url = `${app.globalData.publicUrl}/api/st/dining/order`;
  188. const res = await app.$post(uri, data);
  189. console.log(res);
  190. // wx.request({
  191. // url,
  192. // method: "post",
  193. // header: {
  194. // 'x-tenant': app.globalData.tenant
  195. // },
  196. // data,
  197. // success: res => {
  198. // if (res.data.errcode == 0) {
  199. // wx.showToast({
  200. // title: '完成点餐',
  201. // })
  202. // }
  203. // },
  204. // error: err => {
  205. // wx.showToast({
  206. // title: err.msg,
  207. // icon: 'error'
  208. // })
  209. // }
  210. // })
  211. },
  212. /**
  213. * 生命周期函数--监听页面加载
  214. */
  215. onLoad: function (options) {
  216. this.searchST();
  217. let today = moment().add(1, 'days').format('YYYY-MM-DD');
  218. let endday = moment().add(1, 'months').format('YYYY-MM-DD');
  219. this.setData({ today: today, picker: { start: today, end: endday } })
  220. this.searchDate();
  221. this.searchOrder();
  222. },
  223. // 查询时间
  224. searchDate: async function () {
  225. let today = this.data.today;
  226. const res = await app.$get(`${app.globalData.publicUrl}/api/st/dining/arrange/getByDate?date=${today}`)
  227. console.log(res);
  228. const { arrange } = res.data;
  229. if (!arrange) return;
  230. let { breakfast, lunch, dinner } = arrange
  231. breakfast = this.dealImg(breakfast)
  232. lunch = this.dealImg(lunch)
  233. dinner = this.dealImg(dinner)
  234. this.setData({
  235. breakfastList: breakfast,
  236. lunchList: lunch,
  237. dinnerList: dinner
  238. })
  239. // wx.request({
  240. // url: `${app.globalData.publicUrl}/api/st/dining/arrange/getByDate?date=${today}`,
  241. // method: "get",
  242. // header: {
  243. // 'x-tenant': app.globalData.tenant
  244. // },
  245. // success: res => {
  246. // const { arrange } = res.data.data;
  247. // if (!arrange) return;
  248. // let { breakfast, lunch, dinner } = arrange
  249. // breakfast = this.dealImg(breakfast)
  250. // lunch = this.dealImg(lunch)
  251. // dinner = this.dealImg(dinner)
  252. // this.setData({
  253. // breakfastList: breakfast,
  254. // lunchList: lunch,
  255. // dinnerList: dinner
  256. // })
  257. // },
  258. // error: err => {
  259. // wx.showToast({
  260. // title: err.msg,
  261. // icon: 'error'
  262. // })
  263. // }
  264. // })
  265. },
  266. dealImg(list) {
  267. for (let i of list) {
  268. if (i.img && i.img.length > 0 && i.img[0]) i.url = `${app.globalData.fileUrl}${i.img[0].url}`;
  269. else i.url = this.data.logo;
  270. }
  271. return list;
  272. },
  273. searchST: async function () {
  274. wx.request({
  275. url: `${app.globalData.publicUrl}/api/st/system/tenant/getTenant/${app.globalData.tenant}`,
  276. method: "get",
  277. header: { 'x-tenant': app.globalData.tenant },
  278. data: {},
  279. success: res => {
  280. const { data } = res.data;
  281. this.setData({ tenant: data.name });
  282. this.setData({ logo: `${app.globalData.fileUrl}` + data.img.logo })
  283. },
  284. error: err => {
  285. wx.showToast({
  286. title: err.msg,
  287. icon: 'error'
  288. })
  289. }
  290. })
  291. },
  292. // 查订单
  293. async searchOrder() {
  294. let today = this.data.today;
  295. let openid = app.globalData.wxInfo.openid;
  296. const res = await app.$get(`${app.globalData.publicUrl}/api/st/dining/order/getByOpenid?date=${today}&openid=${openid}`)
  297. const { data } = res
  298. if (!data) return
  299. this.setData({
  300. form: res.data,
  301. })
  302. const robj = res.data
  303. if (robj.breakfast && robj.breakfast.list && robj.breakfast.list.length > 0) this.dealOrderToMenu('breakfast', robj.breakfast.list)
  304. if (robj.lunch && robj.lunch.list && robj.lunch.list.length > 0) this.dealOrderToMenu('lunch', robj.lunch.list)
  305. if (robj.dinner && robj.dinner.list && robj.dinner.list.length > 0) this.dealOrderToMenu('dinner', robj.dinner.list)
  306. // wx.request({
  307. // url: `${app.globalData.publicUrl}/api/st/dining/order/getByOpenid?date=${today}&openid=${openid}`,
  308. // method: "get",
  309. // header: {
  310. // 'x-tenant': app.globalData.tenant
  311. // },
  312. // success: res => {
  313. // const { data } = res.data
  314. // if (!data) return
  315. // this.setData({
  316. // form: res.data.data,
  317. // })
  318. // const robj = res.data.data
  319. // if (robj.breakfast && robj.breakfast.list && robj.breakfast.list.length > 0) this.dealOrderToMenu('breakfast', robj.breakfast.list)
  320. // if (robj.lunch && robj.lunch.list && robj.lunch.list.length > 0) this.dealOrderToMenu('lunch', robj.lunch.list)
  321. // if (robj.dinner && robj.dinner.list && robj.dinner.list.length > 0) this.dealOrderToMenu('dinner', robj.dinner.list)
  322. // },
  323. // error: err => {
  324. // wx.showToast({
  325. // title: err.msg,
  326. // icon: 'error'
  327. // })
  328. // }
  329. // })
  330. },
  331. /**
  332. * 将点过的单还原,继续修改
  333. * @param {String} type 三餐类型
  334. * @param {Array} list 某餐的内容
  335. */
  336. dealOrderToMenu(type, list) {
  337. console.log(type, list)
  338. if (list.length <= 0) return;
  339. const menu = this.data[`${type}List`]
  340. if (!menu) return;
  341. for (const o of list) {
  342. const { num, _id } = o
  343. this.menuNumOpera(type, num, _id)
  344. }
  345. },
  346. /**
  347. * 生命周期函数--监听页面初次渲染完成
  348. */
  349. onReady: function () {
  350. },
  351. /**
  352. * 生命周期函数--监听页面显示
  353. */
  354. onShow: function () {
  355. if (typeof this.getTabBar === 'function' &&
  356. this.getTabBar()) {
  357. this.getTabBar().setData({
  358. selected: 1
  359. })
  360. }
  361. },
  362. /**
  363. * 生命周期函数--监听页面隐藏
  364. */
  365. onHide: function () {
  366. },
  367. /**
  368. * 生命周期函数--监听页面卸载
  369. */
  370. onUnload: function () {
  371. },
  372. /**
  373. * 页面相关事件处理函数--监听用户下拉动作
  374. */
  375. onPullDownRefresh: function () {
  376. },
  377. /**
  378. * 页面上拉触底事件的处理函数
  379. */
  380. onReachBottom: function () {
  381. },
  382. /**
  383. * 用户点击右上角分享
  384. */
  385. onShareAppMessage: function () {
  386. }
  387. })