index.js 31 KB

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