index.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. const Sharing = require('../../../utils/extend/sharing.js');
  2. const Dialog = require('../../../components/dialog/dialog');
  3. const util = require('../../../utils/util.js');
  4. const App = getApp()
  5. // 记录规格的数组
  6. let goodsSpecArr = [];
  7. Page({
  8. data: {
  9. detail: {}, // 拼单详情
  10. goodsList: [], // 更多拼团列表
  11. setting: {}, // 拼团设置
  12. is_join: false, // 当前用户是否已参团
  13. is_creator: false, // 当前是否为创建者(团长)
  14. goods_price: 0, // 商品价格
  15. line_price: 0, // 划线价格
  16. stock_num: 0, // 库存数量
  17. goods_num: 1, // 商品数量
  18. goods_sku_id: 0, // 规格id
  19. goodsMultiSpec: {}, // 多规格信息
  20. },
  21. /**
  22. * 生命周期函数--监听页面加载
  23. */
  24. onLoad(option) {
  25. let _this = this;
  26. // 获取拼团详情
  27. _this.getActiveDetail(option.active_id);
  28. // 获取拼团设置
  29. _this.getSetting();
  30. },
  31. /**
  32. * 点击立即分享,请求订阅消息
  33. */
  34. onShare() {
  35. let _this = this;
  36. _this._onRequestSubscribeMessage();
  37. },
  38. /**
  39. * 订阅消息 => [拼团进度通知]
  40. */
  41. _onRequestSubscribeMessage(callback) {
  42. let _this = this;
  43. let tmplItem = _this.data.setting.order_submsg.active_status;
  44. if (tmplItem.length == 0) {
  45. callback && callback();
  46. return;
  47. }
  48. wx.requestSubscribeMessage({
  49. tmplIds: [tmplItem],
  50. success(res) {},
  51. fail(res) {},
  52. complete(res) {
  53. callback && callback();
  54. },
  55. });
  56. },
  57. /**
  58. * 获取拼团详情
  59. */
  60. getActiveDetail(active_id) {
  61. let _this = this;
  62. App._get('sharing.active/detail', {
  63. active_id
  64. }, result => {
  65. // 创建当前页面数据
  66. _this.createPageData(result.data);
  67. });
  68. },
  69. /**
  70. * 创建页面数据
  71. */
  72. createPageData(data) {
  73. let _this = this;
  74. // 商品详情
  75. let goodsDetail = data.goods;
  76. // 当前用户是否已参团
  77. data['is_join'] = _this.checkUserIsJoin(data.detail.users);
  78. console.log(data['is_join']);
  79. // 当前用户是否为创建者
  80. data['is_creator'] = !!(data.detail.creator_id == App.getUserId())
  81. // 商品价格/划线价/库存
  82. data.goods_sku_id = goodsDetail.goods_sku.spec_sku_id;
  83. data.sharing_price = goodsDetail.goods_sku.sharing_price;
  84. data.goods_price = goodsDetail.goods_sku.goods_price;
  85. data.line_price = goodsDetail.goods_sku.line_price;
  86. data.stock_num = goodsDetail.goods_sku.stock_num;
  87. // 商品封面图(确认弹窗)
  88. data.skuCoverImage = goodsDetail.goods_image;
  89. // 多规格商品封面图(确认弹窗)
  90. if (goodsDetail.spec_type == 20 && goodsDetail.goods_sku['image']) {
  91. data.skuCoverImage = goodsDetail.goods_sku['image']['file_path'];
  92. }
  93. // 初始化商品多规格
  94. if (goodsDetail.spec_type == 20) {
  95. data.goodsMultiSpec = _this._initGoodsDetailData(goodsDetail.goods_multi_spec);
  96. }
  97. // 赋值页面数据
  98. _this.setData(data);
  99. },
  100. /**
  101. * 初始化商品多规格
  102. */
  103. _initGoodsDetailData(data) {
  104. goodsSpecArr = [];
  105. for (let i in data.spec_attr) {
  106. for (let j in data.spec_attr[i].spec_items) {
  107. if (j < 1) {
  108. data.spec_attr[i].spec_items[0].checked = true;
  109. goodsSpecArr[i] = data.spec_attr[i].spec_items[0].item_id;
  110. }
  111. }
  112. }
  113. return data;
  114. },
  115. /**
  116. * 点击切换不同规格
  117. */
  118. onSwitchSpec(e) {
  119. let _this = this,
  120. attrIdx = e.currentTarget.dataset.attrIdx,
  121. itemIdx = e.currentTarget.dataset.itemIdx,
  122. goodsMultiSpec = _this.data.goodsMultiSpec;
  123. for (let i in goodsMultiSpec.spec_attr) {
  124. for (let j in goodsMultiSpec.spec_attr[i].spec_items) {
  125. if (attrIdx == i) {
  126. goodsMultiSpec.spec_attr[i].spec_items[j].checked = false;
  127. if (itemIdx == j) {
  128. goodsMultiSpec.spec_attr[i].spec_items[itemIdx].checked = true;
  129. goodsSpecArr[i] = goodsMultiSpec.spec_attr[i].spec_items[itemIdx].item_id;
  130. }
  131. }
  132. }
  133. }
  134. _this.setData({
  135. goodsMultiSpec
  136. });
  137. // 更新商品规格信息
  138. _this._updateSpecGoods();
  139. },
  140. /**
  141. * 更新商品规格信息
  142. */
  143. _updateSpecGoods() {
  144. let _this = this,
  145. specSkuId = goodsSpecArr.join('_');
  146. // 查找skuItem
  147. let spec_list = _this.data.goodsMultiSpec.spec_list,
  148. skuItem = spec_list.find((val) => {
  149. return val.spec_sku_id == specSkuId;
  150. });
  151. // 记录goods_sku_id
  152. // 更新商品价格、划线价、库存
  153. if (typeof skuItem === 'object') {
  154. _this.setData({
  155. goods_sku_id: skuItem.spec_sku_id,
  156. goods_price: skuItem.form.goods_price,
  157. sharing_price: skuItem.form.sharing_price,
  158. line_price: skuItem.form.line_price,
  159. stock_num: skuItem.form.stock_num,
  160. skuCoverImage: skuItem.form.image_id > 0 ? skuItem.form.image_path : _this.data.goods.goods_image
  161. });
  162. }
  163. },
  164. /**
  165. * 验证当前用户是否已参团
  166. */
  167. checkUserIsJoin(users) {
  168. let user_id = App.getUserId();
  169. if (!user_id) {
  170. return false;
  171. }
  172. let isJoin = false;
  173. users.forEach((item) => {
  174. user_id == item.user_id && (isJoin = true);
  175. });
  176. return isJoin;
  177. },
  178. /**
  179. * 获取拼团设置
  180. */
  181. getSetting() {
  182. let _this = this;
  183. Sharing.getSetting(setting => {
  184. _this.setData({
  185. setting
  186. });
  187. });
  188. },
  189. /**
  190. * 小于10的格式化函数
  191. */
  192. timeFormat(param) {
  193. return param < 10 ? '0' + param : param;
  194. },
  195. /**
  196. * 查看拼团规则
  197. */
  198. onTargetRules() {
  199. wx.navigateTo({
  200. url: '../rules/index',
  201. })
  202. },
  203. /**
  204. * 显示拼团规则
  205. */
  206. onToggleRules() {
  207. let _this = this;
  208. Dialog({
  209. title: '拼团规则',
  210. message: _this.data.setting.basic.rule_detail,
  211. selector: '#zan-base-dialog',
  212. buttons: [{
  213. text: '关闭',
  214. color: 'red',
  215. type: 'cash'
  216. }]
  217. });
  218. },
  219. /**
  220. * 跳转商品详情页
  221. */
  222. onTargetGoods(e) {
  223. let goodsId = e.currentTarget.dataset.id > 0 ? e.currentTarget.dataset.id : this.data.detail.goods_id;
  224. wx.navigateTo({
  225. url: '../goods/index?goods_id=' + goodsId,
  226. });
  227. },
  228. /**
  229. * 增加商品数量
  230. */
  231. onIncGoodsNumber(e) {
  232. let _this = this;
  233. _this.setData({
  234. goods_num: ++_this.data.goods_num
  235. })
  236. },
  237. /**
  238. * 减少商品数量
  239. */
  240. onDecGoodsNumber(e) {
  241. let _this = this;
  242. if (_this.data.goods_num > 1) {
  243. _this.setData({
  244. goods_num: --_this.data.goods_num
  245. });
  246. }
  247. },
  248. /**
  249. * 自定义输入商品数量
  250. */
  251. onInputGoodsNum(e) {
  252. let _this = this,
  253. iptValue = e.detail.value;
  254. if (!util.isPositiveInteger(iptValue) && iptValue !== '') {
  255. iptValue = 1;
  256. }
  257. _this.setData({
  258. goods_num: iptValue
  259. });
  260. },
  261. /**
  262. * 立即参团
  263. */
  264. onCheckout(e) {
  265. let _this = this;
  266. // 表单验证
  267. if (!_this._onVerify()) {
  268. return false;
  269. }
  270. // 立即参团
  271. const onCommitCallback = () => {
  272. wx.navigateTo({
  273. url: '../checkout/index?' + util.urlEncode({
  274. order_type: 20,
  275. active_id: _this.data.detail.active_id,
  276. goods_id: _this.data.goods.goods_id,
  277. goods_num: _this.data.goods_num,
  278. goods_sku_id: _this.data.goods_sku_id,
  279. })
  280. });
  281. };
  282. // 请求用户订阅消息
  283. _this._onRequestSubscribeMessage(onCommitCallback);
  284. },
  285. /**
  286. * 表单验证
  287. */
  288. _onVerify() {
  289. let _this = this;
  290. if (_this.data.goods_num === '') {
  291. App.showError('请输入购买数量');
  292. return false;
  293. }
  294. // 将购买数量转为整型,防止出错
  295. _this.setData({
  296. goods_num: parseInt(_this.data.goods_num)
  297. });
  298. if (_this.data.goods_num <= 0) {
  299. App.showError('购买数量不能为0');
  300. return false;
  301. }
  302. return true;
  303. },
  304. /**
  305. * 预览Sku规格图片
  306. */
  307. onPreviewSkuImage(e) {
  308. let _this = this;
  309. wx.previewImage({
  310. current: _this.data.skuCoverImage,
  311. urls: [_this.data.skuCoverImage]
  312. })
  313. },
  314. /**
  315. * 确认购买弹窗
  316. */
  317. onToggleTrade(e) {
  318. this.setData({
  319. showBottomPopup: !this.data.showBottomPopup
  320. });
  321. },
  322. /**
  323. * 立即下单
  324. */
  325. onTriggerOrder(e) {
  326. let _this = this;
  327. _this.onToggleTrade();
  328. },
  329. /**
  330. * 跳转到拼团首页
  331. */
  332. onTargetIndex(e) {
  333. wx.navigateTo({
  334. url: '../index/index',
  335. })
  336. },
  337. /**
  338. * 分享当前页面
  339. */
  340. onShareAppMessage() {
  341. const _this = this;
  342. // 构建页面参数
  343. const params = App.getShareUrlParams({
  344. 'active_id': _this.data.detail.active_id
  345. });
  346. return {
  347. title: _this.data.goods.goods_name,
  348. path: "/pages/sharing/active/index?" + params
  349. };
  350. },
  351. /**
  352. * 分享到朋友圈
  353. * 本接口为 Beta 版本,暂只在 Android 平台支持,详见分享到朋友圈 (Beta)
  354. * https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share-timeline.html
  355. */
  356. onShareTimeline() {
  357. const _this = this;
  358. // 构建页面参数
  359. const params = App.getShareUrlParams({
  360. 'active_id': _this.data.detail.active_id
  361. });
  362. return {
  363. title: _this.data.goods.goods_name,
  364. path: "/pages/sharing/active/index?" + params
  365. };
  366. },
  367. })