index.js 12 KB

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