1
0

goods.js 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. var app = getApp();
  2. var WxParse = require('../../lib/wxParse/wxParse.js');
  3. var util = require('../../utils/util.js');
  4. var api = require('../../config/api.js');
  5. var goodsUtil = require('../../utils/goods.js');
  6. Page({
  7. data: {
  8. winHeight: "",
  9. id: 0,
  10. referrer: 0,
  11. goods: {},
  12. gallery: [],
  13. attribute: [],
  14. issueList: [],
  15. comment: [],
  16. brand: {},
  17. specificationList: [],
  18. productList: [],
  19. relatedGoods: [],
  20. cartGoodsCount: 0,
  21. userHasCollect: 0,
  22. crashList: {},
  23. number: 1,
  24. checkedSpecText: '请选择规格数量',
  25. openAttr: false,
  26. openAttrByOrder: false,
  27. noCollectImage: "/static/images/icon_collect.png",
  28. hasCollectImage: "/static/images/icon_collect_checked.png",
  29. collectBackImage: "/static/images/icon_collect.png",
  30. showNavList: false,
  31. stockNum: null,
  32. cartNumber: 0,
  33. autoplay: false,
  34. defaultFreight: 0,
  35. current: 0, //banner当前的index
  36. isPlay: true,
  37. indexNum: 0,
  38. windowWidth: 0,
  39. leftWidth: 0,
  40. startX: 0,
  41. curr_id: '',
  42. videoHiddenName: true,
  43. imgHiddenName: false,
  44. page: 1,
  45. size: 3,
  46. detailContent: '',
  47. storeId: '',
  48. detailStoreId: '',
  49. promId: '',
  50. merchSn: '',
  51. openTicketAttr: false,
  52. ticketDiscountList: [],
  53. detailTicketDiscountList: [],
  54. discountSize: 9999, //查看全部
  55. discountDetailSize: 3, //详情查看部分
  56. campDetailSize: 1, //详情查看部分
  57. detailStoreCampMinusList: [],
  58. storeCampMinusList: []
  59. },
  60. onLoad: function(options) {
  61. // 页面初始化 options为页面跳转所带来的参数
  62. //由渠道推广生成的小程序码带来的参数,格式:商品id&门店id&推广id&用户id
  63. // options.scene = "1181115&20&1&26";
  64. // options.scene = "1181167&12&5&26";
  65. // options.scene = "1181133&12&6&26";
  66. // options.scene = "1181133&12&&26";
  67. var paramView = '';
  68. if (options.scene) {
  69. paramView = 'indexGoods';
  70. var scene = decodeURIComponent(options.scene);
  71. console.log("scene is ", scene);
  72. var scanArray = scene.split('&');
  73. var id = scanArray[0];
  74. var storeId = scanArray[1];
  75. var promId = scanArray[2];
  76. var referrer = scanArray[3];
  77. console.log("id is ", id);
  78. console.log("storeId is ", storeId);
  79. console.log("promId is ", promId);
  80. console.log("referrer is ", referrer);
  81. this.setData({
  82. id: id,
  83. storeId: storeId,
  84. promId: promId,
  85. referrer: referrer
  86. });
  87. if (storeId) {
  88. wx.setStorageSync('storeId', storeId);
  89. wx.setStorageSync('isShareGoods', 'true'); //商品详情是由用户分享进入的
  90. }
  91. } else {
  92. //由列表页、与普通用户分享点进来带来的参数,商品id,门店id;商品id,门店id,分享用户
  93. this.setData({
  94. id: parseInt(options.id)
  95. });
  96. if (options.paramView){
  97. paramView = options.paramView;
  98. }
  99. if (options.storeId) {
  100. this.setData({
  101. storeId: options.storeId
  102. });
  103. wx.setStorageSync('storeId', options.storeId);
  104. }
  105. if (options.referrer) {
  106. this.setData({
  107. referrer: options.referrer
  108. });
  109. wx.setStorageSync('isShareGoods', 'true');
  110. } else {
  111. wx.removeStorageSync('isShareGoods'); //由列表页、与普通用户分享点进来则清除是否分享缓存
  112. }
  113. }
  114. var that = this;
  115. if (wx.getStorageSync('userInfo') && wx.getStorageSync('token')){
  116. // 高度自适应
  117. wx.getSystemInfo({
  118. success: function (res) {
  119. var clientHeight = res.windowHeight,
  120. clientWidth = res.windowWidth,
  121. rpxR = 750 / clientWidth;
  122. var calc = clientHeight * rpxR - 100;
  123. // console.log(calc);
  124. that.setData({
  125. winHeight: calc,
  126. windowWidth: res.windowWidth
  127. });
  128. }
  129. });
  130. } else {
  131. // wx.navigateTo({
  132. // url: '/pages/auth/btnAuth/btnAuth',
  133. // })
  134. // wx.navigateTo({
  135. // url: '/pages/ucenter/userLogin/userLogin?view=' + paramView
  136. // })
  137. }
  138. },
  139. onShow: function () {
  140. let that = this;
  141. if (wx.getStorageSync('token')) {
  142. console.log(wx.getStorageSync('userInfo'))
  143. console.log(wx.getStorageSync('token'))
  144. util.request(api.checkToken, {
  145. token: wx.getStorageSync('token'),
  146. isRefusedLogin: wx.getStorageSync('isRefusedLogin'),
  147. userId: wx.getStorageSync('userId')+'',
  148. storeId: parseInt(wx.getStorageSync('storeId'))
  149. }, 'POST').then(function (res) {
  150. if (res.data.errno == 401) {
  151. //token失效
  152. console.log('token失效')
  153. } else {
  154. //token未失效
  155. }
  156. });
  157. }
  158. // 页面显示
  159. if (wx.getStorageSync('token')) {
  160. if (wx.getStorageSync('storeId')) {
  161. //保存商户、门店、token信息到redis
  162. util.request(api.ChooseStoreId, {
  163. storeId: wx.getStorageSync('storeId'),
  164. merchSn: wx.getStorageSync('merchSn'),
  165. isRefusedLogin: wx.getStorageSync('isRefusedLogin')
  166. }, 'POST').then(function (res) {
  167. if (res.errno === 0) {
  168. wx.setStorageSync('storeId', wx.getStorageSync('storeId'));
  169. wx.setStorageSync('merchSn', wx.getStorageSync('merchSn'));
  170. that.reLoad();
  171. that.getGoodsInfo();
  172. if(wx.getStorageSync('userInfo')){
  173. util.request(api.CartGoodsCount, {
  174. storeId: wx.getStorageSync('storeId')
  175. }).then(function (res) {
  176. if (res.errno === 0) {
  177. that.setData({
  178. cartGoodsCount: res.data.cartTotal.goodsCount
  179. });
  180. }
  181. });
  182. }
  183. }
  184. });
  185. }
  186. }
  187. },
  188. onReady: function() {
  189. let that = this;
  190. this.videoContext = wx.createVideoContext('myVideo');
  191. // 页面渲染完成
  192. // wx.setClipboardData({
  193. // data: '/pages/goods/goods?id=' + that.data.id,
  194. // success: function (res) {
  195. // wx.getClipboardData({
  196. // success: function (res) {
  197. // // console.log(res.data) // data
  198. // }
  199. // })
  200. // }
  201. // })
  202. },
  203. toggleNav() {
  204. this.setData({
  205. showNavList: !this.data.showNavList
  206. })
  207. },
  208. switchNav(event) {
  209. let name = event.currentTarget.dataset.name;
  210. wx.switchTab({
  211. url: `/pages/${name}/${name}`,
  212. });
  213. },
  214. //小程序里的转义方法
  215. escape2Html: function(str) {
  216. var arrEntities = {
  217. 'lt': '<',
  218. 'gt': '>',
  219. 'nbsp': ' ',
  220. 'amp': '&',
  221. 'quot': '"'
  222. };
  223. return str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function(all, t) {
  224. return arrEntities[t];
  225. });
  226. },
  227. //调用在需要的地方直接调用即可。如:this.escape2Html(contents)
  228. getGoodsInfo: function() {
  229. wx.showLoading({
  230. title: '加载中...',
  231. });
  232. setTimeout(function() {}, 350)
  233. let that = this;
  234. const regex = new RegExp('<img', 'gi');
  235. console.log('分享的storeId' + wx.getStorageSync('storeId'))
  236. // console.log('promId' + that.data.promId)
  237. util.request(api.GoodsDetail, {
  238. id: that.data.id,
  239. referrer: that.data.referrer,
  240. merchSn: wx.getStorageSync('merchSn'),
  241. storeId: wx.getStorageSync('storeId'),
  242. promId: that.data.promId
  243. }).then(function(res) {
  244. if (res.errno === 0) {
  245. that.setData({
  246. goods: res.data.info,
  247. gallery: res.data.gallery,
  248. attribute: res.data.attribute,
  249. issueList: res.data.issue,
  250. comment: res.data.comment,
  251. brand: res.data.brand,
  252. specificationList: res.data.specificationList,
  253. productList: res.data.productList,
  254. userHasCollect: res.data.userHasCollect,
  255. stockNum: res.data.stockNum,
  256. defaultFreight: res.data.defaultFreight,
  257. checkedSpecText: res.data.specificationList[0].valueList[0].value,
  258. detailContent: that.escape2Html(res.data.info.goods_desc).replace(regex, `<img style="width: 100%;"`),
  259. detailStoreId: res.data.info.storeId,
  260. merchSn: res.data.info.merchSn
  261. });
  262. if (res.data.cartNumber){
  263. that.setData({
  264. cartNumber: res.data.cartNumber
  265. });
  266. }
  267. if (wx.getStorageSync('userInfo')){
  268. that.getDetailTicketDiscountList();
  269. that.getDetailStoreCampMinusList();
  270. }
  271. if (res.data.userHasCollect == 1) {
  272. that.setData({
  273. 'collectBackImage': that.data.hasCollectImage
  274. });
  275. } else {
  276. that.setData({
  277. 'collectBackImage': that.data.noCollectImage
  278. });
  279. }
  280. // WxParse.wxParse('goodsDetail', 'html', res.data.info.goods_desc, that);
  281. that.getGoodsRelated();
  282. // that.getGoodsCrashList();
  283. //
  284. let _specificationList = that.data.specificationList;
  285. for (let i = 0; i < _specificationList.length; i++) {
  286. if (_specificationList[i].valueList.length == 1) {
  287. //如果已经选中,则反选,前端默认选中
  288. _specificationList[i].valueList[0].checked = true;
  289. }
  290. }
  291. that.setData({
  292. 'specificationList': _specificationList
  293. });
  294. }
  295. wx.hideLoading();
  296. });
  297. if (null != that.data.referrer && that.data.referrer > 0) {
  298. //转发获取优惠券
  299. util.request(api.GoodsTransferCoupon, {
  300. goods_id: that.data.id,
  301. referrer: that.data.referrer,
  302. send_type: 2
  303. }).then(function(res) {
  304. });
  305. }
  306. //商品列表图
  307. util.request(api.GoodsGallery, {
  308. goods_id: that.data.id
  309. }).then(function(res) {
  310. if (res.errno === 0) {
  311. that.setData({
  312. gallery: res.data
  313. })
  314. }
  315. });
  316. },
  317. // getGoodsCrashList: function () {
  318. // let that = this;
  319. // util.request(api.GoodsCrashList, { goodsId: that.data.id }).then(function (res) {
  320. // if (res.errno === 0) {
  321. // that.setData({
  322. // crashList: res.data,
  323. // });
  324. // that.data.crashList.length;
  325. // }
  326. // });
  327. // },
  328. clickSkuValue: function(event) {
  329. let that = this;
  330. let specNameId = event.currentTarget.dataset.nameId;
  331. let specValueId = event.currentTarget.dataset.valueId;
  332. //
  333. let _specificationList = this.data.specificationList;
  334. for (let i = 0; i < _specificationList.length; i++) {
  335. if (_specificationList[i].specification_id == specNameId) {
  336. for (let j = 0; j < _specificationList[i].valueList.length; j++) {
  337. if (_specificationList[i].valueList[j].id == specValueId) {
  338. //如果已经选中,则反选
  339. if (_specificationList[i].valueList[j].checked) {
  340. _specificationList[i].valueList[j].checked = false;
  341. } else {
  342. _specificationList[i].valueList[j].checked = true;
  343. }
  344. } else {
  345. _specificationList[i].valueList[j].checked = false;
  346. }
  347. }
  348. }
  349. }
  350. this.setData({
  351. 'specificationList': _specificationList
  352. });
  353. //重新计算spec改变后的信息
  354. goodsUtil.changeSpecInfo(that);
  355. },
  356. //购物车增加
  357. addNumber2: function(e) {
  358. let that = this;
  359. var goodsId = e.currentTarget.dataset.goodsId;
  360. var productId = e.currentTarget.dataset.productId;
  361. var hotGoods = that.data.hotGoods;
  362. util.request(api.CartAdd, {
  363. goodsId: goodsId,
  364. productId: productId,
  365. number: 1
  366. }, 'POST').then(function(res) {
  367. if (res.errno === 0 && null != res.data) {
  368. util.request(api.CartGoodsCount, {
  369. storeId: wx.getStorageSync('storeId')
  370. }).then(function(res) {
  371. if (res.errno === 0) {
  372. that.setData({
  373. cartGoodsCount: res.data.cartTotal.goodsCount
  374. });
  375. }
  376. });
  377. wx.showToast({
  378. title: '添加成功',
  379. icon: 'success',
  380. mask: true
  381. });
  382. } else {
  383. wx.showToast({
  384. title: res.errmsg,
  385. icon: 'none'
  386. })
  387. }
  388. });
  389. },
  390. onHide: function() {
  391. // 页面隐藏
  392. },
  393. onUnload: function() {
  394. // 页面关闭
  395. },
  396. switchAttrPop: function() {
  397. this.setData({
  398. openAttr: !this.data.openAttr,
  399. openAttrByOrder: false,
  400. openTicketAttr: false,
  401. openCampMinusAttr: false
  402. })
  403. },
  404. hideSwitchAttrPop: function() {
  405. this.setData({
  406. openAttr: false,
  407. openAttrByOrder: false,
  408. openTicketAttr: false,
  409. openCampMinusAttr: false
  410. })
  411. },
  412. switchAttrPopByOrder: function () {
  413. this.setData({
  414. openAttr: false,
  415. openAttrByOrder: !this.data.openAttrByOrder,
  416. openTicketAttr: false,
  417. openCampMinusAttr: false
  418. })
  419. },
  420. reLoad: function() {
  421. let that = this;
  422. if (wx.getStorageSync('storeId')) {
  423. if (wx.getStorageSync('userId')) {
  424. wx.request({
  425. url: api.updateLoginUser,
  426. data: {
  427. userId: wx.getStorageSync('userId'),
  428. storeId: wx.getStorageSync('storeId'),
  429. merchSn: wx.getStorageSync('merchSn')
  430. },
  431. method: 'POST',
  432. header: {
  433. 'Content-Type': 'application/json'
  434. },
  435. success: function(wxRes) {
  436. if (wxRes.data.errno === 0) {
  437. // console.log("用户信息更新成功");
  438. }
  439. },
  440. fail: function(err) {
  441. console.log("failed");
  442. }
  443. });
  444. }
  445. }
  446. },
  447. changeProperty: function(e) {
  448. var propertyName = e.currentTarget.dataset.propertyName;
  449. console.log(propertyName);
  450. },
  451. closeAttrOrCollect: function() {
  452. let that = this;
  453. //添加或是取消收藏
  454. util.request(api.CollectAddOrDelete, {
  455. typeId: 0,
  456. valueId: this.data.id
  457. }, "POST")
  458. .then(function(res) {
  459. let _res = res;
  460. if (_res.errno == 0) {
  461. if (_res.data.type == 'add') {
  462. that.setData({
  463. 'collectBackImage': that.data.hasCollectImage
  464. });
  465. wx.showToast({
  466. title: "收藏成功",
  467. mask: true
  468. });
  469. } else {
  470. that.setData({
  471. 'collectBackImage': that.data.noCollectImage
  472. });
  473. wx.showToast({
  474. title: "收藏取消",
  475. mask: true
  476. });
  477. }
  478. } else {
  479. wx.showToast({
  480. image: '/static/images/icon_error.png',
  481. title: _res.errmsg,
  482. mask: true
  483. });
  484. }
  485. });
  486. },
  487. openCartPage: function() {
  488. if (wx.getStorageSync('userInfo')){
  489. wx.switchTab({
  490. url: '/pages/cart/cart',
  491. });
  492. }else{
  493. wx.showModal({
  494. title: '授权提示',
  495. content: '用户未授权',
  496. confirmText: '确定授权',
  497. cancelText: '不授权',
  498. success(res) {
  499. if (res.confirm) {
  500. wx.navigateTo({
  501. url: '/pages/ucenter/userLogin/userLogin?view=goodsView'
  502. })
  503. } else if (res.cancel) {
  504. // console.log('用户点击取消')
  505. }
  506. }
  507. });
  508. }
  509. },
  510. addToCart: function () {
  511. var that = this;
  512. if (!wx.getStorageSync('userInfo')) {
  513. // wx.showToast({
  514. // image: '/static/images/icon_error.png',
  515. // title: '用户未授权'
  516. // });
  517. wx.showModal({
  518. title: '授权提示',
  519. content: '用户未授权',
  520. confirmText: '确定授权',
  521. cancelText: '不授权',
  522. success(res) {
  523. if (res.confirm) {
  524. wx.navigateTo({
  525. url: '/pages/ucenter/userLogin/userLogin?view=goodsView'
  526. })
  527. } else if (res.cancel) {
  528. // console.log('用户点击取消')
  529. }
  530. }
  531. });
  532. return;
  533. }
  534. if (that.data.openAttr == false) {
  535. //打开规格选择窗口
  536. that.setData({
  537. openAttr: !that.data.openAttr,
  538. openAttrByOrder: false,
  539. number: 1
  540. });
  541. } else {
  542. //提示选择完整规格
  543. if (!goodsUtil.isCheckedAllSpec(that)) {
  544. return false;
  545. }
  546. if (that.data.number + that.data.cartNumber > that.data.stockNum) {
  547. // wx.showToast({
  548. // title: '库存不足',
  549. // mask: true
  550. // });
  551. if (that.data.cartNumber == that.data.stockNum){
  552. wx.showToast({
  553. title: '不能再加了,购物车中已加入库存' + that.data.cartNumber + '个',
  554. icon: 'none'
  555. })
  556. }else{
  557. util.showErrorToast('库存不足');
  558. }
  559. // util.showErrorToast('库存不足');
  560. //找不到对应的product信息,提示没有库存
  561. return false;
  562. }
  563. // console.log(that.data.productList)
  564. // //根据选中的规格,判断是否有对应的sku信息
  565. let checkedProduct = goodsUtil.getCheckedProductItem(goodsUtil.getCheckedSpecKey(that), that);
  566. // console.log(goodsUtil.getCheckedSpecKey(that))
  567. if (!checkedProduct || checkedProduct.length <= 0) {
  568. wx.showToast({
  569. title: '库存不足',
  570. icon: 'none'
  571. });
  572. //找不到对应的product信息,提示没有库存
  573. return false;
  574. }
  575. //验证库存
  576. // if (checkedProduct.goods_number < this.data.number) {
  577. // //找不到对应的product信息,提示没有库存
  578. // return false;
  579. // }
  580. //添加到购物车
  581. util.request(api.CartAdd, {
  582. goodsId: this.data.goods.id,
  583. number: this.data.number,
  584. productId: checkedProduct[0].id,
  585. promId: that.data.promId
  586. }, "POST")
  587. .then(function(res) {
  588. let _res = res;
  589. if (_res.errno == 0) {
  590. wx.showToast({
  591. title: '添加成功'
  592. });
  593. that.setData({
  594. openAttr: !that.data.openAttr,
  595. openAttrByOrder: false,
  596. cartGoodsCount: _res.data.cartTotal.goodsCount
  597. });
  598. if (that.data.userHasCollect == 1) {
  599. that.setData({
  600. 'collectBackImage': that.data.hasCollectImage
  601. });
  602. } else {
  603. that.setData({
  604. 'collectBackImage': that.data.noCollectImage
  605. });
  606. }
  607. } else {
  608. wx.showToast({
  609. title: _res.errmsg,
  610. icon: 'none'
  611. })
  612. that.hideSwitchAttrPop();
  613. // that.setData({
  614. // stockNum: 0
  615. // });
  616. }
  617. });
  618. }
  619. },
  620. cutNumber: function() {
  621. this.setData({
  622. number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  623. });
  624. },
  625. addNumber: function() {
  626. this.setData({
  627. number: this.data.number + 1
  628. });
  629. },
  630. onShareAppMessage: function() {
  631. var that = this;
  632. // console.log("url:" + that.data.goods.list_pic_url);
  633. var userId = wx.getStorageSync('userId');
  634. console.log("userId:" + userId);
  635. console.log("detailStoreId:" + that.data.detailStoreId);
  636. console.log("merchSn:" + that.data.merchSn);
  637. console.log("promId:" + that.data.promId);
  638. var requestUrl = "";
  639. if (that.data.promId) {
  640. requestUrl = '/pages/goods/goods?scene=' + that.data.id + '&' + that.data.detailStoreId + '&' + that.data.promId +
  641. '&' + wx.getStorageSync('userId');
  642. } else {
  643. requestUrl = '/pages/goods/goods?id=' + that.data.id + '&&referrer=' + wx.getStorageSync('userId') +
  644. '&&storeId=' + that.data.detailStoreId +
  645. '&&merchSn=' + that.data.merchSn;
  646. }
  647. // console.log("requestUrl:" + requestUrl);
  648. return {
  649. title: '商品详情',
  650. desc: null != that.data.goods.name ? that.data.goods.name : "商品详情",
  651. imageUrl: that.data.goods.list_pic_url,
  652. path: requestUrl,
  653. success: function(res) {
  654. // console.log("转发成功");
  655. console.log("转发成功12:" + that.data.promId);
  656. if (that.data.promId) {
  657. //转发成功记录推广信息转发次数
  658. wx.request({
  659. url: api.GoodsSaveTransNum,
  660. data: {
  661. referrer: wx.getStorageSync('userId'),
  662. storeId: that.data.detailStoreId,
  663. promId: that.data.promId
  664. },
  665. method: 'POST',
  666. header: {
  667. 'Content-Type': 'application/json'
  668. },
  669. success: function(wxRes) {
  670. console.log("转发成功");
  671. }
  672. });
  673. }
  674. },
  675. fail: function(res) {
  676. console.log("转发失败");
  677. }
  678. }
  679. },
  680. //购物车增加
  681. addCrashNumber: function(e) {
  682. let that = this;
  683. var goodsId = e.currentTarget.dataset.goodsId;
  684. var productId = e.currentTarget.dataset.productId;
  685. util.request(api.CartAdd, {
  686. goodsId: goodsId,
  687. productId: productId,
  688. number: 1
  689. }, 'POST').then(function(res) {
  690. if (res.errno === 0 && null != res.data) {
  691. var hotGoods = that.data.hotGoods;
  692. hotGoods.forEach(function(val, index, arr) {
  693. if (val.id == goodsId) {
  694. val.cart_num = res.data;
  695. hotGoods[index] = val;
  696. that.setData({
  697. hotGoods: hotGoods
  698. });
  699. }
  700. }, that);
  701. }
  702. });
  703. },
  704. previewPic(e) {
  705. let url = e.currentTarget.dataset.url;
  706. let urls = [];
  707. urls[0] = url;
  708. wx.previewImage({
  709. urls
  710. })
  711. },
  712. switchNav(event) {
  713. wx.switchTab({
  714. url: '/pages/index/index'
  715. });
  716. },
  717. //触摸事件start
  718. touchStart(e) {
  719. let startX = e.changedTouches[0].pageX
  720. this.setData({
  721. startX: startX
  722. })
  723. console.log("startX:" + startX);
  724. },
  725. //触摸事件end
  726. touchEnd(e) {
  727. let _self = this;
  728. let windowWidth = this.data.windowWidth;
  729. let moveWidth = e.changedTouches[0].pageX - this.data.startX;
  730. let defineWidth = windowWidth / 20;
  731. let rankList = this.data.gallery;
  732. let index_now = e.currentTarget.dataset.index;
  733. if (moveWidth >= defineWidth) {
  734. //上一张
  735. let transWidth = (index_now - 1) * (-this.data.windowWidth);
  736. if (index_now >= 1) {
  737. this.setData({
  738. indexNum: index_now - 1,
  739. rankList: rankList,
  740. leftWidth: transWidth
  741. })
  742. }
  743. } else if (moveWidth <= (0 - defineWidth)) {
  744. //下一张
  745. this.videoContext.pause();
  746. let transWidth = (index_now + 1) * (-this.data.windowWidth);
  747. if (index_now < rankList.length - 1) {
  748. this.setData({
  749. indexNum: index_now + 1,
  750. rankList: rankList,
  751. leftWidth: transWidth
  752. })
  753. }
  754. } else {
  755. //console.log('不能修改样式')
  756. }
  757. },
  758. videoPlay(e) {
  759. // if (this.data.curr_id == e.currentTarget.dataset.id) {
  760. // this.setData({
  761. // curr_id: 0,
  762. // })
  763. // } else {
  764. // this.setData({
  765. // curr_id: e.currentTarget.dataset.id,
  766. // })
  767. // }
  768. this.setData({
  769. imgHiddenName: true,
  770. videoHiddenName: false
  771. })
  772. this.videoContext.play();
  773. },
  774. //触摸事件start
  775. touchStart2(e) {
  776. },
  777. touchEnd2(e) {
  778. this.setData({
  779. curr_id: 0,
  780. imgHiddenName: false,
  781. videoHiddenName: true
  782. })
  783. this.videoContext.pause();
  784. },
  785. getGoodsRelated: function() {
  786. let that = this;
  787. util.request(api.GoodsRelated, {
  788. id: that.data.id,
  789. page: that.data.page,
  790. size: that.data.size
  791. }).then(function(res) {
  792. if (res.errno === 0) {
  793. let goodsList = that.data.relatedGoods.concat(res.data.goodsList);
  794. that.setData({
  795. relatedGoods: goodsList,
  796. });
  797. }
  798. });
  799. },
  800. onReachBottom() {
  801. var that = this;
  802. wx.showLoading({
  803. title: '加载中...',
  804. })
  805. that.setData({
  806. page: that.data.page + 1
  807. });
  808. that.getGoodsRelated();
  809. },
  810. switchTicketPop: function() {
  811. this.setData({
  812. openTicketAttr: !this.data.openTicketAttr,
  813. openAttr: false,
  814. openAttrByOrder: false,
  815. openCampMinusAttr: false
  816. })
  817. if (this.data.openTicketAttr) {
  818. if (wx.getStorageSync('userInfo')) {
  819. this.getTicketDiscountList();
  820. }
  821. }
  822. },
  823. // hideSwitchTicketPop: function () {
  824. // this.setData({
  825. // openTicketAttr: false,
  826. // openAttr: false
  827. // })
  828. // },
  829. /**
  830. * 领取优惠券
  831. */
  832. getUserCoupon(event) {
  833. var that = this;
  834. let tickDiscId = event.currentTarget.dataset.couponId;
  835. let storeTopicId = event.currentTarget.dataset.storeTopicId;
  836. let storeId = that.data.storeId;
  837. util.request(api.getUserCoupon, {
  838. tickDiscId: tickDiscId,
  839. storeTopicId: storeTopicId,
  840. storeId: storeId
  841. }, 'POST').then(function(res) {
  842. if (res.errno === 0) {
  843. wx.showToast({
  844. title: '领取成功'
  845. });
  846. that.setData({
  847. discStatus: 0
  848. });
  849. if (wx.getStorageSync('userInfo')) {
  850. this.getTicketDiscountList();
  851. }
  852. } else {
  853. wx.showToast({
  854. title: res.errmsg,
  855. icon: 'none'
  856. })
  857. }
  858. });
  859. },
  860. /**
  861. * 查询商品所支持的优惠券列表
  862. */
  863. getTicketDiscountList() {
  864. let that = this;
  865. util.request(api.DiscountByGoodsIdList, {
  866. storeId: that.data.storeId,
  867. goodsId: that.data.id,
  868. size: that.data.discountSize
  869. }).then(function(res) {
  870. if (res.errno === 0) {
  871. that.setData({
  872. ticketDiscountList: res.data
  873. });
  874. }
  875. });
  876. },
  877. /**
  878. * 查看优惠券列表,详情页展示
  879. */
  880. getDetailTicketDiscountList() {
  881. let that = this;
  882. util.request(api.DiscountByGoodsIdList, {
  883. storeId: that.data.storeId,
  884. goodsId: that.data.id,
  885. size: that.data.discountDetailSize
  886. }).then(function(res) {
  887. if (res.errno === 0) {
  888. that.setData({
  889. detailTicketDiscountList: res.data
  890. });
  891. }
  892. });
  893. },
  894. /**
  895. * 查看满减满折列表
  896. */
  897. getStoreCampMinusList() {
  898. let that = this;
  899. util.request(api.StoreCampMinusByGoodsIdList, {
  900. storeId: that.data.storeId,
  901. goodsId: that.data.id,
  902. size: that.data.discountSize
  903. }).then(function(res) {
  904. if (res.errno === 0) {
  905. that.setData({
  906. storeCampMinusList: res.data
  907. });
  908. let storeCampMinusList = that.data.storeCampMinusList
  909. for (let i = 0; i < storeCampMinusList.length; i++) {
  910. if (storeCampMinusList[i].campMinusType == '00' && storeCampMinusList[i].minusMode == '00') {
  911. that.setData({
  912. minusList0000: true
  913. });
  914. }
  915. if (storeCampMinusList[i].campMinusType == '01' && storeCampMinusList[i].minusMode == '00') {
  916. that.setData({
  917. minusList0100: true
  918. });
  919. }
  920. if (storeCampMinusList[i].campMinusType == '00' && storeCampMinusList[i].minusMode == '10') {
  921. that.setData({
  922. minusList0010: true
  923. });
  924. }
  925. if (storeCampMinusList[i].campMinusType == '01' && storeCampMinusList[i].minusMode == '10') {
  926. that.setData({
  927. minusList0110: true
  928. });
  929. }
  930. if (storeCampMinusList[i].campMinusType == '10') {
  931. that.setData({
  932. minusList10: true
  933. });
  934. }
  935. if (storeCampMinusList[i].campMinusType == '11') {
  936. that.setData({
  937. minusList11: true
  938. });
  939. }
  940. }
  941. }
  942. });
  943. },
  944. /**
  945. * 查看满减满折列表,详情页展示
  946. */
  947. getDetailStoreCampMinusList() {
  948. let that = this;
  949. util.request(api.StoreCampMinusByGoodsIdList, {
  950. storeId: that.data.storeId,
  951. goodsId: that.data.id,
  952. size: that.data.campDetailSize
  953. }).then(function(res) {
  954. if (res.errno === 0) {
  955. that.setData({
  956. detailStoreCampMinusList: res.data
  957. });
  958. }
  959. });
  960. },
  961. switchCampMinusPop: function() {
  962. this.setData({
  963. openCampMinusAttr: !this.data.openCampMinusAttr,
  964. openTicketAttr: false,
  965. openAttrByOrder: false,
  966. openAttr: false
  967. })
  968. if (this.data.openCampMinusAttr) {
  969. this.getStoreCampMinusList();
  970. }
  971. },
  972. hideSwitchCampMinusPop: function() {
  973. this.setData({
  974. openCampMinusAttr: false,
  975. openTicketAttr: false,
  976. openAttrByOrder: false,
  977. openAttr: false
  978. })
  979. },
  980. selectCampMinus(event) {
  981. var that = this;
  982. let campId = event.currentTarget.dataset.campId;
  983. let campName = event.currentTarget.dataset.campName;
  984. wx.navigateTo({
  985. url: '../ucenter/campMinus/campMinus?campMinusId=' + campId + '&&storeId=' + that.data.storeId + '&&campName=' + campName + "&&isShare=1"
  986. })
  987. },
  988. checkoutOrder: function () {
  989. if (!wx.getStorageSync('userInfo')) {
  990. // wx.showToast({
  991. // image: '/static/images/icon_error.png',
  992. // title: '用户未授权'
  993. // });
  994. wx.showModal({
  995. title: '授权提示',
  996. content: '用户未授权',
  997. confirmText: '确定授权',
  998. cancelText: '不授权',
  999. success(res) {
  1000. if (res.confirm) {
  1001. wx.navigateTo({
  1002. url: '/pages/ucenter/userLogin/userLogin?view=goodsView'
  1003. })
  1004. } else if (res.cancel) {
  1005. // console.log('用户点击取消')
  1006. }
  1007. }
  1008. });
  1009. return;
  1010. }
  1011. //获取已选择的商品
  1012. let that = this;
  1013. util.request(api.getCurUser, {
  1014. userInfo: app.globalData.userInfo
  1015. }, 'POST').then(function(res) {
  1016. if (res.errno === 0) {
  1017. // console.log('that.data.mobile:' + res.data.mobile);
  1018. if (res.data.mobile == '' || res.data.mobile == null) {
  1019. wx.showModal({
  1020. title: '',
  1021. confirmColor: '#b4282d',
  1022. showCancel: false,
  1023. content: '您的手机号码未绑定,请先绑定手机号再进行购买',
  1024. success: function(res) {
  1025. if (res.confirm) {
  1026. wx.navigateTo({
  1027. url: '../../pages/auth/newuser/newuser'
  1028. });
  1029. }
  1030. }
  1031. });
  1032. } else {
  1033. if (that.data.openAttrByOrder == false) {
  1034. //打开规格选择窗口
  1035. that.setData({
  1036. openAttrByOrder: !that.data.openAttrByOrder,
  1037. openAttr: false,
  1038. number: 1
  1039. });
  1040. } else {
  1041. //提示选择完整规格
  1042. if (!goodsUtil.isCheckedAllSpec(that)) {
  1043. return false;
  1044. }
  1045. if (that.data.number > that.data.stockNum) {
  1046. util.showErrorToast('库存不足');
  1047. return false;
  1048. }
  1049. // //根据选中的规格,判断是否有对应的sku信息
  1050. let checkedProduct = goodsUtil.getCheckedProductItem(goodsUtil.getCheckedSpecKey(that), that);
  1051. //找不到对应的product信息,提示没有库存
  1052. if (!checkedProduct || checkedProduct.length <= 0) {
  1053. wx.showToast({
  1054. title: '库存不足',
  1055. icon: 'none'
  1056. });
  1057. return false;
  1058. }
  1059. wx.navigateTo({
  1060. url: '../shopping/checkout/checkout?checkCart=00&&goodsId=' + that.data.id + '&&number=' + that.data.number
  1061. })
  1062. }
  1063. }
  1064. }
  1065. });
  1066. },
  1067. })