goods.js 34 KB

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