prod.js 36 KB

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