prod.js 32 KB

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