goods.js 28 KB

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