prod.js 32 KB

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