index.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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 = value[i];
  463. };
  464. var productSelect = this.data.productValue[value.sort().join(',')];
  465. if (productSelect) {
  466. this.setData({
  467. ["productSelect.store_name"]: storeInfo.store_name,
  468. ["productSelect.image"]: productSelect.image,
  469. ["productSelect.price"]: productSelect.price,
  470. ["productSelect.stock"]: productSelect.stock,
  471. ['productSelect.unique']: productSelect.unique,
  472. ['productSelect.cart_num']: 1,
  473. attrValue: value,
  474. attr: '已选择'
  475. });
  476. } else {
  477. this.setData({
  478. ["productSelect.store_name"]: storeInfo.store_name,
  479. ["productSelect.image"]: storeInfo.image,
  480. ["productSelect.price"]: storeInfo.price,
  481. ["productSelect.stock"]: this.data.productAttr.length ? 0 : storeInfo.stock,
  482. ['productSelect.unique']: '',
  483. ['productSelect.cart_num']: 1,
  484. attrValue: '',
  485. attr: '请选择'
  486. });
  487. }
  488. this.setData({ productAttr: productAttr, cart_num:1});
  489. },
  490. /**
  491. * 获取是否收藏
  492. */
  493. get_product_collect: function () {
  494. var that = this;
  495. getProductDetail(that.data.id).then(res => {
  496. that.setData({ 'storeInfo.userCollect': res.data.storeInfo.userCollect });
  497. });
  498. },
  499. /**
  500. * 获取优惠券
  501. *
  502. */
  503. getCouponList() {
  504. var that = this;
  505. getCoupons({ page: 1, limit: 10, type: 1, product_id: that.data.id}).then(res => {
  506. var couponList = [];
  507. for (var i = 0; i < res.data.length; i++) {
  508. if (!res.data[i].is_use && couponList.length < 2) couponList.push(res.data[i]);
  509. }
  510. that.setData({
  511. ['coupon.list']: res.data,
  512. couponList: couponList
  513. });
  514. });
  515. },
  516. /**
  517. *
  518. *
  519. * 收藏商品
  520. */
  521. setCollect: function () {
  522. if (app.globalData.isLog === false) {
  523. this.setData({
  524. isAuto: true,
  525. iShidden: false,
  526. });
  527. } else {
  528. var that = this;
  529. if (this.data.storeInfo.userCollect) {
  530. collectDel(this.data.storeInfo.id).then(res => {
  531. that.setData({
  532. ['storeInfo.userCollect']: !that.data.storeInfo.userCollect
  533. })
  534. })
  535. } else {
  536. collectAdd(this.data.storeInfo.id).then(res => {
  537. that.setData({
  538. ['storeInfo.userCollect']: !that.data.storeInfo.userCollect
  539. })
  540. })
  541. }
  542. }
  543. },
  544. /**
  545. * 打开属性插件
  546. */
  547. selecAttr: function () {
  548. if (app.globalData.isLog === false)
  549. this.setData({ isAuto: true, iShidden: false })
  550. else
  551. this.setData({ 'attribute.cartAttr': true, isOpen: true })
  552. },
  553. /**
  554. * 打开优惠券插件
  555. */
  556. coupon: function () {
  557. if (app.globalData.isLog === false)
  558. this.setData({ isAuto: true, iShidden: false })
  559. else {
  560. this.getCouponList();
  561. this.setData({ 'coupon.coupon': true })
  562. }
  563. },
  564. onMyEvent: function (e) {
  565. this.setData({ 'attribute.cartAttr': e.detail.window, isOpen: false });
  566. },
  567. /**
  568. * 打开属性加入购物车
  569. *
  570. */
  571. joinCart: function (e) {
  572. //是否登录
  573. if (app.globalData.isLog === false)
  574. this.setData({ isAuto: true, iShidden: false, });
  575. else {
  576. this.goCat();
  577. }
  578. },
  579. /*
  580. * 加入购物车
  581. */
  582. goCat: function (isPay) {
  583. var that = this;
  584. var productSelect = this.data.productValue[this.data.attrValue];
  585. //打开属性
  586. if (this.data.attrValue) {
  587. //默认选中了属性,但是没有打开过属性弹窗还是自动打开让用户查看默认选中的属性
  588. this.setData({ 'attribute.cartAttr': !this.data.isOpen ? true : false })
  589. } else {
  590. if (this.data.isOpen)
  591. this.setData({ 'attribute.cartAttr': true })
  592. else
  593. this.setData({ 'attribute.cartAttr': !this.data.attribute.cartAttr });
  594. }
  595. //只有关闭属性弹窗时进行加入购物车
  596. if (this.data.attribute.cartAttr === true && this.data.isOpen == false) return this.setData({ isOpen: true });
  597. //如果有属性,没有选择,提示用户选择
  598. if (this.data.productAttr.length && productSelect === undefined && this.data.isOpen == true) return app.Tips({ title: '请选择属性' });
  599. if (!that.data.cart_num || parseInt(that.data.cart_num) <= 0) return app.Tips({ title: '请输入购买数量' });
  600. postCartAdd({
  601. productId: that.data.id,
  602. cartNum: that.data.cart_num,
  603. uniqueId: productSelect !== undefined ? productSelect.unique : '',
  604. 'new': isPay === undefined ? 0 : 1,
  605. }).then(res => {
  606. that.setData({ isOpen: false, 'attribute.cartAttr': false });
  607. if (isPay)
  608. wx.navigateTo({ url: '/pages/order_confirm/index?cartId=' + res.data.cartId });
  609. else
  610. app.Tips({ title: '添加购物车成功', icon: 'success' }, function () {
  611. that.getCartCount(true);
  612. });
  613. }).catch(err => {
  614. return app.Tips({ title: err });
  615. });
  616. },
  617. /**
  618. * 获取购物车数量
  619. * @param boolean 是否展示购物车动画和重置属性
  620. */
  621. getCartCount: function (isAnima) {
  622. var that = this;
  623. getCartCounts().then(res => {
  624. that.setData({ CartCount: res.data.count });
  625. //加入购物车后重置属性
  626. if (isAnima) {
  627. that.setData({
  628. animated: true,
  629. attrValue: '',
  630. attr: '请选择',
  631. ["productSelect.image"]: that.data.storeInfo.image,
  632. ["productSelect.price"]: that.data.storeInfo.price,
  633. ["productSelect.stock"]: that.data.storeInfo.stock,
  634. ['productSelect.unique']: '',
  635. ['productSelect.cart_num']: 1,
  636. cart_num:1
  637. });
  638. that.selectComponent('#product-window').ResetAttr();
  639. setTimeout(function () {
  640. that.setData({
  641. animated: false
  642. });
  643. }, 500);
  644. }
  645. });
  646. },
  647. /**
  648. * 立即购买
  649. */
  650. goBuy: function (e) {
  651. if (app.globalData.isLog === false)
  652. this.setData({ isAuto: true, iShidden: false });
  653. else
  654. this.goCat(true);
  655. },
  656. /**
  657. * 分享打开和关闭
  658. *
  659. */
  660. listenerActionSheet: function () {
  661. if (app.globalData.isLog === false)
  662. this.setData({ isAuto: true, iShidden: false });
  663. else
  664. this.setData({ actionSheetHidden: !this.data.actionSheetHidden })
  665. },
  666. //隐藏海报
  667. posterImageClose: function () {
  668. this.setData({ posterImageStatus: false, })
  669. },
  670. //替换安全域名
  671. setDomain: function (url) {
  672. url = url ? url.toString() : '';
  673. //本地调试打开,生产请注销
  674. // return url;
  675. if (url.indexOf("https://") > -1) return url;
  676. else return url.replace('http://', 'https://');
  677. },
  678. //获取海报产品图
  679. downloadFilestoreImage: function () {
  680. var that = this;
  681. wx.downloadFile({
  682. url: that.setDomain(that.data.storeInfo.image),
  683. success: function (res) {
  684. that.setData({
  685. storeImage: res.tempFilePath
  686. })
  687. },
  688. fail: function () {
  689. return app.Tips({ title: '' });
  690. that.setData({
  691. storeImage: '',
  692. })
  693. },
  694. });
  695. },
  696. /**
  697. * 获取产品分销二维码
  698. * @param function successFn 下载完成回调
  699. *
  700. */
  701. downloadFilePromotionCode: function (successFn) {
  702. var that = this;
  703. getProductCode(this.data.id).then(res => {
  704. wx.downloadFile({
  705. url: that.setDomain(res.data.code),
  706. success: function (res) {
  707. that.setData({ isDown: false });
  708. if (typeof successFn == 'function')
  709. successFn && successFn(res.tempFilePath);
  710. else
  711. that.setData({ PromotionCode: res.tempFilePath });
  712. },
  713. fail: function () {
  714. that.setData({ isDown: false });
  715. that.setData({ PromotionCode: '' });
  716. },
  717. });
  718. }).catch(err => {
  719. that.setData({ isDown: false });
  720. that.setData({ PromotionCode: '' });
  721. });
  722. },
  723. /**
  724. * 生成海报
  725. */
  726. goPoster: function () {
  727. var that = this;
  728. that.setData({ canvasStatus: true });
  729. var arr2 = [that.data.posterbackgd, that.data.storeImage, that.data.PromotionCode];
  730. if (that.data.isDown) return app.Tips({ title: '正在下载海报,请稍后再试!' });
  731. wx.getImageInfo({
  732. src: that.data.PromotionCode,
  733. fail: function (res) {
  734. return app.Tips({ 'title': '小程序二维码需要发布正式版后才能获取到' });
  735. },
  736. success() {
  737. if (arr2[2] == '') {
  738. //海报二维码不存在则从新下载
  739. that.downloadFilePromotionCode(function (msgPromotionCode) {
  740. arr2[2] = msgPromotionCode;
  741. if (arr2[2] == '') return app.Tips({ title: '海报二维码生成失败!' });
  742. util.PosterCanvas(arr2, that.data.storeInfo.store_name, that.data.storeInfo.price, function (tempFilePath) {
  743. that.setData({
  744. posterImage: tempFilePath,
  745. posterImageStatus: true,
  746. canvasStatus: false,
  747. actionSheetHidden: !that.data.actionSheetHidden
  748. })
  749. });
  750. });
  751. } else {
  752. //生成推广海报
  753. util.PosterCanvas(arr2, that.data.storeInfo.store_name, that.data.storeInfo.price, function (tempFilePath) {
  754. that.setData({
  755. posterImage: tempFilePath,
  756. posterImageStatus: true,
  757. canvasStatus: false,
  758. actionSheetHidden: !that.data.actionSheetHidden
  759. })
  760. });
  761. }
  762. },
  763. });
  764. },
  765. /*
  766. * 保存到手机相册
  767. */
  768. savePosterPath: function () {
  769. var that = this;
  770. wx.getSetting({
  771. success(res) {
  772. if (!res.authSetting['scope.writePhotosAlbum']) {
  773. wx.authorize({
  774. scope: 'scope.writePhotosAlbum',
  775. success() {
  776. wx.saveImageToPhotosAlbum({
  777. filePath: that.data.posterImage,
  778. success: function (res) {
  779. that.posterImageClose();
  780. app.Tips({ title: '保存成功', icon: 'success' });
  781. },
  782. fail: function (res) {
  783. app.Tips({ title: '保存失败' });
  784. }
  785. })
  786. }
  787. })
  788. } else {
  789. wx.saveImageToPhotosAlbum({
  790. filePath: that.data.posterImage,
  791. success: function (res) {
  792. that.posterImageClose();
  793. app.Tips({ title: '保存成功', icon: 'success' });
  794. },
  795. fail: function (res) {
  796. app.Tips({ title: '保存失败' });
  797. },
  798. })
  799. }
  800. }
  801. })
  802. },
  803. /**
  804. * 用户点击右上角分享
  805. */
  806. onShareAppMessage: function () {
  807. var that = this;
  808. that.setData({ actionSheetHidden: !that.data.actionSheetHidden });
  809. userShare();
  810. return {
  811. title: that.data.storeInfo.store_name || '',
  812. imageUrl: that.data.storeInfo.image || '',
  813. path: '/pages/index/index?a=share&id=' + that.data.id + '&spid=' + that.data.uid,
  814. //path: '/pages/goods_details/index?id=' + that.data.id + '&spid=' + that.data.uid,
  815. }
  816. }
  817. })