products.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. <template>
  2. <Theme>
  3. <view class="wrap" id="pageContainer">
  4. <Navbar
  5. border
  6. fixed
  7. autoBack
  8. @leftClick="leftClick"
  9. @rightClick="searchConfirm"
  10. >
  11. <template #center>
  12. <view class="nav_center">
  13. <view class="img_left" v-if="defaultParams.img">
  14. <up-avatar
  15. :src="defaultParams.img"
  16. shape="square"
  17. size="34"
  18. @click="previewImage(defaultParams.img)"
  19. ></up-avatar>
  20. <up-icon
  21. name="close-circle-fill"
  22. color="#ef4444"
  23. size="14"
  24. class="del"
  25. @click="searchConfirm"
  26. ></up-icon>
  27. </view>
  28. <Search
  29. v-model="searchValue"
  30. :placeholder="t('商品的链接或名称')"
  31. confirm-type="search"
  32. >
  33. <template #prefix>
  34. <i class="icon-font icon-search"></i>
  35. </template>
  36. <template #suffix>
  37. <i class="icon-font icon-camera" @click="openCamera"></i>
  38. </template>
  39. </Search>
  40. </view>
  41. </template>
  42. <template #right>
  43. <view class="nav_right">
  44. <trans _t="搜索" />
  45. </view>
  46. </template>
  47. </Navbar>
  48. <view class="tops" id="tabs" :style="{ top: tabTop + 'px' }">
  49. <view class="tab">
  50. <Tab
  51. :active="actvieNum"
  52. keyName="text"
  53. :tabList="tabList"
  54. @confirm="tabClick"
  55. />
  56. </view>
  57. <view class="product_sort">
  58. <view
  59. class="sort_list"
  60. :class="sortActive == index ? 'sort_active' : ''"
  61. v-for="(item, index) in sortList"
  62. :key="index"
  63. @click="item.callback(item, index)"
  64. >
  65. <trans :_t="item.text" />
  66. <template v-if="item.isTrue == 1">
  67. <view class="sort_arrow">
  68. <view
  69. class="sort_arrow_top"
  70. :style="
  71. defaultParams.sort == 'moneyAsc'
  72. ? { borderBottom: '4px solid var(--black)' }
  73. : {}
  74. "
  75. ></view>
  76. <view
  77. class="sort_arrow_bottom"
  78. :style="
  79. defaultParams.sort == 'moneyDesc'
  80. ? { borderTop: '4px solid var(--black)' }
  81. : {}
  82. "
  83. ></view>
  84. </view>
  85. </template>
  86. </view>
  87. </view>
  88. </view>
  89. <view class="content">
  90. <List
  91. :url="defaultParams.img ? '/shop/goods/search' : '/shop/goods/list'"
  92. isCache
  93. :topHeight="tabHeight"
  94. :defaultParams="defaultParams"
  95. :pageSize="20"
  96. isLoading
  97. ref="listRef"
  98. @datas="getList"
  99. >
  100. <template #item="{ item, index }">
  101. <shopList
  102. :channel="defaultParams.channel"
  103. :item="item"
  104. :search="defaultParams.keyWord"
  105. @collect="collectClick($event, index)"
  106. />
  107. </template>
  108. </List>
  109. </view>
  110. </view>
  111. <popup title="搜索条款" isClose ref="popupRef">
  112. <template #content>
  113. <view class="pop_cont">
  114. <trans _t="搜索条款内容" />
  115. </view>
  116. <view class="pop_tip">
  117. <trans _t="您已同意搜索条款" />
  118. </view>
  119. <view class="pop_btn" @click="popClose">
  120. <trans _t="关闭" />
  121. </view>
  122. </template>
  123. </popup>
  124. <CameraUpload @confirm="getUrl" ref="cameraUploadRef" />
  125. </Theme>
  126. </template>
  127. <script setup>
  128. import { ref, computed, reactive, watch, onMounted, nextTick } from "vue";
  129. import Navbar from "@/components/navbar";
  130. import Search from "@/components/input";
  131. import CameraUpload from "@/components/CameraUpload";
  132. import { t } from "@/locale";
  133. import { onLoad, onReachBottom, onPageScroll } from "@dcloudio/uni-app";
  134. import List from "@/components/list";
  135. import {
  136. query,
  137. systemInfo,
  138. setStorage,
  139. getStorage,
  140. removeStorage,
  141. } from "@/utils";
  142. import shopList from "./components/shop_list";
  143. import popup from "@/components/popup";
  144. import Tab from "@/components/tabs";
  145. import { SHOP_USER_COLLECT } from "@/api";
  146. const searchList = ref(JSON.parse(getStorage("search_list") || "[]"));
  147. const search_list = computed(() => searchList.value || []);
  148. const searchValue = ref("");
  149. const listRef = ref(null);
  150. const popupRef = ref(null);
  151. const actvieNum = ref(1);
  152. const tabHeight = ref(0);
  153. const tabTop = ref(44);
  154. const sortActive = ref(0);
  155. const dataList = ref([]);
  156. const tabList = ref([
  157. {
  158. img: "../../static/shop/icon_0.png",
  159. text: "1688",
  160. channel: 0,
  161. },
  162. {
  163. img: "../../static/shop/icon_1.png",
  164. text: "淘宝",
  165. channel: 1,
  166. },
  167. {
  168. img: "../../static/shop/icon_3.png",
  169. text: "JD",
  170. channel: 3,
  171. },
  172. ]);
  173. const defaultParams = reactive({
  174. channel: tabList.value[actvieNum.value].channel,
  175. keyWord: "",
  176. start_price: "",
  177. end_price: "",
  178. sort: "",
  179. img: "",
  180. });
  181. const sortList = reactive([
  182. {
  183. text: "综合",
  184. sort: "",
  185. callback: (item, index) => {
  186. sortActive.value = index;
  187. defaultParams.sort = "";
  188. nextTick(() => {
  189. listRef.value && listRef.value.handleRefresh();
  190. });
  191. },
  192. },
  193. {
  194. text: "价格",
  195. isTrue: 1,
  196. sort: "moneyDesc", // moneyAsc 升序
  197. callback: (item, index) => {
  198. priceClick(item, index);
  199. },
  200. },
  201. ]);
  202. const cameraUploadRef = ref(null);
  203. const openCamera = () => {
  204. cameraUploadRef.value && cameraUploadRef.value.open();
  205. };
  206. const getUrl = (url) => {
  207. defaultParams.img = url;
  208. defaultParams.keyWord = searchValue.value = "";
  209. listRef.value && listRef.value.handleRefresh(url);
  210. };
  211. // 防抖标记(防止重复触发)
  212. const isLoading = ref(false);
  213. // 自定义触发阈值:距离底部 200px 时触发
  214. const triggerThreshold = ref(600);
  215. // 页面总高度(用于计算距离底部距离)
  216. const pageTotalHeight = ref(0);
  217. // 列表加载完成后调用(需在列表组件的加载回调中执行)
  218. const handleLoadComplete = async () => {
  219. isLoading.value = false;
  220. const rect = await query("#pageContainer", this);
  221. pageTotalHeight.value = rect.height;
  222. };
  223. onMounted(() => {
  224. nextTick(async () => {
  225. const res = await query("#tabs", this);
  226. tabTop.value = res.top + systemInfo().statusBarHeight;
  227. tabHeight.value = res.height;
  228. const rect = await query("#pageContainer", this);
  229. pageTotalHeight.value = rect.height;
  230. nextTick(() => {
  231. listRef.value && listRef.value.getData(defaultParams.img);
  232. });
  233. });
  234. });
  235. onPageScroll((e) => {
  236. nextTick(() => {
  237. if (isLoading.value || !pageTotalHeight.value) return;
  238. const systemInfo = uni.getSystemInfoSync();
  239. const windowHeight = systemInfo.windowHeight;
  240. const scrollTop = e.scrollTop;
  241. // 计算距离底部的距离:总高度 - 滚动距离 - 可视高度
  242. const distanceToBottom = pageTotalHeight.value - scrollTop - windowHeight;
  243. if (distanceToBottom <= triggerThreshold.value) {
  244. isLoading.value = true;
  245. listRef.value && listRef.value.scrolltolower();
  246. }
  247. });
  248. });
  249. const rightClick = () => {
  250. popupRef.value && popupRef.value.open();
  251. };
  252. const getList = (list) => {
  253. dataList.value = list;
  254. handleLoadComplete();
  255. };
  256. const previewImage = (url) => {
  257. uni.previewImage({
  258. urls: [url],
  259. });
  260. };
  261. const searchConfirm = () => {
  262. if (searchValue.value) {
  263. const searchText =
  264. searchValue.value.length > 20
  265. ? searchValue.value.substring(0, 20) + "..."
  266. : searchValue.value;
  267. search_list.value.unshift(searchText);
  268. let arr = search_list.value.slice(0, 8);
  269. setStorage("search_list", JSON.stringify([...new Set(arr)]));
  270. defaultParams.keyWord = searchValue.value;
  271. }
  272. defaultParams.img = "";
  273. nextTick(() => {
  274. listRef.value && listRef.value.handleRefresh();
  275. });
  276. };
  277. const popClose = () => {
  278. popupRef.value && popupRef.value.close();
  279. };
  280. const priceClick = (item, index) => {
  281. if (sortActive.value == index) {
  282. if (defaultParams.sort == "moneyAsc") {
  283. defaultParams.sort = "";
  284. sortActive.value = 0;
  285. } else {
  286. defaultParams.sort = "moneyAsc";
  287. }
  288. } else {
  289. sortActive.value = index;
  290. defaultParams.sort = item.sort;
  291. }
  292. nextTick(() => {
  293. listRef.value && listRef.value.handleRefresh();
  294. });
  295. };
  296. onLoad((options) => {
  297. const { searchText = "", channel = 1, searchImg = "" } = options;
  298. searchValue.value = searchText;
  299. actvieNum.value = channel == 3 ? 2 : Number(channel);
  300. defaultParams.channel = Number(channel);
  301. defaultParams.keyWord = searchText;
  302. defaultParams.img = searchImg || "";
  303. });
  304. const leftClick = () => {
  305. uni.navigateBack();
  306. // uni.switchTab({ url: '/pages/index/index' });
  307. };
  308. const tabClick = (item, index) => {
  309. defaultParams.channel = item.channel;
  310. if (actvieNum.value == index) return;
  311. actvieNum.value = index;
  312. nextTick(() => {
  313. listRef.value && listRef.value.handleRefresh(defaultParams.img);
  314. });
  315. };
  316. const collectClick = (item, index) => {
  317. setCollect(
  318. {
  319. channel: tabList.value[actvieNum.value].channel,
  320. type: "good",
  321. goods_id: item.num_iid,
  322. seller_id: item.seller_id,
  323. title: item.title,
  324. pic_url: item.pic_url,
  325. price: item.price,
  326. },
  327. index
  328. );
  329. };
  330. const setCollect = async (params, index) => {
  331. try {
  332. const res = await SHOP_USER_COLLECT(params);
  333. nextTick(() => {
  334. dataList.value[index].isCollect = res.data.collect;
  335. listRef.value && listRef.value.setList(dataList.value);
  336. });
  337. } catch (error) {
  338. Toast(error.msg);
  339. }
  340. };
  341. // onReachBottom(() => {
  342. // nextTick(() => {
  343. // listRef.value && listRef.value.scrolltolower();
  344. // });
  345. // });
  346. </script>
  347. <style lang="less" scoped>
  348. @import url("@/style.less");
  349. .wrap {
  350. min-height: 100vh;
  351. background-color: var(--bg);
  352. .nav_center {
  353. display: flex;
  354. width: calc(100% - 240rpx);
  355. .icon-camera {
  356. color: #adb8cc;
  357. font-size: 56rpx;
  358. }
  359. .img_left {
  360. position: relative;
  361. .del {
  362. position: absolute;
  363. right: -10rpx;
  364. top: -10rpx;
  365. }
  366. }
  367. }
  368. /deep/ .u-navbar {
  369. .u-navbar__content__left,
  370. .u-navbar__content__right {
  371. position: unset;
  372. }
  373. }
  374. .nav_right {
  375. color: var(--black);
  376. .size(12px);
  377. font-weight: 700;
  378. }
  379. .tops {
  380. // position: fixed;
  381. // top: 44px;
  382. // left: 0;
  383. // right: 0;
  384. // background-color: var(--bg);
  385. // z-index: 1;
  386. .tab {
  387. padding: 0 24rpx;
  388. }
  389. .product_sort {
  390. .flex();
  391. padding: 24rpx 32rpx;
  392. .sort_list {
  393. color: var(--text-01);
  394. .size(24rpx);
  395. line-height: 40rpx;
  396. margin-right: 72rpx;
  397. padding: 16rpx 0;
  398. .ver();
  399. .sort_arrow {
  400. margin-left: 20rpx;
  401. &_top {
  402. border-bottom: 4px solid #adb8cc;
  403. border-left: 4px solid transparent;
  404. border-right: 4px solid transparent;
  405. font-size: 0;
  406. height: 0;
  407. line-height: 0;
  408. margin-bottom: 1px;
  409. width: 0;
  410. }
  411. &_bottom {
  412. border-left: 4px solid transparent;
  413. border-right: 4px solid transparent;
  414. border-top: 4px solid #adb8cc;
  415. font-size: 0;
  416. height: 0;
  417. line-height: 0;
  418. margin-top: 1px;
  419. width: 0;
  420. }
  421. }
  422. }
  423. .sort_active {
  424. color: var(--black);
  425. }
  426. }
  427. }
  428. .content {
  429. padding: 16rpx 32rpx;
  430. /deep/ .wrap_list {
  431. .uni-scroll-view-content {
  432. > uni-view {
  433. .flex();
  434. flex-wrap: wrap;
  435. gap: 16rpx;
  436. .u-list-item {
  437. width: calc((100% - 16rpx) / 2);
  438. }
  439. }
  440. }
  441. }
  442. }
  443. }
  444. .pop_cont {
  445. color: var(--text);
  446. .size(28rpx);
  447. line-height: 60rpx;
  448. }
  449. .pop_tip {
  450. color: var(--text-01);
  451. .size(28rpx);
  452. line-height: 60rpx;
  453. }
  454. .pop_btn {
  455. margin-top: 32rpx;
  456. height: 76rpx;
  457. padding: 16rpx 30rpx;
  458. color: var(--text-01);
  459. border: 1px solid var(--text-01);
  460. border-radius: 16rpx;
  461. .size(24rpx);
  462. .flex_center();
  463. }
  464. </style>