goods.js 38 KB

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