goods.js 31 KB

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