index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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']: this.data.productAttr.length ? 0 : 1,
  116. attrValue: '',
  117. attr: '请选择'
  118. });
  119. }
  120. this.setData({ productAttr: productAttr });
  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. //设置默认数据
  143. if (productSelect.cart_num == undefined) productSelect.cart_num = 1;
  144. //数量+
  145. console.log(this.data.productSelect.cart_num);
  146. if (changeValue) {
  147. productSelect.cart_num++;
  148. //大于库存时,等于库存
  149. if (quotaShow >= productStock) {
  150. if (productSelect.cart_num >= productStock) productSelect.cart_num = productStock;
  151. } else {
  152. if (productSelect.cart_num >= quotaShow) productSelect.cart_num = quotaShow;
  153. }
  154. this.setData({
  155. ['productSelect.cart_num']: productSelect.cart_num,
  156. cart_num: productSelect.cart_num
  157. });
  158. } else {
  159. //数量减
  160. productSelect.cart_num--;
  161. //小于1时,等于1
  162. if (productSelect.cart_num < 1) productSelect.cart_num = 1;
  163. this.setData({
  164. ['productSelect.cart_num']: productSelect.cart_num,
  165. cart_num: productSelect.cart_num
  166. });
  167. }
  168. },
  169. /**
  170. * 属性变动赋值
  171. *
  172. */
  173. ChangeAttr: function (e) {
  174. var values = e.detail;
  175. var productSelect = this.data.productValue[values];
  176. var storeInfo = this.data.storeInfo;
  177. this.setData({
  178. cart_num: 1
  179. });
  180. if (productSelect) {
  181. this.setData({
  182. ["productSelect.image"]: productSelect.image,
  183. ["productSelect.price"]: productSelect.price,
  184. ["productSelect.quota"]: productSelect.quota,
  185. ["productSelect.stock"]: productSelect.stock,
  186. ["productSelect.quota_show"]: productSelect.quota_show,
  187. ["productSelect.product_stock"]: productSelect.product_stock,
  188. ['productSelect.unique']: productSelect.unique,
  189. ['productSelect.cart_num']: 1,
  190. attrValue: values,
  191. attr: '已选择'
  192. });
  193. } else {
  194. this.setData({
  195. ["productSelect.image"]: storeInfo.image,
  196. ["productSelect.price"]: storeInfo.price,
  197. ["productSelect.quota_show"]: 0,
  198. ["productSelect.quota"]: 0,
  199. ['productSelect.unique']: '',
  200. ['productSelect.cart_num']: 0,
  201. attrValue: '',
  202. attr: '请选择'
  203. });
  204. }
  205. },
  206. setProductSelect: function () {
  207. var that = this;
  208. if (that.data.productSelect.length == 0) {
  209. that.setData({
  210. ['productSelect.image']: that.data.storeInfo.image,
  211. ['productSelect.store_name']: that.data.storeInfo.title,
  212. ['productSelect.price']: that.data.storeInfo.price,
  213. ['productSelect.quota']: that.data.storeInfo.stock,
  214. ['productSelect.unique']: '',
  215. ['productSelect.cart_num']: 1
  216. })
  217. }
  218. },
  219. // 打开海报页面
  220. getPinkPoster: function () {
  221. var that = this;
  222. wx.navigateTo({
  223. url: '/pages/activity/poster-poster/index?type=2&id=' + that.data.pinkId,
  224. });
  225. },
  226. /**
  227. * 获取拼团
  228. */
  229. getPink:function(){
  230. var that = this, id = that.data.pinkId;
  231. getCombinationPink(id).then(function(res){
  232. var title ='开团';
  233. that.setData({
  234. count: parseInt(res.data.count),
  235. isOk: res.data.is_ok,
  236. pinkAll: res.data.pinkAll,
  237. current_pink_order: res.data.current_pink_order,
  238. pinkBool: res.data.pinkBool,
  239. pinkT: res.data.pinkT,
  240. storeCombination: res.data.store_combination,
  241. storeCombinationHost: res.data.store_combination_host,
  242. userBool: res.data.userBool,
  243. userInfo: res.data.userInfo,
  244. productAttr:res.data.store_combination.productAttr,
  245. storeInfo: res.data.store_combination,
  246. productValue: res.data.store_combination.productValue
  247. });
  248. that.setProductSelect();
  249. that.DefaultSelect();
  250. if (that.data.isOk && !that.data.count){
  251. title ='拼团成功,等待商家发货';//拼团完成
  252. } else if (that.data.isOk && that.data.count){
  253. title = '拼团失败';//拼团失败
  254. } else if (that.data.userBool && !that.data.isOk){
  255. title = that.data.pinkT.uid == that.data.userInfo.uid ? '开团成功' : '拼团成功';//本人开团成功
  256. wxh.time(that.data.pinkT.stop_time, that);
  257. } else if (!that.data.userBool && !that.data.isOk){
  258. title = '参团';//本人参团
  259. wxh.time(that.data.pinkT.stop_time, that);
  260. }
  261. that.setData({'parameter.title':title});
  262. });
  263. },
  264. /**
  265. * 再次开团
  266. */
  267. againPink:function(){
  268. return app.Tips('/pages/activity/goods_combination_details/index?id='+this.data.storeCombination.id);
  269. },
  270. /**
  271. * 控制属性弹窗
  272. */
  273. goPinkOrder:function(e){
  274. let that = this;
  275. that.setData({
  276. 'attribute.cartAttr': true
  277. });
  278. // let data = {
  279. // productId: that.data.storeCombination.product_id,
  280. // cartNum: that.data.pinkT.total_num,
  281. // uniqueId: '',
  282. // combinationId: that.data.storeCombination.id,
  283. // secKillId: 0
  284. // };
  285. // postCartAdd(data).then(function(res){
  286. // return app.Tips('/pages/order_confirm/index?cartId=' + res.data.cartId +'&pinkId='+that.data.pinkT.id);
  287. // }).catch(err=>{
  288. // return app.Tips({title:err});
  289. // });
  290. },
  291. /**
  292. * 下单
  293. */
  294. goCat: function () {
  295. var that = this;
  296. var productSelect = that.data.productValue[that.data.attrValue];
  297. //如果有属性,没有选择,提示用户选择
  298. if (that.data.productAttr.length && productSelect === undefined) return app.Tips({ title: '请选择属性' });
  299. var data = {
  300. productId: that.data.storeInfo.product_id,
  301. secKillId: 0,
  302. bargainId: 0,
  303. combinationId: that.data.storeCombination.id,
  304. cartNum: that.data.cart_num,
  305. uniqueId: productSelect !== undefined ? productSelect.unique : '',
  306. is_new: 1,
  307. };
  308. postCartAdd(data).then(function (res) {
  309. wx.navigateTo({ url: '/pages/order_confirm/index?cartId=' + res.data.cartId + '&pinkId=' + that.data.pinkT.id});
  310. }).catch(function(res){
  311. return app.Tips({ title: res });
  312. })
  313. },
  314. /**
  315. * 取消开团
  316. *
  317. */
  318. removePink:function(e){
  319. let that = this, data = {
  320. id: this.data.pinkId,
  321. cid: this.data.storeCombination.id,
  322. }
  323. postCombinationRemove(data).then(function(res){
  324. if(res.data.status){
  325. switch (res.data.status) {
  326. case '200':
  327. app.Tips({title:res.data.msg});
  328. that.getPink();
  329. break;
  330. }
  331. }else{
  332. return app.Tips({ title: res.msg, icon: 'success' }, { tab: 4, url:'/pages/order_list/index?is_return=1'});
  333. }
  334. }).catch(function (res) {
  335. return app.Tips({ title: res.msg }, { tab: 3, url: 1 });
  336. })
  337. },
  338. lookAll:function(){
  339. this.setData({iShidden: !this.data.iShidden})
  340. },
  341. /**
  342. * 生命周期函数--监听页面初次渲染完成
  343. */
  344. onReady: function () {
  345. },
  346. /**
  347. * 生命周期函数--监听页面显示
  348. */
  349. onShow: function () {
  350. if(this.data.isClose) this.getPink();
  351. },
  352. /**
  353. * 生命周期函数--监听页面隐藏
  354. */
  355. onHide: function () {
  356. this.setData({ isClose:1});
  357. },
  358. /**
  359. * 生命周期函数--监听页面卸载
  360. */
  361. onUnload: function () {
  362. },
  363. /**
  364. * 页面相关事件处理函数--监听用户下拉动作
  365. */
  366. onPullDownRefresh: function () {
  367. },
  368. /**
  369. * 页面上拉触底事件的处理函数
  370. */
  371. onReachBottom: function () {
  372. },
  373. /**
  374. * 用户点击右上角分享
  375. */
  376. onShareAppMessage: function () {
  377. return {
  378. title: this.data.userInfo.nickname + '邀请您参团',
  379. path: '/pages/activity/goods_combination_status/index?id=' + this.data.pinkId,
  380. imageUrl: this.data.storeCombination.image,
  381. success: function (){
  382. return app.Tips({ title: '分享成功',icon: 'success'});
  383. }
  384. };
  385. }
  386. })