index.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. uni.hideTabBar({
  171. animation: false,
  172. });
  173. },
  174. //页面加载
  175. async onLoad() {
  176. setTimeout(() => {
  177. this.list = new Array(10000).fill(1);
  178. }, 5000);
  179. uni.getLocation({
  180. type: 'gcj02',
  181. success: res => {
  182. this.location.longitude = res.longitude;
  183. this.location.latitude = res.latitude;
  184. this.$store.commit('SET_LOCATION', {
  185. longitude: res.longitude,
  186. latitude: res.latitude,
  187. });
  188. },
  189. fail: () => {
  190. this.manualGetLocation();
  191. },
  192. });
  193. },
  194. methods: {
  195. //轮播图切换
  196. change() {},
  197. //轮播图点击
  198. click(name) {
  199. this.$refs.uToast.success(`点击了第${name}个`);
  200. },
  201. // 获取首页数据
  202. getHomeData(location) {
  203. getHomePageApi(location)
  204. .then(res => {
  205. this.homeData = res.data;
  206. // 将分类数据做缓存处理
  207. this.$store.commit('SET_CATEGORIES', this.homeData.categories);
  208. })
  209. .catch(err => {});
  210. },
  211. // 手动选择城市
  212. manualGetLocation() {
  213. uni.chooseLocation({
  214. success: res => {
  215. this.$store.commit('SET_LOCATION', {
  216. longitude: res.longitude,
  217. latitude: res.latitude,
  218. });
  219. },
  220. });
  221. },
  222. // 点击菜单
  223. handleMenuClick(item) {
  224. if (item.id === 'all') {
  225. this.$Router.push('/pages/client/clientPackage/category');
  226. } else {
  227. uni.navigateTo({
  228. url: `/pages/client/clientPackage/storeList?id=${item.id}`,
  229. });
  230. }
  231. },
  232. /* 询价 */
  233. btnClick() {
  234. // uni.chooseImage({
  235. // count: 9,
  236. // sizeType: ['original', 'compressed'],
  237. // sourceType: ['camera', 'album'],
  238. // success: res => {
  239. // const tempFilePaths = res.tempFilePaths;
  240. // this.uploadFileAlbum();
  241. // },
  242. // });
  243. uni.navigateTo({
  244. url: `/pages/client/clientUser/inquiry`,
  245. });
  246. },
  247. uploadFileAlbum() {
  248. uni.uploadFile({
  249. url: '', //服务器地址
  250. fileType: 'image', //ZFB必填,不然报错
  251. filePath: tempFilePaths[0], //这个就是我们上面拍照返回或者先中照片返回的数组
  252. name: 'imgFile',
  253. success: uploadFileRes => {
  254. let imgData = JSON.parse(uploadFileRes.data);
  255. this.imgDataUrl = imgData.data.imgUrl;
  256. },
  257. });
  258. },
  259. },
  260. };
  261. </script>
  262. <style lang="scss" scoped>
  263. .home {
  264. background-color: #f0efef;
  265. .nav-bar {
  266. ::v-deep .u-navbar__content {
  267. background-color: #ffffff;
  268. }
  269. }
  270. }
  271. /* 头部搜索栏 */
  272. .home-header {
  273. display: flex;
  274. align-items: center;
  275. height: 90rpx;
  276. line-height: 90rpx;
  277. padding: 0 20rpx;
  278. background-color: #ffffff;
  279. .goto-address {
  280. display: flex;
  281. align-items: center;
  282. .icon-sanjiaoxing1 {
  283. font-size: 24rpx;
  284. margin: 17rpx 10rpx 0 6rpx;
  285. }
  286. }
  287. .search-item {
  288. flex: 1;
  289. }
  290. }
  291. /* 轮播图 */
  292. .home-swiper {
  293. padding: 20rpx;
  294. margin-bottom: 10rpx;
  295. }
  296. /* 商品分类 */
  297. .home-list {
  298. background-color: #ffffff;
  299. height: 360rpx;
  300. margin: 0 20rpx 20rpx 20rpx;
  301. padding: 20rpx 0 50rpx 0;
  302. border-radius: 10rpx;
  303. .grid-text {
  304. font-size: 12px;
  305. color: #909399;
  306. padding: 10rpx 0 20rpx 0rpx;
  307. box-sizing: border-box;
  308. }
  309. }
  310. /* 热门推荐 */
  311. .hot {
  312. ::v-deep .u-cell__body {
  313. padding: 10rpx 20rpx;
  314. .hot-title {
  315. border-left: 4px solid $uni-color-primary;
  316. padding-left: 20rpx;
  317. }
  318. }
  319. }
  320. .near {
  321. ::v-deep .u-cell__body {
  322. padding: 10rpx 20rpx;
  323. .near-title {
  324. border-left: 4px solid $uni-color-primary;
  325. padding-left: 20rpx;
  326. }
  327. }
  328. }
  329. </style>