index.js 26 KB

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