index.js 12 KB

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