index.js 24 KB

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