index.js 24 KB

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