index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. // pages/group-con/index.js
  2. import {
  3. getCombinationPink,
  4. postCombinationRemove
  5. } from '../../../api/activity.js';
  6. import {
  7. postCartAdd
  8. } from '../../../api/store.js';
  9. import wxh from '../../../utils/wxh.js';
  10. import util from '../../../utils/util.js';
  11. const app = getApp();
  12. Page({
  13. /**
  14. * 页面的初始数据
  15. */
  16. data: {
  17. parameter: {
  18. 'navbar': '1',
  19. 'return': '1',
  20. 'title': '开团',
  21. 'color': false,
  22. },
  23. countDownHour: '00',
  24. countDownMinute: '00',
  25. countDownSecond: '00',
  26. isHidden: false,
  27. count: 0, //还差多少人拼团完成
  28. isOk: 0, //是否拼团完成
  29. pinkAll: [], //当前拼团列表
  30. pinkBool: 0,
  31. pinkT: {}, //团长信息
  32. storeCombination: {}, //当前拼团产品详情
  33. userBool: 0, //是否为本人开团
  34. current_pink_order: '', //当前订单号
  35. userInfo: {},
  36. isClose: 0,
  37. productAttr: [],
  38. productSelect: [],
  39. productValue: [],
  40. storeInfo: {},
  41. iSbnt: 1,
  42. limitNum: 1,
  43. iSplus: false,
  44. attribute: {
  45. 'cartAttr': false
  46. },
  47. },
  48. /**
  49. * 生命周期函数--监听页面加载
  50. */
  51. onLoad: function (options) {
  52. //扫码携带参数处理
  53. if (options.scene) {
  54. var value = util.getUrlParams(decodeURIComponent(options.scene));
  55. if (value.id) options.id = value.id;
  56. //记录推广人uid
  57. if (value.pid) app.globalData.spid = value.pid;
  58. }
  59. if (!options.id) return app.Tips({
  60. title: '缺少参数'
  61. }, {
  62. tab: 3,
  63. url: 1
  64. });
  65. this.setData({
  66. pinkId: options.id
  67. });
  68. },
  69. onMyEvent: function (e) {
  70. this.setData({
  71. 'attribute.cartAttr': e.detail.window
  72. })
  73. },
  74. /**
  75. * 授权回调
  76. */
  77. onLoadFun: function () {
  78. this.getPink();
  79. },
  80. /**
  81. * 购物车手动填写
  82. */
  83. iptCartNum: function (e) {
  84. this.data.productSelect.cart_num = e.detail;
  85. this.setData({
  86. productSelect: this.data.productSelect,
  87. cart_num: e.detail
  88. })
  89. },
  90. /**
  91. * 默认选中属性
  92. */
  93. DefaultSelect: function () {
  94. var productAttr = this.data.productAttr,
  95. storeInfo = this.data.storeInfo,
  96. productValue = this.data.productValue,
  97. value = [];
  98. for (var key in productValue) {
  99. if (productValue[key].quota > 0 && productValue[key].product_stock > 0) {
  100. value = this.data.productAttr.length ? key.split(",") : [];
  101. break;
  102. }
  103. }
  104. for (var i = 0, len = productAttr.length; i < len; i++) {
  105. if (productAttr[i].attr_value[0]) productAttr[i].checked = value[i];
  106. }
  107. var productSelect = this.data.productValue[value.sort().join(',')];
  108. if (productSelect) {
  109. this.setData({
  110. ["productSelect.store_name"]: storeInfo.store_name,
  111. ["productSelect.image"]: productSelect.image,
  112. ["productSelect.price"]: productSelect.price,
  113. ["productSelect.quota"]: productSelect.quota,
  114. ["productSelect.stock"]: productSelect.stock,
  115. ["productSelect.quota_show"]: productSelect.quota_show,
  116. ["productSelect.product_stock"]: productSelect.product_stock,
  117. ['productSelect.unique']: productSelect.unique,
  118. ['productSelect.cart_num']: 1,
  119. attrValue: value,
  120. attr: '已选择'
  121. });
  122. } else {
  123. this.setData({
  124. ["productSelect.store_name"]: storeInfo.store_name,
  125. ["productSelect.image"]: storeInfo.image,
  126. ["productSelect.price"]: storeInfo.price,
  127. ["productSelect.quota_show"]: 0,
  128. ["productSelect.quota"]: 0,
  129. ['productSelect.unique']: '',
  130. ['productSelect.cart_num']: 1,
  131. attrValue: '',
  132. attr: '请选择'
  133. });
  134. }
  135. this.setData({
  136. productAttr: productAttr,
  137. ["productSelect.num"]: storeInfo.num,
  138. cart_num: 1
  139. });
  140. },
  141. /**
  142. * 购物车数量加和数量减
  143. */
  144. ChangeCartNum: function (e) {
  145. //是否 加|减
  146. var changeValue = e.detail;
  147. //获取当前变动属性
  148. var productSelect = this.data.productValue[this.data.attrValue];
  149. if (this.data.cart_num) {
  150. productSelect.cart_num = this.data.cart_num;
  151. }
  152. //如果没有属性,赋值给商品默认库存
  153. if (productSelect === undefined && !this.data.productAttr.length) productSelect = this.data.productSelect;
  154. //不存在不加数量
  155. if (productSelect === undefined) return;
  156. //提取库存
  157. var stock = productSelect.stock || 0;
  158. var quotaShow = productSelect.quota_show || 0;
  159. var productStock = productSelect.product_stock || 0;
  160. var quota = productSelect.quota || 0;
  161. var num = this.data.storeInfo.num || 0;
  162. //设置默认数据
  163. if (productSelect.cart_num == undefined) productSelect.cart_num = 1;
  164. //数量+
  165. if (changeValue) {
  166. productSelect.cart_num++;
  167. //大于库存时,等于库存
  168. let arrMin = [];
  169. arrMin.push(num);
  170. arrMin.push(quota);
  171. arrMin.push(productStock);
  172. let minN = Math.min.apply(null, arrMin);
  173. if (productSelect.cart_num >= minN) productSelect.cart_num = minN ? minN : 1;
  174. // if (quotaShow >= productStock) {
  175. // if (productSelect.cart_num >= productStock) productSelect.cart_num = productStock;
  176. // } else {
  177. // if (productSelect.cart_num >= quotaShow) productSelect.cart_num = quotaShow;
  178. // }
  179. this.setData({
  180. ['productSelect.cart_num']: productSelect.cart_num,
  181. cart_num: productSelect.cart_num
  182. });
  183. } else {
  184. //数量减
  185. productSelect.cart_num--;
  186. //小于1时,等于1
  187. if (productSelect.cart_num < 1) productSelect.cart_num = 1;
  188. this.setData({
  189. ['productSelect.cart_num']: productSelect.cart_num,
  190. cart_num: productSelect.cart_num
  191. });
  192. }
  193. },
  194. /**
  195. * 属性变动赋值
  196. */
  197. ChangeAttr: function (e) {
  198. var values = e.detail;
  199. var productSelect = this.data.productValue[values];
  200. var storeInfo = this.data.storeInfo;
  201. this.setData({
  202. cart_num: 1,
  203. ["productSelect.num"]: storeInfo.num
  204. });
  205. if (productSelect) {
  206. this.setData({
  207. ["productSelect.image"]: productSelect.image,
  208. ["productSelect.price"]: productSelect.price,
  209. ["productSelect.quota"]: productSelect.quota,
  210. ["productSelect.stock"]: productSelect.stock,
  211. ["productSelect.quota_show"]: productSelect.quota_show,
  212. ["productSelect.product_stock"]: productSelect.product_stock,
  213. ['productSelect.unique']: productSelect.unique,
  214. ['productSelect.cart_num']: 1,
  215. attrValue: values,
  216. attr: '已选择'
  217. });
  218. } else {
  219. this.setData({
  220. ["productSelect.image"]: storeInfo.image,
  221. ["productSelect.price"]: storeInfo.price,
  222. ["productSelect.quota_show"]: 0,
  223. ["productSelect.quota"]: 0,
  224. ['productSelect.unique']: '',
  225. ['productSelect.cart_num']: 1,
  226. attrValue: '',
  227. attr: '请选择'
  228. });
  229. }
  230. },
  231. setProductSelect: function () {
  232. var that = this;
  233. if (that.data.productSelect.length == 0) {
  234. that.setData({
  235. ['productSelect.image']: that.data.storeInfo.image,
  236. ['productSelect.store_name']: that.data.storeInfo.title,
  237. ['productSelect.price']: that.data.storeInfo.price,
  238. ['productSelect.quota']: that.data.storeInfo.stock,
  239. ['productSelect.unique']: '',
  240. ["productSelect.num"]: that.data.storeInfo.num,
  241. ['productSelect.cart_num']: 1,
  242. cart_num: 1
  243. })
  244. }
  245. },
  246. // 打开海报页面
  247. getPinkPoster: function () {
  248. var that = this;
  249. wx.navigateTo({
  250. url: '/pages/activity/poster-poster/index?type=2&id=' + that.data.pinkId,
  251. });
  252. },
  253. /**
  254. * 获取拼团
  255. */
  256. getPink: function () {
  257. var that = this,
  258. id = that.data.pinkId;
  259. getCombinationPink(id).then(function (res) {
  260. var title = '开团';
  261. that.setData({
  262. count: parseInt(res.data.count),
  263. isOk: res.data.is_ok,
  264. pinkAll: res.data.pinkAll,
  265. current_pink_order: res.data.current_pink_order,
  266. pinkBool: res.data.pinkBool,
  267. pinkT: res.data.pinkT,
  268. storeCombination: res.data.store_combination,
  269. storeCombinationHost: res.data.store_combination_host,
  270. userBool: res.data.userBool,
  271. userInfo: res.data.userInfo,
  272. productAttr: res.data.store_combination.productAttr,
  273. storeInfo: res.data.store_combination,
  274. productValue: res.data.store_combination.productValue
  275. });
  276. that.setProductSelect();
  277. that.DefaultSelect();
  278. if (that.data.isOk && !that.data.count) {
  279. title = '拼团成功,等待商家发货'; //拼团完成
  280. } else if (that.data.isOk && that.data.count) {
  281. title = '拼团失败'; //拼团失败
  282. } else if (that.data.userBool && !that.data.isOk) {
  283. title = that.data.pinkT.uid == that.data.userInfo.uid ? '开团成功' : '拼团成功'; //本人开团成功
  284. wxh.time(that.data.pinkT.stop_time, that);
  285. } else if (!that.data.userBool && !that.data.isOk) {
  286. title = '参团'; //本人参团
  287. wxh.time(that.data.pinkT.stop_time, that);
  288. }
  289. that.setData({
  290. 'parameter.title': title
  291. });
  292. });
  293. },
  294. /**
  295. * 再次开团
  296. */
  297. againPink: function () {
  298. return app.Tips('/pages/activity/goods_combination_details/index?id=' + this.data.storeCombination.id);
  299. },
  300. /**
  301. * 控制属性弹窗
  302. */
  303. goPinkOrder: function (e) {
  304. let that = this;
  305. that.setData({
  306. 'attribute.cartAttr': true
  307. });
  308. // let data = {
  309. // productId: that.data.storeCombination.product_id,
  310. // cartNum: that.data.pinkT.total_num,
  311. // uniqueId: '',
  312. // combinationId: that.data.storeCombination.id,
  313. // secKillId: 0
  314. // };
  315. // postCartAdd(data).then(function(res){
  316. // return app.Tips('/pages/order_confirm/index?cartId=' + res.data.cartId +'&pinkId='+that.data.pinkT.id);
  317. // }).catch(err=>{
  318. // return app.Tips({title:err});
  319. // });
  320. },
  321. /**
  322. * 下单
  323. */
  324. goCat: function () {
  325. var that = this;
  326. var productSelect = that.data.productValue[that.data.attrValue];
  327. //如果有属性,没有选择,提示用户选择
  328. if (that.data.productAttr.length && productSelect === undefined) return app.Tips({
  329. title: '请选择属性'
  330. });
  331. var data = {
  332. productId: that.data.storeInfo.product_id,
  333. secKillId: 0,
  334. bargainId: 0,
  335. combinationId: that.data.storeCombination.id,
  336. cartNum: that.data.cart_num,
  337. uniqueId: productSelect !== undefined ? productSelect.unique : '',
  338. is_new: 1,
  339. };
  340. postCartAdd(data).then(function (res) {
  341. wx.navigateTo({
  342. url: '/pages/order_confirm/index?cartId=' + res.data.cartId + '&pinkId=' + that.data.pinkT.id
  343. });
  344. }).catch(function (res) {
  345. return app.Tips({
  346. title: res
  347. });
  348. })
  349. },
  350. /**
  351. * 取消开团
  352. */
  353. removePink: function (e) {
  354. let that = this,
  355. data = {
  356. id: this.data.pinkId,
  357. cid: this.data.storeCombination.id,
  358. }
  359. postCombinationRemove(data).then(function (res) {
  360. if (res.data.status) {
  361. switch (res.data.status) {
  362. case '200':
  363. app.Tips({
  364. title: res.data.msg
  365. });
  366. that.getPink();
  367. break;
  368. }
  369. } else {
  370. return app.Tips({
  371. title: res.msg,
  372. icon: 'success'
  373. }, {
  374. tab: 4,
  375. url: '/pages/order_list/index?is_return=1'
  376. });
  377. }
  378. }).catch(function (res) {
  379. return app.Tips({
  380. title: res.msg
  381. }, {
  382. tab: 3,
  383. url: 1
  384. });
  385. })
  386. },
  387. lookAll: function () {
  388. this.setData({
  389. isHidden: !this.data.isHidden
  390. })
  391. },
  392. /**
  393. * 生命周期函数--监听页面初次渲染完成
  394. */
  395. onReady: function () {
  396. },
  397. /**
  398. * 生命周期函数--监听页面显示
  399. */
  400. onShow: function () {
  401. if (this.data.isClose) this.getPink();
  402. },
  403. /**
  404. * 生命周期函数--监听页面隐藏
  405. */
  406. onHide: function () {
  407. this.setData({
  408. isClose: 1
  409. });
  410. },
  411. /**
  412. * 生命周期函数--监听页面卸载
  413. */
  414. onUnload: function () {
  415. },
  416. /**
  417. * 页面相关事件处理函数--监听用户下拉动作
  418. */
  419. onPullDownRefresh: function () {
  420. },
  421. /**
  422. * 页面上拉触底事件的处理函数
  423. */
  424. onReachBottom: function () {
  425. },
  426. /**
  427. * 用户点击右上角分享
  428. */
  429. onShareAppMessage: function () {
  430. return {
  431. title: this.data.userInfo.nickname + '邀请您参团',
  432. path: '/pages/activity/goods_combination_status/index?id=' + this.data.pinkId,
  433. imageUrl: this.data.storeCombination.image,
  434. success: function () {
  435. return app.Tips({
  436. title: '分享成功',
  437. icon: 'success'
  438. });
  439. }
  440. };
  441. }
  442. })