goodsSku.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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) this.getInfo();
  11. }
  12. },
  13. s1: {
  14. type: Number,
  15. observer: function (newVal) {
  16. this.data.initialSku.s1 = newVal;
  17. this.setData({ initialSku: this.data.initialSku });
  18. },
  19. },
  20. aid: {
  21. type: String,
  22. observer: function (val) {
  23. this.setData({ aid: val });
  24. }
  25. },
  26. atype: {
  27. type: Number,
  28. observer: function (newVal) {
  29. this.setData({ atype: newVal });
  30. }
  31. },
  32. recommender: {
  33. type: String,
  34. observer: function (newVal) {
  35. this.setData({ recommender: newVal });
  36. }
  37. },
  38. street: {
  39. type: String,
  40. observer: function (newVal) {
  41. this.setData({ street: newVal });
  42. }
  43. },
  44. qptype: {
  45. type: Number,
  46. observer: function (newVal) {
  47. this.setData({ qptype: newVal });
  48. }
  49. },
  50. createGroup: {
  51. type: Number,
  52. observer: function (newVal) {
  53. this.setData({ createGroup: newVal });
  54. }
  55. },
  56. groupid: {
  57. type: String,
  58. observer: function (newVal) {
  59. this.setData({ groupid: newVal });
  60. }
  61. },
  62. setMeals: {
  63. type: Object,
  64. observer: function (newVal) {
  65. this.data.sku.tree[0].k = newVal[0] || '选择场次';
  66. this.data.sku.tree[1].k = newVal[1] || '选择套餐';
  67. this.setData({ sku: this.data.sku });
  68. }
  69. },
  70. presellOpen: {
  71. type: Number,
  72. observer: function (newVal) {
  73. this.setData({ presellOpen: newVal });
  74. }
  75. },
  76. maxUserCoin: {
  77. type: Number,
  78. observer: function (newVal) {
  79. this.setData({ maxUserCoin: newVal });
  80. }
  81. },
  82. },
  83. data: {
  84. maxUserCoin:0,
  85. saleType:false,
  86. show: false,
  87. height:'',
  88. presellOpen:0,
  89. aid: '',
  90. atype: 0,
  91. qptype: 0,
  92. recommender:'',
  93. street:'',
  94. createGroup: 0,
  95. groupid: '',
  96. info: {},
  97. sku: {
  98. tree: [
  99. {
  100. k: '选择场次', // skuKeyName:规格类目名称
  101. v: []
  102. },
  103. {
  104. k: '选择套餐', // skuKeyName:规格类目名称
  105. v: []
  106. }
  107. ],
  108. price: '0.00', // 默认价格(单位元)
  109. stock_num: 0, // 商品总库存
  110. quota: 10,//限购,超过99为不限制购买
  111. minT: 1,
  112. maxT: 1,
  113. presellPrice:0.00,
  114. },
  115. initialSku: {
  116. s1: -1,
  117. s2: -1,
  118. selectedNum: 1,
  119. userCoin:0
  120. },
  121. checkeduseCoin:false
  122. },
  123. methods: {
  124. initialiSku(){
  125. let sku={
  126. tree: [{k: '选择场次',v: []},{k: '选择套餐',v: []}],
  127. price: '0.00',
  128. stock_num: 0,
  129. quota: 10,
  130. minT: 1,
  131. maxT: 1,
  132. presellPrice:0.00,
  133. };
  134. this.setData({sku})
  135. },
  136. checkboxChange(e){
  137. this.initialiSku()
  138. this.setData({checkeduseCoin:!this.data.checkeduseCoin})
  139. this.getInfo()
  140. },
  141. close(e) {
  142. this.triggerEvent("close", e.detail);
  143. },
  144. getInfo() {
  145. let that = this, initialSku = this.data.initialSku, s1Status = true, s2Status = true, s3Status = true;
  146. let params ={
  147. aid: that.data.aid,
  148. }
  149. if (this.data.checkeduseCoin) {
  150. params.useUserCoin=1
  151. }
  152. util.ajax({
  153. func: 'v2/article/payinfo',
  154. data: params,
  155. load:false
  156. }, function (res) {
  157. if (res.code == 0) {
  158. let data = res.data.sessions;
  159. if (data && data.length > 0) {
  160. data.forEach((item, index) => {
  161. that.data.sku.tree[0].v[index] = { name: item.name, actnum: item.actnum, status: item.status,waitActNum:item.waitActNum};
  162. if (((item.actnum > 0 && item.status == 0) || (item.actnum <= 0 && item.waitActNum > 0 && item.status == 1 && that.presellOpen==0)) && s1Status) {
  163. that.data.initialSku.s1 = that.data.initialSku.s1 != -1 ? that.data.initialSku.s1 : index;
  164. }
  165. if (item.meals.length > 0 && ((item.actnum > 0 && item.status == 0) || (item.actnum <= 0 && item.waitActNum > 0 && item.status == 1)) && s1Status && index == that.data.initialSku.s1) {
  166. item.meals.forEach((it, i) => {
  167. item.meals[i].name = it.policyName;
  168. it.waitActNum = item.waitActNum;
  169. // it.status = item.status;
  170. that.data.sku.stock_num =(item.actnum <= 0 && item.waitActNum > 0 && item.status == 1)?it.waitActNum:it.actnum;
  171. if ((it.actnum > 0 || (it.actnum <= 0 && it.waitActNum > 0)) && s2Status) {
  172. that.data.initialSku.s2 = i;
  173. that.data.initialSku.selectedNum = it.minT;
  174. that.data.initialSku.userCoin = it.userCoin;
  175. that.data.sku.price = it.price;
  176. that.data.sku.presellPrice = it.presellPrice;
  177. that.data.sku.minT = it.minT;
  178. that.data.sku.maxT = it.maxT;
  179. s2Status = false;
  180. }
  181. });
  182. that.data.sku.tree[1].v = item.meals;
  183. s1Status = false;
  184. } else if (item.status != 0 && s2Status && s3Status) {
  185. item.meals.forEach((it, i) => {
  186. it.waitActNum = item.waitActNum;
  187. it.status = item.status;
  188. item.meals[i].name = it.policyName;
  189. item.meals[i].actnum = 0;
  190. });
  191. that.data.sku.tree[1].v = item.meals;
  192. that.data.sku.stock_num = item.meals[0].actnum;
  193. that.data.sku.price = item.meals[0].price;
  194. that.data.sku.presellPrice = item.meals[0].presellPrice;
  195. that.data.sku.minT = item.meals[0].minT;
  196. that.data.sku.maxT = item.meals[0].maxT;
  197. that.data.initialSku.selectedNum = item.meals[0].minT;
  198. s3Status = false;
  199. }
  200. });
  201. if (that.data.initialSku.s1 !=-1) {
  202. that.data.saleType=(data[that.data.initialSku.s1].actnum<=0 && data[that.data.initialSku.s1].waitActNum>0)?true:false;
  203. }
  204. }else{
  205. this.initialiSku()
  206. }
  207. res.data.sessions = data;
  208. that.setData({ info: res.data, sku: that.data.sku, initialSku: that.data.initialSku,saleType:that.data.saleType})
  209. } else {
  210. util.showTips(data.reason);
  211. }
  212. })
  213. },
  214. checkChange(e) {
  215. let data = e.currentTarget.dataset, that = this, s2Status = true;
  216. if (data.disabled) return false;
  217. if (data.index == 0) {
  218. this.data.initialSku.s1 = data.i;
  219. this.data.sku.tree[1].v = [];
  220. this.data.saleType=(this.data.info.sessions[this.data.initialSku.s1].actnum<=0 && this.data.info.sessions[this.data.initialSku.s1].waitActNum>0)?true:false;
  221. this.setData({saleType:this.data.saleType})
  222. this.data.info.sessions[this.data.initialSku.s1].meals.forEach((item, idx) => {
  223. item.name = item.policyName;
  224. if ((item.actnum > 0 || (item.actnum <= 0 && this.data.info.sessions[this.data.initialSku.s1].waitActNum > 0)) && s2Status) {
  225. console.log(item)
  226. that.data.initialSku.s2 = idx;
  227. that.data.initialSku.userCoin = item.userCoin;
  228. that.data.sku.stock_num = item.actnum>0?item.actnum:this.data.info.sessions[this.data.initialSku.s1].waitActNum;
  229. that.data.sku.price = item.price;
  230. that.data.sku.presellPrice = item.presellPrice;
  231. that.data.sku.minT = item.minT;
  232. that.data.sku.maxT = item.maxT;
  233. s2Status = false;
  234. }
  235. this.data.sku.tree[1].v[idx] = item;
  236. });
  237. } else {
  238. this.data.initialSku.s2 = data.i;
  239. this.data.sku.price = this.data.sku.tree[1].v[data.i].price;
  240. this.data.sku.presellPrice = this.data.sku.tree[1].v[data.i].presellPrice;
  241. this.data.sku.stock_num = this.data.info.sessions[this.data.initialSku.s1].actnum>0?this.data.info.sessions[this.data.initialSku.s1].actnum:this.data.info.sessions[this.data.initialSku.s1].waitActNum;
  242. this.data.sku.minT = this.data.sku.tree[1].v[data.i].minT;
  243. this.data.sku.maxT = this.data.sku.tree[1].v[data.i].maxT;
  244. this.data.initialSku.userCoin = this.data.sku.tree[1].v[data.i].userCoin;
  245. }
  246. this.data.initialSku.selectedNum = this.data.sku.minT;
  247. this.setData({ initialSku: this.data.initialSku, sku: this.data.sku });
  248. },
  249. quantity(e) {
  250. let type = e.currentTarget.dataset.type, val = e.detail.value;
  251. switch (type) {
  252. case 'minus':
  253. this.data.initialSku.selectedNum--;
  254. break;
  255. case 'plus':
  256. this.data.initialSku.selectedNum++;
  257. break;
  258. case 'modfiy':
  259. this.data.initialSku.selectedNum = val;
  260. break;
  261. }
  262. if (parseInt(this.data.initialSku.selectedNum) > this.data.sku.stock_num) {
  263. util.showTips("库存不足");
  264. this.data.initialSku.selectedNum = this.data.sku.stock_num;
  265. }
  266. if ((type == 'minus' || type == 'modfiy') && (parseInt(this.data.initialSku.selectedNum) < this.data.sku.minT || util.isEmpty(this.data.initialSku.selectedNum))) {
  267. util.showTips("至少选择" + this.data.sku.minT + "件");
  268. this.data.initialSku.selectedNum = this.data.sku.minT;
  269. }
  270. if ((type == 'plus' || type == 'modfiy') && (parseInt(this.data.initialSku.selectedNum) > this.data.sku.maxT)) {
  271. util.showTips("最大只能购买" + this.data.sku.maxT + "件");
  272. this.data.initialSku.selectedNum = this.data.sku.maxT;
  273. }
  274. this.setData({ initialSku: this.data.initialSku });
  275. },
  276. sumbit(e) {
  277. let type=e.currentTarget.dataset.type,initialSku = this.data.initialSku;
  278. if (initialSku.s1 == -1) {
  279. util.showTips('请' + this.data.sku.tree[0].k);
  280. return false;
  281. }
  282. if (initialSku.s2 == -1) {
  283. util.showTips('请' + this.data.sku.tree[1].k);
  284. return false;
  285. }
  286. wx.reportAnalytics('active_enroll', {
  287. aid: this.data.aid,
  288. title: this.data.title,
  289. });
  290. if (util.isObjEmpty(app.globalData.userInfo)) {
  291. let that = this
  292. // 静默登录
  293. util.silentLogin().then(res => {
  294. that.toBuy(type)
  295. })
  296. return false;
  297. }
  298. this.toBuy(type)
  299. },
  300. toBuy(type) {
  301. let initialSku = this.data.initialSku
  302. if (type) {
  303. let data = {};
  304. data.aid = this.data.aid;
  305. data.atype = this.data.atype;
  306. // data.createGroup=this.data.createGroup;
  307. // data.groupid=this.data.groupid;
  308. data.createGroup = 0;
  309. data.groupid = '';
  310. data.qptype = this.data.qptype;
  311. data.sessionsIndex = this.data.sessionInfo[this.data.checkSession].sid;
  312. data.mealsIndex = this.data.mealsInfo[this.data.checkMeals].pid;
  313. data.quantity = initialSku.selectedNum;
  314. data.recommender = this.data.recommender;
  315. data.military = this.data.military ? 1 : 0,
  316. data.street = this.data.street;
  317. data.familyCommittee = this.data.familyCommittee;
  318. let age = this.data.mealsInfo[this.data.checkMeals].minAge + '-' + this.data.mealsInfo[this.data.checkMeals].maxAge;
  319. data.ageLimit = this.data.military == 1 ? age : this.data.ageLimit;
  320. data.presellOpen = this.data.presellOpen;
  321. data.btype = (this.data.presellOpen == 0 && this.data.mealsInfo[this.data.checkMeals].actnum <= 0 && this.data.mealsInfo[this.data.checkMeals].waitActNum > 0) ? 3 : -1;
  322. this.triggerEvent('alternateEmit', data)
  323. } else {
  324. let data = [];
  325. data.push('aid=' + this.data.aid)
  326. data.push('atype=' + this.data.atype)
  327. data.push('createGroup=' + this.data.createGroup)
  328. data.push('qptype=' + this.data.qptype)
  329. data.push('groupid=' + this.data.groupid)
  330. data.push('sessionsIndex=' + this.data.sessionInfo[this.data.checkSession].sid)
  331. data.push('mealsIndex=' + this.data.mealsInfo[this.data.checkMeals].pid)
  332. data.push('military=' + (this.data.military ? 1 : 0))
  333. data.push('quantity=' + initialSku.selectedNum)
  334. let age = this.data.mealsInfo[this.data.checkMeals].minAge + '-' + this.data.mealsInfo[this.data.checkMeals].maxAge;
  335. data.push('ageLimit=' + (this.data.military == 1 ? age : this.data.ageLimit))
  336. data.push('recommender=' + this.data.recommender)
  337. data.push('street=' + this.data.street)
  338. data.push('familyCommittee=' + this.data.familyCommittee)
  339. data.push('presellOpen=' + this.data.presellOpen)
  340. 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))
  341. wx.navigateTo({
  342. url: '/pages/product/buy/index?' + data.join('&')
  343. })
  344. }
  345. },
  346. }
  347. })