index.js 24 KB

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