goods.js 35 KB

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