menu.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. // pages/menu/menu.js
  2. import {
  3. imgUrl
  4. } from "../api/request"
  5. import {
  6. baseUrl,
  7. request
  8. } from "../../../pages/api/canteen-request.js"
  9. const FormData = require('./formData.js')
  10. Page({
  11. /**
  12. * 页面的初始数据
  13. */
  14. data: {
  15. imgUrl: imgUrl,
  16. baseUrl: baseUrl,
  17. selectTimeNode: 0,
  18. showModal: false,
  19. // 评分图片列表
  20. starsList: ['/pages/images/stars2.png', '/pages/images/stars2.png', '/pages/images/stars2.png', '/pages/images/stars2.png', '/pages/images/stars2.png'],
  21. // 评分
  22. score: 0,
  23. // 评论
  24. evaluation: '',
  25. // 评论图片列表
  26. imgList: [],
  27. // 日期数据
  28. dateStr: {
  29. 'startWeek': '',
  30. 'endWeek': '',
  31. 'weekList': [],
  32. 'nowDate': ''
  33. },
  34. // 选择的日期索引
  35. dateSelectIndex: 0,
  36. canteenId: '',
  37. // 菜单原始值
  38. menuOrigin: [],
  39. // 4餐菜单
  40. menuCanteen: {
  41. 'breakfastCanteen': [],
  42. 'lunchCanteen': [],
  43. 'dinnerCanteen': [],
  44. 'takeoutCanteen': []
  45. },
  46. // 点击评论详情
  47. dishesDetails: {},
  48. // 食堂名称
  49. canteenName: '',
  50. isManager: false,
  51. },
  52. // 自定义函数
  53. openItem(e) {
  54. var details = e.currentTarget.dataset.item
  55. this.setData({
  56. 'dishesDetails': details
  57. })
  58. this.setData({
  59. showModal: true,
  60. score: 0,
  61. evaluation: '',
  62. imgList: []
  63. })
  64. this.setStarsList();
  65. },
  66. clickLike(e) {
  67. var subIndex = e.currentTarget.dataset.index
  68. var parentIndex = e.currentTarget.dataset.parent
  69. var obj = e.currentTarget.dataset.item
  70. var params = {
  71. 'foodId': obj.id,
  72. 'canteenId': this.data.canteenId
  73. }
  74. request({
  75. url: '/mini/canteen/click',
  76. method: 'GET',
  77. data: params
  78. }).then(res => {
  79. if (res.data.result) {
  80. switch (this.data.selectTimeNode) {
  81. case 0: {
  82. var canteen = this.data.menuCanteen
  83. canteen.breakfastCanteen[parentIndex].list[subIndex].liked = !canteen.breakfastCanteen[parentIndex].list[subIndex].liked
  84. this.setData({
  85. 'menuCanteen': canteen
  86. })
  87. };
  88. break;
  89. case 1: {
  90. var canteen = this.data.menuCanteen
  91. canteen.lunchCanteen[parentIndex].list[subIndex].liked = !canteen.lunchCanteen[parentIndex].list[subIndex].liked
  92. this.setData({
  93. 'menuCanteen': canteen
  94. })
  95. };
  96. break;
  97. case 2: {
  98. var canteen = this.data.menuCanteen
  99. canteen.dinnerCanteen[parentIndex].list[subIndex].liked = !canteen.dinnerCanteen[parentIndex].list[subIndex].liked
  100. this.setData({
  101. 'menuCanteen': canteen
  102. })
  103. };
  104. break;
  105. case 3: {
  106. var canteen = this.data.menuCanteen
  107. canteen.takeoutCanteen[parentIndex].list[subIndex].liked = !canteen.takeoutCanteen[parentIndex].list[subIndex].liked
  108. this.setData({
  109. 'menuCanteen': canteen
  110. })
  111. };
  112. break;
  113. }
  114. }
  115. })
  116. },
  117. // 关闭评论
  118. closeItem(e) {
  119. this.setData({
  120. showModal: false
  121. })
  122. },
  123. // 发布评论
  124. postComment() {
  125. if (this.data.score === 0) {
  126. wx.showToast({
  127. title: '评分不能为空',
  128. icon: 'none',
  129. duration: 2000
  130. })
  131. } else if (this.data.evaluation === '') {
  132. wx.showToast({
  133. title: '评论不能为空',
  134. icon: 'none',
  135. duration: 2000
  136. })
  137. } else if (this.data.evaluation.length < 10) {
  138. wx.showToast({
  139. title: '评论字数不能小于10',
  140. icon: 'none',
  141. duration: 2000
  142. })
  143. } else {
  144. var details = this.data.dishesDetails
  145. let userid = wx.getStorageSync('userid');
  146. let formData = new FormData();
  147. formData.append("score", this.data.score);
  148. formData.append("evaluation", this.data.evaluation);
  149. formData.append("userid", userid);
  150. formData.append("canteenid", this.data.canteenId);
  151. formData.append("dishesid", details.id);
  152. for (let i = 0; i < this.data.imgList.length; i++) {
  153. formData.appendFile("files", this.data.imgList[i]);
  154. }
  155. let data = formData.getData();
  156. request({
  157. url: '/mini/canteen/addEvaluation',
  158. method: 'POST',
  159. data: data.buffer,
  160. contentType: data.contentType
  161. }).then(res => {
  162. if (res.data.result) {
  163. wx.showToast({
  164. title: '发布成功',
  165. icon: 'none',
  166. duration: 2000
  167. })
  168. } else {
  169. wx.showToast({
  170. title: '发布失败',
  171. icon: 'none',
  172. duration: 2000
  173. })
  174. }
  175. })
  176. this.setData({
  177. showModal: false
  178. })
  179. }
  180. },
  181. // 输入评论
  182. blurEvaluation(e) {
  183. this.setData({
  184. evaluation: e.detail.value
  185. })
  186. },
  187. tapTimeNode(e) {
  188. this.setData({
  189. selectTimeNode: e.currentTarget.dataset.item
  190. })
  191. },
  192. // 赋值评分
  193. setStarsList() {
  194. let temp = [];
  195. for (let i = 0; i < 5; i++) {
  196. if (i < this.data.score) {
  197. temp.push('/pages/images/stars1.png')
  198. } else {
  199. temp.push('/pages/images/stars2.png')
  200. }
  201. }
  202. this.setData({
  203. starsList: temp
  204. })
  205. },
  206. // 选择评分
  207. chooseStars(e) {
  208. this.setData({
  209. score: e.currentTarget.dataset.index + 1
  210. })
  211. this.setStarsList();
  212. },
  213. // 上传照片
  214. choosePhoto() {
  215. var that = this
  216. wx.chooseMedia({
  217. count: 4,
  218. mediaType: ['image'],
  219. sizeType: ["original", "compressed"],
  220. sourceType: ["album"],
  221. success: function (res) {
  222. for (let i = 0; i < res.tempFiles.length; i++) {
  223. let tempFilePaths = res.tempFiles[i];
  224. if (that.data.imgList.length >= 4) {
  225. wx.showToast({
  226. title: "只能上传4张图片",
  227. icon: "none",
  228. duration: 1500
  229. })
  230. break;
  231. } else if (tempFilePaths.size > 1024 * 1024 * 5) {
  232. wx.showToast({
  233. title: "图片过大",
  234. icon: "none",
  235. duration: 1500
  236. })
  237. } else {
  238. let temp = that.data.imgList;
  239. temp.push(tempFilePaths.tempFilePath);
  240. that.setData({
  241. imgList: temp
  242. })
  243. }
  244. }
  245. }
  246. })
  247. },
  248. deleteFile(e) {
  249. let temp = this.data.imgList;
  250. temp.splice(e.currentTarget.dataset.index, 1);
  251. this.setData({
  252. imgList: temp
  253. })
  254. },
  255. //格式化日期
  256. formatDate: function (date) {
  257. let myYear = date.getFullYear();
  258. let myMonth = date.getMonth() + 1;
  259. let myWeekDay = date.getDate();
  260. return myYear + "年" + myMonth + "月"
  261. },
  262. //格式化日期2
  263. formatDateNumber: function (date) {
  264. let myYear = date.getFullYear();
  265. let myMonth = date.getMonth() + 1;
  266. let myWeekDay = date.getDate();
  267. return myYear + "-" + myMonth + "-" + myWeekDay
  268. },
  269. initDate: function () {
  270. var now = new Date();
  271. var nowYear = now.getFullYear(); //当前年
  272. var nowMonth = now.getMonth(); //当前月
  273. var nowDay = now.getDate(); //当前日
  274. var nowDayOfWeek = now.getDay(); //今天是本周的第几天
  275. this.setData({
  276. dateSelectIndex: nowDayOfWeek - 1
  277. })
  278. var startWeek = this.formatDate(new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 1));
  279. var endWeek = this.formatDate(new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek + 1)));
  280. var nowDate = this.formatDate(now)
  281. // 一周的日期列表
  282. var weeks = []
  283. for (var i = 1; i <= 7; i++) {
  284. weeks.push(this.formatDateNumber(new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + i)))
  285. }
  286. this.setData({
  287. dateStr: {
  288. 'startWeek': startWeek,
  289. 'endWeek': endWeek,
  290. 'weekList': weeks,
  291. 'nowDate': nowDate
  292. }
  293. })
  294. },
  295. // 切换日期
  296. selectDate: function (e) {
  297. this.setData({
  298. 'dateSelectIndex': e.currentTarget.dataset.item
  299. })
  300. this.getMenuInfo()
  301. },
  302. getMenuInfo() {
  303. var params = {
  304. 'date': this.data.dateStr.weekList[this.data.dateSelectIndex],
  305. 'canteenId': this.data.canteenId
  306. }
  307. request({
  308. url: '/mini/canteen/getMenu',
  309. method: 'GET',
  310. data: params
  311. }).then(res => {
  312. if (!(res.data.data && res.data.data.length != 0)) {
  313. this.setData({
  314. 'menuCanteen': {
  315. 'breakfastCanteen': [],
  316. 'lunchCanteen': [],
  317. 'dinnerCanteen': [],
  318. 'takeoutCanteen': []
  319. }
  320. })
  321. return
  322. }
  323. var jsonStr = res.data.data.dishesList
  324. var menuList = JSON.parse(jsonStr)
  325. this.setData({
  326. 'menuOrigin': menuList
  327. })
  328. var breakfast = []
  329. var lunch = []
  330. var dinner = []
  331. var takeout = []
  332. for (var i = 0; i < menuList.length; i++) {
  333. if (menuList[i].timeName == '早餐') {
  334. breakfast = menuList[i].foods
  335. }
  336. if (menuList[i].timeName == '午餐') {
  337. lunch = menuList[i].foods
  338. }
  339. if (menuList[i].timeName == '晚餐') {
  340. dinner = menuList[i].foods
  341. }
  342. if (menuList[i].timeName == '外卖') {
  343. takeout = menuList[i].foods
  344. }
  345. }
  346. var breakfastCanteen = []
  347. var lunchCanteen = []
  348. var dinnerCanteen = []
  349. var takeoutCanteen = []
  350. for (var j = 0; j < breakfast.length; j++) {
  351. var index = -1;
  352. if (breakfastCanteen.length != 0) {
  353. index = breakfastCanteen.findIndex((item) => item.type == breakfast[j].typeName)
  354. }
  355. if (index == -1) {
  356. var tmpList = []
  357. tmpList.push(breakfast[j])
  358. breakfastCanteen.push({
  359. 'type': breakfast[j].typeName,
  360. 'list': tmpList
  361. })
  362. } else {
  363. breakfastCanteen[index].list.push(breakfast[j])
  364. }
  365. }
  366. for (var j = 0; j < lunch.length; j++) {
  367. var index = -1;
  368. if (lunchCanteen.length != 0) {
  369. index = lunchCanteen.findIndex((item) => item.type == lunch[j].typeName)
  370. }
  371. if (index == -1) {
  372. var tmpList = []
  373. tmpList.push(lunch[j])
  374. lunchCanteen.push({
  375. 'type': lunch[j].typeName,
  376. 'list': tmpList
  377. })
  378. } else {
  379. lunchCanteen[index].list.push(lunch[j])
  380. }
  381. }
  382. for (var j = 0; j < dinner.length; j++) {
  383. var index = -1;
  384. if (dinnerCanteen.length != 0) {
  385. index = dinnerCanteen.findIndex((item) => item.type == dinner[j].typeName)
  386. }
  387. if (index == -1) {
  388. var tmpList = []
  389. tmpList.push(dinner[j])
  390. dinnerCanteen.push({
  391. 'type': dinner[j].typeName,
  392. 'list': tmpList
  393. })
  394. } else {
  395. dinnerCanteen[index].list.push(dinner[j])
  396. }
  397. }
  398. for (var j = 0; j < takeout.length; j++) {
  399. var index = -1;
  400. if (takeoutCanteen.length != 0) {
  401. index = takeoutCanteen.findIndex((item) => item.type == takeout[j].typeName)
  402. }
  403. if (index == -1) {
  404. var tmpList = []
  405. tmpList.push(takeout[j])
  406. takeoutCanteen.push({
  407. 'type': takeout[j].typeName,
  408. 'list': tmpList
  409. })
  410. } else {
  411. takeoutCanteen[index].list.push(takeout[j])
  412. }
  413. }
  414. this.setData({
  415. 'menuCanteen': {
  416. 'breakfastCanteen': breakfastCanteen,
  417. 'lunchCanteen': lunchCanteen,
  418. 'dinnerCanteen': dinnerCanteen,
  419. 'takeoutCanteen': takeoutCanteen
  420. }
  421. })
  422. })
  423. },
  424. gotoComment(e) {
  425. wx.navigateTo({
  426. url: '/pages/commentList/commentList?dishesId=' + e.currentTarget.dataset.id + '&canteenId=' + this.data.canteenId
  427. })
  428. },
  429. imageError(e) {
  430. let temp = this.data.menuCanteen;
  431. switch (e.currentTarget.dataset.name) {
  432. case "breakfastCanteen":
  433. temp.breakfastCanteen[e.currentTarget.dataset.index].list[e.currentTarget.dataset.subindex].imgurl = '';
  434. this.setData({
  435. menuCanteen: temp
  436. })
  437. break;
  438. case "lunchCanteen":
  439. temp.lunchCanteen[e.currentTarget.dataset.index].list[e.currentTarget.dataset.subindex].imgurl = '';
  440. this.setData({
  441. menuCanteen: temp
  442. })
  443. break;
  444. case "dinnerCanteen":
  445. temp.dinnerCanteen[e.currentTarget.dataset.index].list[e.currentTarget.dataset.subindex].imgurl = '';
  446. this.setData({
  447. menuCanteen: temp
  448. })
  449. break;
  450. case "takeoutCanteen":
  451. temp.takeoutCanteen[e.currentTarget.dataset.index].list[e.currentTarget.dataset.subindex].imgurl = '';
  452. this.setData({
  453. menuCanteen: temp
  454. })
  455. break;
  456. }
  457. },
  458. /**
  459. * 生命周期函数--监听页面加载
  460. */
  461. onLoad: function (options) {
  462. // 查询用户所属角色
  463. request({
  464. url: '/mini/worker/getUserType',
  465. method: 'GET',
  466. data: {
  467. userId: wx.getStorageSync('userid')
  468. }
  469. }).then(res => {
  470. if (res.result) {
  471. let userType = res.data;
  472. // 政府工作人员
  473. if ("0" == userType || '2' == userType) {
  474. this.setData({
  475. isManager: false
  476. });
  477. } else if ('1' == userType) {
  478. this.setData({
  479. isManager: true
  480. });
  481. } else {
  482. wx.navigateBack();
  483. }
  484. } else {
  485. wx.navigateBack();
  486. }
  487. });
  488. this.initDate()
  489. this.setData({
  490. 'canteenId': options.canteenId,
  491. canteenName: options.canteenName
  492. })
  493. wx.setNavigationBarTitle({
  494. title: options.canteenName
  495. })
  496. this.getMenuInfo();
  497. },
  498. /**
  499. * 生命周期函数--监听页面初次渲染完成
  500. */
  501. onReady: function () {
  502. },
  503. /**
  504. * 生命周期函数--监听页面显示
  505. */
  506. onShow: function () {
  507. },
  508. /**
  509. * 生命周期函数--监听页面隐藏
  510. */
  511. onHide: function () {
  512. },
  513. /**
  514. * 生命周期函数--监听页面卸载
  515. */
  516. onUnload: function () {
  517. },
  518. /**
  519. * 页面相关事件处理函数--监听用户下拉动作
  520. */
  521. onPullDownRefresh: function () {
  522. },
  523. /**
  524. * 页面上拉触底事件的处理函数
  525. */
  526. onReachBottom: function () {
  527. },
  528. /**
  529. * 用户点击右上角分享
  530. */
  531. onShareAppMessage: function () {
  532. return {
  533. title: this.data.canteenName
  534. };
  535. },
  536. /**
  537. * 上传菜品图片
  538. * @param {*} e
  539. */
  540. photograph: function (e) {
  541. let that = this;
  542. wx.chooseMedia({
  543. mediaType: ['image'],
  544. sourceType: ['camera'],
  545. success: function (res) {
  546. // console.info(res);
  547. if (res.errMsg == 'chooseMedia:ok') {
  548. wx.showLoading({
  549. title: '加载中...',
  550. });
  551. let formData = new FormData();
  552. formData.append('id', e.currentTarget.dataset.id);
  553. formData.append('canteenId', that.data.canteenId);
  554. formData.append('date', that.data.dateStr.weekList[that.data.dateSelectIndex]);
  555. formData.append('userId', wx.getStorageSync('userid'));
  556. formData.appendFile('files', res.tempFiles[0].tempFilePath);
  557. let data = formData.getData();
  558. request({
  559. url: '/mini/canteen/updateDishesPic',
  560. method: 'POST',
  561. data: data.buffer,
  562. contentType: data.contentType
  563. }).then(re => {
  564. // console.info(re)
  565. if (re.result) {
  566. wx.showToast({
  567. title: '上传成功',
  568. icon: 'success',
  569. mask: true,
  570. });
  571. that.getMenuInfo();
  572. } else {
  573. wx.showToast({
  574. title: '上传失败',
  575. icon: 'error',
  576. mask: true
  577. });
  578. }
  579. wx.hideLoading();
  580. });
  581. }
  582. }
  583. });
  584. }
  585. })