goods.js 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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 user = require('../../utils/user.js');
  6. Page({
  7. data: {
  8. selfPayShow: false,
  9. selfPayCheck: false,
  10. spreadPayShow: false,
  11. spreadPayChecked: false,
  12. canShare: false,
  13. id: 0,
  14. goods: {},
  15. groupon: [], //该商品支持的团购规格
  16. grouponLink: {}, //参与的团购
  17. attribute: [],
  18. issueList: [],
  19. comment: [],
  20. //brand: {},
  21. specificationList: [],
  22. productList: [],
  23. relatedGoods: [],
  24. cartGoodsCount: 0,
  25. userHasCollect: 0,
  26. number: 1,
  27. checkedSpecText: '规格数量选择',
  28. tmpSpecText: '请选择规格数量',
  29. checkedSpecPrice: 0,
  30. openAttr: false,
  31. openShare: false,
  32. collect: false,
  33. shareImage: '',
  34. isGroupon: false, //标识是否是一个参团购买
  35. soldout: false,
  36. canWrite: false, //用户是否获取了保存相册的权限
  37. name_height: 58, //根据名称长度 设置高度
  38. userInfo: {},
  39. hasLogin: false,
  40. shareId: 0, //options传递过来的参数 表示是打开哪个分享
  41. wxShareId: 0, //如果要分享此商品的分享id
  42. addressInfo: {},
  43. rId: 0, //分享人id,
  44. selfDiscount: 0,
  45. spreadDiscount: 0,
  46. referee: {
  47. self: 0,
  48. share: 0,
  49. },
  50. myqr_show: false, //我的分享二维码
  51. myqr_url: null, //分享海报及推广语言
  52. myqr_ads: '',
  53. myqr_blur: 100, //背景模糊值
  54. myqr_bottom_btn: 'fixed' //底部功能按钮
  55. },
  56. // 页面分享
  57. onShareAppMessage: function () {
  58. let that = this;
  59. if (!app.globalData.hasLogin) {
  60. wx.navigateTo({
  61. url: "/pages/auth/login/login"
  62. });
  63. return;
  64. }
  65. //获取分享id 用来追踪链接
  66. //引荐人和非引荐人的 链接不同
  67. let imgUrl = that.data.goods.shareWxUrl ? that.data.goods.shareWxUrl : that.data.goods.gallery[0];
  68. var path = '/pages/goods/goods?id=' + that.data.id + '&rId=' + that.data.userInfo.id;
  69. if (that.data.userInfo.isDis) { //是引荐人
  70. path = '/pages/goods/goods?id=' + that.data.id + '&shareId=' + that.data.wxShareId;
  71. }
  72. this.hidePoster();
  73. return {
  74. title: that.data.goods.name + "\n" + that.data.myqr_ads,
  75. path: path,
  76. imageUrl: imgUrl
  77. }
  78. },
  79. shareFriendOrCircle: function () {
  80. if (!app.globalData.hasLogin) {
  81. wx.navigateTo({
  82. url: "/pages/auth/login/login"
  83. });
  84. return;
  85. }
  86. //分享 弹出图片
  87. let that = this;
  88. wx.removeStorageSync(String(that.data.goods.id));
  89. //取出本地缓存中的分享图
  90. // let sUrl=wx.getStorageSync(String(that.data.goods.id));
  91. // if(sUrl!=null&&sUrl!=""){//获取过的分享图 不需要再次获取
  92. // that.setData({
  93. // myqr_url:sUrl,
  94. // myqr_show:true,
  95. // myqr_blur:5
  96. // });
  97. // return;
  98. // }
  99. wx.showLoading({
  100. title: '生成中',
  101. });
  102. util.request(api.QrGoodsCodeCreate, {
  103. goodsId: that.data.goods.id
  104. }).then(function (res) {
  105. wx.hideLoading();
  106. if (res.errno == 0) {
  107. that.setData({
  108. myqr_url: res.data.imgUrl,
  109. myqr_show: true,
  110. myqr_blur: 5
  111. });
  112. //保存分享图url到本地缓存
  113. // wx.setStorageSync(String(that.data.goods.id),res.data.imgUrl);
  114. } else if (res.errno == 501) {
  115. wx.navigateTo({
  116. url: "/pages/auth/login/login"
  117. });
  118. } else {
  119. wx.showToast({
  120. title: res.errmsg,
  121. duration: 5000
  122. })
  123. }
  124. });
  125. //var that = this;
  126. /*if (this.data.openShare === false) {
  127. this.setData({
  128. openShare: !this.data.openShare
  129. });
  130. } else {
  131. return false;
  132. }*/
  133. },
  134. handleSetting: function (e) {
  135. var that = this;
  136. // console.log(e)
  137. if (!e.detail.authSetting['scope.writePhotosAlbum']) {
  138. wx.showModal({
  139. title: '警告',
  140. content: '不授权无法保存',
  141. showCancel: false
  142. })
  143. that.setData({
  144. canWrite: false
  145. })
  146. } else {
  147. wx.showToast({
  148. title: '保存成功'
  149. })
  150. that.setData({
  151. canWrite: true
  152. })
  153. }
  154. },
  155. // 保存分享图
  156. saveShare: function () {
  157. let that = this;
  158. wx.showLoading({
  159. title: '图片保存中',
  160. });
  161. console.log(that.data);
  162. wx.downloadFile({
  163. url: that.data.myqr_url,
  164. success: function (res) {
  165. console.log(res)
  166. wx.saveImageToPhotosAlbum({
  167. filePath: res.tempFilePath,
  168. success: function (res) {
  169. wx.hideLoading();
  170. wx.showModal({
  171. title: '存图成功',
  172. content: '图片成功保存到相册了,可以分享到朋友圈了',
  173. showCancel: false,
  174. confirmText: '好的',
  175. confirmColor: '#a78845',
  176. success: function (res) {
  177. if (res.confirm) {
  178. console.log('用户点击确定');
  179. that.setData({
  180. myqr_show: false,
  181. myqr_blur: 0
  182. });
  183. }
  184. }
  185. })
  186. },
  187. fail: function (res) {
  188. wx.hideLoading();
  189. wx.showModal({
  190. title: '警告',
  191. content: '不授权无法保存',
  192. showCancel: false
  193. })
  194. that.setData({
  195. myqr_show: false,
  196. myqr_blur: 0
  197. });
  198. }
  199. })
  200. },
  201. fail: function (res) {
  202. wx.hideLoading();
  203. console.log(res);
  204. }
  205. })
  206. },
  207. //从分享的团购进入
  208. getGrouponInfo: function (grouponId) {
  209. let that = this;
  210. util.request(api.GroupOnJoin, {
  211. grouponId: grouponId
  212. }).then(function (res) {
  213. if (res.errno === 0) {
  214. that.setData({
  215. grouponLink: res.data.groupon,
  216. id: res.data.goods.id
  217. });
  218. //获取商品详情
  219. that.getGoodsInfo();
  220. }
  221. });
  222. },
  223. getReferee: function () {
  224. let that = this;
  225. util.request(api.GoodsDetail, {
  226. id: that.data.id,
  227. rId: that.data.rId
  228. }).then(res => {
  229. if (res.errno === 0) {
  230. if (res.data.referee.self != null && res.data.referee.self > 0) {
  231. res.data.referee.self = res.data.referee.self.toFixed(2);
  232. res.data.referee.share = res.data.referee.share.toFixed(2);
  233. }
  234. that.setData({
  235. referee: res.data.referee
  236. });
  237. console.info(res.data.referee)
  238. if (JSON.stringify(res.data.referee) == "{}") {
  239. that.setData({
  240. referee: {
  241. self: 0,
  242. share: 0,
  243. isReferee: false
  244. }
  245. });
  246. }
  247. //是引荐人 更新userinfo数据(因为如果后台将客户标记为引荐人 此userinfo是取缓存数据 不会更新状态)
  248. if (that.data.referee != null && that.data.referee.isReferee) {
  249. let userInfo = that.data.userInfo;
  250. userInfo.isDis = true;
  251. that.setData({
  252. userInfo: userInfo
  253. })
  254. wx.setStorageSync('userInfo', userInfo);
  255. }
  256. }
  257. });
  258. },
  259. // 获取商品信息
  260. getGoodsInfo: function () {
  261. console.info('1-》getGoodsInfo start');
  262. let that = this;
  263. util.request(api.GoodsDetail, {
  264. id: that.data.id,
  265. rId: that.data.rId
  266. }).then(function (res) {
  267. console.info('2-》getGoodsInfo end');
  268. console.info(res);
  269. if (res.errno === 0) {
  270. let _specificationList = res.data.specificationList
  271. // 如果仅仅存在一种货品,那么商品页面初始化时默认checked
  272. if (_specificationList.length == 1) {
  273. if (_specificationList[0].valueList.length == 1) {
  274. _specificationList[0].valueList[0].checked = true
  275. console.info(res.data.info);
  276. // 如果仅仅存在一种货品,那么商品价格应该和货品价格一致
  277. // 这里检测一下
  278. let _productPrice = res.data.productList[0].price;
  279. let _goodsPrice = res.data.info.retailPrice;
  280. if (_productPrice != _goodsPrice) {
  281. console.error('商品数量价格和货品不一致');
  282. }
  283. that.setData({
  284. checkedSpecText: _specificationList[0].valueList[0].value,
  285. tmpSpecText: '已选择:' + _specificationList[0].valueList[0].value,
  286. });
  287. }
  288. }
  289. console.info('3-》getGoodsInfo count end');
  290. //设置显示名称的控件高度
  291. let height = 56;
  292. if (res.data.info.name.length > 17 & res.data.info.name.length < 36)
  293. height = 76;
  294. else if (res.data.info.name.length >= 36)
  295. height = 116;
  296. // console.info(res.data.share);
  297. that.setData({
  298. goods: res.data.info,
  299. attribute: res.data.attribute,
  300. issueList: res.data.issue,
  301. comment: res.data.comment,
  302. //brand: res.data.brand,
  303. specificationList: res.data.specificationList,
  304. productList: res.data.productList,
  305. //productList: res.data.info.products,
  306. userHasCollect: res.data.userHasCollect,
  307. shareImage: res.data.shareImage,
  308. checkedSpecPrice: res.data.info.retailPrice,
  309. canShare: res.data.share,
  310. name_height: height,
  311. wxShareId: res.data.shareId
  312. });
  313. //如果是通过分享的团购参加团购,则团购项目应该与分享的一致并且不可更改
  314. /* if (that.data.isGroupon) {
  315. let groupons = that.data.groupon;
  316. for (var i = 0; i < groupons.length; i++) {
  317. if (groupons[i].id != that.data.grouponLink.rulesId) {
  318. groupons.splice(i, 1);
  319. }
  320. }
  321. groupons[0].checked = true;
  322. //重设团购规格
  323. that.setData({
  324. groupon: groupons
  325. });
  326. }*/
  327. if (res.data.userHasCollect == 1) {
  328. that.setData({
  329. collect: true
  330. });
  331. } else {
  332. that.setData({
  333. collect: false
  334. });
  335. }
  336. WxParse.wxParse('goodsDetail', 'html', res.data.info.detail, that);
  337. //获取推荐商品
  338. that.getGoodsRelated();
  339. }
  340. });
  341. },
  342. // 获取推荐商品
  343. getGoodsRelated: function () {
  344. let that = this;
  345. util.request(api.GoodsRelated, {
  346. id: that.data.id,
  347. shopId: this.data.addressInfo.shopId
  348. }).then(function (res) {
  349. if (res.errno === 0) {
  350. that.setData({
  351. relatedGoods: res.data.list,
  352. });
  353. }
  354. });
  355. },
  356. // 团购选择
  357. clickGroupon: function (event) {
  358. let that = this;
  359. //参与团购,不可更改选择
  360. if (that.data.isGroupon) {
  361. return;
  362. }
  363. let specName = event.currentTarget.dataset.name;
  364. let specValueId = event.currentTarget.dataset.valueId;
  365. let _grouponList = this.data.groupon;
  366. for (let i = 0; i < _grouponList.length; i++) {
  367. if (_grouponList[i].id == specValueId) {
  368. if (_grouponList[i].checked) {
  369. _grouponList[i].checked = false;
  370. } else {
  371. _grouponList[i].checked = true;
  372. }
  373. } else {
  374. _grouponList[i].checked = false;
  375. }
  376. }
  377. this.setData({
  378. groupon: _grouponList,
  379. });
  380. },
  381. // 规格选择
  382. clickSkuValue: function (event) {
  383. let that = this;
  384. let specName = event.currentTarget.dataset.name;
  385. let specValueId = event.currentTarget.dataset.valueId;
  386. //判断是否可以点击
  387. //TODO 性能优化,可在wx:for中添加index,可以直接获取点击的属性名和属性值,不用循环
  388. let _specificationList = this.data.specificationList;
  389. for (let i = 0; i < _specificationList.length; i++) {
  390. if (_specificationList[i].name === specName) {
  391. for (let j = 0; j < _specificationList[i].valueList.length; j++) {
  392. if (_specificationList[i].valueList[j].id == specValueId) {
  393. //如果已经选中,则反选
  394. if (_specificationList[i].valueList[j].checked) {
  395. _specificationList[i].valueList[j].checked = false;
  396. } else {
  397. _specificationList[i].valueList[j].checked = true;
  398. }
  399. } else {
  400. _specificationList[i].valueList[j].checked = false;
  401. }
  402. }
  403. }
  404. }
  405. this.setData({
  406. specificationList: _specificationList,
  407. });
  408. //重新计算spec改变后的信息
  409. this.changeSpecInfo();
  410. //重新计算哪些值不可以点击
  411. },
  412. //获取选中的团购信息
  413. getCheckedGrouponValue: function () {
  414. let checkedValues = {};
  415. let _grouponList = this.data.groupon;
  416. for (let i = 0; i < _grouponList.length; i++) {
  417. if (_grouponList[i].checked) {
  418. checkedValues = _grouponList[i];
  419. }
  420. }
  421. return checkedValues;
  422. },
  423. //获取选中的规格信息
  424. getCheckedSpecValue: function () {
  425. let checkedValues = [];
  426. let _specificationList = this.data.specificationList;
  427. for (let i = 0; i < _specificationList.length; i++) {
  428. let _checkedObj = {
  429. name: _specificationList[i].name,
  430. valueId: 0,
  431. valueText: ''
  432. };
  433. for (let j = 0; j < _specificationList[i].valueList.length; j++) {
  434. if (_specificationList[i].valueList[j].checked) {
  435. _checkedObj.valueId = _specificationList[i].valueList[j].id;
  436. _checkedObj.valueText = _specificationList[i].valueList[j].value;
  437. }
  438. }
  439. checkedValues.push(_checkedObj);
  440. }
  441. return checkedValues;
  442. },
  443. //判断规格是否选择完整
  444. isCheckedAllSpec: function () {
  445. return !this.getCheckedSpecValue().some(function (v) {
  446. if (v.valueId == 0) {
  447. return true;
  448. }
  449. });
  450. },
  451. getCheckedSpecKey: function () {
  452. let checkedValue = this.getCheckedSpecValue().map(function (v) {
  453. return v.valueText;
  454. });
  455. return checkedValue;
  456. },
  457. // 规格改变时,重新计算价格及显示信息
  458. changeSpecInfo: function () {
  459. let checkedNameValue = this.getCheckedSpecValue();
  460. //设置选择的信息
  461. let checkedValue = checkedNameValue.filter(function (v) {
  462. if (v.valueId != 0) {
  463. return true;
  464. } else {
  465. return false;
  466. }
  467. }).map(function (v) {
  468. return v.valueText;
  469. });
  470. if (checkedValue.length > 0) {
  471. this.setData({
  472. tmpSpecText: checkedValue.join(' ')
  473. });
  474. } else {
  475. this.setData({
  476. tmpSpecText: '请选择规格数量'
  477. });
  478. }
  479. if (this.isCheckedAllSpec()) {
  480. this.setData({
  481. checkedSpecText: this.data.tmpSpecText
  482. });
  483. // 规格所对应的货品选择以后
  484. let checkedProductArray = this.getCheckedProductItem(this.getCheckedSpecKey());
  485. if (!checkedProductArray || checkedProductArray.length <= 0) {
  486. this.setData({
  487. soldout: true
  488. });
  489. console.error('规格所对应货品不存在');
  490. return;
  491. }
  492. let checkedProduct = checkedProductArray[0];
  493. if (checkedProduct.number > 0) {
  494. this.setData({
  495. checkedSpecPrice: checkedProduct.price,
  496. soldout: false
  497. });
  498. } else {
  499. this.setData({
  500. checkedSpecPrice: this.data.goods.retailPrice,
  501. soldout: true
  502. });
  503. }
  504. } else {
  505. this.setData({
  506. checkedSpecText: '规格数量选择',
  507. checkedSpecPrice: this.data.goods.retailPrice,
  508. soldout: false
  509. });
  510. }
  511. },
  512. // 获取选中的产品(根据规格)
  513. getCheckedProductItem: function (key) {
  514. return this.data.productList.filter(function (v) {
  515. if (v.specifications.toString() == key.toString()) {
  516. return true;
  517. } else {
  518. return false;
  519. }
  520. });
  521. },
  522. onLoad: function (options) {
  523. // if (app.globalData.hasLogin) {
  524. // let userInfo = wx.getStorageSync('userInfo');
  525. // app.globalData.userInfo=userInfo;
  526. // this.setData({
  527. // userInfo: userInfo,
  528. // hasLogin: true
  529. // });
  530. // }
  531. // 页面初始化 options为页面跳转所带来的参数
  532. var addressInfo = wx.getStorageSync('addressInfo');
  533. if (addressInfo) {
  534. this.setData({
  535. addressInfo: addressInfo
  536. })
  537. }
  538. if (options.rId) {
  539. this.setData({
  540. rId: parseInt(options.rId)
  541. })
  542. }
  543. if (options.grouponId) {
  544. this.setData({
  545. isGroupon: true,
  546. });
  547. this.getGrouponInfo(options.grouponId);
  548. }
  549. if (options.shareId) {
  550. this.setData({
  551. shareId: parseInt(options.shareId)
  552. })
  553. }
  554. console.info(this.data.shareId)
  555. if (options.id) {
  556. this.setData({
  557. id: parseInt(options.id)
  558. });
  559. this.getGoodsInfo();
  560. }
  561. // let that = this;
  562. // wx.getSetting({
  563. // success: function (res) {
  564. // console.log(res)
  565. // //不存在相册授权
  566. // if (!res.authSetting['scope.writePhotosAlbum']) {
  567. // wx.authorize({
  568. // scope: 'scope.writePhotosAlbum',
  569. // success: function () {
  570. // that.setData({
  571. // canWrite: true
  572. // })
  573. // },
  574. // fail: function (err) {
  575. // that.setData({
  576. // canWrite: false
  577. // })
  578. // }
  579. // })
  580. // } else {
  581. // that.setData({
  582. // canWrite: true
  583. // });
  584. // }
  585. // }
  586. // })
  587. },
  588. onShow: function () {
  589. // 页面显示
  590. if (app.globalData.hasLogin) {
  591. let userInfo = wx.getStorageSync('userInfo');
  592. app.globalData.userInfo = userInfo;
  593. this.setData({
  594. userInfo: userInfo,
  595. hasLogin: true
  596. });
  597. }
  598. this.getReferee();
  599. // 已登陆并且是引荐人展示自购推广
  600. if (this.data.hasLogin && this.data.userInfo.isDis) {
  601. // this.setData({
  602. // selfDiscount:0,
  603. // spreadDiscount:0,
  604. // })
  605. }
  606. },
  607. //添加或是取消收藏
  608. addCollectOrNot: function () {
  609. let that = this;
  610. util.request(api.CollectAddOrDelete, {
  611. type: 0,
  612. valueId: this.data.id
  613. }, "POST")
  614. .then(function (res) {
  615. if (that.data.userHasCollect == 1) {
  616. that.setData({
  617. collect: false,
  618. userHasCollect: 0
  619. });
  620. } else {
  621. that.setData({
  622. collect: true,
  623. userHasCollect: 1
  624. });
  625. }
  626. });
  627. },
  628. //立即购买(先自动加入购物车)
  629. addFast: function () {
  630. var that = this;
  631. // 如果是安心包只能购买一个
  632. console.log(this.data.goods);
  633. // 如果是中介类,跳转到信息收集页面
  634. if (this.data.goods.type == 2) {
  635. app.globalData.infoGoods = {
  636. name: this.data.goods.name,
  637. brief: this.data.goods.brief,
  638. picUrl: this.data.goods.picUrl,
  639. type: this.data.goods.type
  640. };
  641. wx.navigateTo({
  642. url: '/pages/goods/infoCollect/infoCollect'
  643. });
  644. return false;
  645. }
  646. // if (this.data.openAttr == false) {
  647. // //打开规格选择窗口
  648. // this.setData({
  649. // openAttr: !this.data.openAttr
  650. // });
  651. // } else {
  652. //提示选择完整规格
  653. if (!this.isCheckedAllSpec()) {
  654. util.showErrorToast('请选择完整规格');
  655. return false;
  656. }
  657. //根据选中的规格,判断是否有对应的sku信息
  658. let checkedProductArray = this.getCheckedProductItem(this.getCheckedSpecKey());
  659. if (!checkedProductArray || checkedProductArray.length <= 0) {
  660. //找不到对应的product信息,提示没有库存
  661. util.showErrorToast('没有库存');
  662. return false;
  663. }
  664. let checkedProduct = checkedProductArray[0];
  665. //验证库存
  666. if (checkedProduct.number <= 0) {
  667. util.showErrorToast('没有库存');
  668. return false;
  669. }
  670. //验证团购是否有效
  671. let checkedGroupon = this.getCheckedGrouponValue();
  672. //立即购买
  673. util.request(api.CartFastAdd, {
  674. goodsId: this.data.goods.id,
  675. number: this.data.number,
  676. productId: checkedProduct.id,
  677. shareId: this.data.shareId
  678. }, "POST")
  679. .then(res => {
  680. if (res.errno == 0) {
  681. // 如果storage中设置了cartId,则是立即购买,否则是购物车购买
  682. try {
  683. wx.setStorageSync('cartId', res.data);
  684. wx.setStorageSync('grouponRulesId', checkedGroupon.id);
  685. wx.setStorageSync('grouponLinkId', that.data.grouponLink.id);
  686. // 支付结果页面展示图片用
  687. app.globalData.infoGoods = {
  688. name: this.data.goods.name,
  689. brief: this.data.goods.brief,
  690. picUrl: this.data.goods.picUrl,
  691. type: this.data.goods.type
  692. };
  693. console.info(that.data.shareId)
  694. if (that.data.shareId > 0)
  695. wx.navigateTo({
  696. url: '/pages/checkout/checkout?shareId=' + that.data.shareId
  697. })
  698. else
  699. wx.navigateTo({
  700. url: '/pages/checkout/checkout'
  701. })
  702. } catch (e) {
  703. console.info(e)
  704. }
  705. } else {
  706. util.showErrorToast(res.errmsg);
  707. }
  708. });
  709. //}
  710. },
  711. //添加到购物车
  712. addToCart: function () {
  713. var that = this;
  714. if (this.data.openAttr == false) {
  715. //打开规格选择窗口
  716. this.setData({
  717. openAttr: !this.data.openAttr
  718. });
  719. } else {
  720. //提示选择完整规格
  721. if (!this.isCheckedAllSpec()) {
  722. util.showErrorToast('请选择完整规格');
  723. return false;
  724. }
  725. //根据选中的规格,判断是否有对应的sku信息
  726. let checkedProductArray = this.getCheckedProductItem(this.getCheckedSpecKey());
  727. if (!checkedProductArray || checkedProductArray.length <= 0) {
  728. //找不到对应的product信息,提示没有库存
  729. util.showErrorToast('没有库存');
  730. return false;
  731. }
  732. let checkedProduct = checkedProductArray[0];
  733. //验证库存
  734. if (checkedProduct.number <= 0) {
  735. util.showErrorToast('没有库存');
  736. return false;
  737. }
  738. //添加到购物车
  739. util.request(api.CartAdd, {
  740. goodsId: this.data.goods.id,
  741. number: this.data.number,
  742. productId: checkedProduct.id
  743. }, "POST")
  744. .then(function (res) {
  745. let _res = res;
  746. if (_res.errno == 0) {
  747. wx.showToast({
  748. title: '添加成功'
  749. });
  750. that.setData({
  751. openAttr: !that.data.openAttr,
  752. cartGoodsCount: _res.data
  753. });
  754. //显示tab购物车缓存
  755. //user.cartadd(that.data.number);
  756. if (that.data.userHasCollect == 1) {
  757. that.setData({
  758. collect: true
  759. });
  760. } else {
  761. that.setData({
  762. collect: false
  763. });
  764. }
  765. } else {
  766. util.showErrorToast(_res.errmsg);
  767. }
  768. });
  769. }
  770. },
  771. cutNumber: function () {
  772. this.setData({
  773. number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  774. });
  775. },
  776. addNumber: function () {
  777. // 如果是安心包只能购买一个
  778. console.log(this.data.goods);
  779. if (this.data.goods.goodsSn == '1036016') {
  780. wx.showToast({
  781. title: '此款商品只能购买一个',
  782. icon: 'none',
  783. duration: 2000
  784. });
  785. } else {
  786. this.setData({
  787. number: this.data.number + 1
  788. });
  789. }
  790. },
  791. onHide: function () {
  792. // 页面隐藏
  793. },
  794. onUnload: function () {
  795. // 页面关闭
  796. },
  797. switchAttrPop: function () {
  798. if (this.data.openAttr == false) {
  799. this.setData({
  800. openAttr: !this.data.openAttr
  801. });
  802. }
  803. },
  804. closeAttr: function () {
  805. this.setData({
  806. openAttr: false,
  807. });
  808. },
  809. closeShare: function () {
  810. this.setData({
  811. openShare: false,
  812. });
  813. },
  814. openCartPage: function () {
  815. wx.switchTab({
  816. url: '/pages/tabBar/cart/cart'
  817. });
  818. },
  819. onReady: function () {
  820. // 页面渲染完成
  821. },
  822. qrtouchstart: function () {
  823. this.setData({
  824. myqr_blur: 100,
  825. myqr_show: false
  826. });
  827. },
  828. copyToHM() {
  829. let copyLink = this.data.goods.copyLink;
  830. console.log(copyLink);
  831. if (copyLink != undefined && copyLink != '') {
  832. wx.setClipboardData({
  833. data: copyLink,
  834. success(res) {
  835. wx.getClipboardData({
  836. success(res) {
  837. console.log(res.data) // data
  838. }
  839. })
  840. }
  841. });
  842. } else {
  843. wx.showToast({
  844. title: '该商品暂未配置盒马链接',
  845. icon: 'none'
  846. })
  847. }
  848. },
  849. onSelfPayChange(e) {
  850. console.log(e);
  851. this.setData({
  852. selfPayChecked: e.detail,
  853. });
  854. },
  855. onSpreadPayChange(e) {
  856. console.log(e);
  857. this.setData({
  858. spreadPayChecked: e.detail,
  859. });
  860. },
  861. cancelSelfPay() {
  862. let selfPayChecked = this.data.selfPayChecked;
  863. if (selfPayChecked == true) {
  864. wx.setStorageSync('selfPayChecked', true);
  865. }
  866. this.setData({
  867. selfPayShow: false,
  868. });
  869. },
  870. cancelSpreadPay() {
  871. let spreadPayChecked = this.data.spreadPayChecked;
  872. if (spreadPayChecked == true) {
  873. wx.setStorageSync('spreadPayChecked', true);
  874. }
  875. this.setData({
  876. spreadPayShow: false,
  877. });
  878. },
  879. goSelfPay() {
  880. let selfPayChecked = wx.getStorageSync('selfPayChecked');
  881. if (selfPayChecked != true) {
  882. this.setData({
  883. selfPayShow: true,
  884. });
  885. } else {
  886. this.addFast();
  887. }
  888. },
  889. goSpread() {
  890. let spreadPayChecked = wx.getStorageSync('spreadPayChecked');
  891. console.log(spreadPayChecked);
  892. if (spreadPayChecked != true) {
  893. this.setData({
  894. spreadPayShow: true,
  895. });
  896. } else {
  897. // wx.navigateTo({
  898. // url: '/pages/goods/goodsPoster/goodsPoster?goodsId='+this.data.goods.id,
  899. // })
  900. this.createPoster();
  901. }
  902. },
  903. posterCopy() {
  904. let that = this
  905. wx.setClipboardData({
  906. data: that.data.myqr_ads,
  907. success: function (res) {
  908. wx.getClipboardData({
  909. success: function (res) {
  910. wx.showToast({
  911. title: '文案复制成功',
  912. icon: 'success',
  913. duration: 2000
  914. });
  915. // that.hidePoster();
  916. }
  917. })
  918. }
  919. })
  920. },
  921. hidePoster() {
  922. this.setData({
  923. myqr_show: false,
  924. myqr_blur: 100,
  925. myqr_bottom_btn: 'fixed'
  926. })
  927. },
  928. createPoster() {
  929. wx.showLoading({
  930. title: '生成中',
  931. })
  932. wx.pageScrollTo({
  933. scrollTop: 0
  934. })
  935. if (this.data.myqr_url != null) {
  936. wx.hideLoading();
  937. this.setData({
  938. myqr_show: true,
  939. myqr_blur: 25,
  940. myqr_bottom_btn: 'unset'
  941. })
  942. return;
  943. }
  944. util.request(api.QrGoodsCodeCreate, {
  945. goodsId: this.data.goods.id
  946. }).then(res => {
  947. wx.hideLoading();
  948. if (res.errno == 0) {
  949. this.setData({
  950. myqr_url: res.data.imgUrl,
  951. myqr_ads: res.data.ads,
  952. myqr_show: true,
  953. myqr_blur: 25,
  954. myqr_bottom_btn: 'unset'
  955. })
  956. } else {
  957. util.showErrorToast("未设置分享图")
  958. }
  959. });
  960. },
  961. goSaveQrcode: function () { //保存二维码图片
  962. let that = this;
  963. console.info(that.data.myqr_url);
  964. wx.downloadFile({
  965. url: that.data.myqr_url,
  966. success: function (res) {
  967. if (res.statusCode === 200) {
  968. let img = res.tempFilePath;
  969. wx.saveImageToPhotosAlbum({
  970. filePath: img,
  971. success(res) {
  972. that.goCloseMyqr('个人专属二维码图片已保存到您的相册');
  973. },
  974. fail(res) {
  975. that.goCloseMyqr('网络或授权问题 图片保存失败 请重试');
  976. }
  977. })
  978. } else
  979. that.goCloseMyqr('网络或授权问题 图片保存失败 请重试');
  980. },
  981. fail(res) {
  982. that.goCloseMyqr('网络或授权问题 图片保存失败 请重试');
  983. }
  984. })
  985. },
  986. goCloseMyqr: function (msg) { //关闭分享图片的弹层
  987. this.hidePoster();
  988. if (msg) {
  989. wx.showToast({
  990. title: msg,
  991. icon: 'none',
  992. duration: 3000
  993. })
  994. }
  995. },
  996. reSelfPay() {
  997. let selfPayChecked = this.data.selfPayChecked;
  998. if (selfPayChecked == true) {
  999. wx.setStorageSync('selfPayChecked', true);
  1000. }
  1001. this.setData({
  1002. selfPayShow: false,
  1003. });
  1004. this.addFast();
  1005. },
  1006. reSpread() {
  1007. let spreadPayChecked = this.data.spreadPayChecked;
  1008. if (spreadPayChecked == true) {
  1009. wx.setStorageSync('spreadPayChecked', true);
  1010. }
  1011. this.setData({
  1012. spreadPayShow: false,
  1013. });
  1014. this.createPoster();
  1015. // wx.navigateTo({
  1016. // url: '/pages/goods/goodsPoster/goodsPoster?goodsId='+this.data.goods.id,
  1017. // })
  1018. },
  1019. goMemberCenter() {
  1020. if (!app.globalData.hasLogin) {
  1021. wx.navigateTo({
  1022. url: "/pages/auth/login/login"
  1023. });
  1024. return;
  1025. } else {
  1026. wx.navigateTo({
  1027. url: "/pages/member/memberCenter/memberCenter"
  1028. });
  1029. }
  1030. },
  1031. })