order.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. date1: this.data.today,
  119. });
  120. this.getSingleRule();
  121. }
  122. this.setData({
  123. ordertype:e.detail.value,
  124. total:0
  125. });
  126. this.calcOrder();
  127. },
  128. getSingleRule() {
  129. var that = this
  130. var url = 'newapi:food/check-date?date='+this.data.date1;
  131. var params = {
  132. }
  133. var success = function (res) {
  134. console.log('res',res)
  135. var data = res.data.data;
  136. that.setData({
  137. can1:data['1'],
  138. can2:data['2'],
  139. can3:data['3'],
  140. })
  141. }
  142. _request.$get(url, params, success)
  143. },
  144. getOrdrList:function(e){
  145. var that = this
  146. var url = 'newapi:food/get-info'
  147. var params = {
  148. }
  149. var success = function (res) {
  150. var data = res.data.data.list;
  151. var hisOrderList = [];
  152. for (let item in data) {
  153. let thedate = data[item]['food_date'];
  154. let xingqi;
  155. if(new Date(thedate).getDay() == 0) xingqi = '星期日';
  156. if(new Date(thedate).getDay() == 1) xingqi = '星期一';
  157. if(new Date(thedate).getDay() == 2) xingqi = '星期二';
  158. if(new Date(thedate).getDay() == 3) xingqi = '星期三';
  159. if(new Date(thedate).getDay() == 4) xingqi = '星期四';
  160. if(new Date(thedate).getDay() == 5) xingqi = '星期五';
  161. if(new Date(thedate).getDay() == 6) xingqi = '星期六';
  162. data[item]['food_day'] = xingqi;
  163. data[item]['can_break_fast'] = data[item]['break_fast'] ? false : data[item]['can_break_fast'];
  164. data[item]['can_lunch'] = data[item]['lunch'] ? false : data[item]['can_lunch'];
  165. data[item]['can_dinner'] = data[item]['dinner'] ? false : data[item]['can_dinner'];
  166. if(data[item]['break_fast']) hisOrderList.push(`${data[item]['food_date']},1,1`);
  167. if(data[item]['lunch']) hisOrderList.push(`${data[item]['food_date']},1,2`);
  168. if(data[item]['dinner']) hisOrderList.push(`${data[item]['food_date']},1,3`);
  169. }
  170. that.setData({
  171. preOrderList: data,
  172. date1:data[0]['food_date'],
  173. date3:data[0]['food_date'],
  174. date2:data[data.length - 1]['food_date'],
  175. historyOrderList:hisOrderList
  176. })
  177. console.log('data',data);
  178. }
  179. _request.$get(url, params, success)
  180. },
  181. checkAllfunc: function(e){
  182. console.log('checkAll');//checkAll
  183. let nowcheckAll = !this.data.checkAll;
  184. let checkarr = [];
  185. let preOrderList = this.data.preOrderList;
  186. for(let i in preOrderList){
  187. if(preOrderList[i]['can_break_fast']){
  188. preOrderList[i]['break_fast'] = !preOrderList[i]['break_fast'];
  189. checkarr.push(`${preOrderList[i]['food_date']},1,1`);
  190. }
  191. if(preOrderList[i]['can_lunch']){
  192. preOrderList[i]['lunch'] = !preOrderList[i]['lunch'];
  193. checkarr.push(`${preOrderList[i]['food_date']},1,2`);
  194. }
  195. if(preOrderList[i]['can_dinner']){
  196. preOrderList[i]['dinner'] = !preOrderList[i]['dinner'];
  197. checkarr.push(`${preOrderList[i]['food_date']},1,3`);
  198. }
  199. }
  200. if(!nowcheckAll) checkarr = [];
  201. let a = checkarr;
  202. let b = this.data.historyOrderList;
  203. // 从数组a中筛选出不包含在数组b中的值
  204. var resultA = a.filter(itemA => !b.includes(itemA));
  205. console.log("数组a中与数组b不同的值: ",resultA);
  206. this.setData({
  207. checkAll:nowcheckAll,
  208. orderListMul:resultA,
  209. preOrderList:preOrderList
  210. });
  211. this.calcOrder();
  212. },
  213. checkboxChangeList(e){
  214. console.log('checkboxChangeList checkbox发生change事件,携带value值为:', e.detail.value);
  215. console.log('historylist:', this.data.historyOrderList);
  216. let a = e.detail.value;
  217. let b = this.data.historyOrderList;
  218. // 从数组a中筛选出不包含在数组b中的值
  219. var resultA = a.filter(itemA => !b.includes(itemA));
  220. console.log("数组a中与数组b不同的值: ",resultA);
  221. this.setData({
  222. orderListMul:resultA
  223. });
  224. this.calcOrder();
  225. },
  226. checkboxChange(e) {
  227. console.log('checkboxChange checkbox发生change事件,携带value值为:', e.detail.value);
  228. this.setData({
  229. checkbox:[e.detail.value]
  230. });
  231. this.calcOrder();
  232. },
  233. checkboxChangeUn(e) {
  234. console.log('checkboxUn发生change事件,携带value值为:', e.detail.value);
  235. this.setData({
  236. checkbox_un:e.detail.value
  237. });
  238. this.calcOrder();
  239. },
  240. calcOrder(){
  241. console.log('calcOrder',this.data.ordertype,this.data.date1,this.data.checkbox);
  242. if(this.data.ordertype == 0){
  243. let orderl = [];
  244. if(this.data.date1 && this.data.checkbox.length>0){
  245. let total = 0;
  246. for(let i in this.data.checkbox){
  247. let val = this.data.checkbox[i];
  248. let num;
  249. let price;
  250. if(val==1){
  251. num = this.data.num1;
  252. }else if(val==2){
  253. num = this.data.num2;
  254. }else{
  255. num = this.data.num3;
  256. }
  257. price = this.data.orderprice[val-1] * num;
  258. orderl.push([this.data.date1,val,num,price]);
  259. total += price;
  260. }
  261. this.setData({
  262. orderList:orderl,
  263. total:total
  264. })
  265. console.log('orderl',orderl);
  266. }else{
  267. this.setData({
  268. orderList:[]
  269. })
  270. }
  271. }else{
  272. if(this.data.orderListMul.length>0){
  273. let total=0;
  274. for(let i in this.data.orderListMul){
  275. let val = this.data.orderListMul[i].split(',')[2];
  276. let price;
  277. price = this.data.ordersubprice[val-1] * 1;
  278. total += price;
  279. }
  280. this.setData({
  281. total:total
  282. })
  283. }else{
  284. this.setData({
  285. orderList:[],
  286. total:0
  287. })
  288. }
  289. }
  290. },
  291. getDatesInRange(startDate, endDate) {
  292. const dates = [];
  293. let currentDate = new Date(startDate);
  294. while (currentDate <= endDate) {
  295. console.log('1',this.data.checkbox_un.includes("1"));
  296. console.log('2',this.data.checkbox_un.includes("2"));
  297. if(this.data.checkbox_un.includes("1") && this.data.checkbox_un.includes("2")){
  298. if (currentDate.getDay() !== 0 && currentDate.getDay() !== 6) {
  299. dates.push(new Date(currentDate));
  300. }
  301. }else if(this.data.checkbox_un.includes("1") && !this.data.checkbox_un.includes("2")){//剔除周六
  302. if (currentDate.getDay() !== 6) {
  303. dates.push(new Date(currentDate));
  304. }
  305. }else if(this.data.checkbox_un.includes("2") && !this.data.checkbox_un.includes("1")){//剔除周日
  306. if (currentDate.getDay() !== 0) {
  307. dates.push(new Date(currentDate));
  308. }
  309. }else{
  310. dates.push(new Date(currentDate));
  311. }
  312. currentDate.setDate(currentDate.getDate() + 1);
  313. }
  314. return dates;
  315. },
  316. add: function (e) {
  317. var type = e.target.dataset.type;
  318. if(type == 1){
  319. this.setData({
  320. num1:this.data.num1+1
  321. })
  322. }else if(type == 2){
  323. this.setData({
  324. num2:this.data.num2+1
  325. })
  326. }else{
  327. this.setData({
  328. num3:this.data.num3+1
  329. })
  330. }
  331. this.calcOrder();
  332. },
  333. mius: function (e) {
  334. var type = e.target.dataset.type;
  335. if(type == 1){
  336. let num = this.data.num1 - 1 <1?1:this.data.num1-1;
  337. this.setData({
  338. num1:num
  339. })
  340. }else if(type == 2){
  341. let num = this.data.num2 - 1 <1?1:this.data.num2-1;
  342. this.setData({
  343. num2:num
  344. })
  345. }else{
  346. let num = this.data.num3 - 1 <1?1:this.data.num3-1;
  347. this.setData({
  348. num3:num
  349. })
  350. }
  351. this.calcOrder();
  352. },
  353. changeCount: function (val) {
  354. var count = val.detail.value
  355. var type = val.target.dataset.type;
  356. if(type == 1){
  357. this.setData({
  358. num1:count
  359. })
  360. }else if(type == 2){
  361. this.setData({
  362. num2:count
  363. })
  364. }else{
  365. this.setData({
  366. num3:count
  367. })
  368. }
  369. this.calcOrder();
  370. },
  371. toOrder: function(){
  372. let paramorder = {
  373. "foodInfoElement": [
  374. // {
  375. // "date": "string",
  376. // "nums": 0,
  377. // "type": 0
  378. // }
  379. ],
  380. "single": this.data.ordertype == 0 ? true:false
  381. }
  382. if(this.data.ordertype == 0){
  383. console.log('order',this.data.orderList);
  384. for(let i in this.data.orderList){
  385. let thepar = {
  386. "date": this.data.orderList[i][0],
  387. "nums": parseInt(this.data.orderList[i][2]),
  388. "type": parseInt(this.data.orderList[i][1])
  389. }
  390. paramorder['foodInfoElement'].push(thepar);
  391. }
  392. }else{
  393. console.log('order',this.data.orderListMul);
  394. for(let i in this.data.orderListMul){
  395. let thepar = {
  396. "date": this.data.orderListMul[i].split(',')[0],
  397. "nums": parseInt(this.data.orderListMul[i].split(',')[1]),
  398. "type": parseInt(this.data.orderListMul[i].split(',')[2])
  399. }
  400. paramorder['foodInfoElement'].push(thepar);
  401. }
  402. }
  403. console.log('paramorder',paramorder);
  404. if(paramorder['foodInfoElement'].length == 0){
  405. wx.showToast({
  406. title: '请先选餐',
  407. icon: 'error',
  408. duration: 2000
  409. })
  410. return;
  411. }
  412. this.setData({
  413. loading:true
  414. })
  415. var that = this
  416. var url = 'newapi:food/order-food'
  417. var success = function (res) {
  418. console.log('res',res);
  419. if(res.data.code == 0){
  420. wx.showToast({
  421. title: '已成功下单',
  422. icon: 'success',
  423. duration: 2000
  424. })
  425. setTimeout(function(){
  426. wx.navigateBack()
  427. },1000);
  428. }else{
  429. wx.showToast({
  430. title: res.data.msg,
  431. icon: 'none',
  432. duration: 2000
  433. })
  434. setTimeout(function(){
  435. that.setData({
  436. loading:false
  437. })
  438. },500)
  439. }
  440. }
  441. _request.$post(url, JSON.stringify(paramorder), success);
  442. }
  443. })