prod.js 36 KB

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