index.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <template>
  2. <uv-navbar placeholder leftIcon="" title="Yeswash洗车"
  3. :border="false"></uv-navbar>
  4. <view class="content" :style="containerStyle">
  5. <!-- <image class="logo" src="/static/logo.png"/>
  6. <view class="text-area">
  7. <text class="title">{{ title }}</text>
  8. </view>-->
  9. <view style="width: 100%;" class="swiper-content">
  10. <swiper class="swiper" circular :indicator-dots="true" :autoplay="true" :interval="3000"
  11. :duration="500">
  12. <swiper-item v-for="(item,idx) in state.swiperList" :key="idx">
  13. <image :src="item" mode="widthFix" style="width: 100%" @click="handleBannerClick(idx)"></image>
  14. </swiper-item>
  15. </swiper>
  16. <!-- <uv-swiper
  17. style="width: 100%"
  18. :list="state.swiperList"
  19. @click="handleBannerClick"
  20. circular
  21. :autoplay="true"
  22. bgColor="#ffffff">
  23. </uv-swiper>-->
  24. </view>
  25. <!--
  26. <view class="w100">
  27. <uv-notice-bar mode="closable" speed="50" url="/pages/coupon/index" :text="state.notice"></uv-notice-bar>
  28. </view>
  29. -->
  30. <view class="w100 gap"></view>
  31. <view class="menu-content w100">
  32. <uv-grid :border="false" :col="4">
  33. <uv-grid-item v-for="(item,index) in state.menuList" :key="index" @click="handleMenuClick(item)">
  34. <image :src="item.icon" mode="widthFit" style="width: 48rpx;height: 48rpx"></image>
  35. <!-- <uv-icon :customStyle="{paddingTop:20+'rpx'}" :name="item.name" :size="22"></uv-icon>-->
  36. <text class="grid-text" style="font-size: 14px;">{{ item.title }}</text>
  37. </uv-grid-item>
  38. </uv-grid>
  39. </view>
  40. <view class="w100 gap"></view>
  41. <!-- 站点清单 start-->
  42. <view class="w100 content_station">
  43. <template v-if="isLogin">
  44. <WashStation v-for="item in state.stationList" :key="item.id" :item="item"></WashStation>
  45. </template>
  46. <uv-empty v-else mode="order" text="请先登录" :marginTop="100"></uv-empty>
  47. </view>
  48. <!-- 站点清单 end-->
  49. <!-- <cover-view class="login_bar">
  50. <login-bar class="w100 text-center"></login-bar>
  51. </cover-view>-->
  52. <!-- <cover-view class="login_bar" v-show="!isLogin">-->
  53. <login-bar class="w100 text-center"></login-bar>
  54. <!-- </cover-view>-->
  55. <tab-bar :index="0"></tab-bar>
  56. </view>
  57. </template>
  58. <script setup lang="ts">
  59. import {nextTick, reactive, ref} from 'vue'
  60. import {onHide, onShow, onLoad} from "@dcloudio/uni-app";
  61. import TabBar from "@/components/tab-bar/index.vue";
  62. import LoginBar from "@/components/login-bar/index.vue";
  63. import WashStation from "@/components/station/index.vue"
  64. import {get, post} from "@/utils/https";
  65. import {calcMapDistance} from "@/utils/common"
  66. import {checkLogin, fetchToken, loadUserInfo, tryLogin} from "@/utils/auth";
  67. const title = ref('Hello')
  68. const isLogin = ref(false)
  69. const containerStyle = ref();
  70. const change = () => {
  71. console.log("change")
  72. }
  73. const initState = () => ({
  74. bannerList: [],
  75. swiperList: [],
  76. menuList: [
  77. {name: "photo", title: "洗车", icon: '/static/iconfont/default/fastwash.svg'},
  78. {name: "lock", title: "充值", icon: '/static/iconfont/default/chongzhi_1.svg'},
  79. {name: "star", title: "卡券", icon: '/static/iconfont/default/coupon.svg'},
  80. {name: "red-packet", title: "停车", icon: '/static/iconfont/default/parking.svg'},
  81. ],
  82. qrcodeOption: {
  83. style: 'round',
  84. size: 230,
  85. // 指定二维码前景,一般可在中间放logo
  86. foregroundImageSrc: 'https://www.uvui.cn/common/logo.png'
  87. },
  88. customStyle: {
  89. 'margin-top': '40rpx',
  90. height: '80rpx',
  91. width: '80%',
  92. borderRadius: '40rpx',//圆角
  93. },
  94. notice: '中秋佳节,全场8折,快来洗车吧!',
  95. stationList: [],
  96. location: {
  97. latitude: 0,
  98. longitude: 0,
  99. }
  100. })
  101. const state = reactive(initState())
  102. onLoad((e: any) => {
  103. const bound = uni.getMenuButtonBoundingClientRect();
  104. containerStyle.value = {
  105. top: `${bound.bottom + 10}px`,
  106. };
  107. /* if (query) {
  108. uni.showToast({
  109. title: query,
  110. duration: 5000
  111. })
  112. }*/
  113. })
  114. onShow(() => {
  115. let token = fetchToken();
  116. if (token) {
  117. getApp<any>().globalData.token = token;
  118. getApp<any>().globalData.isLogin = true;
  119. let gd = getApp<any>().globalData;
  120. console.log(gd)
  121. if (!gd.user || !gd.user.id) {
  122. loadUserInfo();
  123. } else {
  124. isLogin.value = true;
  125. }
  126. } else {
  127. //主动退出的不自动登录
  128. let manualLogout = getApp<any>().globalData.manualLogout;
  129. if (manualLogout) {
  130. return;
  131. }
  132. tryLogin().then(token => {
  133. getApp<any>().globalData.token = token;
  134. getApp<any>().globalData.isLogin = true;
  135. isLogin.value = true;
  136. }).catch(e => {
  137. console.error(e)
  138. })
  139. }
  140. /* const userData = getApp<any>().globalData.user;
  141. if (userData && userData.id) {
  142. isLogin.value = true;
  143. }*/
  144. console.log("show index>>>>", isLogin.value, state.stationList, state.location)
  145. let locationData = getApp<any>().globalData.location;
  146. if (!locationData || !locationData.longitude) {
  147. loadCurrentLocation();
  148. } else {
  149. state.location = locationData;
  150. loadWashStationList();
  151. }
  152. if (isLogin.value) {
  153. loadBannerList();
  154. } else {
  155. addListener();
  156. }
  157. });
  158. onHide(() => {
  159. Object.assign(state, initState());
  160. removeListener();
  161. })
  162. const addListener = () => {
  163. uni.$on('login', function (data) {
  164. isLogin.value = data.isLogin;
  165. if (!state.bannerList || state.bannerList.length === 0) {
  166. loadBannerList();
  167. }
  168. loadWashStationList();
  169. })
  170. uni.$on('logout', function (data) {
  171. isLogin.value = false;
  172. })
  173. }
  174. const removeListener = () => {
  175. uni.$off('logout');
  176. uni.$off('login');
  177. }
  178. const loadBannerList = () => {
  179. get(`/banner`).then((res: any) => {
  180. nextTick(() => {
  181. state.bannerList = res;
  182. state.swiperList = res.map(k => k.bannerUrl);
  183. console.log(state.swiperList)
  184. })
  185. })
  186. }
  187. const handleBannerClick = (index: number) => {
  188. let ele = state.bannerList[index];
  189. if (ele && ele.linkUrl) {
  190. uni.navigateTo({url: ele.linkUrl})
  191. }
  192. }
  193. const handleMenuClick = (menu: any) => {
  194. checkLogin().then((res) => {
  195. if (menu.title === '洗车') {
  196. uni.navigateTo({
  197. url: '/pages-wash/scan/index'
  198. })
  199. } else if (menu.title === '充值') {
  200. uni.navigateTo({
  201. url: '/pages-user/wallet/recharge'
  202. })
  203. } else {
  204. uni.showToast({
  205. icon: 'none',
  206. title: '敬请期待',
  207. })
  208. }
  209. })
  210. }
  211. /**
  212. * 获取当前的地理位置
  213. */
  214. const loadCurrentLocation = () => {
  215. uni.authorize({
  216. scope: 'scope.userLocation',
  217. success() {
  218. uni.getLocation({
  219. type: 'gcj02', //返回可以用于uni.openLocation的经纬度
  220. success: res => {
  221. let location = {
  222. latitude: res.latitude,
  223. longitude: res.longitude
  224. }
  225. getApp<any>().globalData.location = location;
  226. state.location = location
  227. console.log("loadCurrentLocation location", res)
  228. loadWashStationList();
  229. }
  230. });
  231. },
  232. fail(e) {
  233. loadWashStationList();
  234. console.log("authorize location fail", e)
  235. }
  236. })
  237. }
  238. const loadWashStationList = () => {
  239. if (isLogin.value) {
  240. if (state.stationList && state.stationList.length > 0) {
  241. return;
  242. }
  243. post(`/wash-station/listStation`, {pageSize: 5}).then((res: any) => {
  244. console.log(res)
  245. let {list, total} = res;
  246. list.forEach((station: any) => {
  247. if (station.location) {
  248. let stationLocation = JSON.parse(station.location);
  249. station.distance = calcMapDistance(state.location.latitude, state.location.longitude, stationLocation.stationLat, stationLocation.stationLng);
  250. }
  251. })
  252. state.stationList = list;
  253. })
  254. }
  255. }
  256. const handleNavMap = (station: any) => {
  257. let location = JSON.parse(station.location)
  258. uni.openLocation({
  259. latitude: location.stationLat,
  260. longitude: location.stationLng,
  261. scale: 18,
  262. name: station.stationName,
  263. address: station.address,
  264. });
  265. }
  266. const handleNavStation = (station: any) => {
  267. getApp<any>().globalData.pageData = {station}
  268. uni.navigateTo({
  269. url: '/pages-wash/station/index?id=' + station.id
  270. })
  271. console.log(station)
  272. }
  273. </script>
  274. <style scoped lang="scss">
  275. page {
  276. background: #fafafa;
  277. }
  278. .content {
  279. width: 100%;
  280. display: flex;
  281. flex-direction: column;
  282. align-items: center;
  283. justify-content: center;
  284. border-radius: 8rpx;
  285. }
  286. .logo {
  287. height: 200rpx;
  288. width: 200rpx;
  289. margin: 200rpx auto 50rpx;
  290. }
  291. .menu-content {
  292. width: 100%;
  293. padding: 0 20rpx;
  294. border-radius: 8rpx;
  295. }
  296. .qrcode-content {
  297. margin-top: 40rpx;
  298. padding: 0 20rpx;
  299. width: 100%;
  300. display: flex;
  301. flex-direction: column;
  302. align-items: center;
  303. align-content: center;
  304. justify-content: center;
  305. }
  306. .gap {
  307. width: 100%;
  308. height: 36rpx;
  309. background-color: #f5f3f3;
  310. margin-top: 15rpx;
  311. margin-bottom: 10rpx;
  312. }
  313. .swiper-content {
  314. height: 300rpx;
  315. overflow: hidden;
  316. }
  317. .guide-content {
  318. text-align: center;
  319. }
  320. .loginbar {
  321. position: fixed;
  322. bottom: 20rpx;
  323. }
  324. .content_station {
  325. height: calc(100vh - 810rpx);
  326. overflow: scroll;
  327. }
  328. </style>