index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. import wxParse from '../../../wxParse/wxParse.js';
  2. import Dialog from '../../../components/dialog/dialog';
  3. const App = getApp()
  4. let goodsSpecArr = []
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. indicatorDots: true, // 是否显示面板指示点
  11. autoplay: true, // 是否自动切换
  12. interval: 3000, // 自动切换时间间隔
  13. duration: 800, // 滑动动画时长
  14. currentIndex: 1, // 轮播图指针
  15. floorstatus: false, // 返回顶部
  16. detail: {}, // 商品详情信息
  17. goods_price: 0, // 商品价格
  18. line_price: 0, // 划线价格
  19. stock_num: 0, // 库存数量
  20. goods_num: 1, // 商品数量
  21. goods_sku_id: 0, // 规格id
  22. cart_total_num: 0, // 购物车商品总数量
  23. goodsMultiSpec: {}, // 多规格信息
  24. // 分享按钮组件
  25. share: {
  26. show: false,
  27. cancelWithMask: true,
  28. cancelText: '关闭',
  29. actions: [{
  30. name: '生成商品海报',
  31. className: 'action-class',
  32. loading: false
  33. }, {
  34. name: '发送给朋友',
  35. openType: 'share'
  36. }],
  37. // 商品海报
  38. showPopup: false,
  39. },
  40. // 返回顶部
  41. showTopWidget: false,
  42. countDownTime: false, // 倒计时日期
  43. active: {}, // 砍价活动详情
  44. goods: {}, // 商品详情
  45. setting: {}, // 砍价配置
  46. is_partake: false, // 当前用户是否正在参与
  47. task_id: false, // 当前用户正在参与的任务id
  48. },
  49. /**
  50. * 生命周期函数--监听页面加载
  51. */
  52. onLoad(options) {
  53. let _this = this,
  54. scene = App.getSceneData(options);
  55. // 砍价商品id
  56. _this.setData({
  57. active_id: options.active_id ? options.active_id : scene.aid
  58. });
  59. },
  60. /**
  61. * 生命周期函数--监听页面显示
  62. */
  63. onShow(options) {
  64. let _this = this;
  65. // 获取砍价商品信息
  66. _this.getActiveDetail();
  67. },
  68. /**
  69. * 获取砍价商品信息
  70. */
  71. getActiveDetail() {
  72. let _this = this;
  73. App._get('bargain.active/detail', {
  74. active_id: _this.data.active_id
  75. }, (result) => {
  76. // 初始化详情数据
  77. const data = result.data
  78. _this._initData(data)
  79. // 初始化倒计时组件
  80. _this._initCountDownData(data)
  81. });
  82. },
  83. /**
  84. * 初始化详情数据
  85. */
  86. _initData(data) {
  87. let _this = this;
  88. // 商品详情
  89. let goodsDetail = data.goods;
  90. // 富文本转码
  91. if (goodsDetail.content.length > 0) {
  92. wxParse.wxParse('content', 'html', goodsDetail.content, _this, 0);
  93. }
  94. // 商品价格/划线价/库存
  95. data.goods_sku_id = goodsDetail.goods_sku.spec_sku_id;
  96. data.goods_price = goodsDetail.goods_sku.goods_price;
  97. data.line_price = goodsDetail.goods_sku.line_price;
  98. data.stock_num = goodsDetail.goods_sku.stock_num;
  99. // 商品封面图(确认弹窗)
  100. data.skuCoverImage = goodsDetail.goods_image;
  101. // 多规格商品封面图(确认弹窗)
  102. if (goodsDetail.spec_type == 20 && goodsDetail.goods_sku['image']) {
  103. data.skuCoverImage = goodsDetail.goods_sku['image']['file_path'];
  104. }
  105. // 初始化商品多规格
  106. if (goodsDetail.spec_type == 20) {
  107. data.goodsMultiSpec = _this._initManySpecData(goodsDetail.goods_multi_spec);
  108. }
  109. // 记录活动到期时间
  110. data.countDownObj = [{
  111. date: data.active.end_time
  112. }];
  113. _this.setData(data)
  114. data.countDownObj.date = data.active.end_time
  115. return data;
  116. },
  117. // 初始化倒计时组件
  118. _initCountDownData(data) {
  119. const app = this
  120. app.setData({
  121. countDownTime: data.active.end_time
  122. })
  123. },
  124. /**
  125. * 初始化商品多规格
  126. */
  127. _initManySpecData(data) {
  128. goodsSpecArr = [];
  129. for (let i in data.spec_attr) {
  130. for (let j in data.spec_attr[i].spec_items) {
  131. if (j < 1) {
  132. data.spec_attr[i].spec_items[0].checked = true;
  133. goodsSpecArr[i] = data.spec_attr[i].spec_items[0].item_id;
  134. }
  135. }
  136. }
  137. return data;
  138. },
  139. /**
  140. * 点击切换不同规格
  141. */
  142. onSwitchSpec(e) {
  143. let _this = this,
  144. attrIdx = e.currentTarget.dataset.attrIdx,
  145. itemIdx = e.currentTarget.dataset.itemIdx,
  146. goodsMultiSpec = _this.data.goodsMultiSpec;
  147. for (let i in goodsMultiSpec.spec_attr) {
  148. for (let j in goodsMultiSpec.spec_attr[i].spec_items) {
  149. if (attrIdx == i) {
  150. goodsMultiSpec.spec_attr[i].spec_items[j].checked = false;
  151. if (itemIdx == j) {
  152. goodsMultiSpec.spec_attr[i].spec_items[itemIdx].checked = true;
  153. goodsSpecArr[i] = goodsMultiSpec.spec_attr[i].spec_items[itemIdx].item_id;
  154. }
  155. }
  156. }
  157. }
  158. _this.setData({
  159. goodsMultiSpec
  160. });
  161. // 更新商品规格信息
  162. _this._updateSpecGoods();
  163. },
  164. /**
  165. * 更新商品规格信息
  166. */
  167. _updateSpecGoods() {
  168. let _this = this,
  169. specSkuId = goodsSpecArr.join('_');
  170. // 查找skuItem
  171. let spec_list = _this.data.goodsMultiSpec.spec_list,
  172. skuItem = spec_list.find((val) => {
  173. return val.spec_sku_id == specSkuId;
  174. });
  175. // 记录goods_sku_id
  176. // 更新商品价格、划线价、库存
  177. if (typeof skuItem === 'object') {
  178. _this.setData({
  179. goods_sku_id: skuItem.spec_sku_id,
  180. goods_price: skuItem.form.goods_price,
  181. line_price: skuItem.form.line_price,
  182. stock_num: skuItem.form.stock_num,
  183. skuCoverImage: skuItem.form.image_id > 0 ? skuItem.form.image_path : _this.data.goods.goods_image
  184. });
  185. }
  186. },
  187. /**
  188. * 设置轮播图当前指针 数字
  189. */
  190. setCurrent(e) {
  191. let _this = this;
  192. _this.setData({
  193. currentIndex: e.detail.current + 1
  194. });
  195. },
  196. /**
  197. * 浏览商品图片
  198. */
  199. onPreviewImages(e) {
  200. let _this = this;
  201. let index = e.currentTarget.dataset.index,
  202. imageUrls = [];
  203. _this.data.goods.image.forEach(item => {
  204. imageUrls.push(item.file_path);
  205. });
  206. wx.previewImage({
  207. current: imageUrls[index],
  208. urls: imageUrls
  209. })
  210. },
  211. /**
  212. * 预览Sku规格图片
  213. */
  214. onPreviewSkuImage(e) {
  215. let _this = this;
  216. wx.previewImage({
  217. current: _this.data.skuCoverImage,
  218. urls: [_this.data.skuCoverImage]
  219. })
  220. },
  221. /**
  222. * 跳转到评论
  223. */
  224. onTargetToComment() {
  225. let _this = this;
  226. wx.navigateTo({
  227. url: `../../goods/comment/comment?goods_id=${_this.data.goods.goods_id}`
  228. });
  229. },
  230. /**
  231. * 返回顶部
  232. */
  233. onScrollTop(t) {
  234. let _this = this;
  235. _this.setData({
  236. scrollTop: 0
  237. });
  238. },
  239. /**
  240. * 显示/隐藏 返回顶部按钮
  241. */
  242. onScrollEvent(e) {
  243. let _this = this;
  244. _this.setData({
  245. showTopWidget: e.detail.scrollTop > 200
  246. })
  247. },
  248. /**
  249. * 显示分享选项
  250. */
  251. onClickShare(e) {
  252. let _this = this;
  253. _this.setData({
  254. 'share.show': true
  255. });
  256. },
  257. /**
  258. * 关闭分享选项
  259. */
  260. onCloseShare() {
  261. let _this = this;
  262. _this.setData({
  263. 'share.show': false
  264. });
  265. },
  266. /**
  267. * 点击生成商品海报
  268. */
  269. onClickShareItem(e) {
  270. let _this = this;
  271. if (e.detail.index === 0) {
  272. // 显示商品海报
  273. _this._showPoster();
  274. }
  275. _this.onCloseShare();
  276. },
  277. /**
  278. * 切换商品海报
  279. */
  280. onTogglePopup() {
  281. let _this = this;
  282. _this.setData({
  283. 'share.showPopup': !_this.data.share.showPopup
  284. });
  285. },
  286. /**
  287. * 显示商品海报图
  288. */
  289. _showPoster() {
  290. let _this = this;
  291. wx.showLoading({
  292. title: '加载中',
  293. });
  294. App._get('bargain.active/poster', {
  295. active_id: _this.data.active_id
  296. }, (result) => {
  297. _this.setData(result.data, () => {
  298. _this.onTogglePopup();
  299. });
  300. }, null, () => {
  301. wx.hideLoading();
  302. });
  303. },
  304. /**
  305. * 保存海报图片
  306. */
  307. onSavePoster(e) {
  308. let _this = this;
  309. wx.showLoading({
  310. title: '加载中',
  311. });
  312. // 下载海报图片
  313. wx.downloadFile({
  314. url: _this.data.qrcode,
  315. success(res) {
  316. wx.hideLoading();
  317. // 图片保存到本地
  318. wx.saveImageToPhotosAlbum({
  319. filePath: res.tempFilePath,
  320. success(data) {
  321. wx.showToast({
  322. title: '保存成功',
  323. icon: 'success',
  324. duration: 2000
  325. });
  326. // 关闭商品海报
  327. _this.onTogglePopup();
  328. },
  329. fail(err) {
  330. console.log(err.errMsg);
  331. if (err.errMsg === 'saveImageToPhotosAlbum:fail auth deny') {
  332. wx.showToast({
  333. title: "请允许访问相册后重试",
  334. icon: "none",
  335. duration: 1000
  336. });
  337. setTimeout(() => {
  338. wx.openSetting();
  339. }, 1000);
  340. }
  341. },
  342. complete(res) {
  343. console.log('complete');
  344. // wx.hideLoading();
  345. }
  346. })
  347. }
  348. })
  349. },
  350. /**
  351. * 确认购买弹窗
  352. */
  353. onToggleTrade() {
  354. let _this = this;
  355. _this.setData({
  356. showBottomPopup: !_this.data.showBottomPopup
  357. });
  358. },
  359. /**
  360. * 显示砍价规则
  361. */
  362. onToggleRules(e) {
  363. // 显示砍价规则
  364. let _this = this;
  365. Dialog({
  366. title: '砍价规则',
  367. message: _this.data.setting.bargain_rules,
  368. selector: '#zan-base-dialog',
  369. isScroll: true, // 滚动
  370. buttons: [{
  371. text: '关闭',
  372. color: 'red',
  373. type: 'cash'
  374. }]
  375. });
  376. },
  377. /**
  378. * 确认砍价
  379. */
  380. onSubmit(e) {
  381. let _this = this;
  382. // 判断是否已参与当前的砍价活动,如果已参与的话跳转到砍价任务
  383. if (_this.data.is_partake) {
  384. wx.navigateTo({
  385. url: `../task/index?task_id=${_this.data.task_id}`,
  386. });
  387. return;
  388. }
  389. // 多规格商品,弹出选择器
  390. if (_this.data.goods.spec_type == 20) {
  391. _this.onToggleTrade();
  392. return;
  393. }
  394. // 确认发起砍价
  395. _this.onCheckout();
  396. },
  397. /**
  398. * 确认砍价(选择规格后的二次确认)
  399. */
  400. onSubmit2(e) {
  401. let _this = this;
  402. // 关闭选择器
  403. _this.onToggleTrade();
  404. // 确认发起砍价
  405. _this.onCheckout();
  406. },
  407. /**
  408. * 确认发起砍价
  409. */
  410. onCheckout() {
  411. let _this = this;
  412. if (_this.data.disabled) {
  413. return false;
  414. }
  415. // 显示loading
  416. wx.showLoading({
  417. title: '正在处理...'
  418. });
  419. // 创建砍价活动订单
  420. App._post_form('bargain.task/partake', {
  421. active_id: _this.data.active_id,
  422. goods_sku_id: _this.data.goods_sku_id,
  423. }, result => {
  424. // success
  425. console.log('success');
  426. // 创建成功,跳转到砍价任务详情
  427. wx.navigateTo({
  428. url: `../task/index?task_id=${result.data.task_id}`
  429. });
  430. }, result => {
  431. // fail
  432. console.log('fail');
  433. }, () => {
  434. // complete
  435. console.log('complete');
  436. wx.hideLoading();
  437. // 解除按钮禁用
  438. _this.data.disabled = false;
  439. });
  440. },
  441. /**
  442. * 跳转到首页
  443. */
  444. onTargetHome(e) {
  445. wx.switchTab({
  446. url: '../../index/index',
  447. })
  448. },
  449. /**
  450. * 分享当前页面
  451. */
  452. onShareAppMessage() {
  453. const _this = this;
  454. // 构建页面参数
  455. const params = App.getShareUrlParams({
  456. active_id: _this.data.active.active_id
  457. });
  458. return {
  459. title: _this.data.detail.goods_name,
  460. path: `/pages/bargain/goods/index?${params}`
  461. };
  462. },
  463. /**
  464. * 分享到朋友圈
  465. * 本接口为 Beta 版本,暂只在 Android 平台支持,详见分享到朋友圈 (Beta)
  466. * https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share-timeline.html
  467. */
  468. onShareTimeline() {
  469. const _this = this;
  470. // 构建页面参数
  471. const params = App.getShareUrlParams({
  472. active_id: _this.data.active.active_id
  473. });
  474. return {
  475. title: _this.data.detail.goods_name,
  476. path: `/pages/bargain/goods/index?${params}`
  477. };
  478. },
  479. })