list.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <template>
  2. <view>
  3. <view class="top-box">
  4. <view class="top-nav">
  5. <view class="city-text" @click="regionSelectionRef.open()">
  6. <uv-icon name="map" color="#999999" size="34rpx"></uv-icon>
  7. <view>{{ cityName }}</view>
  8. </view>
  9. <view class="searchBox">
  10. <image src="/static/image/order/icon_search.png" mode="widthFix"></image>
  11. <input v-model="param.businessName" type="text" @confirm="search" placeholder-style="color:#B3B3B3"
  12. placeholder="请输入关键字" @input="search" />
  13. </view>
  14. </view>
  15. <view class="tabsBox">
  16. <view class="type-box">
  17. <view class="item" @click.stop="pickerRef.open()">
  18. <view>{{
  19. selectIndex === null ? "全部分类" : selector[selectIndex].name
  20. }}</view>
  21. <uv-icon name="arrow-down" color="#4d4d4d" size="26rpx"></uv-icon>
  22. </view>
  23. <view class="item" v-for="(v, i) in typeList" :key="i" :class="{ activeCls: tabCurrent == i }"
  24. @click="tabsChange2(i)">
  25. <view>{{ v.name }}</view>
  26. </view>
  27. </view>
  28. </view>
  29. </view>
  30. <view :style="{ height: placeholderH + 'px' }"></view>
  31. <view class="shopListBox" v-if="RowsList.length > 0">
  32. <!-- 附近门店 -->
  33. <ShopItem v-for="(v, index) in RowsList" :key="v.businessId" :v="v" />
  34. </view>
  35. <view>
  36. <!-- <loadMore v-if="RowsList.length > 0" :status="LoadStatus"></loadMore> -->
  37. <noData v-if="RowsList.length <= 0" :config="{ top: 2 }"></noData>
  38. </view>
  39. <!-- 地区选择 -->
  40. <RegionSelection ref="regionSelectionRef" :hierarchy="2" @confirm="addressConfirm" />
  41. <uv-picker ref="pickerRef" :columns="[selector]" keyName="name" :default-selector="[sIndex]"
  42. @confirm="confirmSelect"></uv-picker>
  43. </view>
  44. </template>
  45. <script setup>
  46. import { computed, getCurrentInstance, nextTick, reactive, ref } from "vue";
  47. import { onLoad, onPullDownRefresh, onShow } from "@dcloudio/uni-app";
  48. import { getBusinessList, getBusinessCategoryList } from "@/api/shop.js";
  49. import ShopItem from "./components/ShopItem.vue";
  50. const pickerRef = ref(null);
  51. const regionSelectionRef = ref(null);
  52. const instance = getCurrentInstance();
  53. const placeholderH = ref(0);
  54. const cityName = ref("武汉市");
  55. const sIndex = ref(0);
  56. const param = reactive({
  57. businessName: "",
  58. nearMe: true,
  59. });
  60. const RowsList = ref([]);
  61. const tabCurrent = ref(0);
  62. const typeList = ref([
  63. { name: "距离优先", order: "distance" },
  64. { name: "评分优先", order: "average" },
  65. ]);
  66. const selector = ref([]);
  67. const selectIndex = ref(null);
  68. const getRect = (selectorStr) =>
  69. new Promise((resolve) => {
  70. const query = uni.createSelectorQuery();
  71. const ctx = instance?.proxy || undefined;
  72. if (ctx) query.in(ctx);
  73. query
  74. .select(selectorStr)
  75. .boundingClientRect((res) => resolve(res || {}))
  76. .exec();
  77. });
  78. const tabsChange2 = (index) => {
  79. tabCurrent.value = index;
  80. let order = typeList.value[index]?.order || "distance";
  81. // console.log(param);
  82. param.nearMe = false;
  83. param.orderByColumn = undefined;
  84. param.isAsc = undefined;
  85. if (order == 'distance') {
  86. param.nearMe = true;
  87. } else {
  88. param.orderByColumn = 'average';
  89. param.isAsc = 'desc';
  90. }
  91. initData(param);
  92. };
  93. const confirmSelect = (e) => {
  94. // console.log(e);
  95. const idx = e?.indexs[0] ?? 0;
  96. // console.log(idx);
  97. selectIndex.value = idx;
  98. sIndex.value = idx;
  99. param.businessCategoryId = selector.value[idx]?.categoryId || undefined;
  100. // console.log(param);
  101. initData(param);
  102. };
  103. // 地区选择确认
  104. const addressConfirm = (e) => {
  105. const picked = e.areaName || e.cityName || e.provinceName;
  106. cityName.value = picked || cityName.value;
  107. Object.assign(param, { ...e });
  108. initData(param);
  109. };
  110. const getTppe = async (id) => {
  111. await getBusinessCategoryList().then((res) => {
  112. if (res && res.code == 200) {
  113. selector.value = [{ name: "全部分类", categoryId: "" }, ...(res.data || [])];
  114. // console.log(selector.value);
  115. if (id) {
  116. selector.value.forEach((v, i) => {
  117. if (String(v.categoryId) === String(id)) selectIndex.value = i;
  118. });
  119. }
  120. }
  121. });
  122. };
  123. // 获取当前位置
  124. const getLoca = async () => {
  125. await uni.getLocation({
  126. type: "gcj02",
  127. success: (res) => {
  128. param.userLongitude = res.longitude;
  129. param.userLatitude = res.latitude;
  130. initData(param);
  131. },
  132. fail: () => { },
  133. });
  134. };
  135. const searchTimer = ref(null);
  136. const search = () => {
  137. if (searchTimer.value) clearTimeout(searchTimer.value);
  138. searchTimer.value = setTimeout(() => {
  139. initData(param);
  140. }, 1000);
  141. };
  142. const initPage = async (options = {}) => {
  143. if (options?.id) {
  144. param.businessCategoryId = options.id;
  145. }
  146. if (options?.cityName) {
  147. cityName.value = options.cityName;
  148. }
  149. if (options?.cityCode) {
  150. param.cityCode = options.cityCode;
  151. }
  152. try {
  153. await getTppe(param.businessCategoryId);
  154. await getLoca();
  155. } catch (error) {
  156. console.log(error);
  157. }
  158. };
  159. const initData = (params = {}) => {
  160. uni.showLoading({
  161. title: '加载中',
  162. mask: true
  163. })
  164. getBusinessList(params).then((res) => {
  165. uni.hideLoading();
  166. if (res && res.code == 200) {
  167. RowsList.value = res.data || [];
  168. } else {
  169. RowsList.value = [];
  170. }
  171. }).finally(() => {
  172. uni.stopPullDownRefresh();
  173. });
  174. }
  175. onLoad((options) => {
  176. initPage(options || {});
  177. });
  178. onShow(async () => {
  179. await nextTick();
  180. const rect = await getRect(".top-box");
  181. placeholderH.value = rect?.height || 0;
  182. });
  183. onPullDownRefresh(() => {
  184. initData(param);
  185. // Promise.allSettled([getLoca(), initData(param)]).finally(() => {
  186. // uni.stopPullDownRefresh();
  187. // });
  188. });
  189. </script>
  190. <style lang="scss" scoped>
  191. .top-box {
  192. width: 750rpx;
  193. position: fixed;
  194. background-color: #fff;
  195. z-index: 3;
  196. }
  197. .top-nav {
  198. height: 120rpx;
  199. display: flex;
  200. justify-content: space-between;
  201. align-items: center;
  202. padding: 0 30rpx;
  203. background-color: #fff;
  204. }
  205. .city-text {
  206. margin-right: 20rpx;
  207. font-size: 30rpx;
  208. font-family: PingFang SC, PingFang SC-Regular;
  209. font-weight: 400;
  210. text-align: center;
  211. color: #666666;
  212. display: flex;
  213. align-items: center;
  214. view {
  215. max-width: 100rpx;
  216. white-space: nowrap;
  217. text-overflow: ellipsis;
  218. overflow: hidden;
  219. }
  220. image {
  221. width: 26rpx;
  222. height: 36rpx;
  223. margin-right: 10rpx;
  224. vertical-align: middle;
  225. }
  226. }
  227. .searchBox {
  228. // width: 560rpx;
  229. flex: 1;
  230. display: flex;
  231. padding: 10rpx 27rpx;
  232. border: 1rpx solid #cccccc;
  233. border-radius: 10rpx;
  234. display: flex;
  235. align-items: center;
  236. image {
  237. width: 35rpx;
  238. height: 35rpx;
  239. margin-right: 15rpx;
  240. flex-shrink: 0;
  241. }
  242. input {
  243. text-align: left;
  244. }
  245. }
  246. .tabsBox {
  247. width: 100%;
  248. background-color: #fff;
  249. .tabsBox_tabs {
  250. padding: 0 10rpx;
  251. ::v-deep .u-tab-item {
  252. font-weight: 700;
  253. }
  254. }
  255. }
  256. .type-box {
  257. display: flex;
  258. align-items: center;
  259. padding: 0 40rpx 20rpx;
  260. justify-content: space-between;
  261. .item {
  262. display: flex;
  263. font-size: 28rpx;
  264. font-family: PingFang SC, PingFang SC-Regular;
  265. font-weight: 400;
  266. color: #4d4d4d;
  267. align-items: center;
  268. color: #4d4d4d;
  269. &:first-child {
  270. min-width: 142rpx;
  271. }
  272. image {
  273. width: 14rpx;
  274. height: 10rpx;
  275. margin-left: 10rpx;
  276. vertical-align: middle;
  277. }
  278. }
  279. .activeCls {
  280. color: #fa6138;
  281. }
  282. .item2 {
  283. margin-left: 100rpx;
  284. padding-top: 20rpx;
  285. }
  286. }
  287. .content-box {
  288. padding: 0 30rpx;
  289. }
  290. .shopListBox {
  291. padding: 0 30rpx;
  292. .shopListBox_item {
  293. margin-top: 30rpx;
  294. }
  295. }
  296. </style>