prod.js 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  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. res.skuList&&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: {platform:1},
  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. if(this.data.totalStocks == 0) return
  808. let skuList = this.data.skuList[0]
  809. console.log(333333,skuList);
  810. if (skuList.weight > 20000) {
  811. wx.showToast({
  812. title: "商品超过20kg,太重咯~",
  813. icon: "none"
  814. })
  815. return;
  816. }
  817. if(this.data.totalStocks<0){
  818. wx.showToast({
  819. title: "该商品暂无库存~",
  820. icon: "none"
  821. })
  822. return;
  823. }
  824. if (this.data.isPlaying) {
  825. this._stopPlay()
  826. }
  827. if (!this.data.skuShow) {
  828. this.setData({
  829. skuShow: true
  830. })
  831. return
  832. }
  833. wx.showLoading({
  834. mask: true
  835. });
  836. // 查看是否授权
  837. util.checkAuthInfo(this.callChangeItem)
  838. },
  839. callChangeItem() {
  840. wx.showLoading({
  841. mask: true
  842. });
  843. http.request({
  844. url: "/p/shopCart/changeItem",
  845. method: "POST",
  846. data: {
  847. basketId: 0,
  848. count: this.data.prodNum,
  849. prodId: this.data.prodId,
  850. shopId: this.data.shopId,
  851. shopName: this.data.shopName,
  852. skuId: this.data.defaultSku.skuId,
  853. distributionCardNo: this.data.distributionCardNo,
  854. platform:1
  855. },
  856. callBack: (res) => {
  857. this.setData({
  858. totalCartNum: this.data.totalCartNum + this.data.prodNum,
  859. skuShow: false,
  860. });
  861. wx.hideLoading();
  862. wx.showToast({
  863. title: "加入购物车成功",
  864. icon: "none"
  865. })
  866. }
  867. });
  868. },
  869. /**
  870. * 立即购买
  871. */
  872. buyNow: function () {
  873. if(this.data.totalStocks == 0) return
  874. let skuList = this.data.skuList[0]
  875. if (skuList.weight > 20000) {
  876. wx.showToast({
  877. title: "商品超过20kg,太重咯~",
  878. icon: "none"
  879. })
  880. return;
  881. }
  882. if(this.data.totalStocks<0){
  883. wx.showToast({
  884. title: "该商品暂无库存~",
  885. icon: "none"
  886. })
  887. return;
  888. }
  889. if (this.data.isPlaying) {
  890. this._stopPlay()
  891. }
  892. if (!this.data.skuShow) {
  893. this.setData({
  894. skuShow: true
  895. })
  896. return
  897. }
  898. wx.showLoading({
  899. mask: true
  900. });
  901. // 查看是否授权
  902. util.checkAuthInfo(this._buyNowData(this))
  903. },
  904. /**
  905. * 立即购买参数配置
  906. */
  907. _buyNowData(ctx) {
  908. wx.setStorageSync("orderItem", JSON.stringify({
  909. prodId: ctx.data.prodId,
  910. skuId: ctx.data.defaultSku.skuId,
  911. prodCount: ctx.data.prodNum,
  912. shopId: ctx.data.shopId,
  913. distributionCardNo: ctx.data.distributionCardNo
  914. }));
  915. wx.navigateTo({
  916. url: '/pages/submit-order/submit-order?orderEntry=1',
  917. })
  918. },
  919. /**
  920. * 减数量
  921. */
  922. onCountMinus: function () {
  923. var prodNum = this.data.prodNum;
  924. if (prodNum > 1) {
  925. this.setData({
  926. prodNum: prodNum - 1
  927. });
  928. }
  929. },
  930. /**
  931. * 加数量
  932. */
  933. onCountPlus: function () {
  934. var prodNum = this.data.prodNum;
  935. // 判断是否限购
  936. if (this.data.hasMaxNum) {
  937. if (prodNum < this.data.totalStocks && prodNum < this.data.maxNum) {
  938. this.setData({
  939. prodNum: prodNum + 1
  940. });
  941. } else {
  942. wx.showToast({
  943. title: '限购' + this.data.maxNum + '件',
  944. icon: 'none',
  945. })
  946. }
  947. } else {
  948. if (prodNum < this.data.totalStocks) {
  949. this.setData({
  950. prodNum: prodNum + 1
  951. });
  952. } else {
  953. wx.showToast({
  954. title: '库存不足!',
  955. icon: 'none'
  956. })
  957. }
  958. }
  959. },
  960. /**
  961. * 用户点击转发
  962. */
  963. onShareAppMessage: function (res) {
  964. if (res.from === 'button') {
  965. var cardno = wx.getStorageSync("distCardNo");
  966. return {
  967. title: this.data.prodName,
  968. path: '/pages/prod/prod?prodid=' + this.data.prodId + '&cardno=' + cardno + '&type=' + this.data.shareType,
  969. imageUrl: this.data.pic,
  970. }
  971. } else {
  972. return {
  973. title: this.data.prodName,
  974. path: '/pages/prod/prod?prodid=' + this.data.prodId
  975. }
  976. }
  977. },
  978. /**
  979. * 优惠券
  980. */
  981. showPopup: function () {
  982. if (this.data.loadCouponIds) {
  983. this.setData({
  984. popupShow: true
  985. });
  986. return;
  987. }
  988. http.request({
  989. url: "/p/myCoupon/listCouponIds",
  990. method: "GET",
  991. data: {},
  992. callBack: (couponIds) => {
  993. var couponList = this.data.couponList;
  994. couponList.forEach(coupon => {
  995. if (couponIds && couponIds.length) {
  996. // 领取该优惠券数量
  997. var couponLimit = 0;
  998. couponIds.forEach(couponId => {
  999. if (couponId == coupon.couponId) {
  1000. couponLimit++;
  1001. }
  1002. });
  1003. // 小于用户领取优惠券上限,可以领取优惠券
  1004. if (couponLimit < coupon.limitNum) {
  1005. coupon.canReceive = true;
  1006. } else {
  1007. coupon.canReceive = false;
  1008. }
  1009. } else {
  1010. coupon.canReceive = true;
  1011. }
  1012. });
  1013. this.setData({
  1014. couponList: couponList,
  1015. popupShow: true,
  1016. loadCouponIds: true
  1017. })
  1018. }
  1019. })
  1020. },
  1021. showSku: function (e) {
  1022. if (e.currentTarget.dataset.alonebuy == 1) {
  1023. this.setData({
  1024. skuShow: true,
  1025. skuShowType: 0
  1026. });
  1027. } else {
  1028. if (this.data.groupActivityId) {
  1029. this.setData({
  1030. skuShow: true,
  1031. skuShowType: 1
  1032. });
  1033. } else {
  1034. this.setData({
  1035. skuShow: true,
  1036. skuShowType: 0
  1037. });
  1038. }
  1039. }
  1040. },
  1041. showGroupSku: function () {
  1042. if (this.data.isPlaying) {
  1043. this._stopPlay()
  1044. }
  1045. this.setData({
  1046. skuShow: true,
  1047. skuShowType: 1
  1048. });
  1049. },
  1050. showComment: function () {
  1051. this.setData({
  1052. commentShow: true
  1053. });
  1054. },
  1055. closePopup: function () {
  1056. this.setData({
  1057. popupShow: false,
  1058. skuShow: false,
  1059. commentShow: false
  1060. });
  1061. },
  1062. /**
  1063. * 赚字浮层
  1064. */
  1065. onShowShare: function (e) {
  1066. if (this.data.isPlaying) {
  1067. this._stopPlay()
  1068. }
  1069. this.setData({
  1070. shareShow: true,
  1071. shareType: e.currentTarget.dataset.type,
  1072. });
  1073. },
  1074. /**
  1075. * 回到顶部
  1076. */
  1077. backToTop: function () {
  1078. wx.pageScrollTo({
  1079. scrollTop: 0
  1080. })
  1081. },
  1082. /**
  1083. * 监听页面滚动
  1084. */
  1085. onPageScroll: function (e) {
  1086. if (e.scrollTop > 80) {
  1087. this.setData({
  1088. showBacktop: true
  1089. })
  1090. } else if (e.scrollTop < 80) {
  1091. this.setData({
  1092. showBacktop: false
  1093. })
  1094. }
  1095. },
  1096. closeEarn: function () {
  1097. this.setData({
  1098. shareShow: false
  1099. })
  1100. },
  1101. toDistCenterPage: function () {
  1102. wx.navigateTo({
  1103. url: '/pages/dis-center/dis-center',
  1104. })
  1105. },
  1106. /**
  1107. * 生成小程序二维码
  1108. */
  1109. genWeixinCode() {
  1110. wx.showLoading({
  1111. mask: true
  1112. })
  1113. var cardNo = wx.getStorageSync("distCardNo");
  1114. var content = JSON.stringify({
  1115. shareProdId: this.data.prodId,
  1116. cardNo: cardNo,
  1117. })
  1118. http.request({
  1119. url: "/qrcodeTicket/miniQrCode",
  1120. method: "GET",
  1121. responseType: 'arraybuffer',
  1122. data: {
  1123. content: content,
  1124. type: 2
  1125. },
  1126. callBack: (res) => {
  1127. this.setData({
  1128. wxCodeShow: true,
  1129. shareShow: false,
  1130. shareWxCode: "data:image/jpg;base64," + wx.arrayBufferToBase64(res)
  1131. });
  1132. wx.hideLoading();
  1133. }
  1134. })
  1135. console.log('分销员id:' + cardNo)
  1136. },
  1137. /**
  1138. * 关闭二维码弹窗
  1139. */
  1140. closeCodePopup() {
  1141. this.setData({
  1142. wxCodeShow: false
  1143. });
  1144. },
  1145. /**
  1146. * 保存图片至相册
  1147. */
  1148. downloadImg() {
  1149. var ths = this
  1150. var imgSrc = this.data.shareWxCode.slice(22); //base64编码
  1151. var save = wx.getFileSystemManager();
  1152. save.writeFile({
  1153. filePath: wx.env.USER_DATA_PATH + '/分享商品-' + this.data.prodName + '.png',
  1154. data: imgSrc,
  1155. encoding: 'base64',
  1156. success: res => {
  1157. wx.saveImageToPhotosAlbum({
  1158. filePath: wx.env.USER_DATA_PATH + '/分享商品-' + this.data.prodName + '.png',
  1159. success: function (res) {
  1160. wx.showToast({
  1161. title: '保存成功',
  1162. })
  1163. ths.setData({
  1164. wxCodeShow: false
  1165. })
  1166. },
  1167. fail: function (err) {
  1168. wx.showModal({
  1169. title: '提示',
  1170. content: '保存图片需要授予存储权限',
  1171. success: modalRes => {
  1172. if (modalRes.confirm) {
  1173. wx.openSetting({
  1174. success(settingdata) {
  1175. if (settingdata.authSetting['scope.writePhotosAlbum']) {
  1176. console.log('用户权限成功')
  1177. } else {
  1178. console.log('用户取消权限')
  1179. }
  1180. }
  1181. })
  1182. }
  1183. }
  1184. })
  1185. }
  1186. })
  1187. // console.log(res)
  1188. },
  1189. fail: err => {
  1190. // console.log(err)
  1191. }
  1192. })
  1193. },
  1194. /**
  1195. * 获取用户信息
  1196. */
  1197. onGotUserInfo: function (e) {
  1198. if (e.detail.errMsg != "getUserInfo:fail auth deny") {
  1199. this.setData({
  1200. isAuthInfo: true
  1201. });
  1202. http.updateUserInfo();
  1203. }
  1204. },
  1205. /**
  1206. * 确认开团
  1207. */
  1208. groupConfirmOrder: function (e) {
  1209. if (!this.data.findSku) {
  1210. return;
  1211. }
  1212. wx.showLoading({
  1213. mask: true
  1214. });
  1215. this.setData({
  1216. teamId: e.currentTarget.dataset.teamid
  1217. })
  1218. // 查看是否授权
  1219. util.checkAuthInfo(this._groupConfirmParams)
  1220. },
  1221. // 拼团确认订单参数
  1222. _groupConfirmParams() {
  1223. wx.setStorageSync("groupOrderItem", JSON.stringify({
  1224. groupSkuId: this.data.detaultGroupSku.groupSkuId, // this.data.groupSkuId,
  1225. prodCount: this.data.prodNum, // this.data.prodNum,
  1226. groupTeamId: this.data.teamId // this.data.groupTeamId
  1227. }));
  1228. wx.navigateTo({
  1229. url: '/pages/groupConfirmOrder/groupConfirmOrder',
  1230. })
  1231. },
  1232. /**
  1233. * 跳转到店铺页
  1234. */
  1235. toShopPage: function () {
  1236. wx.navigateTo({
  1237. url: '/pages/shopPage/shopPage?shopId=' + this.data.shopId,
  1238. })
  1239. },
  1240. /**
  1241. * 通过商品id获取商品所有促销活动
  1242. */
  1243. getPordDiscountList() {
  1244. http.request({
  1245. url: "/marking/discount/getDiscountByProdId",
  1246. method: "GET",
  1247. data: {
  1248. prodId: this.data.prodId
  1249. },
  1250. callBack: res => {
  1251. this.setData({
  1252. prodDiscountList: res
  1253. })
  1254. }
  1255. })
  1256. },
  1257. /**
  1258. * 促销活动弹窗
  1259. */
  1260. clickDiscount: function () {
  1261. var showDiscountPopup = !this.data.showDiscountPopup
  1262. this.setData({
  1263. showDiscountPopup
  1264. })
  1265. },
  1266. /**
  1267. * 页面从前台变为后台时执行
  1268. */
  1269. onHide: function () {
  1270. this.setData({
  1271. isCollection: false,
  1272. skuShow: false,
  1273. })
  1274. clearTimeout(this.data.joinGroupListTimer)
  1275. },
  1276. /**
  1277. * 视频信息
  1278. */
  1279. getVideoInfo() {
  1280. wx.showLoading()
  1281. wx.request({
  1282. url: this.data.video + '?avinfo',
  1283. success: res => {
  1284. wx.hideLoading()
  1285. let time = Number(res.data.format.duration).toFixed(0)
  1286. this.setData({
  1287. videoDuration: time < 60 ? '00′' + time + '″' : ((parseInt(time / 60) < 10 ? '0' + parseInt(time / 60) : parseInt(time / 60)) + '′' + (time % 60 < 10 ? '0' + time % 60 : time % 60) + '″')
  1288. })
  1289. }
  1290. })
  1291. },
  1292. /**
  1293. * 视频播放/停止按钮
  1294. */
  1295. videoOper() {
  1296. if (this.data.isPlaying) {
  1297. this._stopPlay()
  1298. } else {
  1299. this._startPlay()
  1300. }
  1301. },
  1302. /**
  1303. * 视频播放结束时
  1304. */
  1305. playEnd() {
  1306. this._stopPlay()
  1307. },
  1308. _startPlay() {
  1309. this.setData({
  1310. isPlaying: true,
  1311. })
  1312. this.videoContext.play()
  1313. this.videoContext.seek(0)
  1314. },
  1315. _stopPlay() {
  1316. this.setData({
  1317. isPlaying: false
  1318. })
  1319. this.videoContext.stop()
  1320. this.videoContext.seek(0)
  1321. },
  1322. })