index.js 24 KB

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