goods.js 38 KB

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