goods.js 31 KB

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