prod.js 32 KB

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