sku.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. const util = require("../../utils/util.js");
  2. const app = getApp();
  3. Component({
  4. options: { addGlobalClass: true },
  5. properties: {
  6. show: {
  7. type: Boolean,
  8. observer: function (newVal) {
  9. this.setData({ show: newVal, checkeduseCoin: false });
  10. if (this.data.show) {
  11. this.getCalendarInfo()
  12. }
  13. }
  14. },
  15. military: { //是否为军事营 1-是 0-否
  16. type: Number,
  17. value: 0,
  18. observer: function (newVal) {
  19. this.setData({ military: newVal });
  20. },
  21. },
  22. ageLimit: { //活动年龄限制
  23. type: String,
  24. value: '',
  25. observer: function (newVal) {
  26. this.setData({ ageLimit: newVal });
  27. },
  28. },
  29. s1: {
  30. type: Number,
  31. observer: function (newVal) {
  32. this.data.initialSku.s1 = (newVal == '' || newVal == 'undefined') ? -1 : newVal;
  33. this.setData({ initialSku: this.data.initialSku });
  34. },
  35. },
  36. s2: {
  37. type: String,
  38. observer: function (newVal) {
  39. this.data.initialSku.s2 = (newVal == '' || newVal == 'undefined') ? -1 : newVal;
  40. this.setData({ initialSku: this.data.initialSku });
  41. },
  42. },
  43. shareInfo: {
  44. type: Object,
  45. observer: function (newVal) {
  46. this.setData({ shareInfo: newVal });
  47. },
  48. },
  49. aid: {
  50. type: String,
  51. observer: function (val) {
  52. this.setData({ aid: val });
  53. }
  54. },
  55. atype: {
  56. type: Number,
  57. observer: function (newVal) {
  58. this.setData({ atype: newVal });
  59. }
  60. },
  61. changeSku: { //是否为换场次
  62. type: Boolean,
  63. observer: function (newVal) {
  64. this.setData({ changeSku: newVal });
  65. }
  66. },
  67. recommender: {
  68. type: String,
  69. observer: function (newVal) {
  70. this.setData({ recommender: newVal });
  71. }
  72. },
  73. street: {
  74. type: String,
  75. observer: function (newVal) {
  76. this.setData({ street: newVal });
  77. }
  78. },
  79. familyCommittee: { //0-不需要 1-需要&必填 2-需要&非必填 ,
  80. type: Number,
  81. observer: function (newVal) {
  82. this.setData({ familyCommittee: newVal });
  83. }
  84. },
  85. qptype: {
  86. type: Number,
  87. observer: function (newVal) {
  88. this.setData({ qptype: newVal });
  89. }
  90. },
  91. createGroup: {
  92. type: Number,
  93. observer: function (newVal) {
  94. this.setData({ createGroup: newVal });
  95. }
  96. },
  97. groupid: {
  98. type: String,
  99. observer: function (newVal) {
  100. this.setData({ groupid: newVal });
  101. }
  102. },
  103. orderInfo: {
  104. type: Object,
  105. observer: function (newVal) {
  106. this.setData({ orderInfo: newVal });
  107. }
  108. },
  109. setMeals: {
  110. type: Object,
  111. observer: function (newVal) {
  112. this.data.sku.tree[0].k = newVal[0] || '选择场次';
  113. this.data.sku.tree[1].k = newVal[1] || '选择套餐';
  114. this.setData({ sku: this.data.sku });
  115. }
  116. },
  117. presellOpen: {
  118. type: Number,
  119. observer: function (newVal) {
  120. this.setData({ presellOpen: newVal });
  121. }
  122. },
  123. maxUserCoin: {
  124. type: Number,
  125. observer: function (newVal) {
  126. this.setData({ maxUserCoin: newVal });
  127. }
  128. }
  129. },
  130. data: {
  131. checkeduseCoin: false,
  132. maxUserCoin: 0,//最大可使用宝贝币数
  133. saleType: false, //是否为候补
  134. show: false,
  135. height: '85%',
  136. FixHeight: '85%',
  137. presellOpen: 0,
  138. aid: '',
  139. atype: 0,
  140. changeSku: false,
  141. orderInfo: null,
  142. qptype: 0,
  143. recommender: '',
  144. street: '',
  145. familyCommittee: 0,
  146. createGroup: 0,
  147. groupid: '',
  148. info: {},
  149. sku: {
  150. tree: [
  151. {
  152. k: '选择场次', // skuKeyName:规格类目名称
  153. v: []
  154. },
  155. {
  156. k: '选择套餐', // skuKeyName:规格类目名称
  157. v: []
  158. }
  159. ],
  160. price: '0.00', // 默认价格(单位元)
  161. stock_num: 0, // 商品总库存
  162. quota: 10,//限购,超过99为不限制购买
  163. minT: 1,
  164. maxT: 1,
  165. presellPrice: 0.00,
  166. },
  167. initialSku: {
  168. s1: -1,
  169. s2: -1,
  170. selectedNum: 1
  171. },
  172. military: 0,
  173. ageLimit: '',
  174. //日历信息
  175. calendarInfo: [],
  176. sessionInfo: [],
  177. mealsInfo: [],
  178. // 日历场次选中项
  179. checkDate: {},
  180. checkSession: -1,
  181. checkMeals: -1,
  182. // 分享者信息
  183. shareInfo: {}
  184. },
  185. methods: {
  186. showFund() {
  187. this.triggerEvent("showFund")
  188. },
  189. checkboxChange(e) {
  190. let initialSku = {
  191. s1: -1,
  192. s2: -1,
  193. selectedNum: 1
  194. }
  195. this.setData({ checkeduseCoin: !this.data.checkeduseCoin, initialSku })
  196. this.getCalendarInfo()
  197. },
  198. // 获取日历信息
  199. getCalendarInfo() {
  200. let that = this;
  201. let data = {
  202. aid: that.data.aid,
  203. sid: that.data.initialSku.s1 != -1 ? that.data.initialSku.s1 : '',
  204. }
  205. if (this.data.checkeduseCoin) {
  206. data.useUserCoin = 1
  207. }
  208. util.ajax({
  209. func: 'v2/article/date/list',
  210. data,
  211. load: false
  212. }, function (res) {
  213. if (res.code == 0) {
  214. that.setData({ calendarInfo: res.data || [] })
  215. } else {
  216. util.showTips(data.reason);
  217. }
  218. })
  219. },
  220. // 获取场次列表
  221. getSessionInfo(e, type) {
  222. let that = this;
  223. let data = {
  224. aid: that.data.aid,
  225. date: e.dateString,
  226. }
  227. if (this.data.checkeduseCoin) {
  228. data.useUserCoin = 1
  229. }
  230. util.ajax({
  231. func: 'v2/article/session/list',
  232. data,
  233. load: false
  234. }, function (res) {
  235. if (res.code == 0) {
  236. if (res.data && res.data.length > 0) {
  237. that.setData({
  238. sessionInfo: res.data,
  239. checkSession: that.getDefaultOption(res.data, 0, type == 'click' ? -1 : that.data.initialSku.s1, 1, 'sid') != -1 ? that.getDefaultOption(res.data, 0, type == 'click' ? -1 : that.data.initialSku.s1, 1, 'sid') : that.getDefaultOption(res.data, 0, type == 'click' ? -1 : that.data.initialSku.s1, 2, 'sid')
  240. })
  241. if (that.data.checkSession != -1) {
  242. if (that.data.mealsInfo.length > 0) {
  243. that.setData({ mealsInfo: [] })
  244. }
  245. that.getMealsInfo(type)
  246. }
  247. }
  248. } else {
  249. util.showTips(data.reason);
  250. }
  251. })
  252. },
  253. // 获取套餐/排列表
  254. getMealsInfo(type) {
  255. let that = this;
  256. if (that.data.mealsInfo.length > 0) {
  257. that.setData({ mealsInfo: [] })
  258. }
  259. let sid = that.data.sessionInfo[that.data.checkSession].sid;
  260. let data = { sid: sid, }
  261. if (this.data.checkeduseCoin) {
  262. data.useUserCoin = 1
  263. }
  264. util.ajax({
  265. func: 'v2/article/meal/list',
  266. data,
  267. load: false
  268. }, function (res) {
  269. if (res.code == 0) {
  270. if (res.data && res.data.length > 0) {
  271. that.setData({
  272. mealsInfo: res.data,
  273. checkMeals: that.getDefaultOption(res.data, 0, type == 'click' ? -1 : that.data.initialSku.s2, 1, 'pid') != -1 ? that.getDefaultOption(res.data, 0, type == 'click' ? -1 : that.data.initialSku.s2, 1, 'pid') : that.getDefaultOption(res.data, 0, type == 'click' ? -1 : that.data.initialSku.s2, 2, 'pid')
  274. })
  275. if (that.data.checkMeals != -1) {
  276. let num = 0;
  277. if (that.data.presellOpen == 0) {
  278. num = that.data.mealsInfo[that.data.checkMeals].actnum > 0 ? that.data.mealsInfo[that.data.checkMeals].actnum : (that.data.mealsInfo[that.data.checkMeals].actnum <= 0 && that.data.mealsInfo[that.data.checkMeals].waitActNum > 0 ? that.data.mealsInfo[that.data.checkMeals].waitActNum : 0)
  279. that.data.saleType = (that.data.mealsInfo[that.data.checkMeals].actnum <= 0 && that.data.mealsInfo[that.data.checkMeals].waitActNum > 0) ? true : false;
  280. that.setData({ saleType: that.data.saleType })
  281. } else {
  282. num = that.data.mealsInfo[that.data.checkMeals].actnum
  283. }
  284. that.setData({
  285. 'initialSku.selectedNum': that.data.mealsInfo[that.data.checkMeals].minT,
  286. 'sku.stock_num': num,
  287. 'sku.maxT': that.data.mealsInfo[that.data.checkMeals].maxT,
  288. 'sku.minT': that.data.mealsInfo[that.data.checkMeals].minT,
  289. })
  290. }
  291. }
  292. } else {
  293. util.showTips(data.reason);
  294. }
  295. })
  296. },
  297. // 递归判断场次默认项
  298. getDefaultOption(arr, index, id, opt, type) {
  299. if (arr.length > 0 && index < arr.length) {
  300. if (id != -1) {
  301. if (arr[index][type] == id && (
  302. (arr[index].status == 0 && arr[index].actnum > 0) ||
  303. (arr[index].status == 1 && arr[index].actnum <= 0 && arr[index].waitActNum > 0 && this.data.presellOpen == 0)
  304. )) {
  305. return index;
  306. } else {
  307. return this.getDefaultOption(arr, index + 1, id, opt, type);
  308. }
  309. } else {
  310. if (opt == 1) {
  311. let obj = arr[index];
  312. if (obj.actnum > 0 && obj.status == 0) {
  313. return index;
  314. } else {
  315. return this.getDefaultOption(arr, index + 1, id, opt, type);
  316. }
  317. } else if (opt == 2) {
  318. let obj = arr[index];
  319. if (obj.actnum <= 0 && obj.waitActNum > 0 && obj.status == 1 && this.data.presellOpen == 0) {
  320. return index;
  321. } else {
  322. return this.getDefaultOption(arr, index + 1, id, opt, type);
  323. }
  324. }
  325. }
  326. }
  327. return -1;
  328. },
  329. close(e) {
  330. this.triggerEvent("close", e.detail);
  331. },
  332. // 选择日历
  333. changeDate(e) {
  334. let obj = e.detail, type = '';
  335. if (obj.bol) {
  336. this.setData({ 'initialSku.s1': -1 })
  337. type = 'click';
  338. }
  339. this.setData({ checkDate: obj });
  340. if (this.data.sessionInfo.length > 0) {
  341. this.setData({ sessionInfo: [] })
  342. }
  343. if (!util.isEmpty(obj) && obj.day != 0) {
  344. this.getSessionInfo(obj, type);
  345. }
  346. },
  347. // 选择场次
  348. selectSession(e) {
  349. let val = e.currentTarget.dataset.item, i = e.currentTarget.dataset.i;
  350. if (val.status == 2 || (val.status == 1 && this.data.presellOpen == 1) || (val.actnum <= 0 && val.waitActNum <= 0 && this.data.presellOpen == 0) || this.data.checkSession == i) { return false; }
  351. this.setData({ checkSession: i });
  352. this.getMealsInfo('click');
  353. },
  354. // 选择套餐
  355. selectMeals(e) {
  356. let val = e.currentTarget.dataset.item, i = e.currentTarget.dataset.i, num = 0, that = this;
  357. if (this.data.military == 1 && (val.status == 2 || (val.status == 1 && this.data.presellOpen == 1) || (val.actnum <= 0 && val.waitActNum <= 0 && this.data.presellOpen == 0) || this.data.checkMeals == i)) { return false; }
  358. this.setData({ checkMeals: i })
  359. if (that.data.presellOpen == 0) {
  360. num = that.data.mealsInfo[that.data.checkMeals].actnum > 0 ? that.data.mealsInfo[that.data.checkMeals].actnum : (that.data.mealsInfo[that.data.checkMeals].actnum <= 0 && that.data.mealsInfo[that.data.checkMeals].waitActNum > 0 ? that.data.mealsInfo[that.data.checkMeals].waitActNum : 0)
  361. that.data.saleType = (that.data.mealsInfo[that.data.checkMeals].actnum <= 0 && that.data.mealsInfo[that.data.checkMeals].waitActNum > 0) ? true : false;
  362. that.setData({ saleType: that.data.saleType })
  363. } else {
  364. num = that.data.mealsInfo[that.data.checkMeals].actnum
  365. }
  366. this.setData({
  367. 'initialSku.selectedNum': val.minT,
  368. 'sku.stock_num': num,
  369. 'sku.maxT': that.data.mealsInfo[that.data.checkMeals].maxT,
  370. 'sku.minT': that.data.mealsInfo[that.data.checkMeals].minT
  371. })
  372. },
  373. quantity(e) {
  374. let type = e.currentTarget.dataset.type, val = e.detail.value;
  375. switch (type) {
  376. case 'minus':
  377. this.data.initialSku.selectedNum--;
  378. break;
  379. case 'plus':
  380. this.data.initialSku.selectedNum++;
  381. break;
  382. case 'modfiy':
  383. this.data.initialSku.selectedNum = val;
  384. break;
  385. }
  386. if (parseInt(this.data.initialSku.selectedNum) > this.data.sku.stock_num) {
  387. util.showTips("库存不足");
  388. this.data.initialSku.selectedNum = this.data.sku.stock_num;
  389. }
  390. if ((type == 'minus' || type == 'modfiy') && (parseInt(this.data.initialSku.selectedNum) < this.data.sku.minT || util.isEmpty(this.data.initialSku.selectedNum))) {
  391. util.showTips("至少选择" + this.data.sku.minT + "件");
  392. this.data.initialSku.selectedNum = this.data.sku.minT;
  393. }
  394. if ((type == 'plus' || type == 'modfiy') && (parseInt(this.data.initialSku.selectedNum) > this.data.sku.maxT)) {
  395. util.showTips("最大只能购买" + this.data.sku.maxT + "件");
  396. this.data.initialSku.selectedNum = this.data.sku.maxT;
  397. }
  398. this.setData({ initialSku: this.data.initialSku });
  399. },
  400. sumbit(e) {
  401. let type = e.currentTarget.dataset.type, initialSku = this.data.initialSku;
  402. if (this.data.checkSession == -1) {
  403. util.showTips('请选择场次');
  404. return false;
  405. }
  406. if (this.data.checkMeals == -1) {
  407. util.showTips('请选择' + this.data.military == 1 ? "排" : "套餐");
  408. return false;
  409. }
  410. wx.reportAnalytics('active_enroll', {
  411. aid: this.data.aid,
  412. title: this.data.title,
  413. });
  414. if (util.isObjEmpty(app.globalData.userInfo)) {
  415. let that = this
  416. // 静默登录
  417. util.silentLogin().then(res => {
  418. that.toBuy(type)
  419. })
  420. return false;
  421. }
  422. this.toBuy(type)
  423. },
  424. toBuy(type) {
  425. let initialSku = this.data.initialSku
  426. if (type) {
  427. let data = {};
  428. data.aid = this.data.aid;
  429. data.atype = this.data.atype;
  430. // data.createGroup=this.data.createGroup;
  431. // data.groupid=this.data.groupid;
  432. data.createGroup = 0;
  433. data.groupid = '';
  434. data.qptype = this.data.qptype;
  435. data.sessionsIndex = this.data.sessionInfo[this.data.checkSession].sid;
  436. data.mealsIndex = this.data.mealsInfo[this.data.checkMeals].pid;
  437. data.quantity = initialSku.selectedNum;
  438. data.recommender = this.data.recommender;
  439. data.military = this.data.military ? 1 : 0,
  440. data.street = this.data.street;
  441. data.familyCommittee = this.data.familyCommittee;
  442. let age = this.data.mealsInfo[this.data.checkMeals].minAge + '-' + this.data.mealsInfo[this.data.checkMeals].maxAge;
  443. data.ageLimit = this.data.military == 1 ? age : this.data.ageLimit;
  444. data.presellOpen = this.data.presellOpen;
  445. data.btype = (this.data.presellOpen == 0 && this.data.mealsInfo[this.data.checkMeals].actnum <= 0 && this.data.mealsInfo[this.data.checkMeals].waitActNum > 0) ? 3 : -1;
  446. this.triggerEvent('alternateEmit', data)
  447. } else {
  448. let data = [];
  449. data.push('aid=' + this.data.aid)
  450. data.push('atype=' + this.data.atype)
  451. data.push('createGroup=' + this.data.createGroup)
  452. data.push('qptype=' + this.data.qptype)
  453. data.push('groupid=' + this.data.groupid)
  454. data.push('sessionsIndex=' + this.data.sessionInfo[this.data.checkSession].sid)
  455. data.push('mealsIndex=' + this.data.mealsInfo[this.data.checkMeals].pid)
  456. data.push('military=' + (this.data.military ? 1 : 0))
  457. data.push('quantity=' + initialSku.selectedNum)
  458. let age = this.data.mealsInfo[this.data.checkMeals].minAge + '-' + this.data.mealsInfo[this.data.checkMeals].maxAge;
  459. data.push('ageLimit=' + (this.data.military == 1 ? age : this.data.ageLimit))
  460. data.push('recommender=' + this.data.recommender)
  461. data.push('street=' + this.data.street)
  462. data.push('familyCommittee=' + this.data.familyCommittee)
  463. data.push('presellOpen=' + this.data.presellOpen)
  464. data.push('btype=' + ((this.data.presellOpen == 0 && this.data.mealsInfo[this.data.checkMeals].actNum <= 0 && this.data.mealsInfo[this.data.checkMeals].waitActNum > 0) ? 3 : -1))
  465. wx.navigateTo({
  466. url: '/pages/product/buy/index?' + data.join('&')
  467. })
  468. }
  469. },
  470. // 关注服务号
  471. confirm() {
  472. this.triggerEvent("showCode")
  473. },
  474. // 确认换场次
  475. confirmChange() {
  476. // console.log(this.data.orderInfo);
  477. let dateTimeNow = new Date().getTime(), that = this;
  478. if (this.data.sessionInfo[this.data.checkSession].stopBuy < dateTimeNow) {
  479. util.showTips('所选择的场次已经截止报名,请选择其他场次~');
  480. return false;
  481. }
  482. if (this.data.military != 1 && this.data.initialSku.s1 == this.data.sessionInfo[this.data.checkSession].sid) {
  483. util.showTips('本活动为非军事营活动,不可更换同场次下的套餐!');
  484. return false;
  485. }
  486. if (this.data.military != 1 && this.data.sessionInfo[this.data.checkSession].actnum <= 0 && this.data.sessionInfo[this.data.checkSession].waitActNum > 0) {
  487. util.showTips('不可更换为候补状态的场次!');
  488. return false;
  489. }
  490. if (this.data.military == 1 && that.data.mealsInfo[that.data.checkMeals].actnum <= 0 && that.data.mealsInfo[that.data.checkMeals].waitActNum > 0) {
  491. util.showTips('不可更换为候补状态的排!');
  492. return false;
  493. }
  494. util.requestMsg(['NWR1qEbBHgY6y1zin4uveLZlVBhkqBtob8V5J2ydkoM', '9N_BQYOCniXrjVJ52O9AvbNHs2T078uIcR1iAJI7ql0'], true).then(e => {
  495. let zid = that.data.mealsInfo[that.data.checkMeals].zid;
  496. that.triggerEvent('confirmChange', zid)
  497. })
  498. }
  499. }
  500. })