goods.js 33 KB

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