order.js 12 KB

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