prod.js 32 KB

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