menu.js 12 KB

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