prod.js 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  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. this.setData({
  526. prodList: res.records,
  527. pages: res.pages,
  528. current: res.current
  529. });
  530. } else {
  531. prodList = this.data.prodList
  532. prodList.push(...res.records)
  533. this.setData({
  534. prodList
  535. })
  536. if (res.records.length < 1) {
  537. this.setData({
  538. isAll: true
  539. })
  540. }
  541. }
  542. }
  543. }
  544. })
  545. },
  546. // 获取经常一起买
  547. getOften(){
  548. let that = this
  549. http.request({
  550. url: "/sku/buyTogetherOften",
  551. method: "GET",
  552. data: {
  553. shopId:this.data.shopId,
  554. prodId:this.data.prodId,
  555. channelId:wx.getStorageSync('channelId'),
  556. current: 1,
  557. size: 18,
  558. },
  559. callBack: (res) => {
  560. let list = that.splitArrayToPages(res.records)
  561. that.setData({
  562. list
  563. })
  564. }
  565. })
  566. },
  567. // 获取相似商品
  568. getSimilarProdList(){
  569. let that = this
  570. http.request({
  571. url: "/prod/similarProdList",
  572. method: "GET",
  573. data: {
  574. shopId:this.data.shopId,
  575. prodId:this.data.prodId,
  576. channelId:wx.getStorageSync('channelId'),
  577. current: 1,
  578. size: 18,
  579. },
  580. callBack: (res) => {
  581. if(res.code == 200){
  582. let list = that.splitArrayToPages(res.data.records)
  583. that.setData({
  584. list
  585. })
  586. }
  587. }
  588. })
  589. },
  590. // 工具函数:拆分数组为「最多3页,每页6个」
  591. splitArrayToPages(arr) {
  592. const pageSize = 6; // 每页6个
  593. const maxPages = 3; // 最多3页
  594. const result = [];
  595. // 循环拆分:最多循环3次(确保只分3页)
  596. for (let i = 0; i < maxPages; i++) {
  597. // 计算当前页的起始索引:i * 6(第0页:0-5,第1页:6-11,第2页:12-17)
  598. const start = i * pageSize;
  599. // 截取当前页元素(不足6个时取剩余所有,超过18个时截取到17索引)
  600. const page = arr.slice(start, start + pageSize);
  601. // 加入结果数组(不足3页时,后续循环会push空数组)
  602. result.push(page);
  603. }
  604. return result;
  605. },
  606. /**
  607. * 跳转到商品详情页
  608. */
  609. toProdPage: function (e) {
  610. var prodid = e.currentTarget.dataset.prodid;
  611. var shopid = e.currentTarget.dataset.shopid;
  612. if (prodid) {
  613. wx.navigateTo({
  614. url: `/pages/prod/prod?prodid=${prodid}&shopid=${shopid}`,
  615. })
  616. }
  617. },
  618. /**
  619. * 团购商品详情信息
  620. */
  621. getGroupActivity() {
  622. http.request({
  623. url: "/groupProd/info",
  624. method: "GET",
  625. data: {
  626. prodId: this.data.prodId,
  627. groupActivityId: this.data.groupActivityId,
  628. },
  629. callBack: (res) => {
  630. if (res.success) {
  631. this.setData({
  632. groupActivity: res.obj,
  633. maxNum: res.obj.maxNum,
  634. hasMaxNum: res.obj.hasMaxNum
  635. })
  636. this.setDefaultGroupSku();
  637. this.groupActivityCountdown(res.obj)
  638. } else {
  639. this.setData({
  640. groupActivityId: null
  641. });
  642. }
  643. }
  644. })
  645. },
  646. // 团购倒计时
  647. groupActivityCountdown(groupActivity) {
  648. let endOfGroupTime;
  649. // activityStatus 1 未开始
  650. if (groupActivity.activityStatus === 1) {
  651. endOfGroupTime = util.endOfStartTime(util.dateToTimestamp(groupActivity.startTime), new Date().getTime())
  652. } else {
  653. endOfGroupTime = util.endOfStartTime(new Date().getTime(), util.dateToTimestamp(groupActivity.endTime))
  654. }
  655. this.setData({
  656. endOfGroupTime: endOfGroupTime,
  657. endOfGroupTimer: setTimeout(() => this.groupActivityCountdown(groupActivity), 1000)
  658. })
  659. },
  660. // 可加入的拼团列表
  661. getJoinGroupList() {
  662. http.request({
  663. url: "/groupProd/joinGroupList",
  664. method: "GET",
  665. data: {
  666. prodId: this.data.prodId,
  667. groupActivityId: this.data.groupActivityId,
  668. showSize: 3
  669. },
  670. callBack: (res) => {
  671. this.setData({
  672. joinGroupList: res
  673. })
  674. this.joinGroupListCountdown(res)
  675. }
  676. })
  677. },
  678. joinGroupListCountdown(joinGroupList) {
  679. joinGroupList.forEach(item => {
  680. item.endOfGroupTime = util.endOfStartTime(new Date().getTime(), util.dateToTimestamp(item.endTime))
  681. })
  682. this.setData({
  683. joinGroupList: joinGroupList,
  684. joinGroupListTimer: setTimeout(() => this.joinGroupListCountdown(joinGroupList), 1000)
  685. })
  686. },
  687. //根据sku的属性 分组
  688. groupSkuProp: function (skuList) {
  689. // var skuList = this.data.skuList;
  690. if (skuList.length == 1 && skuList[0].properties == "") {
  691. this.setData({
  692. defaultSku: skuList[0]
  693. });
  694. this.setDefaultGroupSku();
  695. return;
  696. }
  697. var skuGroup = {};
  698. var allProperties = [];
  699. var propKeys = [];
  700. this.setData({
  701. selectedPropObj: {}
  702. });
  703. for (var i = 0; i < skuList.length; i++) {
  704. var defaultSku = this.data.defaultSku;
  705. var isDefault = false;
  706. if (!defaultSku && skuList[i].price == this.data.price) { //找到和商品价格一样的那个SKU,作为默认选中的SKU
  707. defaultSku = skuList[i];
  708. isDefault = true;
  709. this.setData({
  710. defaultSku: defaultSku
  711. });
  712. }
  713. var properties = skuList[i].properties; //版本:公开版;颜色:金色;内存:64GB
  714. allProperties.push(properties);
  715. var propList = properties.split(";"); // ["版本:公开版","颜色:金色","内存:64GB"]
  716. var selectedPropObj = this.data.selectedPropObj;
  717. for (var j = 0; j < propList.length; j++) {
  718. var propval = propList[j].split(":"); //["版本","公开版"]
  719. var props = skuGroup[propval[0]]; //先取出 版本对应的值数组
  720. //如果当前是默认选中的sku,把对应的属性值 组装到selectedProp
  721. if (isDefault) {
  722. propKeys.push(propval[0]);
  723. selectedPropObj[propval[0]] = propval[1];
  724. }
  725. if (props == undefined) {
  726. props = []; //假设还没有版本,新建个新的空数组
  727. props.push(propval[1]); //把 "公开版" 放进空数组
  728. } else {
  729. if (!this.array_contain(props, propval[1])) { //如果数组里面没有"公开版"
  730. props.push(propval[1]); //把 "公开版" 放进数组
  731. }
  732. }
  733. skuGroup[propval[0]] = props; //最后把数据 放回版本对应的值
  734. }
  735. this.setData({
  736. selectedPropObj: selectedPropObj,
  737. propKeys: propKeys
  738. });
  739. }
  740. this.parseSelectedObjToVals();
  741. this.setData({
  742. skuGroup: skuGroup,
  743. allProperties: allProperties
  744. });
  745. this.setDefaultGroupSku();
  746. },
  747. //将已选的 {key:val,key2:val2}转换成 [val,val2]
  748. parseSelectedObjToVals: function () {
  749. var selectedPropObj = this.data.selectedPropObj;
  750. var selectedProperties = "";
  751. var selectedProp = [];
  752. for (var key in selectedPropObj) {
  753. selectedProp.push(selectedPropObj[key]);
  754. selectedProperties += key + ":" + selectedPropObj[key] + ";";
  755. }
  756. selectedProperties = selectedProperties.substring(0, selectedProperties.length - 1);
  757. this.setData({
  758. selectedProp: selectedProp,
  759. selectedProperties: selectedProperties
  760. });
  761. var findSku = false;
  762. for (var i = 0; i < this.data.skuList.length; i++) {
  763. if (this.data.skuList[i].properties == selectedProperties) {
  764. findSku = true;
  765. this.setData({
  766. defaultSku: this.data.skuList[i]
  767. });
  768. break;
  769. }
  770. }
  771. this.setData({
  772. findSku: findSku
  773. });
  774. this.setDefaultGroupSku();
  775. },
  776. //点击选择规格
  777. toChooseItem: function (e) {
  778. var ok = e.currentTarget.dataset.ok;
  779. if (ok == 0) {
  780. return;
  781. }
  782. var val = e.currentTarget.dataset.val;
  783. var key = e.currentTarget.dataset.key;
  784. var selectedPropObj = this.data.selectedPropObj;
  785. selectedPropObj[key] = val;
  786. this.setData({
  787. selectedPropObj: selectedPropObj
  788. });
  789. this.parseSelectedObjToVals();
  790. },
  791. //判断数组是否包含某对象
  792. array_contain: function (array, obj) {
  793. for (var i = 0; i < array.length; i++) {
  794. if (array[i] == obj) //如果要求数据类型也一致,这里可使用恒等号===
  795. return true;
  796. }
  797. return false;
  798. },
  799. /**
  800. * 设置选中的拼团sku
  801. */
  802. setDefaultGroupSku() {
  803. if (this.data.groupActivityId) {
  804. var groupSkuList = this.data.groupActivity.groupSkuList;
  805. if (groupSkuList) {
  806. for (var i = 0; i < groupSkuList.length; i++) {
  807. if (groupSkuList[i].properties == this.data.selectedProperties) {
  808. this.setData({
  809. detaultGroupSku: groupSkuList[i]
  810. });
  811. break;
  812. }
  813. }
  814. }
  815. }
  816. },
  817. /**
  818. * 去凑团
  819. */
  820. toSpellGroupDetail(e) {
  821. const groupTeamId = e.currentTarget.dataset.groupteamid
  822. util.checkAuthInfo(() => {
  823. wx.navigateTo({
  824. url: '/pages/spellGroupDetails/spellGroupDetails?groupTeamId=' + groupTeamId,
  825. })
  826. })
  827. },
  828. /**
  829. * 生命周期函数--监听页面初次渲染完成
  830. */
  831. onReady: function () {
  832. this.videoContext = wx.createVideoContext('myVideo')
  833. },
  834. /**
  835. * 生命周期函数--监听页面显示
  836. */
  837. onShow: function () {
  838. this.getCartCount()
  839. if (this.data.prodType == 1) {
  840. this.getJoinGroupList()
  841. }
  842. },
  843. getCartCount: function () {
  844. var params = {
  845. url: "/p/shopCart/prodCount",
  846. method: "GET",
  847. dontTrunLogin: true,
  848. data: {platform:1},
  849. callBack: (res) => {
  850. if (res > 0) {
  851. // wx.setTabBarBadge({
  852. // index: 2,
  853. // text: res + "",
  854. // })
  855. var app = getApp();
  856. app.globalData.totalCartCount = res;
  857. } else {
  858. // wx.removeTabBarBadge({
  859. // index: 2
  860. // })
  861. var app = getApp();
  862. app.globalData.totalCartCount = 0;
  863. }
  864. this.setData({
  865. totalCartNum: res
  866. })
  867. }
  868. };
  869. http.request(params);
  870. },
  871. /**
  872. * 阻止滚动
  873. */
  874. preventScorll() {
  875. },
  876. /**
  877. * 页面相关事件处理函数--监听用户下拉动作
  878. */
  879. onPullDownRefresh: function () {
  880. },
  881. /**
  882. * 页面上拉触底事件的处理函数
  883. */
  884. onReachBottom: function () {
  885. this.getNextPage()
  886. },
  887. // 触底加载下一页
  888. getNextPage() {
  889. console.log('getNextPage',this.data.current);
  890. if (this.data.pages > this.data.current) {
  891. this.setData({
  892. current: this.data.current + 1
  893. })
  894. this.getProdRecommended()
  895. } else {
  896. this.setData({
  897. isAll: true
  898. })
  899. }
  900. },
  901. /**
  902. * 跳转到首页
  903. */
  904. toHomePage: function () {
  905. wx.switchTab({
  906. url: '/pages/index/index',
  907. })
  908. },
  909. /**
  910. * 跳转到购物车
  911. */
  912. toCartPage: function () {
  913. wx.switchTab({
  914. url: '/pages/basket/basket',
  915. })
  916. },
  917. handleContact: function (e) {
  918. console.log(e.detail, '----路径')
  919. console.log(e.detail.query, '----参数')
  920. // this.get_register()
  921. },
  922. /**
  923. * 创建商户
  924. * @param {*} event
  925. */
  926. // get_register: function () {
  927. // let url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxc032a09413289004&secret=453c5047ae43288f1d015d48df32a5c0'
  928. // wx.request({
  929. // url:url,
  930. // method: 'GET',
  931. // data: {},
  932. // header: {
  933. // 'content-type': 'application/json' // 默认值
  934. // },
  935. // success(res) {
  936. // // console.log(res.data.access_token)
  937. // wx.request({
  938. // url: 'https://api.weixin.qq.com/cgi-bin/business/register?access_token='+res.data.access_token,
  939. // method: 'POST',
  940. // data: {
  941. // account_name: "test_shopname",
  942. // nickname: "苹果",
  943. // icon_media_id: "png",
  944. // transfer_to_commkf: true
  945. // },
  946. // header: {
  947. // 'content-type': 'application/json' // 默认值
  948. // },
  949. // success(res) {
  950. // console.log(res.data)
  951. // }
  952. // })
  953. // }
  954. // })
  955. // },
  956. /**
  957. * 加入购物车
  958. */
  959. addToCart: function (event) {
  960. if(this.data.totalStocks == 0) return
  961. let skuList = this.data.skuList[0]
  962. if (skuList.weight > 20000) {
  963. wx.showToast({
  964. title: "商品超过20kg,太重咯~",
  965. icon: "none"
  966. })
  967. return;
  968. }
  969. if(this.data.totalStocks<0){
  970. wx.showToast({
  971. title: "该商品暂无库存~",
  972. icon: "none"
  973. })
  974. return;
  975. }
  976. if (this.data.isPlaying) {
  977. this._stopPlay()
  978. }
  979. if (!this.data.skuShow) {
  980. this.setData({
  981. skuShow: true
  982. })
  983. return
  984. }
  985. wx.showLoading({
  986. mask: true
  987. });
  988. // 查看是否授权
  989. util.checkAuthInfo(this.callChangeItem)
  990. },
  991. callChangeItem() {
  992. wx.showLoading({
  993. mask: true
  994. });
  995. http.request({
  996. url: "/p/shopCart/changeItem",
  997. method: "POST",
  998. data: {
  999. basketId: 0,
  1000. count: this.data.prodNum,
  1001. prodId: this.data.prodId,
  1002. shopId: this.data.shopId,
  1003. shopName: this.data.shopName,
  1004. skuId: this.data.defaultSku.skuId,
  1005. distributionCardNo: this.data.distributionCardNo,
  1006. platform:1
  1007. },
  1008. callBack: (res) => {
  1009. this.setData({
  1010. totalCartNum: this.data.totalCartNum + this.data.prodNum,
  1011. skuShow: false,
  1012. });
  1013. wx.hideLoading();
  1014. wx.showToast({
  1015. title: "加入购物车成功",
  1016. icon: "none"
  1017. })
  1018. }
  1019. });
  1020. },
  1021. /**
  1022. * 立即购买
  1023. */
  1024. buyNow: function () {
  1025. if(this.data.totalStocks == 0) return
  1026. let skuList = this.data.skuList[0]
  1027. if (skuList.weight > 20000) {
  1028. wx.showToast({
  1029. title: "商品超过20kg,太重咯~",
  1030. icon: "none"
  1031. })
  1032. return;
  1033. }
  1034. if(this.data.totalStocks<0){
  1035. wx.showToast({
  1036. title: "该商品暂无库存~",
  1037. icon: "none"
  1038. })
  1039. return;
  1040. }
  1041. if (this.data.isPlaying) {
  1042. this._stopPlay()
  1043. }
  1044. if (!this.data.skuShow) {
  1045. this.setData({
  1046. skuShow: true
  1047. })
  1048. return
  1049. }
  1050. wx.showLoading({
  1051. mask: true
  1052. });
  1053. // 查看是否授权
  1054. util.checkAuthInfo(this._buyNowData(this))
  1055. },
  1056. /**
  1057. * 立即购买参数配置
  1058. */
  1059. _buyNowData(ctx) {
  1060. wx.setStorageSync("orderItem", JSON.stringify({
  1061. prodId: ctx.data.prodId,
  1062. skuId: ctx.data.defaultSku.skuId,
  1063. prodCount: ctx.data.prodNum,
  1064. shopId: ctx.data.shopId,
  1065. distributionCardNo: ctx.data.distributionCardNo
  1066. }));
  1067. wx.navigateTo({
  1068. url: '/pages/submit-order/submit-order?orderEntry=1&shopId='+ctx.data.shopId,
  1069. })
  1070. },
  1071. /**
  1072. * 减数量
  1073. */
  1074. onCountMinus: function () {
  1075. var prodNum = this.data.prodNum;
  1076. if (prodNum > 1) {
  1077. this.setData({
  1078. prodNum: prodNum - 1
  1079. });
  1080. }
  1081. },
  1082. /**
  1083. * 加数量
  1084. */
  1085. onCountPlus: function () {
  1086. var prodNum = this.data.prodNum;
  1087. // 判断是否限购
  1088. if (this.data.hasMaxNum) {
  1089. if (prodNum < this.data.totalStocks && prodNum < this.data.maxNum) {
  1090. this.setData({
  1091. prodNum: prodNum + 1
  1092. });
  1093. } else {
  1094. wx.showToast({
  1095. title: '限购' + this.data.maxNum + '件',
  1096. icon: 'none',
  1097. })
  1098. }
  1099. } else {
  1100. if (prodNum < this.data.totalStocks) {
  1101. this.setData({
  1102. prodNum: prodNum + 1
  1103. });
  1104. } else {
  1105. wx.showToast({
  1106. title: '库存不足!',
  1107. icon: 'none'
  1108. })
  1109. }
  1110. }
  1111. },
  1112. /**
  1113. * 用户点击转发
  1114. */
  1115. onShareAppMessage: function (res) {
  1116. if (res.from === 'button') {
  1117. var cardno = wx.getStorageSync("distCardNo");
  1118. return {
  1119. title: this.data.prodName,
  1120. path: '/pages/prod/prod?prodid=' + this.data.prodId + '&cardno=' + cardno + '&type=' + this.data.shareType,
  1121. imageUrl: this.data.pic,
  1122. }
  1123. } else {
  1124. return {
  1125. title: this.data.prodName,
  1126. path: '/pages/prod/prod?prodid=' + this.data.prodId
  1127. }
  1128. }
  1129. },
  1130. /**
  1131. * 优惠券
  1132. */
  1133. showPopup: function () {
  1134. if (this.data.loadCouponIds) {
  1135. this.setData({
  1136. popupShow: true
  1137. });
  1138. return;
  1139. }
  1140. http.request({
  1141. url: "/p/myCoupon/listCouponIds",
  1142. method: "GET",
  1143. data: {},
  1144. callBack: (couponIds) => {
  1145. var couponList = this.data.couponList;
  1146. couponList.forEach(coupon => {
  1147. if (couponIds && couponIds.length) {
  1148. // 领取该优惠券数量
  1149. var couponLimit = 0;
  1150. couponIds.forEach(couponId => {
  1151. if (couponId == coupon.couponId) {
  1152. couponLimit++;
  1153. }
  1154. });
  1155. // 小于用户领取优惠券上限,可以领取优惠券
  1156. if (couponLimit < coupon.limitNum) {
  1157. coupon.canReceive = true;
  1158. } else {
  1159. coupon.canReceive = false;
  1160. }
  1161. } else {
  1162. coupon.canReceive = true;
  1163. }
  1164. });
  1165. this.setData({
  1166. couponList: couponList,
  1167. popupShow: true,
  1168. loadCouponIds: true
  1169. })
  1170. }
  1171. })
  1172. },
  1173. showSku: function (e) {
  1174. if (e.currentTarget.dataset.alonebuy == 1) {
  1175. this.setData({
  1176. skuShow: true,
  1177. skuShowType: 0
  1178. });
  1179. } else {
  1180. if (this.data.groupActivityId) {
  1181. this.setData({
  1182. skuShow: true,
  1183. skuShowType: 1
  1184. });
  1185. } else {
  1186. this.setData({
  1187. skuShow: true,
  1188. skuShowType: 0
  1189. });
  1190. }
  1191. }
  1192. },
  1193. showGroupSku: function () {
  1194. if (this.data.isPlaying) {
  1195. this._stopPlay()
  1196. }
  1197. this.setData({
  1198. skuShow: true,
  1199. skuShowType: 1
  1200. });
  1201. },
  1202. showComment: function () {
  1203. this.setData({
  1204. commentShow: true
  1205. });
  1206. },
  1207. closePopup: function () {
  1208. this.setData({
  1209. popupShow: false,
  1210. skuShow: false,
  1211. commentShow: false
  1212. });
  1213. },
  1214. /**
  1215. * 赚字浮层
  1216. */
  1217. onShowShare: function (e) {
  1218. if (this.data.isPlaying) {
  1219. this._stopPlay()
  1220. }
  1221. this.setData({
  1222. shareShow: true,
  1223. shareType: e.currentTarget.dataset.type,
  1224. });
  1225. },
  1226. /**
  1227. * 回到顶部
  1228. */
  1229. backToTop: function () {
  1230. wx.pageScrollTo({
  1231. scrollTop: 0
  1232. })
  1233. },
  1234. /**
  1235. * 监听页面滚动
  1236. */
  1237. onPageScroll: function (e) {
  1238. if (e.scrollTop > 80) {
  1239. this.setData({
  1240. showBacktop: true
  1241. })
  1242. } else if (e.scrollTop < 80) {
  1243. this.setData({
  1244. showBacktop: false
  1245. })
  1246. }
  1247. },
  1248. closeEarn: function () {
  1249. this.setData({
  1250. shareShow: false
  1251. })
  1252. },
  1253. toDistCenterPage: function () {
  1254. wx.navigateTo({
  1255. url: '/pages/dis-center/dis-center',
  1256. })
  1257. },
  1258. /**
  1259. * 生成小程序二维码
  1260. */
  1261. genWeixinCode() {
  1262. wx.showLoading({
  1263. mask: true
  1264. })
  1265. var cardNo = wx.getStorageSync("distCardNo");
  1266. var content = JSON.stringify({
  1267. shareProdId: this.data.prodId,
  1268. cardNo: cardNo,
  1269. })
  1270. http.request({
  1271. url: "/qrcodeTicket/miniQrCode",
  1272. method: "GET",
  1273. responseType: 'arraybuffer',
  1274. data: {
  1275. content: content,
  1276. type: 2
  1277. },
  1278. callBack: (res) => {
  1279. this.setData({
  1280. wxCodeShow: true,
  1281. shareShow: false,
  1282. shareWxCode: "data:image/jpg;base64," + wx.arrayBufferToBase64(res)
  1283. });
  1284. wx.hideLoading();
  1285. }
  1286. })
  1287. console.log('分销员id:' + cardNo)
  1288. },
  1289. /**
  1290. * 关闭二维码弹窗
  1291. */
  1292. closeCodePopup() {
  1293. this.setData({
  1294. wxCodeShow: false
  1295. });
  1296. },
  1297. /**
  1298. * 保存图片至相册
  1299. */
  1300. downloadImg() {
  1301. var ths = this
  1302. var imgSrc = this.data.shareWxCode.slice(22); //base64编码
  1303. var save = wx.getFileSystemManager();
  1304. save.writeFile({
  1305. filePath: wx.env.USER_DATA_PATH + '/分享商品-' + this.data.prodName + '.png',
  1306. data: imgSrc,
  1307. encoding: 'base64',
  1308. success: res => {
  1309. wx.saveImageToPhotosAlbum({
  1310. filePath: wx.env.USER_DATA_PATH + '/分享商品-' + this.data.prodName + '.png',
  1311. success: function (res) {
  1312. wx.showToast({
  1313. title: '保存成功',
  1314. })
  1315. ths.setData({
  1316. wxCodeShow: false
  1317. })
  1318. },
  1319. fail: function (err) {
  1320. wx.showModal({
  1321. title: '提示',
  1322. content: '保存图片需要授予存储权限',
  1323. success: modalRes => {
  1324. if (modalRes.confirm) {
  1325. wx.openSetting({
  1326. success(settingdata) {
  1327. if (settingdata.authSetting['scope.writePhotosAlbum']) {
  1328. console.log('用户权限成功')
  1329. } else {
  1330. console.log('用户取消权限')
  1331. }
  1332. }
  1333. })
  1334. }
  1335. }
  1336. })
  1337. }
  1338. })
  1339. // console.log(res)
  1340. },
  1341. fail: err => {
  1342. // console.log(err)
  1343. }
  1344. })
  1345. },
  1346. /**
  1347. * 获取用户信息
  1348. */
  1349. onGotUserInfo: function (e) {
  1350. if (e.detail.errMsg != "getUserInfo:fail auth deny") {
  1351. this.setData({
  1352. isAuthInfo: true
  1353. });
  1354. http.updateUserInfo();
  1355. }
  1356. },
  1357. /**
  1358. * 确认开团
  1359. */
  1360. groupConfirmOrder: function (e) {
  1361. if (!this.data.findSku) {
  1362. return;
  1363. }
  1364. wx.showLoading({
  1365. mask: true
  1366. });
  1367. this.setData({
  1368. teamId: e.currentTarget.dataset.teamid
  1369. })
  1370. // 查看是否授权
  1371. util.checkAuthInfo(this._groupConfirmParams)
  1372. },
  1373. // 拼团确认订单参数
  1374. _groupConfirmParams() {
  1375. wx.setStorageSync("groupOrderItem", JSON.stringify({
  1376. groupSkuId: this.data.detaultGroupSku.groupSkuId, // this.data.groupSkuId,
  1377. prodCount: this.data.prodNum, // this.data.prodNum,
  1378. groupTeamId: this.data.teamId // this.data.groupTeamId
  1379. }));
  1380. wx.navigateTo({
  1381. url: '/pages/groupConfirmOrder/groupConfirmOrder',
  1382. })
  1383. },
  1384. /**
  1385. * 跳转到店铺页
  1386. */
  1387. toShopPage: function () {
  1388. wx.navigateTo({
  1389. url: '/pages/shopPage/shopPage?shopId=' + this.data.shopId,
  1390. })
  1391. },
  1392. /**
  1393. * 通过商品id获取商品所有促销活动
  1394. */
  1395. getPordDiscountList() {
  1396. http.request({
  1397. url: "/marking/discount/getDiscountByProdId",
  1398. method: "GET",
  1399. data: {
  1400. prodId: this.data.prodId
  1401. },
  1402. callBack: res => {
  1403. this.setData({
  1404. prodDiscountList: res
  1405. })
  1406. }
  1407. })
  1408. },
  1409. /**
  1410. * 促销活动弹窗
  1411. */
  1412. clickDiscount: function () {
  1413. var showDiscountPopup = !this.data.showDiscountPopup
  1414. this.setData({
  1415. showDiscountPopup
  1416. })
  1417. },
  1418. /**
  1419. * 页面从前台变为后台时执行
  1420. */
  1421. onHide: function () {
  1422. this.setData({
  1423. isCollection: false,
  1424. skuShow: false,
  1425. })
  1426. clearTimeout(this.data.joinGroupListTimer)
  1427. },
  1428. /**
  1429. * 视频信息
  1430. */
  1431. getVideoInfo() {
  1432. wx.showLoading()
  1433. wx.request({
  1434. url: this.data.video + '?avinfo',
  1435. success: res => {
  1436. wx.hideLoading()
  1437. let time = Number(res.data.format.duration).toFixed(0)
  1438. this.setData({
  1439. videoDuration: time < 60 ? '00′' + time + '″' : ((parseInt(time / 60) < 10 ? '0' + parseInt(time / 60) : parseInt(time / 60)) + '′' + (time % 60 < 10 ? '0' + time % 60 : time % 60) + '″')
  1440. })
  1441. }
  1442. })
  1443. },
  1444. /**
  1445. * 视频播放/停止按钮
  1446. */
  1447. videoOper() {
  1448. if (this.data.isPlaying) {
  1449. this._stopPlay()
  1450. } else {
  1451. this._startPlay()
  1452. }
  1453. },
  1454. /**
  1455. * 视频播放结束时
  1456. */
  1457. playEnd() {
  1458. this._stopPlay()
  1459. },
  1460. _startPlay() {
  1461. this.setData({
  1462. isPlaying: true,
  1463. })
  1464. this.videoContext.play()
  1465. this.videoContext.seek(0)
  1466. },
  1467. _stopPlay() {
  1468. this.setData({
  1469. isPlaying: false
  1470. })
  1471. this.videoContext.stop()
  1472. this.videoContext.seek(0)
  1473. },
  1474. })