index.js 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. import {
  2. getProductDetail,
  3. getProductCode,
  4. collectAdd,
  5. collectDel,
  6. postCartAdd,
  7. storeListApi
  8. } from '../../api/store.js';
  9. import {
  10. getUserInfo,
  11. userShare
  12. } from '../../api/user.js';
  13. import {
  14. getCoupons
  15. } from '../../api/api.js';
  16. import {
  17. getCartCounts
  18. } from '../../api/order.js';
  19. import WxParse from '../../wxParse/wxParse.js';
  20. import util from '../../utils/util.js';
  21. import d from '../../utils/d.js';
  22. import wxh from '../../utils/wxh.js';
  23. import {
  24. CACHE_LONGITUDE,
  25. CACHE_LATITUDE
  26. } from '../../config.js';
  27. const app = getApp();
  28. Page({
  29. /**
  30. * 页面的初始数据
  31. */
  32. data: {
  33. parameter: {
  34. 'navbar': '1',
  35. 'return': '1',
  36. 'title': '商品详情'
  37. },
  38. attribute: {
  39. 'cartAttr': false
  40. }, //属性是否打开
  41. coupon: {
  42. 'coupon': false,
  43. list: [],
  44. },
  45. show_benefit: false,
  46. attr: '请选择', //属性页面提示
  47. attrValue: '', //已选属性
  48. animated: false, //购物车动画
  49. id: 0, //商品id
  50. replyCount: 0, //总评论数量
  51. reply: [], //评论列表
  52. storeInfo: {}, //商品详情
  53. productAttr: [], //组件展示属性
  54. productValue: [], //系统属性
  55. couponList: [], //优惠券
  56. productSelect: {}, //属性选中规格
  57. cart_num: 1, //购买数量
  58. isAuto: false, //没有授权的不会自动授权
  59. isHidden: true, //是否隐藏授权
  60. isOpen: false, //是否打开属性组件
  61. isLog: app.globalData.isLog, //是否登录
  62. actionSheetHidden: true,
  63. posterImageStatus: false,
  64. storeImage: '', //海报产品图
  65. PromotionCode: '', //二维码图片
  66. canvasStatus: false, //海报绘图标签
  67. posterImage: '', //海报路径
  68. posterbackgd: '/images/posterbackgd.png',
  69. sharePacket: {
  70. isState: true, //默认不显示
  71. priceName: 0,
  72. }, //分销商详细
  73. uid: 0, //用户uid
  74. circular: false,
  75. autoplay: false,
  76. interval: 3000,
  77. duration: 500,
  78. clientHeight: "",
  79. systemStore: {}, //门店信息
  80. good_list: [],
  81. isDown: true,
  82. storeSelfMention: true,
  83. // storeItems: {},
  84. storeList: [],
  85. activity: [],
  86. iSplus: true,
  87. navH: "",
  88. navList: [],
  89. opacity: 0,
  90. scrollY: 0,
  91. topArr: [],
  92. toView: '',
  93. height: 0,
  94. heightArr: [],
  95. lock: false,
  96. scrollTop: 0,
  97. fixed: false,
  98. },
  99. returns: function () {
  100. wx.navigateBack();
  101. },
  102. tap: function (e) {
  103. var id = e.currentTarget.dataset.id;
  104. var index = e.currentTarget.dataset.index;
  105. var that = this;
  106. if (!this.data.good_list.length && id == "past2") {
  107. id = "past3"
  108. }
  109. this.setData({
  110. toView: id,
  111. navActive: index,
  112. lock: true,
  113. scrollTop: index > 0 ? that.data.topArr[index] - (app.globalData.navHeight / 2) : that.data.topArr[index]
  114. });
  115. },
  116. scroll: function (e) {
  117. var that = this,
  118. scrollY = e.detail.scrollTop;
  119. var opacity = scrollY / 200;
  120. opacity = opacity > 1 ? 1 : opacity;
  121. that.setData({
  122. opacity: opacity,
  123. scrollY: scrollY
  124. })
  125. if (that.data.lock) {
  126. that.setData({
  127. lock: false
  128. })
  129. return;
  130. }
  131. for (var i = 0; i < that.data.topArr.length; i++) {
  132. if (scrollY < that.data.topArr[i] - (app.globalData.navHeight / 2) + that.data.heightArr[i]) {
  133. that.setData({
  134. navActive: i
  135. });
  136. break
  137. }
  138. }
  139. },
  140. /**
  141. * 登录后加载
  142. */
  143. onLoadFun: function (e) {
  144. d.debug('details->onLoadFun()')
  145. this.setData({
  146. isLog: true
  147. });
  148. // this.getCouponList();
  149. this.getCartCount();
  150. // this.downloadFilePromotionCode();
  151. this.getUserInfo();
  152. // this.get_product_collect();
  153. },
  154. ChangCouponsClone: function () {
  155. this.setData({
  156. 'coupon.coupon': false
  157. });
  158. },
  159. goActivity: function (e) {
  160. let item = e.currentTarget.dataset.items;
  161. if (item.type === "1") {
  162. wx.navigateTo({
  163. url: `/pages/activity/goods_seckill_details/index?id=${item.id}&time=${item.time}&status=1`
  164. });
  165. } else if (item.type === "2") {
  166. wx.navigateTo({
  167. url: `/pages/activity/goods_bargain_details/index?id=${item.id}`
  168. });
  169. } else {
  170. wx.navigateTo({
  171. url: `/pages/activity/goods_combination_details/index?id=${item.id}`
  172. });
  173. }
  174. },
  175. /**
  176. * 商品详情跳转
  177. */
  178. goDetail: function (e) {
  179. let item = e.currentTarget.dataset.items;
  180. if (item.activity && item.activity.type === "1") {
  181. wx.navigateTo({
  182. url: `/pages/activity/goods_seckill_details/index?id=${item.activity.id}&time=${item.activity.time}&status=1`
  183. });
  184. } else if (item.activity && item.activity.type === "2") {
  185. wx.navigateTo({
  186. url: `/pages/activity/goods_bargain_details/index?id=${item.activity.id}`
  187. });
  188. } else if (item.activity && item.activity.type === "3") {
  189. wx.navigateTo({
  190. url: `/pages/activity/goods_combination_details/index?id=${item.activity.id}`
  191. });
  192. } else {
  193. wx.navigateTo({
  194. url: `/pages/goods_details/index?id=${item.id}`
  195. });
  196. }
  197. },
  198. /*
  199. * 跳转门店列表
  200. */
  201. showStoreList: function () {
  202. wx.navigateTo({
  203. url: '/pages/goods_details_store/index?go=details'
  204. })
  205. },
  206. /**
  207. * 获取门店列表数据
  208. */
  209. getList: function () {
  210. let longitude = wx.getStorageSync(CACHE_LONGITUDE); //经度
  211. let latitude = wx.getStorageSync(CACHE_LATITUDE); //纬度
  212. let data = {
  213. latitude: latitude, //纬度
  214. longitude: longitude, //经度
  215. page: 1,
  216. limit: 10
  217. }
  218. // storeListApi(data).then(res => {
  219. // let list = res.data.list || [];
  220. // this.setData({
  221. // storeList: list,
  222. // storeItems: list[0]
  223. // });
  224. // }).catch(err => {
  225. // })
  226. },
  227. /*
  228. * 获取用户信息
  229. */
  230. getUserInfo: function () {
  231. var that = this;
  232. getUserInfo().then(res => {
  233. let isState = true;
  234. if ((res.data.is_promoter || res.data.statu == 2) && this.data.sharePacket.priceName != 0) {
  235. isState = false;
  236. }
  237. that.setData({
  238. 'sharePacket.isState': isState,
  239. uid: res.data.uid
  240. });
  241. });
  242. },
  243. /**
  244. * 购物车手动填写
  245. */
  246. iptCartNum: function (e) {
  247. this.setData({
  248. ['productSelect.cart_num']: e.detail,
  249. cart_num: e.detail
  250. });
  251. },
  252. /**
  253. * 购物车数量加和数量减
  254. */
  255. ChangeCartNum: function (e) {
  256. //是否 加|减
  257. var changeValue = e.detail;
  258. //获取当前变动属性
  259. var productSelect = this.data.productValue[this.data.attrValue];
  260. //如果没有属性,赋值给商品默认库存
  261. if (productSelect === undefined && !this.data.productAttr.length) productSelect = this.data.productSelect;
  262. //不存在不加数量
  263. if (productSelect === undefined) return;
  264. if (this.data.cart_num) {
  265. productSelect.cart_num = this.data.cart_num;
  266. };
  267. //提取库存
  268. var stock = productSelect.stock || 0;
  269. //设置默认数据
  270. if (productSelect.cart_num == undefined) productSelect.cart_num = 1;
  271. //数量+
  272. if (changeValue) {
  273. productSelect.cart_num++;
  274. //大于库存时,等于库存
  275. if (productSelect.cart_num > stock) productSelect.cart_num = stock ? stock : 1;
  276. this.setData({
  277. ['productSelect.cart_num']: productSelect.cart_num,
  278. cart_num: productSelect.cart_num
  279. });
  280. } else {
  281. //数量减
  282. productSelect.cart_num--;
  283. //小于1时,等于1
  284. if (productSelect.cart_num < 1) productSelect.cart_num = 1;
  285. this.setData({
  286. ['productSelect.cart_num']: productSelect.cart_num,
  287. cart_num: productSelect.cart_num
  288. });
  289. }
  290. },
  291. /**
  292. * 属性变动赋值
  293. */
  294. ChangeAttr: function (e) {
  295. var values = e.detail;
  296. var productSelect = this.data.productValue[values];
  297. var storeInfo = this.data.storeInfo;
  298. this.setData({
  299. cart_num: 1
  300. })
  301. if (productSelect) {
  302. this.setData({
  303. ["productSelect.image"]: productSelect.image,
  304. ["productSelect.price"]: productSelect.price,
  305. ["productSelect.stock"]: productSelect.stock,
  306. ['productSelect.unique']: productSelect.unique,
  307. ['productSelect.cart_num']: 1,
  308. attrValue: values,
  309. attr: '已选择'
  310. });
  311. } else {
  312. this.setData({
  313. ["productSelect.image"]: storeInfo.image,
  314. ["productSelect.price"]: storeInfo.price,
  315. ["productSelect.stock"]: 0,
  316. ['productSelect.unique']: '',
  317. ['productSelect.cart_num']: 1,
  318. attrValue: '',
  319. attr: '请选择'
  320. });
  321. }
  322. },
  323. /**
  324. * 领取完毕移除当前页面领取过的优惠券展示
  325. */
  326. ChangCoupons: function (e) {
  327. var coupon = e.detail;
  328. var couponList = util.ArrayRemove(this.data.couponList, 'id', coupon.id);
  329. this.setData({
  330. couponList: couponList
  331. });
  332. },
  333. /**
  334. * 生命周期函数--监听页面加载
  335. */
  336. onLoad: function (options) {
  337. var that = this;
  338. this.setData({
  339. navH: app.globalData.navHeight,
  340. fixed: app.globalData.isFixed,
  341. show_benefit: app.globalData.show_benefit,
  342. });
  343. //设置商品列表高度
  344. wx.getSystemInfo({
  345. success: function (res) {
  346. that.setData({
  347. height: res.windowHeight
  348. //res.windowHeight:获取整个窗口高度为px,*2为rpx;98为头部占据的高度;
  349. })
  350. },
  351. });
  352. //扫码携带参数处理
  353. if (options.scene) {
  354. var value = util.getUrlParams(decodeURIComponent(options.scene));
  355. if (value.id) options.id = value.id;
  356. //记录推广人uid
  357. if (value.pid) app.globalData.spid = value.pid;
  358. }
  359. if (!options.id) {
  360. return app.Tips({
  361. title: '缺少参数无法查看商品'
  362. }, {
  363. tab: 3,
  364. url: 1
  365. });
  366. }
  367. this.setData({
  368. id: options.id
  369. });
  370. //记录推广人uid
  371. if (options.spid) {
  372. app.globalData.spid = options.spid;
  373. }
  374. this.getGoodsDetails();
  375. this.getList();
  376. },
  377. setClientHeight: function () {
  378. if (!this.data.good_list.length) return;
  379. var query = wx.createSelectorQuery().in(this);
  380. query.select("#list0").boundingClientRect();
  381. var that = this;
  382. query.exec(function (res) {
  383. that.setData({
  384. clientHeight: res[0].height + 20
  385. });
  386. });
  387. },
  388. infoScroll: function () {
  389. var that = this,
  390. topArr = [],
  391. heightArr = [];
  392. for (var i = 0; i < that.data.navList.length; i++) { //productList
  393. //获取元素所在位置
  394. var query = wx.createSelectorQuery().in(this);
  395. var idView = "#past" + i;
  396. if (!that.data.good_list.length && i == 2) {
  397. var idView = "#past" + 3;
  398. }
  399. query.select(idView).boundingClientRect();
  400. query.exec(function (res) {
  401. var top = res[0].top;
  402. var height = res[0].height;
  403. topArr.push(top);
  404. heightArr.push(height);
  405. that.setData({
  406. topArr: topArr,
  407. heightArr: heightArr
  408. });
  409. });
  410. };
  411. },
  412. /**
  413. * 获取产品详情
  414. */
  415. getGoodsDetails: function () {
  416. var that = this;
  417. getProductDetail(that.data.id).then(res => {
  418. var storeInfo = res.data.storeInfo;
  419. var good_list = res.data.good_list || [];
  420. var count = Math.ceil(good_list.length / 6);
  421. var goodArray = new Array();
  422. for (var i = 0; i < count; i++) {
  423. var list = good_list.slice(i * 6, i * 6 + 6);
  424. if (list.length) goodArray.push({
  425. list: list
  426. });
  427. }
  428. that.setData({
  429. storeInfo: storeInfo,
  430. reply: res.data.reply ? [res.data.reply] : [],
  431. replyCount: res.data.replyCount,
  432. description: storeInfo.description,
  433. replyChance: res.data.replyChance,
  434. productAttr: res.data.productAttr,
  435. productValue: res.data.productValue,
  436. ["sharePacket.priceName"]: res.data.priceName,
  437. // ['parameter.title']: storeInfo.store_name,
  438. systemStore: res.data.system_store,
  439. storeSelfMention: res.data.store_self_mention,
  440. good_list: goodArray,
  441. activity: res.data.activity ? res.data.activity : []
  442. });
  443. var navList = ['商品', '评价', '详情'];
  444. if (goodArray.length) {
  445. navList.splice(2, 0, '推荐')
  446. }
  447. that.setData({
  448. navList: navList
  449. });
  450. if (app.globalData.isLog) {
  451. that.setData({
  452. 'storeInfo.userCollect': res.data.storeInfo.userCollect
  453. });
  454. // that.getUserInfo();
  455. }
  456. // that.downloadFilestoreImage();
  457. that.DefaultSelect();
  458. setTimeout(function () {
  459. that.setClientHeight();
  460. }, 500);
  461. setTimeout(function () {
  462. that.infoScroll();
  463. }, 500);
  464. //html转wxml
  465. WxParse.wxParse('description', 'html', that.data.description, that, 0);
  466. }).catch(err => {
  467. //状态异常返回上级页面
  468. return app.Tips({
  469. title: err.toString()
  470. }, {
  471. tab: 3,
  472. url: 1
  473. });
  474. })
  475. },
  476. goPages: function (e) {
  477. wx.navigateTo({
  478. url: 'pages/goods_details/index?id=' + e.currentTarget.dataset.id
  479. });
  480. },
  481. /**
  482. * 拨打电话
  483. */
  484. makePhone: function () {
  485. wx.makePhoneCall({
  486. phoneNumber: this.data.systemStore.phone
  487. })
  488. },
  489. /**
  490. * 打开地图
  491. */
  492. showMaoLocation: function () {
  493. if (!this.data.systemStore.latitude || !this.data.systemStore.longitude) return app.Tips({
  494. title: '缺少经纬度信息无法查看地图!'
  495. });
  496. wx.openLocation({
  497. latitude: parseFloat(this.data.systemStore.latitude),
  498. longitude: parseFloat(this.data.systemStore.longitude),
  499. scale: 8,
  500. name: this.data.systemStore.name,
  501. address: this.data.systemStore.address + this.data.systemStore.detailed_address,
  502. success: function () {
  503. },
  504. });
  505. },
  506. /**
  507. * 默认选中属性
  508. */
  509. DefaultSelect: function () {
  510. var productAttr = this.data.productAttr,
  511. storeInfo = this.data.storeInfo,
  512. productValue = this.data.productValue,
  513. value = [];
  514. for (var key in productValue) {
  515. if (productValue[key].stock > 0) {
  516. value = this.data.productAttr.length ? key.split(",") : [];
  517. break;
  518. }
  519. }
  520. for (var i = 0, len = productAttr.length; i < len; i++) {
  521. if (productAttr[i].attr_value[0]) productAttr[i].checked = false; // value[i];
  522. };
  523. var productSelect = this.data.productValue[value.sort().join(',')];
  524. productSelect = null;
  525. if (productSelect) {
  526. this.setData({
  527. ["productSelect.store_name"]: storeInfo.store_name,
  528. ["productSelect.image"]: productSelect.image,
  529. ["productSelect.price"]: productSelect.price,
  530. ["productSelect.stock"]: productSelect.stock,
  531. ['productSelect.unique']: productSelect.unique,
  532. ['productSelect.cart_num']: 1,
  533. attrValue: value,
  534. attr: '已选择'
  535. });
  536. } else {
  537. this.setData({
  538. ["productSelect.store_name"]: storeInfo.store_name,
  539. ["productSelect.image"]: storeInfo.image,
  540. ["productSelect.price"]: storeInfo.price,
  541. ["productSelect.stock"]: this.data.productAttr.length ? 0 : storeInfo.stock,
  542. ['productSelect.unique']: '',
  543. ['productSelect.cart_num']: 1,
  544. attrValue: '',
  545. attr: '请选择'
  546. });
  547. }
  548. this.setData({
  549. productAttr: productAttr,
  550. cart_num: 1
  551. });
  552. },
  553. /**
  554. * 获取是否收藏
  555. */
  556. get_product_collect: function () {
  557. var that = this;
  558. getProductDetail(that.data.id).then(res => {
  559. that.setData({
  560. 'storeInfo.userCollect': res.data.storeInfo.userCollect
  561. });
  562. });
  563. },
  564. /**
  565. * 获取优惠券
  566. */
  567. getCouponList() {
  568. var that = this;
  569. getCoupons({
  570. page: 1,
  571. limit: 10,
  572. type: 1,
  573. product_id: that.data.id
  574. }).then(res => {
  575. var couponList = [];
  576. for (var i = 0; i < res.data.length; i++) {
  577. if (!res.data[i].is_use && couponList.length < 2) couponList.push(res.data[i]);
  578. }
  579. that.setData({
  580. ['coupon.list']: res.data,
  581. couponList: couponList
  582. });
  583. });
  584. },
  585. /**
  586. * 收藏商品
  587. */
  588. setCollect: function () {
  589. if (app.globalData.isLog === false) {
  590. this.setData({
  591. isAuto: true,
  592. isHidden: false,
  593. });
  594. } else {
  595. var that = this;
  596. if (this.data.storeInfo.userCollect) {
  597. collectDel(this.data.storeInfo.id).then(res => {
  598. that.setData({
  599. ['storeInfo.userCollect']: !that.data.storeInfo.userCollect
  600. })
  601. })
  602. } else {
  603. collectAdd(this.data.storeInfo.id).then(res => {
  604. that.setData({
  605. ['storeInfo.userCollect']: !that.data.storeInfo.userCollect
  606. })
  607. })
  608. }
  609. }
  610. },
  611. /**
  612. * 打开属性插件
  613. */
  614. selecAttr: function () {
  615. if (app.globalData.isLog === false)
  616. this.setData({
  617. isAuto: true,
  618. isHidden: false
  619. })
  620. else
  621. this.setData({
  622. 'attribute.cartAttr': true,
  623. isOpen: true
  624. })
  625. },
  626. /**
  627. * 打开优惠券插件
  628. */
  629. coupon: function () {
  630. if (app.globalData.isLog === false)
  631. this.setData({
  632. isAuto: true,
  633. isHidden: false
  634. })
  635. else {
  636. this.getCouponList();
  637. this.setData({
  638. 'coupon.coupon': true
  639. })
  640. }
  641. },
  642. onMyEvent: function (e) {
  643. this.setData({
  644. 'attribute.cartAttr': e.detail.window,
  645. isOpen: false
  646. });
  647. },
  648. /**
  649. * 打开属性加入购物车
  650. */
  651. joinCart: function (e) {
  652. //是否登录
  653. if (app.globalData.isLog === false)
  654. this.setData({
  655. isAuto: true,
  656. isHidden: false,
  657. });
  658. else {
  659. this.goCat();
  660. }
  661. },
  662. /*
  663. * 加入购物车
  664. */
  665. goCat: function (isPay) {
  666. var that = this;
  667. var productSelect = this.data.productValue[this.data.attrValue];
  668. //打开属性
  669. if (this.data.attrValue) {
  670. //默认选中了属性,但是没有打开过属性弹窗还是自动打开让用户查看默认选中的属性
  671. this.setData({
  672. 'attribute.cartAttr': !this.data.isOpen ? true : false
  673. })
  674. } else {
  675. if (this.data.isOpen)
  676. this.setData({
  677. 'attribute.cartAttr': true
  678. })
  679. else
  680. this.setData({
  681. 'attribute.cartAttr': !this.data.attribute.cartAttr
  682. });
  683. }
  684. //只有关闭属性弹窗时进行加入购物车
  685. if (this.data.attribute.cartAttr === true && this.data.isOpen == false) return this.setData({
  686. isOpen: true
  687. });
  688. //如果有属性,没有选择,提示用户选择
  689. if (this.data.productAttr.length && productSelect === undefined && this.data.isOpen == true) return app.Tips({
  690. title: '请选择属性'
  691. });
  692. if (!that.data.cart_num || parseInt(that.data.cart_num) <= 0) return app.Tips({
  693. title: '请输入购买数量'
  694. });
  695. postCartAdd({
  696. productId: that.data.id,
  697. cartNum: that.data.cart_num,
  698. uniqueId: productSelect !== undefined ? productSelect.unique : '',
  699. 'new': isPay === undefined ? 0 : 1,
  700. }).then(res => {
  701. that.setData({
  702. isOpen: false,
  703. 'attribute.cartAttr': false
  704. });
  705. if (isPay)
  706. wx.navigateTo({
  707. url: '/pages/order_confirm/index?cartId=' + res.data.cartId
  708. });
  709. else
  710. app.Tips({
  711. title: '添加购物车成功',
  712. icon: 'success'
  713. }, function () {
  714. that.getCartCount(true);
  715. });
  716. }).catch(err => {
  717. return app.Tips({
  718. title: err
  719. });
  720. });
  721. },
  722. /**
  723. * 获取购物车数量
  724. * @param boolean 是否展示购物车动画和重置属性
  725. */
  726. getCartCount: function (isAnima) {
  727. var that = this;
  728. getCartCounts().then(res => {
  729. that.setData({
  730. CartCount: res.data.count
  731. });
  732. //加入购物车后重置属性
  733. if (isAnima) {
  734. that.setData({
  735. animated: true,
  736. attrValue: '',
  737. attr: '请选择',
  738. ["productSelect.image"]: that.data.storeInfo.image,
  739. ["productSelect.price"]: that.data.storeInfo.price,
  740. ["productSelect.stock"]: that.data.storeInfo.stock,
  741. ['productSelect.unique']: '',
  742. ['productSelect.cart_num']: 1,
  743. cart_num: 1
  744. });
  745. that.selectComponent('#product-window').ResetAttr();
  746. setTimeout(function () {
  747. that.setData({
  748. animated: false
  749. });
  750. }, 500);
  751. }
  752. });
  753. },
  754. /**
  755. * 立即购买
  756. */
  757. goBuy: function (e) {
  758. if (app.globalData.isLog === false)
  759. this.setData({
  760. isAuto: true,
  761. isHidden: false
  762. });
  763. else
  764. this.goCat(true);
  765. },
  766. /**
  767. * 分享打开和关闭
  768. */
  769. listenerActionSheet: function () {
  770. if (app.globalData.isLog === false)
  771. this.setData({
  772. isAuto: true,
  773. isHidden: false
  774. });
  775. else
  776. this.setData({
  777. actionSheetHidden: !this.data.actionSheetHidden
  778. })
  779. },
  780. //隐藏海报
  781. posterImageClose: function () {
  782. this.setData({
  783. posterImageStatus: false,
  784. })
  785. },
  786. //替换安全域名
  787. setDomain: function (url) {
  788. url = url ? url.toString() : '';
  789. //本地调试打开,生产请注销
  790. // return url;
  791. if (url.indexOf("https://") > -1) return url;
  792. else return url.replace('http://', 'https://');
  793. },
  794. //获取海报产品图
  795. downloadFilestoreImage: function () {
  796. var that = this;
  797. wx.downloadFile({
  798. url: that.setDomain(that.data.storeInfo.image),
  799. success: function (res) {
  800. that.setData({
  801. storeImage: res.tempFilePath
  802. })
  803. },
  804. fail: function () {
  805. return app.Tips({
  806. title: ''
  807. });
  808. that.setData({
  809. storeImage: '',
  810. })
  811. },
  812. });
  813. },
  814. /**
  815. * 获取产品分销二维码
  816. * @param function successFn 下载完成回调
  817. */
  818. downloadFilePromotionCode: function (successFn) {
  819. var that = this;
  820. getProductCode(this.data.id).then(res => {
  821. wx.downloadFile({
  822. url: that.setDomain(res.data.code),
  823. success: function (res) {
  824. that.setData({
  825. isDown: false
  826. });
  827. if (typeof successFn == 'function')
  828. successFn && successFn(res.tempFilePath);
  829. else
  830. that.setData({
  831. PromotionCode: res.tempFilePath
  832. });
  833. },
  834. fail: function () {
  835. that.setData({
  836. isDown: false
  837. });
  838. that.setData({
  839. PromotionCode: ''
  840. });
  841. },
  842. });
  843. }).catch(err => {
  844. that.setData({
  845. isDown: false
  846. });
  847. that.setData({
  848. PromotionCode: ''
  849. });
  850. });
  851. },
  852. /**
  853. * 生成海报
  854. */
  855. goPoster: function () {
  856. var that = this;
  857. that.setData({
  858. canvasStatus: true
  859. });
  860. var arr2 = [that.data.posterbackgd, that.data.storeImage, that.data.PromotionCode];
  861. if (that.data.isDown) return app.Tips({
  862. title: '正在下载海报,请稍后再试!'
  863. });
  864. wx.getImageInfo({
  865. src: that.data.PromotionCode,
  866. fail: function (res) {
  867. return app.Tips({
  868. 'title': '小程序二维码需要发布正式版后才能获取到'
  869. });
  870. },
  871. success() {
  872. if (arr2[2] == '') {
  873. //海报二维码不存在则从新下载
  874. that.downloadFilePromotionCode(function (msgPromotionCode) {
  875. arr2[2] = msgPromotionCode;
  876. if (arr2[2] == '') return app.Tips({
  877. title: '海报二维码生成失败!'
  878. });
  879. util.PosterCanvas(arr2, that.data.storeInfo.store_name, that.data.storeInfo.price, function (tempFilePath) {
  880. that.setData({
  881. posterImage: tempFilePath,
  882. posterImageStatus: true,
  883. canvasStatus: false,
  884. actionSheetHidden: !that.data.actionSheetHidden
  885. })
  886. });
  887. });
  888. } else {
  889. //生成推广海报
  890. util.PosterCanvas(arr2, that.data.storeInfo.store_name, that.data.storeInfo.price, function (tempFilePath) {
  891. that.setData({
  892. posterImage: tempFilePath,
  893. posterImageStatus: true,
  894. canvasStatus: false,
  895. actionSheetHidden: !that.data.actionSheetHidden
  896. })
  897. });
  898. }
  899. },
  900. });
  901. },
  902. /*
  903. * 保存到手机相册
  904. */
  905. savePosterPath: function () {
  906. var that = this;
  907. wx.getSetting({
  908. success(res) {
  909. if (!res.authSetting['scope.writePhotosAlbum']) {
  910. wx.authorize({
  911. scope: 'scope.writePhotosAlbum',
  912. success() {
  913. wx.saveImageToPhotosAlbum({
  914. filePath: that.data.posterImage,
  915. success: function (res) {
  916. that.posterImageClose();
  917. app.Tips({
  918. title: '保存成功',
  919. icon: 'success'
  920. });
  921. },
  922. fail: function (res) {
  923. app.Tips({
  924. title: '保存失败'
  925. });
  926. }
  927. })
  928. }
  929. })
  930. } else {
  931. wx.saveImageToPhotosAlbum({
  932. filePath: that.data.posterImage,
  933. success: function (res) {
  934. that.posterImageClose();
  935. app.Tips({
  936. title: '保存成功',
  937. icon: 'success'
  938. });
  939. },
  940. fail: function (res) {
  941. app.Tips({
  942. title: '保存失败'
  943. });
  944. },
  945. })
  946. }
  947. }
  948. })
  949. },
  950. /**
  951. * 用户点击分享
  952. */
  953. onShareAppMessage: function () {
  954. var that = this;
  955. that.setData({
  956. actionSheetHidden: !that.data.actionSheetHidden
  957. });
  958. userShare();
  959. return {
  960. title: that.data.storeInfo.store_name || '',
  961. imageUrl: that.data.storeInfo.image || '',
  962. path: '/pages/index/index?a=share&id=' + that.data.id + '&spid=' + that.data.uid,
  963. //path: '/pages/goods_details/index?id=' + that.data.id + '&spid=' + that.data.uid,
  964. }
  965. }
  966. })