prod.js 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571
  1. // pages/prod/prod.js
  2. const app = getApp();
  3. var http = require('../../utils/http.js');
  4. var config = require('../../utils/config.js');
  5. var util = require('../../utils/util.js');
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. tab:0,
  12. list:[],//相似商品和一起买
  13. shopId: 1,
  14. shopName: '',
  15. picDomain: config.picDomain,
  16. indicatorDots: true,
  17. indicatorColor: '#f2f2f2',
  18. indicatorActiveColor: '#006CE8',
  19. isAuthInfo: true,
  20. autoplay: true,
  21. interval: 3000,
  22. duration: 1000,
  23. prodNum: 1,
  24. totalCartNum: 0,
  25. groupActivityId: 0,
  26. pic: "",
  27. imgs: '',
  28. prodName: '',
  29. price: 0,
  30. actPrice: 0,
  31. content: '',
  32. prodId: 0,
  33. brief: '',
  34. halfTitle: '',
  35. skuId: 0,
  36. popupShow: false,
  37. // 是否获取过用户领取过的优惠券id
  38. loadCouponIds: false,
  39. skuShow: false,
  40. skuGroupShow: false,
  41. commentShow: false,
  42. couponList: [],
  43. skuList: [],
  44. skuGroup: {},
  45. findSku: true,
  46. defaultSku: undefined,
  47. detaultGroupSku: undefined,
  48. selectedProp: [],
  49. selectedPropObj: {},
  50. selectedProperties: "",
  51. propKeys: [],
  52. allProperties: [],
  53. prodCommData: {},
  54. prodCommPage: {
  55. current: 0,
  56. pages: 0,
  57. records: []
  58. },
  59. isAll:false,
  60. prodList: [],
  61. pages: 0,
  62. current: 1,
  63. littleCommPage: [],
  64. evaluate: -1,
  65. isCollection: false,
  66. shareShow: false,
  67. isDist: false, //是否分销员
  68. isDistProd: false, //是否分销商品
  69. distributionCardNo: "", //分销员卡号
  70. wxCodeShow: false, //是否显示二维码弹层
  71. shareWxCode: "", //分享二维码图片路径
  72. groupActivity: {}, // 团购活动
  73. endOfGroupTime: {}, // 距离团购活动结束还剩多久
  74. joinGroupList: [], // 可加入的团列表
  75. skuShowType: 0, // sku的显示类型 0普通sku 1拼团sku
  76. totalStocks: 0, // 活动剩余库存
  77. hasMaxNum: false, // 是否限购
  78. maxNum: 0, // 限购数量
  79. joinGroupListTimer: '',
  80. endOfGroupTimer: '',
  81. scene: '',
  82. prodDiscountList: [], // 商品促销活动列表
  83. phoneNumber: '', //客服电话
  84. showBacktop: false, //回到顶部显隐
  85. showDiscountPopup: false, //促销弹窗显隐
  86. videoContext: '', // 视频组件
  87. isPlaying: false, // 视频是否播放中
  88. currentSwiper: 0, // 当前轮播index
  89. },
  90. /**
  91. * 生命周期函数--监听页面加载
  92. */
  93. onLoad: function (options) {
  94. console.log(options)
  95. //根据Ticket获取保存的内容(pc团购扫码 || 分销)
  96. if (options.scene) {
  97. this.setData({
  98. scene: options.scene,
  99. })
  100. this.getContent() //根据Ticket获取保存的内容
  101. }
  102. if(options.shopid){
  103. this.setData({
  104. shopId: Number(options.shopid) ,
  105. });
  106. }
  107. // 加载商品信息
  108. if (options.prodid) {
  109. this.setData({
  110. prodId: options.prodid,
  111. });
  112. this.executionFunction() //所有需要加载的函数
  113. }
  114. if (options.cardno && options.type == 2) {
  115. // this.bindDistUser(options.cardno)
  116. this.setData({
  117. distributionCardNo: options.cardno,
  118. })
  119. }
  120. this.getCartCount() //请求购物车数量
  121. },
  122. /**
  123. * 执行函数
  124. */
  125. executionFunction: function () {
  126. // 加载评论数据
  127. this.getProdCommData();
  128. // 加载评论项
  129. this.getLittleProdComm();
  130. // 获取商品是否被收藏信息
  131. this.getCollection();
  132. // 查询分销开关是否开启
  133. this.getDistInfo();
  134. //加载商品数据
  135. this.getProdInfo();
  136. // 获取商品所有促销活动
  137. this.getPordDiscountList()
  138. //获取推荐商品
  139. this.getProdRecommended()
  140. // 获取相似商品
  141. this.getSimilarProdList()
  142. },
  143. /**
  144. * 根据Ticket获取保存的内容
  145. */
  146. getContent: function () {
  147. http.request({
  148. url: "/qrcodeTicket/getContent",
  149. method: "GET",
  150. data: {
  151. ticket: this.data.scene
  152. },
  153. callBack: (res) => {
  154. console.log(res)
  155. var content = JSON.parse(res.content);
  156. var ids = JSON.parse(content.content)
  157. if (res.type == 1) { //pc团购二维码跳转
  158. this.setData({
  159. prodId: ids.prodId,
  160. groupActivityId: ids.groupActivityId
  161. })
  162. this.executionFunction() //所有需要加载的函数
  163. } else if (res.type == 2) { //分销员二维码跳转
  164. this.setData({
  165. prodId: ids.shareProdId,
  166. distributionCardNo: ids.cardNo
  167. })
  168. this.executionFunction() //所有需要加载的函数
  169. }
  170. }
  171. })
  172. },
  173. /**
  174. * 生命周期函数--监听页面卸载
  175. */
  176. onUnload: function () {
  177. clearTimeout(this.data.endOfGroupTimer)
  178. },
  179. handleTab(e){
  180. let tab = e.currentTarget.dataset.tab
  181. if(tab == this.data.tab) return
  182. this.setData({
  183. tab,
  184. list:[]
  185. })
  186. if(tab == 0){//相似商品
  187. this.getSimilarProdList()
  188. }else{
  189. this.getOften()
  190. }
  191. },
  192. /**
  193. * 分销绑定用户
  194. */
  195. // bindDistUser(cardno) {
  196. // var params = {
  197. // url: "/p/distribution/bindUser",
  198. // method: "post",
  199. // data: cardno,
  200. // callBack:res=>{
  201. // console.log('绑定分销员',res)
  202. // }
  203. // }
  204. // http.request(params)
  205. // },
  206. /**
  207. * 查询分销相关信息
  208. */
  209. getDistInfo() {
  210. //查询分销开关是否开启
  211. http.request({
  212. url: "/p/distribution/distributionBasicSet/canDistribution",
  213. method: "GET",
  214. dontTrunLogin: true,
  215. // data: {
  216. // shopId: this.data.shopId,
  217. // },
  218. callBack: (res) => {
  219. if (res == 1) {
  220. this.getIsDistInfo();
  221. }
  222. }
  223. })
  224. },
  225. /**
  226. * 查询用户 是否为分销员
  227. */
  228. getIsDistInfo() {
  229. http.request({
  230. url: "/p/distribution/user/distributionUserInfo",
  231. method: "GET",
  232. callBack: (res) => {
  233. console.log('是否分销员', res)
  234. if (res && res.state == 1) {
  235. this.setData({
  236. isDist: true
  237. });
  238. this.getIsDistProd();
  239. wx.setStorageSync("distCardNo", res.cardNo);
  240. }
  241. }
  242. })
  243. },
  244. /**
  245. * 查询是否为分销商品
  246. */
  247. getIsDistProd() {
  248. http.request({
  249. url: "/p/distribution/prod/isDistributionProd",
  250. method: "GET",
  251. data: {
  252. prodId: this.data.prodId,
  253. },
  254. callBack: (res) => {
  255. console.log('是否分销商品', res)
  256. if (res) {
  257. this.setData({
  258. isDistProd: true
  259. });
  260. }
  261. }
  262. })
  263. },
  264. /**
  265. * 获取商品是否被收藏信息
  266. */
  267. getCollection() {
  268. wx.showLoading();
  269. var params = {
  270. url: "/p/user/collection/isCollection",
  271. method: "GET",
  272. dontTrunLogin: true,
  273. data: {
  274. prodId: this.data.prodId
  275. },
  276. callBack: (res) => {
  277. this.setData({
  278. isCollection: res
  279. })
  280. wx.hideLoading();
  281. }
  282. };
  283. http.request(params);
  284. },
  285. /**
  286. * 添加或者取消收藏商品
  287. */
  288. addOrCannelCollection() {
  289. util.checkAuthInfo(() => {
  290. var params = {
  291. url: "/p/user/collection/addOrCancel",
  292. method: "POST",
  293. data: this.data.prodId,
  294. callBack: (res) => {
  295. this.setData({
  296. isCollection: !this.data.isCollection
  297. })
  298. wx.showToast({
  299. title: res ? '已添加收藏' : '已取消收藏',
  300. duration: 3000,
  301. icon: 'none',
  302. })
  303. setTimeout(() => {
  304. wx.hideToast()
  305. }, 3000)
  306. }
  307. };
  308. http.request(params);
  309. })
  310. },
  311. // 获取商品信息
  312. getProdInfo() {
  313. wx.showLoading();
  314. var params = {
  315. url: "/prod/prodInfo",
  316. method: "GET",
  317. data: {
  318. prodId: this.data.prodId,
  319. platform:1,
  320. shopId:this.data.shopId
  321. // userType: 0
  322. },
  323. callBack: (res) => {
  324. let pic = ''
  325. var imgStrs = res.imgs;
  326. if(imgStrs){
  327. var imgs = imgStrs.split(",");
  328. }
  329. var content = res.content?util.formatHtml(res.content):'';
  330. if(res.pic){
  331. pic = res.pic.split(',')
  332. res.pic = pic[0]
  333. }
  334. this.setData({
  335. imgs: imgs,
  336. content: content,
  337. price: res.price,
  338. prodName: res.prodName,
  339. prodId: res.prodId,
  340. brief: res.brief,
  341. halfTitle: res.halfTitle,
  342. totalStocks: res.totalStocks,
  343. // skuId: res.skuId
  344. skuList: res.skuList,
  345. pic: res.pic,
  346. // shopId: res.shopId,
  347. video: res.video,
  348. prodType: res.prodType
  349. });
  350. if (res.video) {
  351. this.getVideoInfo()
  352. }
  353. // 团购商品
  354. if (res.prodType === 1) {
  355. this.setData({
  356. groupActivityId: res.activityId
  357. });
  358. this.getGroupActivity();
  359. this.getJoinGroupList();
  360. }
  361. // 获取优惠券
  362. this.getCouponList();
  363. // 组装sku
  364. res.skuList&&this.groupSkuProp(res.skuList);
  365. this.getShopInfo();
  366. wx.hideLoading();
  367. },
  368. errCallBack: (err) => {
  369. console.log(err)
  370. if (err.statusCode == 400) {
  371. wx.showModal({
  372. title: '提示',
  373. content: err.data,
  374. showCancel: false,
  375. success: (res) => {
  376. if (res.confirm) {
  377. wx.navigateBack({
  378. delta: 1
  379. })
  380. }
  381. }
  382. })
  383. }
  384. }
  385. };
  386. http.request(params);
  387. },
  388. /**
  389. * 获取店铺信息
  390. */
  391. getShopInfo() {
  392. http.request({
  393. url: "/shop/headInfo",
  394. method: "GET",
  395. data: {
  396. shopId: this.data.shopId,
  397. },
  398. callBack: (res) => {
  399. this.setData({
  400. shopName: res.shopName,
  401. shopLogo: res.shopLogo,
  402. phoneNumber: res.tel
  403. })
  404. }
  405. })
  406. },
  407. getProdCommData() {
  408. http.request({
  409. url: "/p/prodComm/commentCount",
  410. method: "GET",
  411. data: {
  412. prodId: this.data.prodId,
  413. },
  414. callBack: (res) => {
  415. this.setData({
  416. prodCommData: res
  417. })
  418. }
  419. })
  420. },
  421. // 获取部分评论
  422. getLittleProdComm() {
  423. if (this.data.prodCommPage.records.length) {
  424. return;
  425. }
  426. this.getProdCommPage();
  427. },
  428. getMoreCommPage(e) {
  429. this.getProdCommPage();
  430. },
  431. // 获取分页获取评论
  432. getProdCommPage(e) {
  433. if (e) {
  434. if (e.currentTarget.dataset.evaluate === this.data.evaluate) {
  435. return;
  436. }
  437. this.setData({
  438. prodCommPage: {
  439. current: 0,
  440. pages: 0,
  441. records: []
  442. },
  443. evaluate: e.currentTarget.dataset.evaluate
  444. })
  445. }
  446. http.request({
  447. url: "/prod/prodCommPageByProd",
  448. method: "GET",
  449. data: {
  450. prodId: this.data.prodId,
  451. size: 10,
  452. current: this.data.prodCommPage.current + 1,
  453. evaluate: this.data.evaluate
  454. },
  455. callBack: (res) => {
  456. res.records.forEach(item => {
  457. if (item.pics) {
  458. item.pics = item.pics.split(',')
  459. }
  460. })
  461. let records = this.data.prodCommPage.records
  462. records = records.concat(res.records)
  463. this.setData({
  464. prodCommPage: {
  465. current: res.current,
  466. pages: res.pages,
  467. records: records
  468. }
  469. })
  470. // 如果商品详情中没有评论的数据,截取两条到商品详情页商品详情
  471. if (!this.data.littleCommPage.length) {
  472. this.setData({
  473. littleCommPage: records.slice(0, 2)
  474. })
  475. }
  476. }
  477. })
  478. },
  479. /**
  480. * 评论小图点击事件(点击图片显示大图)
  481. */
  482. clickImg: function (e) {
  483. const current = e.currentTarget.dataset.index //获取当前点击的 图片 url
  484. const pics = e.currentTarget.dataset.pics
  485. wx.previewImage({
  486. current: pics[current],
  487. urls: pics
  488. })
  489. },
  490. getCouponList() {
  491. http.request({
  492. url: "/coupon/listByProdId",
  493. method: "GET",
  494. data: {
  495. prodId: this.data.prodId,
  496. shopId: this.data.shopId,
  497. },
  498. callBack: (res) => {
  499. this.setData({
  500. couponList: res
  501. })
  502. }
  503. })
  504. },
  505. // 获取推荐商品
  506. getProdRecommended(){
  507. http.request({
  508. url: "/prod/prodRecommended",
  509. method: "GET",
  510. data: {
  511. current: this.data.current,
  512. size: 10,
  513. },
  514. callBack: (res) => {
  515. if(res.records&&res.records.length){
  516. let prodList = []
  517. let img = ''
  518. res.records.map(e => {
  519. if(e.pic){
  520. img = e.pic.split(',')
  521. e.pic = img[0]
  522. }
  523. })
  524. if (this.data.current == 1) {
  525. prodList = res.records
  526. this.setData({
  527. prodList: res.records,
  528. pages: res.pages,
  529. current: res.current
  530. });
  531. } else {
  532. prodList = this.data.prodList
  533. prodList.push(...res.records)
  534. this.setData({
  535. prodList
  536. })
  537. }
  538. if (prodList.length >= res.total) {
  539. this.setData({
  540. isAll: true
  541. })
  542. }
  543. }
  544. }
  545. })
  546. },
  547. // 获取经常一起买
  548. getOften(){
  549. let that = this
  550. http.request({
  551. url: "/sku/buyTogetherOften",
  552. method: "GET",
  553. data: {
  554. shopId:this.data.shopId,
  555. prodId:this.data.prodId,
  556. channelId:wx.getStorageSync('channelId'),
  557. current: 1,
  558. size: 18,
  559. },
  560. callBack: (res) => {
  561. let list = that.splitArrayToPages(res.records)
  562. that.setData({
  563. list
  564. })
  565. }
  566. })
  567. },
  568. // 获取相似商品
  569. getSimilarProdList(){
  570. let that = this
  571. http.request({
  572. url: "/prod/similarProdList",
  573. method: "GET",
  574. data: {
  575. shopId:this.data.shopId,
  576. prodId:this.data.prodId,
  577. channelId:wx.getStorageSync('channelId'),
  578. current: 1,
  579. size: 18,
  580. },
  581. callBack: (res) => {
  582. if(res.code == 200){
  583. let list = that.splitArrayToPages(res.data.records)
  584. that.setData({
  585. list
  586. })
  587. }
  588. }
  589. })
  590. },
  591. // 工具函数:拆分数组为「最多3页,每页6个」
  592. splitArrayToPages(arr) {
  593. const pageSize = 6; // 每页6个
  594. const maxPages = 3; // 最多3页
  595. const result = [];
  596. // 循环拆分:最多循环3次(确保只分3页)
  597. for (let i = 0; i < maxPages; i++) {
  598. // 计算当前页的起始索引:i * 6(第0页:0-5,第1页:6-11,第2页:12-17)
  599. const start = i * pageSize;
  600. // 截取当前页元素(不足6个时取剩余所有,超过18个时截取到17索引)
  601. const page = arr.slice(start, start + pageSize);
  602. // 加入结果数组(不足3页时,后续循环会push空数组)
  603. result.push(page);
  604. }
  605. return result;
  606. },
  607. /**
  608. * 跳转到商品详情页
  609. */
  610. toProdPage: function (e) {
  611. var prodid = e.currentTarget.dataset.prodid;
  612. var shopid = e.currentTarget.dataset.shopid;
  613. if (prodid) {
  614. wx.navigateTo({
  615. url: `/pages/prod/prod?prodid=${prodid}&shopid=${shopid}`,
  616. })
  617. }
  618. },
  619. /**
  620. * 团购商品详情信息
  621. */
  622. getGroupActivity() {
  623. http.request({
  624. url: "/groupProd/info",
  625. method: "GET",
  626. data: {
  627. prodId: this.data.prodId,
  628. groupActivityId: this.data.groupActivityId,
  629. },
  630. callBack: (res) => {
  631. if (res.success) {
  632. this.setData({
  633. groupActivity: res.obj,
  634. maxNum: res.obj.maxNum,
  635. hasMaxNum: res.obj.hasMaxNum
  636. })
  637. this.setDefaultGroupSku();
  638. this.groupActivityCountdown(res.obj)
  639. } else {
  640. this.setData({
  641. groupActivityId: null
  642. });
  643. }
  644. }
  645. })
  646. },
  647. // 团购倒计时
  648. groupActivityCountdown(groupActivity) {
  649. let endOfGroupTime;
  650. // activityStatus 1 未开始
  651. if (groupActivity.activityStatus === 1) {
  652. endOfGroupTime = util.endOfStartTime(util.dateToTimestamp(groupActivity.startTime), new Date().getTime())
  653. } else {
  654. endOfGroupTime = util.endOfStartTime(new Date().getTime(), util.dateToTimestamp(groupActivity.endTime))
  655. }
  656. this.setData({
  657. endOfGroupTime: endOfGroupTime,
  658. endOfGroupTimer: setTimeout(() => this.groupActivityCountdown(groupActivity), 1000)
  659. })
  660. },
  661. // 可加入的拼团列表
  662. getJoinGroupList() {
  663. http.request({
  664. url: "/groupProd/joinGroupList",
  665. method: "GET",
  666. data: {
  667. prodId: this.data.prodId,
  668. groupActivityId: this.data.groupActivityId,
  669. showSize: 3
  670. },
  671. callBack: (res) => {
  672. this.setData({
  673. joinGroupList: res
  674. })
  675. this.joinGroupListCountdown(res)
  676. }
  677. })
  678. },
  679. joinGroupListCountdown(joinGroupList) {
  680. joinGroupList.forEach(item => {
  681. item.endOfGroupTime = util.endOfStartTime(new Date().getTime(), util.dateToTimestamp(item.endTime))
  682. })
  683. this.setData({
  684. joinGroupList: joinGroupList,
  685. joinGroupListTimer: setTimeout(() => this.joinGroupListCountdown(joinGroupList), 1000)
  686. })
  687. },
  688. //根据sku的属性 分组
  689. groupSkuProp: function (skuList) {
  690. // var skuList = this.data.skuList;
  691. if (skuList.length == 1 && skuList[0].properties == "") {
  692. this.setData({
  693. defaultSku: skuList[0]
  694. });
  695. this.setDefaultGroupSku();
  696. return;
  697. }
  698. var skuGroup = {};
  699. var allProperties = [];
  700. var propKeys = [];
  701. this.setData({
  702. selectedPropObj: {}
  703. });
  704. for (var i = 0; i < skuList.length; i++) {
  705. var defaultSku = this.data.defaultSku;
  706. var isDefault = false;
  707. if (!defaultSku && skuList[i].price == this.data.price) { //找到和商品价格一样的那个SKU,作为默认选中的SKU
  708. defaultSku = skuList[i];
  709. isDefault = true;
  710. this.setData({
  711. defaultSku: defaultSku
  712. });
  713. }
  714. var properties = skuList[i].properties; //版本:公开版;颜色:金色;内存:64GB
  715. allProperties.push(properties);
  716. var propList = properties.split(";"); // ["版本:公开版","颜色:金色","内存:64GB"]
  717. var selectedPropObj = this.data.selectedPropObj;
  718. for (var j = 0; j < propList.length; j++) {
  719. var propval = propList[j].split(":"); //["版本","公开版"]
  720. var props = skuGroup[propval[0]]; //先取出 版本对应的值数组
  721. //如果当前是默认选中的sku,把对应的属性值 组装到selectedProp
  722. if (isDefault) {
  723. propKeys.push(propval[0]);
  724. selectedPropObj[propval[0]] = propval[1];
  725. }
  726. if (props == undefined) {
  727. props = []; //假设还没有版本,新建个新的空数组
  728. props.push(propval[1]); //把 "公开版" 放进空数组
  729. } else {
  730. if (!this.array_contain(props, propval[1])) { //如果数组里面没有"公开版"
  731. props.push(propval[1]); //把 "公开版" 放进数组
  732. }
  733. }
  734. skuGroup[propval[0]] = props; //最后把数据 放回版本对应的值
  735. }
  736. this.setData({
  737. selectedPropObj: selectedPropObj,
  738. propKeys: propKeys
  739. });
  740. }
  741. this.parseSelectedObjToVals();
  742. this.setData({
  743. skuGroup: skuGroup,
  744. allProperties: allProperties
  745. });
  746. this.setDefaultGroupSku();
  747. },
  748. //将已选的 {key:val,key2:val2}转换成 [val,val2]
  749. parseSelectedObjToVals: function () {
  750. var selectedPropObj = this.data.selectedPropObj;
  751. var selectedProperties = "";
  752. var selectedProp = [];
  753. for (var key in selectedPropObj) {
  754. selectedProp.push(selectedPropObj[key]);
  755. selectedProperties += key + ":" + selectedPropObj[key] + ";";
  756. }
  757. selectedProperties = selectedProperties.substring(0, selectedProperties.length - 1);
  758. this.setData({
  759. selectedProp: selectedProp,
  760. selectedProperties: selectedProperties
  761. });
  762. var findSku = false;
  763. for (var i = 0; i < this.data.skuList.length; i++) {
  764. if (this.data.skuList[i].properties == selectedProperties) {
  765. findSku = true;
  766. this.setData({
  767. defaultSku: this.data.skuList[i]
  768. });
  769. break;
  770. }
  771. }
  772. this.setData({
  773. findSku: findSku
  774. });
  775. this.setDefaultGroupSku();
  776. },
  777. //点击选择规格
  778. toChooseItem: function (e) {
  779. var ok = e.currentTarget.dataset.ok;
  780. if (ok == 0) {
  781. return;
  782. }
  783. var val = e.currentTarget.dataset.val;
  784. var key = e.currentTarget.dataset.key;
  785. var selectedPropObj = this.data.selectedPropObj;
  786. selectedPropObj[key] = val;
  787. this.setData({
  788. selectedPropObj: selectedPropObj
  789. });
  790. this.parseSelectedObjToVals();
  791. },
  792. //判断数组是否包含某对象
  793. array_contain: function (array, obj) {
  794. for (var i = 0; i < array.length; i++) {
  795. if (array[i] == obj) //如果要求数据类型也一致,这里可使用恒等号===
  796. return true;
  797. }
  798. return false;
  799. },
  800. /**
  801. * 设置选中的拼团sku
  802. */
  803. setDefaultGroupSku() {
  804. if (this.data.groupActivityId) {
  805. var groupSkuList = this.data.groupActivity.groupSkuList;
  806. if (groupSkuList) {
  807. for (var i = 0; i < groupSkuList.length; i++) {
  808. if (groupSkuList[i].properties == this.data.selectedProperties) {
  809. this.setData({
  810. detaultGroupSku: groupSkuList[i]
  811. });
  812. break;
  813. }
  814. }
  815. }
  816. }
  817. },
  818. /**
  819. * 去凑团
  820. */
  821. toSpellGroupDetail(e) {
  822. const groupTeamId = e.currentTarget.dataset.groupteamid
  823. util.checkAuthInfo(() => {
  824. wx.navigateTo({
  825. url: '/pages/spellGroupDetails/spellGroupDetails?groupTeamId=' + groupTeamId,
  826. })
  827. })
  828. },
  829. /**
  830. * 生命周期函数--监听页面初次渲染完成
  831. */
  832. onReady: function () {
  833. this.videoContext = wx.createVideoContext('myVideo')
  834. },
  835. /**
  836. * 生命周期函数--监听页面显示
  837. */
  838. onShow: function () {
  839. this.getCartCount()
  840. if (this.data.prodType == 1) {
  841. this.getJoinGroupList()
  842. }
  843. },
  844. getCartCount: function () {
  845. var params = {
  846. url: "/p/shopCart/prodCount",
  847. method: "GET",
  848. dontTrunLogin: true,
  849. data: {platform:1},
  850. callBack: (res) => {
  851. if (res > 0) {
  852. // wx.setTabBarBadge({
  853. // index: 2,
  854. // text: res + "",
  855. // })
  856. var app = getApp();
  857. app.globalData.totalCartCount = res;
  858. } else {
  859. // wx.removeTabBarBadge({
  860. // index: 2
  861. // })
  862. var app = getApp();
  863. app.globalData.totalCartCount = 0;
  864. }
  865. this.setData({
  866. totalCartNum: res
  867. })
  868. }
  869. };
  870. http.request(params);
  871. },
  872. /**
  873. * 阻止滚动
  874. */
  875. preventScorll() {
  876. },
  877. /**
  878. * 页面相关事件处理函数--监听用户下拉动作
  879. */
  880. onPullDownRefresh: function () {
  881. },
  882. /**
  883. * 页面上拉触底事件的处理函数
  884. */
  885. onReachBottom: function () {
  886. this.getNextPage()
  887. },
  888. // 触底加载下一页
  889. getNextPage() {
  890. console.log('getNextPage',this.data.current);
  891. if (this.data.pages > this.data.current) {
  892. this.setData({
  893. current: this.data.current + 1
  894. })
  895. this.getProdRecommended()
  896. } else {
  897. this.setData({
  898. isAll: true
  899. })
  900. }
  901. },
  902. /**
  903. * 跳转到首页
  904. */
  905. toHomePage: function () {
  906. wx.switchTab({
  907. url: '/pages/index/index',
  908. })
  909. },
  910. /**
  911. * 跳转到购物车
  912. */
  913. toCartPage: function () {
  914. wx.switchTab({
  915. url: '/pages/basket/basket',
  916. })
  917. },
  918. handleContact: function (e) {
  919. console.log(e.detail, '----路径')
  920. console.log(e.detail.query, '----参数')
  921. // this.get_register()
  922. },
  923. /**
  924. * 创建商户
  925. * @param {*} event
  926. */
  927. // get_register: function () {
  928. // let url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxc032a09413289004&secret=453c5047ae43288f1d015d48df32a5c0'
  929. // wx.request({
  930. // url:url,
  931. // method: 'GET',
  932. // data: {},
  933. // header: {
  934. // 'content-type': 'application/json' // 默认值
  935. // },
  936. // success(res) {
  937. // // console.log(res.data.access_token)
  938. // wx.request({
  939. // url: 'https://api.weixin.qq.com/cgi-bin/business/register?access_token='+res.data.access_token,
  940. // method: 'POST',
  941. // data: {
  942. // account_name: "test_shopname",
  943. // nickname: "苹果",
  944. // icon_media_id: "png",
  945. // transfer_to_commkf: true
  946. // },
  947. // header: {
  948. // 'content-type': 'application/json' // 默认值
  949. // },
  950. // success(res) {
  951. // console.log(res.data)
  952. // }
  953. // })
  954. // }
  955. // })
  956. // },
  957. /**
  958. * 加入购物车
  959. */
  960. addToCart: function (event) {
  961. if(this.data.totalStocks == 0) return
  962. let skuList = this.data.skuList[0]
  963. if (skuList.weight > 20000) {
  964. wx.showToast({
  965. title: "商品超过20kg,太重咯~",
  966. icon: "none"
  967. })
  968. return;
  969. }
  970. if(this.data.totalStocks<0){
  971. wx.showToast({
  972. title: "该商品暂无库存~",
  973. icon: "none"
  974. })
  975. return;
  976. }
  977. if (this.data.isPlaying) {
  978. this._stopPlay()
  979. }
  980. if (!this.data.skuShow) {
  981. this.setData({
  982. skuShow: true
  983. })
  984. return
  985. }
  986. wx.showLoading({
  987. mask: true
  988. });
  989. // 查看是否授权
  990. util.checkAuthInfo(this.callChangeItem)
  991. },
  992. callChangeItem() {
  993. wx.showLoading({
  994. mask: true
  995. });
  996. http.request({
  997. url: "/p/shopCart/changeItem",
  998. method: "POST",
  999. data: {
  1000. basketId: 0,
  1001. count: this.data.prodNum,
  1002. prodId: this.data.prodId,
  1003. shopId: this.data.shopId,
  1004. shopName: this.data.shopName,
  1005. skuId: this.data.defaultSku.skuId,
  1006. distributionCardNo: this.data.distributionCardNo,
  1007. platform:1
  1008. },
  1009. callBack: (res) => {
  1010. this.setData({
  1011. totalCartNum: this.data.totalCartNum + this.data.prodNum,
  1012. skuShow: false,
  1013. });
  1014. wx.hideLoading();
  1015. wx.showToast({
  1016. title: "加入购物车成功",
  1017. icon: "none"
  1018. })
  1019. }
  1020. });
  1021. },
  1022. /**
  1023. * 立即购买
  1024. */
  1025. buyNow: function () {
  1026. if(this.data.totalStocks == 0) return
  1027. let skuList = this.data.skuList[0]
  1028. if (skuList.weight > 20000) {
  1029. wx.showToast({
  1030. title: "商品超过20kg,太重咯~",
  1031. icon: "none"
  1032. })
  1033. return;
  1034. }
  1035. if(this.data.totalStocks<0){
  1036. wx.showToast({
  1037. title: "该商品暂无库存~",
  1038. icon: "none"
  1039. })
  1040. return;
  1041. }
  1042. if (this.data.isPlaying) {
  1043. this._stopPlay()
  1044. }
  1045. if (!this.data.skuShow) {
  1046. this.setData({
  1047. skuShow: true
  1048. })
  1049. return
  1050. }
  1051. wx.showLoading({
  1052. mask: true
  1053. });
  1054. // 查看是否授权
  1055. util.checkAuthInfo(this._buyNowData(this))
  1056. },
  1057. /**
  1058. * 立即购买参数配置
  1059. */
  1060. _buyNowData(ctx) {
  1061. wx.setStorageSync("orderItem", JSON.stringify({
  1062. prodId: ctx.data.prodId,
  1063. skuId: ctx.data.defaultSku.skuId,
  1064. prodCount: ctx.data.prodNum,
  1065. shopId: ctx.data.shopId,
  1066. distributionCardNo: ctx.data.distributionCardNo
  1067. }));
  1068. wx.navigateTo({
  1069. url: '/pages/submit-order/submit-order?orderEntry=1&shopId='+ctx.data.shopId,
  1070. })
  1071. },
  1072. /**
  1073. * 减数量
  1074. */
  1075. onCountMinus: function () {
  1076. var prodNum = this.data.prodNum;
  1077. if (prodNum > 1) {
  1078. this.setData({
  1079. prodNum: prodNum - 1
  1080. });
  1081. }
  1082. },
  1083. /**
  1084. * 加数量
  1085. */
  1086. onCountPlus: function () {
  1087. var prodNum = this.data.prodNum;
  1088. // 判断是否限购
  1089. if (this.data.hasMaxNum) {
  1090. if (prodNum < this.data.totalStocks && prodNum < this.data.maxNum) {
  1091. this.setData({
  1092. prodNum: prodNum + 1
  1093. });
  1094. } else {
  1095. wx.showToast({
  1096. title: '限购' + this.data.maxNum + '件',
  1097. icon: 'none',
  1098. })
  1099. }
  1100. } else {
  1101. if (prodNum < this.data.totalStocks) {
  1102. this.setData({
  1103. prodNum: prodNum + 1
  1104. });
  1105. } else {
  1106. wx.showToast({
  1107. title: '库存不足!',
  1108. icon: 'none'
  1109. })
  1110. }
  1111. }
  1112. },
  1113. /**
  1114. * 用户点击转发
  1115. */
  1116. onShareAppMessage: function (res) {
  1117. if (res.from === 'button') {
  1118. var cardno = wx.getStorageSync("distCardNo");
  1119. return {
  1120. title: this.data.prodName,
  1121. path: '/pages/prod/prod?prodid=' + this.data.prodId + '&cardno=' + cardno + '&type=' + this.data.shareType,
  1122. imageUrl: this.data.pic,
  1123. }
  1124. } else {
  1125. return {
  1126. title: this.data.prodName,
  1127. path: '/pages/prod/prod?prodid=' + this.data.prodId
  1128. }
  1129. }
  1130. },
  1131. /**
  1132. * 优惠券
  1133. */
  1134. showPopup: function () {
  1135. if (this.data.loadCouponIds) {
  1136. this.setData({
  1137. popupShow: true
  1138. });
  1139. return;
  1140. }
  1141. http.request({
  1142. url: "/p/myCoupon/listCouponIds",
  1143. method: "GET",
  1144. data: {},
  1145. callBack: (couponIds) => {
  1146. var couponList = this.data.couponList;
  1147. couponList.forEach(coupon => {
  1148. if (couponIds && couponIds.length) {
  1149. // 领取该优惠券数量
  1150. var couponLimit = 0;
  1151. couponIds.forEach(couponId => {
  1152. if (couponId == coupon.couponId) {
  1153. couponLimit++;
  1154. }
  1155. });
  1156. // 小于用户领取优惠券上限,可以领取优惠券
  1157. if (couponLimit < coupon.limitNum) {
  1158. coupon.canReceive = true;
  1159. } else {
  1160. coupon.canReceive = false;
  1161. }
  1162. } else {
  1163. coupon.canReceive = true;
  1164. }
  1165. });
  1166. this.setData({
  1167. couponList: couponList,
  1168. popupShow: true,
  1169. loadCouponIds: true
  1170. })
  1171. }
  1172. })
  1173. },
  1174. showSku: function (e) {
  1175. if (e.currentTarget.dataset.alonebuy == 1) {
  1176. this.setData({
  1177. skuShow: true,
  1178. skuShowType: 0
  1179. });
  1180. } else {
  1181. if (this.data.groupActivityId) {
  1182. this.setData({
  1183. skuShow: true,
  1184. skuShowType: 1
  1185. });
  1186. } else {
  1187. this.setData({
  1188. skuShow: true,
  1189. skuShowType: 0
  1190. });
  1191. }
  1192. }
  1193. },
  1194. showGroupSku: function () {
  1195. if (this.data.isPlaying) {
  1196. this._stopPlay()
  1197. }
  1198. this.setData({
  1199. skuShow: true,
  1200. skuShowType: 1
  1201. });
  1202. },
  1203. showComment: function () {
  1204. this.setData({
  1205. commentShow: true
  1206. });
  1207. },
  1208. closePopup: function () {
  1209. this.setData({
  1210. popupShow: false,
  1211. skuShow: false,
  1212. commentShow: false
  1213. });
  1214. },
  1215. /**
  1216. * 赚字浮层
  1217. */
  1218. onShowShare: function (e) {
  1219. if (this.data.isPlaying) {
  1220. this._stopPlay()
  1221. }
  1222. this.setData({
  1223. shareShow: true,
  1224. shareType: e.currentTarget.dataset.type,
  1225. });
  1226. },
  1227. /**
  1228. * 回到顶部
  1229. */
  1230. backToTop: function () {
  1231. wx.pageScrollTo({
  1232. scrollTop: 0
  1233. })
  1234. },
  1235. /**
  1236. * 监听页面滚动
  1237. */
  1238. onPageScroll: function (e) {
  1239. if (e.scrollTop > 80) {
  1240. this.setData({
  1241. showBacktop: true
  1242. })
  1243. } else if (e.scrollTop < 80) {
  1244. this.setData({
  1245. showBacktop: false
  1246. })
  1247. }
  1248. },
  1249. closeEarn: function () {
  1250. this.setData({
  1251. shareShow: false
  1252. })
  1253. },
  1254. toDistCenterPage: function () {
  1255. wx.navigateTo({
  1256. url: '/pages/dis-center/dis-center',
  1257. })
  1258. },
  1259. /**
  1260. * 生成小程序二维码
  1261. */
  1262. genWeixinCode() {
  1263. wx.showLoading({
  1264. mask: true
  1265. })
  1266. var cardNo = wx.getStorageSync("distCardNo");
  1267. var content = JSON.stringify({
  1268. shareProdId: this.data.prodId,
  1269. cardNo: cardNo,
  1270. })
  1271. http.request({
  1272. url: "/qrcodeTicket/miniQrCode",
  1273. method: "GET",
  1274. responseType: 'arraybuffer',
  1275. data: {
  1276. content: content,
  1277. type: 2
  1278. },
  1279. callBack: (res) => {
  1280. this.setData({
  1281. wxCodeShow: true,
  1282. shareShow: false,
  1283. shareWxCode: "data:image/jpg;base64," + wx.arrayBufferToBase64(res)
  1284. });
  1285. wx.hideLoading();
  1286. }
  1287. })
  1288. console.log('分销员id:' + cardNo)
  1289. },
  1290. /**
  1291. * 关闭二维码弹窗
  1292. */
  1293. closeCodePopup() {
  1294. this.setData({
  1295. wxCodeShow: false
  1296. });
  1297. },
  1298. /**
  1299. * 保存图片至相册
  1300. */
  1301. downloadImg() {
  1302. var ths = this
  1303. var imgSrc = this.data.shareWxCode.slice(22); //base64编码
  1304. var save = wx.getFileSystemManager();
  1305. save.writeFile({
  1306. filePath: wx.env.USER_DATA_PATH + '/分享商品-' + this.data.prodName + '.png',
  1307. data: imgSrc,
  1308. encoding: 'base64',
  1309. success: res => {
  1310. wx.saveImageToPhotosAlbum({
  1311. filePath: wx.env.USER_DATA_PATH + '/分享商品-' + this.data.prodName + '.png',
  1312. success: function (res) {
  1313. wx.showToast({
  1314. title: '保存成功',
  1315. })
  1316. ths.setData({
  1317. wxCodeShow: false
  1318. })
  1319. },
  1320. fail: function (err) {
  1321. wx.showModal({
  1322. title: '提示',
  1323. content: '保存图片需要授予存储权限',
  1324. success: modalRes => {
  1325. if (modalRes.confirm) {
  1326. wx.openSetting({
  1327. success(settingdata) {
  1328. if (settingdata.authSetting['scope.writePhotosAlbum']) {
  1329. console.log('用户权限成功')
  1330. } else {
  1331. console.log('用户取消权限')
  1332. }
  1333. }
  1334. })
  1335. }
  1336. }
  1337. })
  1338. }
  1339. })
  1340. // console.log(res)
  1341. },
  1342. fail: err => {
  1343. // console.log(err)
  1344. }
  1345. })
  1346. },
  1347. /**
  1348. * 获取用户信息
  1349. */
  1350. onGotUserInfo: function (e) {
  1351. if (e.detail.errMsg != "getUserInfo:fail auth deny") {
  1352. this.setData({
  1353. isAuthInfo: true
  1354. });
  1355. http.updateUserInfo();
  1356. }
  1357. },
  1358. /**
  1359. * 确认开团
  1360. */
  1361. groupConfirmOrder: function (e) {
  1362. if (!this.data.findSku) {
  1363. return;
  1364. }
  1365. wx.showLoading({
  1366. mask: true
  1367. });
  1368. this.setData({
  1369. teamId: e.currentTarget.dataset.teamid
  1370. })
  1371. // 查看是否授权
  1372. util.checkAuthInfo(this._groupConfirmParams)
  1373. },
  1374. // 拼团确认订单参数
  1375. _groupConfirmParams() {
  1376. wx.setStorageSync("groupOrderItem", JSON.stringify({
  1377. groupSkuId: this.data.detaultGroupSku.groupSkuId, // this.data.groupSkuId,
  1378. prodCount: this.data.prodNum, // this.data.prodNum,
  1379. groupTeamId: this.data.teamId // this.data.groupTeamId
  1380. }));
  1381. wx.navigateTo({
  1382. url: '/pages/groupConfirmOrder/groupConfirmOrder',
  1383. })
  1384. },
  1385. /**
  1386. * 跳转到店铺页
  1387. */
  1388. toShopPage: function () {
  1389. wx.navigateTo({
  1390. url: '/pages/shopPage/shopPage?shopId=' + this.data.shopId,
  1391. })
  1392. },
  1393. /**
  1394. * 通过商品id获取商品所有促销活动
  1395. */
  1396. getPordDiscountList() {
  1397. http.request({
  1398. url: "/marking/discount/getDiscountByProdId",
  1399. method: "GET",
  1400. data: {
  1401. prodId: this.data.prodId
  1402. },
  1403. callBack: res => {
  1404. this.setData({
  1405. prodDiscountList: res
  1406. })
  1407. }
  1408. })
  1409. },
  1410. /**
  1411. * 促销活动弹窗
  1412. */
  1413. clickDiscount: function () {
  1414. var showDiscountPopup = !this.data.showDiscountPopup
  1415. this.setData({
  1416. showDiscountPopup
  1417. })
  1418. },
  1419. /**
  1420. * 页面从前台变为后台时执行
  1421. */
  1422. onHide: function () {
  1423. this.setData({
  1424. isCollection: false,
  1425. skuShow: false,
  1426. })
  1427. clearTimeout(this.data.joinGroupListTimer)
  1428. },
  1429. /**
  1430. * 视频信息
  1431. */
  1432. getVideoInfo() {
  1433. wx.showLoading()
  1434. wx.request({
  1435. url: this.data.video + '?avinfo',
  1436. success: res => {
  1437. wx.hideLoading()
  1438. let time = Number(res.data.format.duration).toFixed(0)
  1439. this.setData({
  1440. videoDuration: time < 60 ? '00′' + time + '″' : ((parseInt(time / 60) < 10 ? '0' + parseInt(time / 60) : parseInt(time / 60)) + '′' + (time % 60 < 10 ? '0' + time % 60 : time % 60) + '″')
  1441. })
  1442. }
  1443. })
  1444. },
  1445. /**
  1446. * 视频播放/停止按钮
  1447. */
  1448. videoOper() {
  1449. if (this.data.isPlaying) {
  1450. this._stopPlay()
  1451. } else {
  1452. this._startPlay()
  1453. }
  1454. },
  1455. /**
  1456. * 视频播放结束时
  1457. */
  1458. playEnd() {
  1459. this._stopPlay()
  1460. },
  1461. _startPlay() {
  1462. this.setData({
  1463. isPlaying: true,
  1464. })
  1465. this.videoContext.play()
  1466. this.videoContext.seek(0)
  1467. },
  1468. _stopPlay() {
  1469. this.setData({
  1470. isPlaying: false
  1471. })
  1472. this.videoContext.stop()
  1473. this.videoContext.seek(0)
  1474. },
  1475. })