goods.js 27 KB

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