order.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. var _request = require('../../../utils/request.js')
  2. var rid;
  3. Page({
  4. data: {
  5. detail: {},
  6. ordertype:1,
  7. today:"",
  8. date1:"",
  9. date3:"",
  10. startdate1:"",
  11. enddate1:"",
  12. date2:"",
  13. startdate2:"",
  14. enddate2:"",
  15. checkbox:[],
  16. num1:1,
  17. num2:1,
  18. num3:1,
  19. orderList:[],
  20. orderListMul:[],
  21. total:0,
  22. checkbox_un:[],
  23. preOrderList:[],
  24. historyOrderList:[],
  25. balance: 0,
  26. cashTotal: 0,
  27. orderprice:[4,10,10],//单点价格早,中,晚
  28. ordersubprice:[2,4,4],//订餐价格早,中,晚
  29. checkAll:false//全选
  30. },
  31. onLoad: function (options) {
  32. console.log('onload onLoad == canteen',options)
  33. // this.getRadish()
  34. this.getBalanceInfo();
  35. this.getOrdrList();
  36. // 将当前日期加7天
  37. const currentDate = new Date();
  38. const futureDate = new Date(currentDate);
  39. futureDate.setDate(futureDate.getDate() + 8);
  40. const formattedDate = this.formatDate(futureDate);
  41. this.setData({
  42. today: this.getDateString(0),
  43. startdate1: this.getDateString(0),
  44. enddate1:this.getDateString(1),
  45. startdate2: this.getDateString(0),
  46. enddate2:this.getDateString(1),
  47. date1:this.getDateString(0),
  48. date2:formattedDate,
  49. })
  50. },
  51. getBalanceInfo () {
  52. var that = this
  53. var url = 'v1/user/cash/balance/info'
  54. var params = {
  55. }
  56. var success = function (res) {
  57. var result = res.data.available
  58. that.setData({
  59. balance: result,
  60. cashTotal: res.data
  61. })
  62. }
  63. _request.$get(url, params, success)
  64. },
  65. getList () {
  66. var that = this
  67. var url = 'v1/user/balances'
  68. var params = {
  69. }
  70. var success = function (res) {
  71. var data = res.data
  72. that.setData({
  73. detail: data
  74. })
  75. }
  76. _request.$get(url, params, success)
  77. },
  78. getDateString(addnum){
  79. let year = new Date().getFullYear() + addnum;
  80. let month = new Date().getMonth() +1;
  81. let day = new Date().getDate();
  82. if(month<10) month = '0'+month;
  83. if(day<10) day = '0'+day;
  84. let times = year +'-'+ month +'-'+ day;
  85. return times;
  86. },
  87. formatDate(date) {
  88. const year = date.getFullYear();
  89. const month = String(date.getMonth() + 1).padStart(2, '0'); // Month is 0-indexed
  90. const day = String(date.getDate()).padStart(2, '0');
  91. return `${year}-${month}-${day}`;
  92. },
  93. bindDateChange: function(e) {
  94. console.log('picker发送选择改变,携带值为', e.detail.value)
  95. this.setData({
  96. date1: e.detail.value,
  97. startdate2: e.detail.value
  98. })
  99. this.calcOrder();
  100. },
  101. bindDateChange2: function(e) {
  102. console.log('picker发送选择改变,携带值为', e.detail.value)
  103. this.setData({
  104. date2: e.detail.value
  105. })
  106. this.calcOrder();
  107. },
  108. radioChange:function(e){
  109. console.log('radio发生change事件,携带value值为:', e.detail.value);
  110. if(e.detail.value == 0){
  111. this.setData({
  112. checkbox:[]
  113. });
  114. }
  115. this.setData({
  116. ordertype:e.detail.value,
  117. total:0
  118. });
  119. this.calcOrder();
  120. },
  121. getOrdrList:function(e){
  122. var that = this
  123. var url = 'newapi:food/get-info'
  124. var params = {
  125. }
  126. var success = function (res) {
  127. var data = res.data.data.list;
  128. var hisOrderList = [];
  129. for (let item in data) {
  130. let thedate = data[item]['food_date'];
  131. let xingqi;
  132. if(new Date(thedate).getDay() == 0) xingqi = '星期日';
  133. if(new Date(thedate).getDay() == 1) xingqi = '星期一';
  134. if(new Date(thedate).getDay() == 2) xingqi = '星期二';
  135. if(new Date(thedate).getDay() == 3) xingqi = '星期三';
  136. if(new Date(thedate).getDay() == 4) xingqi = '星期四';
  137. if(new Date(thedate).getDay() == 5) xingqi = '星期五';
  138. if(new Date(thedate).getDay() == 6) xingqi = '星期六';
  139. data[item]['food_day'] = xingqi;
  140. data[item]['can_break_fast'] = data[item]['break_fast'] ? false : data[item]['can_break_fast'];
  141. data[item]['can_lunch'] = data[item]['lunch'] ? false : data[item]['can_lunch'];
  142. data[item]['can_dinner'] = data[item]['dinner'] ? false : data[item]['can_dinner'];
  143. if(data[item]['break_fast']) hisOrderList.push(`${data[item]['food_date']},1,1`);
  144. if(data[item]['lunch']) hisOrderList.push(`${data[item]['food_date']},1,2`);
  145. if(data[item]['dinner']) hisOrderList.push(`${data[item]['food_date']},1,3`);
  146. }
  147. that.setData({
  148. preOrderList: data,
  149. date1:data[0]['food_date'],
  150. date3:data[0]['food_date'],
  151. date2:data[data.length - 1]['food_date'],
  152. historyOrderList:hisOrderList
  153. })
  154. console.log('data',data);
  155. }
  156. _request.$get(url, params, success)
  157. },
  158. checkAllfunc: function(e){
  159. console.log('checkAll');//checkAll
  160. let nowcheckAll = !this.data.checkAll;
  161. let checkarr = [];
  162. let preOrderList = this.data.preOrderList;
  163. for(let i in preOrderList){
  164. if(preOrderList[i]['can_break_fast']){
  165. preOrderList[i]['break_fast'] = !preOrderList[i]['break_fast'];
  166. checkarr.push(`${preOrderList[i]['food_date']},1,1`);
  167. }
  168. if(preOrderList[i]['can_lunch']){
  169. preOrderList[i]['lunch'] = !preOrderList[i]['lunch'];
  170. checkarr.push(`${preOrderList[i]['food_date']},1,2`);
  171. }
  172. if(preOrderList[i]['can_dinner']){
  173. preOrderList[i]['dinner'] = !preOrderList[i]['dinner'];
  174. checkarr.push(`${preOrderList[i]['food_date']},1,3`);
  175. }
  176. }
  177. if(!nowcheckAll) checkarr = [];
  178. let a = checkarr;
  179. let b = this.data.historyOrderList;
  180. // 从数组a中筛选出不包含在数组b中的值
  181. var resultA = a.filter(itemA => !b.includes(itemA));
  182. console.log("数组a中与数组b不同的值: ",resultA);
  183. this.setData({
  184. checkAll:nowcheckAll,
  185. orderListMul:resultA,
  186. preOrderList:preOrderList
  187. });
  188. this.calcOrder();
  189. },
  190. checkboxChangeList(e){
  191. console.log('checkboxChangeList checkbox发生change事件,携带value值为:', e.detail.value);
  192. console.log('historylist:', this.data.historyOrderList);
  193. let a = e.detail.value;
  194. let b = this.data.historyOrderList;
  195. // 从数组a中筛选出不包含在数组b中的值
  196. var resultA = a.filter(itemA => !b.includes(itemA));
  197. console.log("数组a中与数组b不同的值: ",resultA);
  198. this.setData({
  199. orderListMul:resultA
  200. });
  201. this.calcOrder();
  202. },
  203. checkboxChange(e) {
  204. console.log('checkboxChange checkbox发生change事件,携带value值为:', e.detail.value);
  205. this.setData({
  206. checkbox:[e.detail.value]
  207. });
  208. this.calcOrder();
  209. },
  210. checkboxChangeUn(e) {
  211. console.log('checkboxUn发生change事件,携带value值为:', e.detail.value);
  212. this.setData({
  213. checkbox_un:e.detail.value
  214. });
  215. this.calcOrder();
  216. },
  217. calcOrder(){
  218. console.log('calcOrder',this.data.ordertype,this.data.date1,this.data.checkbox);
  219. if(this.data.ordertype == 0){
  220. let orderl = [];
  221. if(this.data.date1 && this.data.checkbox.length>0){
  222. let total = 0;
  223. for(let i in this.data.checkbox){
  224. let val = this.data.checkbox[i];
  225. let num;
  226. let price;
  227. if(val==1){
  228. num = this.data.num1;
  229. }else if(val==2){
  230. num = this.data.num2;
  231. }else{
  232. num = this.data.num3;
  233. }
  234. price = this.data.orderprice[val-1] * num;
  235. orderl.push([this.data.date1,val,num,price]);
  236. total += price;
  237. }
  238. this.setData({
  239. orderList:orderl,
  240. total:total
  241. })
  242. console.log('orderl',orderl);
  243. }else{
  244. this.setData({
  245. orderList:[]
  246. })
  247. }
  248. }else{
  249. if(this.data.orderListMul.length>0){
  250. let total=0;
  251. for(let i in this.data.orderListMul){
  252. let val = this.data.orderListMul[i].split(',')[2];
  253. let price;
  254. price = this.data.ordersubprice[val-1] * 1;
  255. total += price;
  256. }
  257. this.setData({
  258. total:total
  259. })
  260. }else{
  261. this.setData({
  262. orderList:[],
  263. total:0
  264. })
  265. }
  266. }
  267. },
  268. getDatesInRange(startDate, endDate) {
  269. const dates = [];
  270. let currentDate = new Date(startDate);
  271. while (currentDate <= endDate) {
  272. console.log('1',this.data.checkbox_un.includes("1"));
  273. console.log('2',this.data.checkbox_un.includes("2"));
  274. if(this.data.checkbox_un.includes("1") && this.data.checkbox_un.includes("2")){
  275. if (currentDate.getDay() !== 0 && currentDate.getDay() !== 6) {
  276. dates.push(new Date(currentDate));
  277. }
  278. }else if(this.data.checkbox_un.includes("1") && !this.data.checkbox_un.includes("2")){//剔除周六
  279. if (currentDate.getDay() !== 6) {
  280. dates.push(new Date(currentDate));
  281. }
  282. }else if(this.data.checkbox_un.includes("2") && !this.data.checkbox_un.includes("1")){//剔除周日
  283. if (currentDate.getDay() !== 0) {
  284. dates.push(new Date(currentDate));
  285. }
  286. }else{
  287. dates.push(new Date(currentDate));
  288. }
  289. currentDate.setDate(currentDate.getDate() + 1);
  290. }
  291. return dates;
  292. },
  293. add: function (e) {
  294. var type = e.target.dataset.type;
  295. if(type == 1){
  296. this.setData({
  297. num1:this.data.num1+1
  298. })
  299. }else if(type == 2){
  300. this.setData({
  301. num2:this.data.num2+1
  302. })
  303. }else{
  304. this.setData({
  305. num3:this.data.num3+1
  306. })
  307. }
  308. this.calcOrder();
  309. },
  310. mius: function (e) {
  311. var type = e.target.dataset.type;
  312. if(type == 1){
  313. let num = this.data.num1 - 1 <1?1:this.data.num1-1;
  314. this.setData({
  315. num1:num
  316. })
  317. }else if(type == 2){
  318. let num = this.data.num2 - 1 <1?1:this.data.num2-1;
  319. this.setData({
  320. num2:num
  321. })
  322. }else{
  323. let num = this.data.num3 - 1 <1?1:this.data.num3-1;
  324. this.setData({
  325. num3:num
  326. })
  327. }
  328. this.calcOrder();
  329. },
  330. changeCount: function (val) {
  331. var count = val.detail.value
  332. var type = val.target.dataset.type;
  333. if(type == 1){
  334. this.setData({
  335. num1:count
  336. })
  337. }else if(type == 2){
  338. this.setData({
  339. num2:count
  340. })
  341. }else{
  342. this.setData({
  343. num3:count
  344. })
  345. }
  346. this.calcOrder();
  347. },
  348. toOrder: function(){
  349. let paramorder = {
  350. "foodInfoElement": [
  351. // {
  352. // "date": "string",
  353. // "nums": 0,
  354. // "type": 0
  355. // }
  356. ],
  357. "single": this.data.ordertype == 0 ? true:false
  358. }
  359. if(this.data.ordertype == 0){
  360. console.log('order',this.data.orderList);
  361. for(let i in this.data.orderList){
  362. let thepar = {
  363. "date": this.data.orderList[i][0],
  364. "nums": parseInt(this.data.orderList[i][2]),
  365. "type": parseInt(this.data.orderList[i][1])
  366. }
  367. paramorder['foodInfoElement'].push(thepar);
  368. }
  369. }else{
  370. console.log('order',this.data.orderListMul);
  371. for(let i in this.data.orderListMul){
  372. let thepar = {
  373. "date": this.data.orderListMul[i].split(',')[0],
  374. "nums": parseInt(this.data.orderListMul[i].split(',')[1]),
  375. "type": parseInt(this.data.orderListMul[i].split(',')[2])
  376. }
  377. paramorder['foodInfoElement'].push(thepar);
  378. }
  379. }
  380. console.log('paramorder',paramorder);
  381. if(paramorder['foodInfoElement'].length == 0){
  382. wx.showToast({
  383. title: '请先选餐',
  384. icon: 'error',
  385. duration: 2000
  386. })
  387. return;
  388. }
  389. var that = this
  390. var url = 'newapi:food/order-food'
  391. var success = function (res) {
  392. console.log('res',res);
  393. if(res.data.code == 0){
  394. wx.showToast({
  395. title: '已成功下单',
  396. icon: 'success',
  397. duration: 2000
  398. })
  399. setTimeout(function(){
  400. wx.navigateBack()
  401. },1000);
  402. }else{
  403. wx.showToast({
  404. title: res.data.msg,
  405. icon: 'none',
  406. duration: 2000
  407. })
  408. }
  409. }
  410. _request.$post(url, JSON.stringify(paramorder), success);
  411. }
  412. })