home.js 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. import {
  2. get,
  3. post
  4. } from '../../utils/http'
  5. import {
  6. formatActivity
  7. } from '../../utils/time'
  8. import {
  9. throttle,
  10. debounce,
  11. trackUserEvent
  12. } from '../../utils/util'
  13. var WxParse = require('../../components/local/wxParse/wxParse.js')
  14. const app = getApp()
  15. Page({
  16. /**
  17. * 页面的初始数据
  18. */
  19. jsData: {
  20. columnsHeight: [0, 0],
  21. isListLoading: false
  22. },
  23. data: {
  24. list: [],
  25. page: 1,
  26. total: 0,
  27. coupon: null,
  28. homeData: null,
  29. visible: false,
  30. posterPopup: false,
  31. isLoading: true,
  32. btmToast: true,
  33. activity: {},
  34. needReqActivity: true,
  35. topBannerCurrentSwiper: 0, // 顶部轮播图当前滑动位置
  36. cardBannerCurrentSwiper: 0, // 开卡轮播图当前滑动位置
  37. navBarData: app.globalData.navBarData,
  38. navBarBgClass: '',
  39. currentNewAcount: 0,
  40. guildMessage: false, // 顶部导航栏背景
  41. allow_invite: 0, //是否展示邀请入口,1是,0否
  42. allow_receive: 0, //是否展示再次领取奖励入口,1是,0否
  43. newAcountImage: ['https://we-spa.oss-cn-shenzhen.aliyuncs.com/wxapp/20220609/newAcount.png', 'https://we-spa.oss-cn-shenzhen.aliyuncs.com/wxapp/20220609/vFace.png', 'https://we-spa.oss-cn-shenzhen.aliyuncs.com/wxapp/20220609/showLike.png'],
  44. inviteImg: 'https://we-spa.oss-cn-shenzhen.aliyuncs.com/wxapp/20220811/%E4%BA%8C%E5%9B%BE/%E5%9B%BE%E7%89%87/%E9%82%80%E8%AF%B7%E8%80%85banner%402x.png', //邀请
  45. beInvitedImg: 'https://we-spa.oss-cn-shenzhen.aliyuncs.com/wxapp/20220811/%E4%BA%8C%E5%9B%BE/%E5%9B%BE%E7%89%87/%E7%BC%96%E7%BB%84%2010%402x.png', //被邀请
  46. matching: false,
  47. PopShow: true, //十月一弹窗显示
  48. swiperIndex: 0, //推荐项目轮播图下标
  49. classify: [], //美丽秘籍分类
  50. current: 0, //美丽秘籍下标
  51. value: '',
  52. columns: [
  53. [],
  54. []
  55. ],
  56. tempPics: [], //美丽秘籍内容列表
  57. tabClass: '', //美丽秘籍滑到顶部类名
  58. messageList: [], //消息列表
  59. notMessageTotal: 0, //未读消息数量
  60. skinInfo: "", //测服详情概要
  61. skin_type: ['干性肌肤', '中性肌肤', '油性肌肤', '混合型肌肤', '敏感肌肤', '痤疮肌肤'], //肤质:1-干性肌肤 2-中性肌肤 3-油性肌肤 4-混合型肌肤,5敏感肌肤,6 痤疮肌肤
  62. isTips: true, //距离提示
  63. },
  64. /**
  65. * 生命周期函数--监听页面加载
  66. */
  67. onLoad: function (options) {
  68. // 获取美丽秘籍分类
  69. // this.getClassify()
  70. console.log(options, 'options');
  71. if (options.fromAuthOrder && JSON.parse(options.fromAuthOrder)) {
  72. // 弹出订单确认等待loading层,用户也可主动关闭loading层
  73. this.setData({
  74. matching: true
  75. })
  76. }
  77. if (wx.getStorageSync('userInfo')) {
  78. // 调方法实时收取服务端推送过来的订单确认消息
  79. this.createSocketToReceiveMessage()
  80. }
  81. this.getInviteStatus()
  82. this.setData({
  83. visible: app.globalData.couponPopup,
  84. coupon: app.globalData.coupon,
  85. navBarHeight: app.globalData.navBarData.navBarHeight
  86. })
  87. this.getHomeData()
  88. this.check_new_record()
  89. if (wx.getStorageSync('day') != new Date().getDate()) {
  90. this.setData({
  91. posterPopup: true
  92. })
  93. }
  94. this.activityProgress()
  95. this.activityRules()
  96. wx.setStorageSync('day', new Date().getDate())
  97. if (options.click) {
  98. trackUserEvent(options.click)
  99. }
  100. },
  101. /**
  102. * 生命周期函数--监听页面初次渲染完成
  103. */
  104. onReady: function () {},
  105. /**
  106. * 生命周期函数--监听页面显示
  107. */
  108. onShow: function (e) {
  109. if (wx.getStorageSync('token') && this.data.needReqActivity) {
  110. this.getUserActivity()
  111. }
  112. this.getVersion()
  113. // 当切换了店铺时,重新请求一次数据
  114. let storeData = getApp().globalData.storeData
  115. let storeId = storeData.id || wx.getStorageSync('store_id')
  116. if (this.data.homeData && storeId != this.data.homeData.store.id && this.data.homeData.store.id) {
  117. this.getStoreInfo(storeId)
  118. }
  119. if (wx.getStorageSync('userInfo')) {
  120. // 获取消息列表
  121. this.getMessageList()
  122. // 获取未读消息数量
  123. this.getNotMessageTotal()
  124. }
  125. // 获取测服详情概要
  126. this.getSkinInfo()
  127. },
  128. //获取当前用户邀请状态
  129. getInviteStatus() {
  130. get('/api/user/power', {}, res => {
  131. this.setData({
  132. allow_invite: res.data.allow_invite,
  133. allow_receive: res.data.allow_receive,
  134. invite_id: res.data.invite_id
  135. })
  136. })
  137. },
  138. onInvite() {
  139. if (this.data.allow_receive != 1 && this.data.allow_invite != 1) {
  140. wx.showToast({
  141. title: '您还没有体验过项目,不满足参与活动条件',
  142. icon: "none"
  143. })
  144. return
  145. }
  146. wx.navigateTo({
  147. url: this.data.allow_invite == 1 ? '/pages/invitationDetails/invitationDetails' : `/pages/invitees/invitees?invite_id=${this.data.invite_id}`,
  148. })
  149. },
  150. createSocketToReceiveMessage() {
  151. if (!wx.getStorageSync('token')) {
  152. return
  153. }
  154. let token = wx.getStorageSync('token')
  155. let timer = null
  156. console.log('开始连接...')
  157. wx.connectSocket({
  158. url: `wss://ws.ijolijoli.com?access_token=${token}`,
  159. header: {
  160. 'content-type': 'application/json'
  161. },
  162. method: "GET",
  163. success: (res) => {
  164. console.log('连接成功', res)
  165. },
  166. fail: (err) => {
  167. console.log('连接失败', err)
  168. },
  169. complete: (e) => {
  170. console.log('连接完成', e)
  171. }
  172. })
  173. wx.onSocketOpen((e) => {
  174. //当WebSocket创建成功时,触发onSocketOpen事件
  175. console.log("连接已打开", e);
  176. timer = setInterval(() => {
  177. wx.sendSocketMessage({
  178. data: JSON.stringify({
  179. type: 'ping'
  180. })
  181. });
  182. }, 30000);
  183. })
  184. wx.onSocketMessage((e) => {
  185. //当客户端收到服务端发来的消息时,触发onSocketMessage事件,参数e.data包含server传递过来的数据
  186. // console.log('收到服务端消息', e.data)
  187. let data = JSON.parse(e.data);
  188. // console.log(data)
  189. // 收到服务端订单添加消息后,若订单确认loading层未关闭,则直接跳转订单确认页;否则,对服务端推送消息不做任何逻辑处理
  190. if (this.data.matching && data.data.type === 'order_add') {
  191. // console.log('关闭loading层并跳转订单确认')
  192. this.orderConfirmOnclose()
  193. wx.redirectTo({
  194. url: '/pages/confirmProjectOrder/confirmProjectOrder?id=' + data.data.order_id,
  195. })
  196. }
  197. // 收到消息通知
  198. if (data.data.type === 'instation') {
  199. // 重新获取消息列表
  200. this.getMessageList()
  201. // 重新获取未读数量
  202. this.getNotMessageTotal()
  203. }
  204. })
  205. wx.onSocketClose((e) => {
  206. //当客户端收到服务端发送的关闭连接请求时,触发onSocketClose事件
  207. console.log("连接已关闭", e);
  208. })
  209. wx.onSocketError((e) => {
  210. //如果出现连接、处理、接收、发送数据失败的时候触发onSocketError事件
  211. console.log(e);
  212. })
  213. },
  214. orderConfirmOnclose() {
  215. this.setData({
  216. matching: false
  217. })
  218. },
  219. // 获取是否需要更新
  220. getVersion() {
  221. get('api/app/version', {}, (res) => {
  222. let version = wx.getStorageSync('versionIn')
  223. if (!version || res.data.op_version != version || version == null) {
  224. wx.setStorageSync('version1', '0')
  225. wx.setStorageSync('version2', '0')
  226. wx.setStorageSync('version3', '0')
  227. wx.setStorageSync('version4', '0')
  228. wx.setStorageSync('versionIn', res.data.op_version)
  229. }
  230. })
  231. },
  232. /**
  233. * 生命周期函数--监听页面隐藏
  234. */
  235. onHide: function () {},
  236. /**
  237. * 生命周期函数--监听页面卸载
  238. */
  239. onUnload: function () {
  240. if (this.timer_) {
  241. clearInterval(this.timer_)
  242. }
  243. wx.onSocketClose((e) => {
  244. console.log('WebSocket 已关闭!')
  245. })
  246. },
  247. /**
  248. * 页面相关事件处理函数--监听用户下拉动作
  249. */
  250. onPullDownRefresh: function () {},
  251. /**
  252. * 页面上拉触底事件的处理函数
  253. */
  254. onReachBottom: function () {
  255. if (this.data.page * 10 < this.data.total) {
  256. this.getArticleList(++this.data.page)
  257. }
  258. },
  259. /**
  260. * 用户点击右上角分享
  261. */
  262. onShareAppMessage: function () {},
  263. /**
  264. * 监听页面滚动事件
  265. */
  266. onPageScroll: function (e) {
  267. let top = 6 // 超过多少像素开始显示导航栏背景
  268. let tabScrollTop = 782 //超过多少像素固定美丽秘籍tab到顶部
  269. let scrollTop = e.scrollTop
  270. let {
  271. navBarBgClass,
  272. tabClass
  273. } = this.data
  274. if (scrollTop > tabScrollTop && !tabClass) {
  275. tabClass = 'articleFixed'
  276. } else if (scrollTop < tabScrollTop && tabClass) {
  277. tabClass = ''
  278. }
  279. this.setData({
  280. tabClass
  281. })
  282. if (scrollTop >= top && !navBarBgClass) {
  283. navBarBgClass = ' navbar-bg-white '
  284. app.globalData.navBarData.navBarBgClass = navBarBgClass
  285. this.setData({
  286. navBarBgClass,
  287. isTips: false
  288. })
  289. }
  290. if (scrollTop < top && navBarBgClass) {
  291. navBarBgClass = ''
  292. this.setData({
  293. navBarBgClass
  294. })
  295. app.globalData.navBarData.navBarBgClass = navBarBgClass
  296. }
  297. },
  298. // 去测肤
  299. goSkin() {
  300. wx.navigateTo({
  301. url: '/pages/plan/plan',
  302. })
  303. },
  304. goOrder(e) {
  305. let index = e.currentTarget.dataset.index
  306. let urlIndex = e.currentTarget.dataset.urlindex
  307. if (urlIndex == 7) {
  308. wx.navigateTo({
  309. url: `/pages/moreClassify/moreClassify?data=${JSON.stringify(this.data.homeData.shortcut)}`,
  310. })
  311. return
  312. }
  313. app.globalData.toViewIndex = index
  314. wx.switchTab({
  315. url: '/pages/orderBy/orderBy',
  316. success: function () {
  317. var page = getCurrentPages().pop();
  318. page.onPullDownRefresh();
  319. }
  320. })
  321. },
  322. // 轮播图改变
  323. changeSwiper(e) {
  324. this.setData({
  325. swiperIndex: e.detail.current
  326. })
  327. },
  328. /**
  329. * 切换状态
  330. */
  331. onTabsChange(e) {
  332. let current = e.currentTarget.dataset.index
  333. if (current == this.data.current) {
  334. return;
  335. }
  336. this.setData({
  337. current,
  338. page: 1,
  339. }, () => {
  340. this.getArticleList(1)
  341. })
  342. },
  343. /**
  344. * 获取美丽秘籍分类
  345. * /api/info/classify
  346. */
  347. getClassify() {
  348. get('api/info/classify', {}, (res) => {
  349. this.setData({
  350. classify: res.data.list
  351. }, () => {
  352. this.getArticleList()
  353. })
  354. })
  355. },
  356. /**
  357. * 获取美丽秘籍列表
  358. * /api/info
  359. */
  360. getArticleList(_page) {
  361. this.setData({
  362. spinning: true
  363. })
  364. let {
  365. tempPics,
  366. classify,
  367. page,
  368. value,
  369. current
  370. } = this.data;
  371. get('api/info', {
  372. page: _page || page,
  373. limit: 10,
  374. keyword: value,
  375. classify_id: classify[current].id,
  376. }, (res) => {
  377. res.data.list.map(item => {
  378. item.create_time = item.create_time.substring(0, 10)
  379. })
  380. if (_page == 1 || page == 1) {
  381. tempPics = []
  382. this.data.page = 1
  383. }
  384. tempPics.push(...res.data.list)
  385. this.setData({
  386. tempPics,
  387. total: res.data.total,
  388. spinning: false
  389. })
  390. this.jsData.isListLoading = true
  391. })
  392. },
  393. //获取图片尺寸数据
  394. loadPic: function (e) {
  395. var that = this,
  396. data = that.data,
  397. tempPics = data.tempPics,
  398. index = e.currentTarget.dataset.index
  399. if (tempPics[index]) {
  400. //以750为宽度算出相对应的高度
  401. tempPics[index].height = e.detail.height * 750 / e.detail.width
  402. tempPics[index].isLoad = true
  403. }
  404. that.setData({
  405. tempPics: tempPics
  406. }, function () {
  407. that.finLoadPic()
  408. })
  409. },
  410. //图片加载错误处理
  411. loadPicError: function (e) {
  412. var that = this,
  413. data = that.data,
  414. tempPics = data.tempPics,
  415. index = e.currentTarget.dataset.index
  416. if (tempPics[index]) {
  417. //图片加载错误时高度固定750,展示为正方形
  418. tempPics[index].height = 750
  419. tempPics[index].isLoad = true
  420. }
  421. that.setData({
  422. tempPics: tempPics
  423. }, function () {
  424. that.finLoadPic()
  425. })
  426. },
  427. //判断图片是否加载完成
  428. finLoadPic: function () {
  429. var that = this,
  430. data = that.data,
  431. tempPics = data.tempPics,
  432. length = tempPics.length,
  433. fin = true
  434. for (var i = 0; i < length; i++) {
  435. if (!tempPics[i].isLoad) {
  436. fin = false
  437. break
  438. }
  439. }
  440. if (fin) {
  441. if (that.jsData.isListLoading) {
  442. that.jsData.isListLoading = false
  443. that.renderPage()
  444. }
  445. }
  446. },
  447. //渲染到瀑布流
  448. renderPage: function () {
  449. var that = this,
  450. data = that.data,
  451. columns = data.columns,
  452. tempPics = data.tempPics,
  453. length = tempPics.length,
  454. columnsHeight = that.jsData.columnsHeight,
  455. index = 0
  456. if (this.data.page == 1) {
  457. columns = [
  458. [],
  459. []
  460. ]
  461. }
  462. for (var i = 0; i < length; i++) {
  463. index = columnsHeight[1] < columnsHeight[0] ? 1 : 0
  464. columns[index].push(tempPics[i])
  465. columnsHeight[index] += tempPics[i].height
  466. }
  467. that.setData({
  468. columns: columns,
  469. tempPics: []
  470. })
  471. that.jsData.columnsHeight = columnsHeight
  472. },
  473. /**
  474. * swiper滚动事件
  475. * @param {*} e
  476. */
  477. swiperChange: function (e) {
  478. let currentSource = e.currentTarget.dataset.currentSource
  479. if (currentSource == 'topBanner') {
  480. this.setData({
  481. topBannerCurrentSwiper: e.detail.current
  482. })
  483. }
  484. if (currentSource == 'cardBanner') {
  485. this.setData({
  486. cardBannerCurrentSwiper: e.detail.current
  487. })
  488. }
  489. },
  490. /**
  491. * 关闭弹框
  492. */
  493. onClose(e, _bool) {
  494. let key = _bool ? e : e.currentTarget.dataset.key
  495. this.setData({
  496. [key]: false
  497. })
  498. getApp().globalData.couponPopup = false
  499. },
  500. bindchanges(e) {
  501. this.setData({
  502. currentNewAcount: e.detail.current
  503. })
  504. },
  505. /**
  506. * 确认领取
  507. */
  508. onConfirm() {
  509. wx.showToast({
  510. title: '领取成功',
  511. icon: 'none'
  512. })
  513. getApp().globalData.couponPopup = false
  514. this.onClose('visible', true)
  515. // this.setData({ visible: false })
  516. },
  517. /**
  518. * 两点之间距离计算
  519. * @param {*} lat1
  520. * @param {*} lng1
  521. * @param {*} lat2
  522. * @param {*} lng2
  523. */
  524. getDistance(lat1, lng1, lat2, lng2) {
  525. lat1 = lat1 || 0;
  526. lng1 = lng1 || 0;
  527. lat2 = lat2 || 0;
  528. lng2 = lng2 || 0;
  529. var rad1 = lat1 * Math.PI / 180.0;
  530. var rad2 = lat2 * Math.PI / 180.0;
  531. var a = rad1 - rad2;
  532. var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
  533. var r = 6378137;
  534. var distance = r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2)));
  535. return distance;
  536. },
  537. /**
  538. * 获取店铺接口
  539. * /api/store
  540. */
  541. getStore() {
  542. get(
  543. 'api/store', {
  544. page: 1,
  545. limit: 100,
  546. keyword: ""
  547. },
  548. (res) => {
  549. let lat1 = app.address.latitude,
  550. lng1 = app.address.longitude,
  551. storeList = res.data.list,
  552. distanceList = []
  553. //循环遍历计算获取当前定位到各门店的距离
  554. storeList.forEach(item => {
  555. let distanceInfo = {}
  556. distanceInfo["id"] = item.id
  557. distanceInfo["dist"] = this.getDistance(lat1, lng1, item.latitude, item.longitude)
  558. distanceList.push(distanceInfo)
  559. })
  560. //近到远排序
  561. let _distanceList = distanceList.sort(function (a, b) {
  562. return a.dist - b.dist
  563. })
  564. //切换门店
  565. this.getStoreInfo(String(_distanceList[0].id))
  566. wx.setStorageSync('store_id', String(_distanceList[0].id))
  567. }
  568. )
  569. },
  570. /**
  571. * 获取当前定位
  572. * @param {*} obj
  573. */
  574. getPermission: function (obj) {
  575. let that = this
  576. wx.getLocation({
  577. success: function (res) {
  578. that.getStore()
  579. app.address = res
  580. let lat1 = app.address.latitude
  581. let lng1 = app.address.longitude
  582. that.setData({
  583. lat1,
  584. lng1,
  585. })
  586. },
  587. fail: function () {
  588. wx.getSetting({
  589. success: function (res) {
  590. var statu = res.authSetting;
  591. if (!statu['scope.userLocation']) {
  592. wx.showModal({
  593. title: '是否授权当前位置',
  594. content: '需要获取您的地理位置,请确认授权,否则无法定位最近店铺',
  595. success: function (tip) {
  596. if (tip.confirm) {
  597. wx.openSetting({
  598. success: function (data) {
  599. if (data.authSetting["scope.userLocation"] === true) {
  600. wx.showToast({
  601. title: '授权成功',
  602. icon: 'success',
  603. duration: 1000
  604. })
  605. //授权成功之后,再调用chooseLocation选择地方
  606. wx.getLocation({
  607. success: function (res) {
  608. that.getStore()
  609. app.address = res
  610. let lat1 = app.address.latitude
  611. let lng1 = app.address.longitude
  612. that.setData({
  613. lat1,
  614. lng1,
  615. })
  616. },
  617. })
  618. } else {
  619. wx.showToast({
  620. title: '授权失败',
  621. icon: 'success',
  622. duration: 1000
  623. })
  624. }
  625. }
  626. })
  627. }
  628. }
  629. })
  630. }
  631. },
  632. fail: function (res) {
  633. wx.showToast({
  634. title: '调用授权窗口失败',
  635. icon: 'success',
  636. duration: 1000
  637. })
  638. }
  639. })
  640. }
  641. })
  642. },
  643. /**
  644. * 首页数据
  645. */
  646. getHomeData() {
  647. let that = this;
  648. this.getPermission(that); //获取当前定位
  649. get('api/home', {}, (res) => {
  650. let nowDate = Math.round(new Date().getTime() / 1000).toString();
  651. // 判断推荐项目展示时间
  652. // let referrals = []
  653. // res.data.referrals.forEach(item => {
  654. // if (nowDate.toString() > item.start_time.toString() && nowDate.toString() < item.end_time.toString()) {
  655. // referrals.push(item)
  656. // }
  657. // })
  658. // res.data.referrals = referrals
  659. // 判断推荐项目为两个的话添加进行展示
  660. // if (res.data.referrals.length < 3 && res.data.referrals.length != 1) {
  661. // res.data.referrals.forEach(item => {
  662. // res.data.referrals.push(item)
  663. // })
  664. // }
  665. this.setData({
  666. homeData: res.data
  667. },
  668. () => {
  669. // this.getProjectList(1)
  670. this.setData({
  671. isLoading: false
  672. })
  673. }
  674. )
  675. })
  676. },
  677. /**
  678. * 首页数据
  679. */
  680. activityProgress() {
  681. get('api/activity/progress', {}, (res) => {
  682. this.setData({
  683. progressList: res.data.list
  684. })
  685. })
  686. },
  687. /**
  688. * 首页数据
  689. */
  690. check_new_record() {
  691. let that = this
  692. // get('api/user/plan/check_new_record', {}, (res) => {
  693. // if (res.data.status == 1) {
  694. // wx.showModal({
  695. // title: '提示',
  696. // content: '检测到新的美容计划,是否替换现有计划',
  697. // success(res1) {
  698. // if (res1.confirm) {
  699. // console.log('用户点击确定', res.data.analysis_id)
  700. // that.planReplace('yes', res.data.analysis_id)
  701. // } else if (res1.cancel) {
  702. // console.log('用户点击取消', res.data.analysis_id)
  703. // that.planReplace('no', res.data.analysis_id)
  704. // }
  705. // }
  706. // })
  707. // }
  708. // })
  709. },
  710. /**
  711. * 首页数据
  712. */
  713. planReplace(op_type, analysis_id) {
  714. post('api/user/plan/replace', {
  715. op_type: op_type,
  716. analysis_id: analysis_id
  717. }, (res) => {
  718. this.getHomeData()
  719. })
  720. },
  721. cardingCatchTap() {
  722. wx.navigateTo({
  723. url: '../guildMessage/index',
  724. })
  725. },
  726. /**
  727. * 首页数据
  728. */
  729. activityRules() {
  730. let that = this
  731. // get('api/activity/rules', {}, (res) => {
  732. // WxParse.wxParse('article', 'html', res.data.content, that, 5)
  733. // })
  734. },
  735. guildMessage() {
  736. this.setData({
  737. guildMessage: true
  738. })
  739. },
  740. onCloseLandscape() {
  741. this.setData({
  742. guildMessage: false
  743. })
  744. },
  745. /**
  746. * 首页列表
  747. */
  748. getProjectList(_page) {
  749. return
  750. let {
  751. list,
  752. page
  753. } = this.data
  754. get(
  755. 'api/project', {
  756. store_id: this.data.homeData.store.id,
  757. page: _page || page,
  758. limit: 10
  759. },
  760. (res) => {
  761. if (_page == 1 || page == 1) {
  762. list = []
  763. this.data.page = 1
  764. }
  765. list.push(...res.data.list)
  766. this.setData({
  767. list,
  768. isLoading: false,
  769. total: res.data.total
  770. })
  771. }
  772. )
  773. },
  774. /**
  775. * 店铺信息
  776. * /api/store/info
  777. */
  778. getStoreInfo(store_id) {
  779. get(
  780. 'api/store/info', {
  781. store_id
  782. },
  783. (res) => {
  784. this.setData({
  785. ['homeData.store']: res.data
  786. },
  787. () => {
  788. wx.setStorageSync('store_id', String(store_id))
  789. app.globalData.storeData = res.data
  790. this.getProjectList(1)
  791. }
  792. )
  793. }
  794. )
  795. },
  796. /**
  797. * 首页广告跳转
  798. */
  799. goToUrl(e) {
  800. console.log(e);
  801. let type = e.currentTarget.dataset.urltype
  802. if(type==0){
  803. return
  804. }
  805. if (e.currentTarget.dataset.url && type == 1) {
  806. if (e.currentTarget.dataset.url == '/pages/orderBy/orderBy') {
  807. wx.switchTab({
  808. url: '/pages/orderBy/orderBy'
  809. })
  810. } else if (e.currentTarget.dataset.url == '/pages/shoppingMall/shoppingMall') {
  811. wx.
  812. switchTab({
  813. url: '/pages/shoppingMall/shoppingMall'
  814. })
  815. } else {
  816. wx.navigateTo({
  817. url: e.currentTarget.dataset.url
  818. })
  819. trackUserEvent(e.currentTarget?.dataset?.source?.type)
  820. }
  821. }
  822. this.setData({
  823. posterPopup: false
  824. })
  825. },
  826. /**
  827. * 跳转店铺列表
  828. */
  829. goToStore() {
  830. wx.navigateTo({
  831. url: '/pages/store/store?defaultStore=' +
  832. JSON.stringify(this.data.homeData.store)
  833. })
  834. },
  835. /**
  836. * 跳转项目详情
  837. */
  838. goToProject(e) {
  839. let item = e.currentTarget.dataset.item || {}
  840. let projectId = item.project_id || item.id
  841. let storeId = item.store_id || this.data.homeData.store.id
  842. wx.navigateTo({
  843. url: `/pages/projectDetail/projectDetail?project_id=${projectId}&store_id=${storeId}`
  844. })
  845. },
  846. /**
  847. * 跳转变美计划
  848. */
  849. goToBeautyPlan(e) {
  850. let analysisId = e.currentTarget.dataset.analysisId
  851. wx.navigateTo({
  852. url: `/pages/beautyPlan/beautyPlan?analysis_id=${analysisId}`
  853. })
  854. },
  855. /**
  856. * 获取首次开卡优惠
  857. * api/user/activity
  858. */
  859. getUserActivity() {
  860. if (this.timer) {
  861. clearInterval(this.timer_)
  862. }
  863. get('api/user/activity', {}, (res) => {
  864. this.data.needReqActivity = false
  865. if (res.data) {
  866. res.data.currentTime = formatActivity(res.data.expire_time_seconds)
  867. this.setData({
  868. activity: res.data
  869. })
  870. this.timer_ = setInterval(() => {
  871. if (res.data.expire_time_seconds <= 0) {
  872. clearInterval(this.timer_)
  873. }
  874. res.data.expire_time_seconds--
  875. res.data.currentTime = formatActivity(res.data.expire_time_seconds)
  876. this.setData({
  877. activity: res.data
  878. })
  879. }, 1000)
  880. }
  881. })
  882. },
  883. /**
  884. * 开卡优惠关闭
  885. */
  886. onCloseToast() {
  887. this.setData({
  888. btmToast: false
  889. })
  890. },
  891. /**
  892. * 肤质检测结果
  893. */
  894. goToAnalysisResult(e) {
  895. if (!this.data.homeData.analysis.score > 0) {
  896. wx.navigateTo({
  897. url: '/pages/plan/plan?defaultStore=' +
  898. JSON.stringify(this.data.homeData.store)
  899. })
  900. return
  901. }
  902. let analysis = e.currentTarget.dataset.analysis || {}
  903. let analysisId = analysis.analysis_id
  904. let url = `/pages/skinRecord/skinRecord?id=${analysisId}`
  905. if (!analysisId) {
  906. this.gotoOrderBy()
  907. return
  908. }
  909. wx.navigateTo({
  910. url: url
  911. })
  912. },
  913. /**
  914. * 根据Url类型跳转
  915. */
  916. jumpByUrlType(e) {
  917. let item = e.currentTarget.dataset.item || {}
  918. let urlType = item.url_type || 0
  919. let url = item.url || ''
  920. // 跳转类型,0不跳转,1内部地址,2外部地址
  921. switch (urlType) {
  922. case 1:
  923. url && wx.navigateTo({
  924. url: url
  925. })
  926. break
  927. default:
  928. break
  929. }
  930. },
  931. //获取当前推荐原则
  932. recommendRule() {
  933. if (!this.data.homeData.analysis.score > 0) {
  934. wx.navigateTo({
  935. url: '/pages/plan/plan?defaultStore=' +
  936. JSON.stringify(this.data.homeData.store)
  937. })
  938. return
  939. }
  940. let url = `/pages/orderBy/orderBy`
  941. wx.switchTab({
  942. url: url
  943. })
  944. },
  945. /**
  946. * 预约下单tab
  947. */
  948. gotoOrderBy() {
  949. if (this.data.homeData.analysis_check == 1) {
  950. get('/v2/api/user/recommend_rule', {}, res => {
  951. if (res.data.status == 0) {
  952. wx.navigateTo({
  953. url: '/pages/recommendRule/recommendRule',
  954. })
  955. } else {
  956. wx.navigateTo({
  957. url: '/pages/faceDetectionDetails/faceDetectionDetails',
  958. })
  959. }
  960. })
  961. } else {
  962. if (!this.data.homeData.analysis.score > 0) {
  963. wx.navigateTo({
  964. url: '/pages/plan/plan?defaultStore=' +
  965. JSON.stringify(this.data.homeData.store)
  966. })
  967. return
  968. }
  969. let url = `/pages/orderBy/orderBy`
  970. wx.switchTab({
  971. url: url
  972. })
  973. }
  974. },
  975. onOderBy() {
  976. let url = `/pages/orderBy/orderBy`
  977. wx.switchTab({
  978. url: url
  979. })
  980. },
  981. // 跳转到领券详情
  982. getCoupon() {
  983. wx.navigateTo({
  984. url: '/pages/couponCenter/couponCenter',
  985. })
  986. },
  987. // 十月一活动
  988. onClosePop() {
  989. this.setData({
  990. PopShow: false,
  991. })
  992. },
  993. gotoActive() {
  994. wx.navigateTo({
  995. url: '/pages/activePage2/activePage2',
  996. })
  997. this.setData({
  998. PopShow: false
  999. })
  1000. },
  1001. // 点击跳转美丽秘籍
  1002. goArticleDetail(e) {
  1003. let id = e.currentTarget.dataset.id
  1004. wx.navigateTo({
  1005. url: `/pages/articleDetail/articleDetail?id=${id}`,
  1006. })
  1007. },
  1008. // 点击推荐项目跳转到项目
  1009. goOtherUrl(e) {
  1010. let url = e.currentTarget.dataset.url
  1011. let type = e.currentTarget.dataset.type
  1012. if (url == '/pages/orderBy/orderBy') {
  1013. wx.switchTab({
  1014. url: '/pages/orderBy/orderBy'
  1015. })
  1016. } else if (url == '/pages/shoppingMall/shoppingMall') {
  1017. wx.switchTab({
  1018. url: '/pages/shoppingMall/shoppingMall'
  1019. })
  1020. } else {
  1021. wx.navigateTo({
  1022. url: url,
  1023. })
  1024. }
  1025. // if(type==1){
  1026. // wx.navigateTo({
  1027. // url:url,
  1028. // })
  1029. // }else if(type==2){
  1030. // wx.navigateTo({
  1031. // url: `/pages/projectDetail/projectDetail?source=goods&id=${id}&store_id=${this.data.storeId}`,
  1032. // })
  1033. // }else if(type==3){
  1034. // wx.navigateTo({
  1035. // url: `/pages/projectDetail/projectDetail?source=project&id=${id}&store_id=${this.data.storeId}`,
  1036. // })
  1037. // }else{
  1038. // return
  1039. // }
  1040. },
  1041. //获取消息列表
  1042. getMessageList() {
  1043. get('v2/api/message/list', {
  1044. page: 1,
  1045. limit: 3,
  1046. }, (res) => {
  1047. if (res.code == 200) {
  1048. // 是否首页通知 1是 0不是
  1049. let list = res.data.list.filter(item => {
  1050. return item.notice != 0
  1051. })
  1052. this.setData({
  1053. messageList: list
  1054. })
  1055. this.getWorkMessageList()
  1056. }
  1057. })
  1058. },
  1059. // 获取业务消息列表
  1060. getWorkMessageList() {
  1061. let that = this
  1062. get('v2/api/message/notice', {
  1063. page: 1,
  1064. limit: 3,
  1065. }, (res) => {
  1066. if (res.code == 200) {
  1067. let workMessageList = that.data.messageList
  1068. workMessageList.push(...res.data.list)
  1069. // 对通知和业务消息的集合做排序 最新的在上面
  1070. workMessageList.sort((a, b) => {
  1071. return new Date(b.create_time) - new Date(a.create_time)
  1072. });
  1073. // 截取前三个
  1074. workMessageList = workMessageList.slice(0, 3)
  1075. this.setData({
  1076. messageList: workMessageList,
  1077. })
  1078. }
  1079. })
  1080. },
  1081. // 获取未读消息数量
  1082. getNotMessageTotal() {
  1083. get('v2/api/message/total', {}, (res) => {
  1084. if (res.code == 200) {
  1085. this.getWorkMessage(res.data)
  1086. }
  1087. })
  1088. },
  1089. // 获取未读业务消息数量
  1090. getWorkMessage(total) {
  1091. get('v2/api/message/all', {}, (res) => {
  1092. if (res.code == 200) {
  1093. this.setData({
  1094. notMessageTotal: Number(total) + Number(res.data)
  1095. })
  1096. }
  1097. })
  1098. },
  1099. // 跳转到消息详情
  1100. goMessageDetail(e) {
  1101. let id = e.currentTarget.dataset.id
  1102. let notice = e.currentTarget.dataset.notice
  1103. // 是否是业务消息 是业务消息就跳转到消息中心
  1104. if (notice) {
  1105. wx.navigateTo({
  1106. url: `/subPackagesC/pages/messageDetail/messageDetail?id=${id}`,
  1107. })
  1108. } else {
  1109. wx.navigateTo({
  1110. url: '/subPackagesC/pages/messageCenter/messageCenter',
  1111. })
  1112. }
  1113. },
  1114. // 跳转到消息列表
  1115. goMessageCenter() {
  1116. wx.navigateTo({
  1117. url: '/subPackagesC/pages/messageCenter/messageCenter',
  1118. })
  1119. },
  1120. // 获取测肤概要信息
  1121. getSkinInfo() {
  1122. // get('v2/api/home/analysis', {}, (res) => {
  1123. // if (res.code == 200) {
  1124. // this.setData({
  1125. // skinInfo: res.data
  1126. // })
  1127. // }
  1128. // })
  1129. },
  1130. // 跳转到测肤记录详情
  1131. goTestSkin() {
  1132. wx.navigateTo({
  1133. url: '/subPackagesD/pages/testSkin/testSkin?isRadio=true',
  1134. })
  1135. },
  1136. })