index.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <template>
  2. <view class="home">
  3. <view>
  4. <!-- 顶部导航 -->
  5. <view class="nav-bar">
  6. <u-navbar :bgColor="'#ffffff'" :placeholder="true">
  7. <view slot="left" @click="manualGetLocation">
  8. <text>地址</text>
  9. <text class="iconfont icon-chevron-down" />
  10. </view>
  11. <view slot="center">
  12. <u-search
  13. placeholder="请输入搜索内容"
  14. :showAction="false"
  15. v-model="keyword"
  16. @click="$Router.push('/pages/client/clientPackage/search')"
  17. :disabled="true"
  18. />
  19. </view>
  20. </u-navbar>
  21. </view>
  22. <!-- 轮播图 -->
  23. <view class="home-swiper">
  24. <u-swiper :list="swiperList" indicator @change="change" @click="click" />
  25. </view>
  26. <!-- 商品分类 -->
  27. <view class="home-list">
  28. <u-grid :border="false" col="5">
  29. <u-grid-item v-for="item in categories" :key="item.id" @tap="handleMenuClick(item)">
  30. <u-icon :customStyle="{ paddingTop: 20 + 'rpx' }" :name="item.icon" :size="25"></u-icon>
  31. <text class="grid-text">{{ item.name }}</text>
  32. </u-grid-item>
  33. </u-grid>
  34. <u-toast ref="uToast" />
  35. </view>
  36. <!-- 热门推荐 -->
  37. <view class="hot">
  38. <u-cell
  39. :border="false"
  40. value="查看更多"
  41. isLink
  42. @click="$Router.push('/pages/client/clientPackage/storeHot')"
  43. >
  44. <view slot="title" class="hot-title"> 热门推荐 </view>
  45. </u-cell>
  46. <block v-if="homeData.recommends && homeData.recommends.length > 0">
  47. <view>
  48. <u-grid :border="false" col="2">
  49. <u-grid-item
  50. @click="$Router.push(`/pages/client/clientPackage/storeDetail?id=${item.id}`)"
  51. v-for="item in homeData.recommends"
  52. :key="item.id"
  53. >
  54. <recommend-item :item="item"></recommend-item>
  55. </u-grid-item>
  56. </u-grid>
  57. </view>
  58. </block>
  59. <block v-else>
  60. <u-empty icon="/static/default-graph/no-business.png" text="暂无推荐商家"></u-empty>
  61. </block>
  62. </view>
  63. <!-- 附近商家 -->
  64. <view class="near">
  65. <u-cell
  66. :border="false"
  67. value="查看更多"
  68. isLink
  69. @click="$Router.push('/pages/client/clientPackage/storeNearby')"
  70. >
  71. <view slot="title" class="near-title"> 附近商家 </view>
  72. </u-cell>
  73. <block v-if="homeData.nears && homeData.nears.length > 0">
  74. <view>
  75. <u-grid :border="false" col="2">
  76. <u-grid-item
  77. @click="$Router.push(`/pages/client/clientPackage/storeDetail?id=${item.id}`)"
  78. v-for="item in homeData.nears"
  79. :key="item.id"
  80. >
  81. <hot-item :item="item"></hot-item>
  82. </u-grid-item>
  83. </u-grid>
  84. </view>
  85. </block>
  86. <block v-else>
  87. <u-empty icon="/static/default-graph/no-business.png" text="暂无附近商家"></u-empty>
  88. </block>
  89. </view>
  90. <!-- 询价 -->
  91. <drag-button :isDock="true" :existTabBar="true" @btnClick="btnClick" />
  92. </view>
  93. <tabbar currentTab="clientHome" />
  94. </view>
  95. </template>
  96. <script>
  97. import { getCurrentLocation, getHomePageApi } from '@/api/client/home';
  98. import { mapGetters } from 'vuex';
  99. import RecommendItem from '@/components/merchant/RecommendItem.vue';
  100. import HotItem from '@/components/merchant/HotItem.vue';
  101. import dragButton from '@/components/drag/drag-button.vue';
  102. export default {
  103. components: {
  104. RecommendItem,
  105. HotItem,
  106. dragButton,
  107. },
  108. data() {
  109. return {
  110. list: [],
  111. keyword: '',
  112. swiperList: [
  113. 'https://cdn.uviewui.com/uview/swiper/swiper1.png',
  114. 'https://cdn.uviewui.com/uview/swiper/swiper2.png',
  115. 'https://cdn.uviewui.com/uview/swiper/swiper3.png',
  116. ],
  117. homeData: {
  118. categories: [],
  119. nears: [],
  120. recommends: [],
  121. },
  122. };
  123. },
  124. computed: {
  125. ...mapGetters(['location']),
  126. categories() {
  127. let initData = this.homeData.categories.map(item => {
  128. if (!item.icon) {
  129. item.icon = 'level';
  130. }
  131. return item;
  132. });
  133. let result =
  134. initData.length > 14
  135. ? initData.splice(0, 14).concat([
  136. {
  137. id: 'all',
  138. icon: 'grid-fill',
  139. name: '全部',
  140. type: {
  141. code: 'ALL',
  142. description: '全部',
  143. },
  144. },
  145. ])
  146. : initData.push({
  147. id: 'all',
  148. icon: 'grid-fill',
  149. name: '全部',
  150. });
  151. return result;
  152. },
  153. },
  154. watch: {
  155. location: {
  156. handler: async function (newVal, oldVal) {
  157. const result = await getCurrentLocation(newVal);
  158. const param = Object.assign(
  159. {},
  160. {
  161. region: result.data.id,
  162. },
  163. newVal,
  164. );
  165. this.getHomeData(param);
  166. },
  167. },
  168. },
  169. onShow() {
  170. /* 隐藏原生的tabbar */
  171. uni.hideTabBar({
  172. animation: false,
  173. });
  174. },
  175. //页面加载
  176. async onLoad() {
  177. setTimeout(() => {
  178. this.list = new Array(10000).fill(1);
  179. }, 5000);
  180. uni.getLocation({
  181. type: 'gcj02',
  182. success: res => {
  183. this.location.longitude = res.longitude;
  184. this.location.latitude = res.latitude;
  185. this.$store.commit('SET_LOCATION', {
  186. longitude: res.longitude,
  187. latitude: res.latitude,
  188. });
  189. },
  190. fail: () => {
  191. this.manualGetLocation();
  192. },
  193. });
  194. },
  195. methods: {
  196. //轮播图切换
  197. change() {},
  198. //轮播图点击
  199. click(name) {
  200. this.$refs.uToast.success(`点击了第${name}个`);
  201. },
  202. // 获取首页数据
  203. getHomeData(location) {
  204. getHomePageApi(location)
  205. .then(res => {
  206. this.homeData = res.data;
  207. // 将分类数据做缓存处理
  208. this.$store.commit('SET_CATEGORIES', this.homeData.categories);
  209. })
  210. .catch(err => {});
  211. },
  212. // 手动选择城市
  213. manualGetLocation() {
  214. uni.chooseLocation({
  215. success: res => {
  216. this.$store.commit('SET_LOCATION', {
  217. longitude: res.longitude,
  218. latitude: res.latitude,
  219. });
  220. },
  221. });
  222. },
  223. // 点击菜单
  224. handleMenuClick(item) {
  225. if (item.id === 'all') {
  226. this.$Router.push('/pages/client/clientPackage/category');
  227. } else {
  228. uni.navigateTo({
  229. url: `/pages/client/clientPackage/storeList?id=${item.id}`,
  230. });
  231. }
  232. },
  233. /* 询价 */
  234. btnClick() {
  235. // uni.chooseImage({
  236. // count: 9,
  237. // sizeType: ['original', 'compressed'],
  238. // sourceType: ['camera', 'album'],
  239. // success: res => {
  240. // const tempFilePaths = res.tempFilePaths;
  241. // this.uploadFileAlbum();
  242. // },
  243. // });
  244. uni.navigateTo({
  245. url: `/pages/client/clientUser/inquiry`,
  246. });
  247. },
  248. uploadFileAlbum() {
  249. uni.uploadFile({
  250. url: '', //服务器地址
  251. fileType: 'image', //ZFB必填,不然报错
  252. filePath: tempFilePaths[0], //这个就是我们上面拍照返回或者先中照片返回的数组
  253. name: 'imgFile',
  254. success: uploadFileRes => {
  255. let imgData = JSON.parse(uploadFileRes.data);
  256. this.imgDataUrl = imgData.data.imgUrl;
  257. },
  258. });
  259. },
  260. },
  261. };
  262. </script>
  263. <style lang="scss" scoped>
  264. .home {
  265. background-color: #f0efef;
  266. .nav-bar {
  267. ::v-deep .u-navbar__content {
  268. background-color: #ffffff;
  269. }
  270. }
  271. }
  272. /* 头部搜索栏 */
  273. .home-header {
  274. display: flex;
  275. align-items: center;
  276. height: 90rpx;
  277. line-height: 90rpx;
  278. padding: 0 20rpx;
  279. background-color: #ffffff;
  280. .goto-address {
  281. display: flex;
  282. align-items: center;
  283. .icon-sanjiaoxing1 {
  284. font-size: 24rpx;
  285. margin: 17rpx 10rpx 0 6rpx;
  286. }
  287. }
  288. .search-item {
  289. flex: 1;
  290. }
  291. }
  292. /* 轮播图 */
  293. .home-swiper {
  294. padding: 20rpx;
  295. margin-bottom: 10rpx;
  296. }
  297. /* 商品分类 */
  298. .home-list {
  299. background-color: #ffffff;
  300. height: 360rpx;
  301. margin: 0 20rpx 20rpx 20rpx;
  302. padding: 20rpx 0 50rpx 0;
  303. border-radius: 10rpx;
  304. .grid-text {
  305. font-size: 12px;
  306. color: #909399;
  307. padding: 10rpx 0 20rpx 0rpx;
  308. box-sizing: border-box;
  309. }
  310. }
  311. /* 热门推荐 */
  312. .hot {
  313. ::v-deep .u-cell__body {
  314. padding: 10rpx 20rpx;
  315. .hot-title {
  316. border-left: 4px solid $uni-color-primary;
  317. padding-left: 20rpx;
  318. }
  319. }
  320. }
  321. .near {
  322. ::v-deep .u-cell__body {
  323. padding: 10rpx 20rpx;
  324. .near-title {
  325. border-left: 4px solid $uni-color-primary;
  326. padding-left: 20rpx;
  327. }
  328. }
  329. }
  330. </style>