index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <template>
  2. <view style="padding-bottom: 32rpx">
  3. <view class="searchGood">
  4. <view class="search acea-row row-between-wrapper">
  5. <view class="input acea-row row-between-wrapper">
  6. <text class="iconfont icon-sousuo" style="color: #999"></text>
  7. <input
  8. type="text"
  9. confirm-type="search"
  10. :value="searchValue"
  11. :focus="focus"
  12. placeholder="点击搜索商品"
  13. placeholder-class="placeholder"
  14. @input="setValue"
  15. @confirm="searchBut"
  16. />
  17. </view>
  18. <view class="bnt" @tap="searchBut">搜索</view>
  19. </view>
  20. <!-- <view class='title'>热门搜索</view>-->
  21. <!-- <view class='list acea-row'>-->
  22. <!-- <block v-for="(item, index) in hotSearchList" :key="index">-->
  23. <!-- <view class='item' @tap='setHotSearchValue(item.title)'>{{ item.title }}</view>-->
  24. <!-- </block>-->
  25. <!-- </view>-->
  26. <view class="line"></view>
  27. <!-- <goodList :bastList="calculatedProducts" v-if="bastList.length > 0"></goodList> -->
  28. <view class="index-product-wrapper">
  29. <view
  30. class="list-box animated"
  31. :class="bastList.length > 0 ? 'fadeIn on' : ''"
  32. >
  33. <view
  34. class="item"
  35. v-for="(item, index) in calculatedProducts"
  36. :key="index"
  37. @click="goDetail(item)"
  38. >
  39. <view class="pictrue">
  40. <image :src="item.image" mode=""></image>
  41. </view>
  42. <view class="text-info">
  43. <view class="title">
  44. <view class="text line1">{{ item.storeName }}</view>
  45. <view class="weight">{{ item.weight }}g</view>
  46. </view>
  47. <view class="bottom-row">
  48. <!-- <text class="price">工费: {{ item.price }}/克</text> -->
  49. <text class="price">¥ {{ item.totalPrice }}</text>
  50. <text class="sales" style="color: #666">
  51. 销量:{{
  52. Number(item.sales || 0) + Number(item.ficti || 0)
  53. }}件
  54. </text>
  55. <!-- <view class="txt">券</view> -->
  56. </view>
  57. <view class="bottom-row">
  58. <!-- <text class="price">工费: {{ item.price }}/克</text> -->
  59. <text class="sales">工费:¥{{ item.totalLaborCost }}</text>
  60. <text class="sales">
  61. 附加费:¥{{ item.additionalAmount }}
  62. </text>
  63. <!-- <view class="txt">券</view> -->
  64. </view>
  65. <template v-if="item?.merchant?.id && merchantNameShow()">
  66. <view
  67. class="merchantInfo"
  68. @click.stop="toMerchant(item.merchant.id)"
  69. >
  70. <image
  71. class="merchantLogo"
  72. :src="item.merchant.merchantLogo"
  73. mode="scaleToFill"
  74. ></image>
  75. <text class="merchantName">{{
  76. item.merchant.merchantName
  77. }}</text>
  78. <uni-icons
  79. style="margin-left: 10rpx"
  80. type="right"
  81. size="16"
  82. color="#999999"
  83. ></uni-icons>
  84. </view>
  85. </template>
  86. </view>
  87. </view>
  88. </view>
  89. </view>
  90. <view
  91. class="loadingicon acea-row row-center-wrapper"
  92. v-if="bastList.length > 0"
  93. >
  94. <text
  95. class="loading iconfont icon-jiazai"
  96. :hidden="loading == false"
  97. ></text
  98. >{{ loadTitle }}
  99. </view>
  100. </view>
  101. <view class="noCommodity">
  102. <view class="pictrue" v-if="bastList.length == 0 && isbastList">
  103. <image :src="HTTP_REQUEST_URL_IMG + 'noSearch.png'"></image>
  104. </view>
  105. <!-- <recommend :hostProduct='hostProduct' v-if="bastList.length == 0"></recommend>-->
  106. </view>
  107. </view>
  108. </template>
  109. <script setup>
  110. import { computed, ref } from "vue";
  111. import { onShow, onReachBottom, onLoad } from "@dcloudio/uni-app";
  112. import {
  113. getSearchKeyword,
  114. getProductslist,
  115. getProductHot,
  116. } from "@/api/store.js";
  117. import goodList from "@/components/goodList";
  118. import recommend from "@/components/recommend";
  119. import { HTTP_REQUEST_URL_IMG } from "@/config/app";
  120. // 获取实物金价
  121. import useRealGoldPrice from "@/hooks/useRealGoldPrice";
  122. import { useAppStore } from "@/stores/app";
  123. const appStore = useAppStore();
  124. // 响应式数据
  125. const hostProduct = ref([]);
  126. const searchValue = ref("");
  127. const focus = ref(true);
  128. const bastList = ref([]);
  129. const hotSearchList = ref([]);
  130. const limit = ref(8);
  131. const page = ref(1);
  132. const loading = ref(false);
  133. const loadend = ref(false);
  134. const loadTitle = ref("加载更多");
  135. const hotPage = ref(1);
  136. const isScroll = ref(true);
  137. const isbastList = ref(false);
  138. const query = ref({});
  139. const merchantId = ref("");
  140. // 实时价格处理
  141. const {
  142. realGoldprice, // 黄金实时销售价(基础)
  143. realPtprice, // 铂金实时销售价(基础)
  144. realAgprice, // 白银实时销售价(基础)
  145. } = useRealGoldPrice({});
  146. // Navigation to detail page
  147. const goDetail = async (item) => {
  148. try {
  149. uni.navigateTo({
  150. url: `/pages/goods/goods_details/index?id=${item.id}`,
  151. });
  152. } catch (err) {
  153. console.error("Navigation error:", err);
  154. }
  155. };
  156. const merchantNameShow = () => {
  157. if (
  158. !appStore.userInfo ||
  159. (appStore.userInfo &&
  160. !appStore.userInfo.merchant &&
  161. !appStore.merchantId &&
  162. !appStore.userInfo.lastVisitedMerchantId)
  163. ) {
  164. return true;
  165. } else {
  166. return false;
  167. }
  168. };
  169. const toMerchant = (merchantId) => {
  170. uni.navigateTo({
  171. url: "/pages/merchantCenters/merchant?merchantId=" + merchantId,
  172. });
  173. };
  174. const calculatedProducts = computed(() => {
  175. // 计算逻辑与原代码一致,但基于响应式数据 products
  176. return bastList.value.map((product) => {
  177. // 1. 将price字符串转为数字
  178. const price = Number(product.price);
  179. // 2. 计算乘积
  180. const total = (price + product.sales) * realGoldprice.value;
  181. // 3. 向上取整到两位小数(先放大100倍取整,再缩小100倍)
  182. const roundedTotal = Math.ceil(total * 100) / 100;
  183. // 4. 格式化保留两位小数
  184. const formattedTotal = roundedTotal.toFixed(2);
  185. const totalLaborCost = Number(product.totalLaborCost);
  186. const additionalAmount = Number(product.additionalAmount);
  187. const totalPrice = (totalLaborCost + additionalAmount).toFixed(2);
  188. return {
  189. ...product,
  190. calculatedTotal: formattedTotal, // 新增计算结果字段
  191. totalPrice,
  192. };
  193. });
  194. });
  195. // 获取热搜
  196. function getRoutineHotSearch() {
  197. getSearchKeyword().then((res) => {
  198. hotSearchList.value = res.data;
  199. });
  200. }
  201. // 获取商品列表
  202. function getProductList() {
  203. if (loadend.value || loading.value) return;
  204. loading.value = true;
  205. loadTitle.value = "";
  206. getProductslist({
  207. ...query.value,
  208. keyword: searchValue.value,
  209. page: page.value,
  210. limit: limit.value,
  211. merchantId: merchantId.value,
  212. })
  213. .then((res) => {
  214. const list = res.data.list;
  215. const isLoadend = list.length < limit.value;
  216. // 合并数组
  217. bastList.value = (bastList.value || []).concat(list);
  218. loading.value = false;
  219. loadend.value = isLoadend;
  220. loadTitle.value = isLoadend ? "我是有底线的" : "加载更多";
  221. page.value += 1;
  222. isbastList.value = true;
  223. })
  224. .catch(() => {
  225. loading.value = false;
  226. loadTitle.value = "加载更多";
  227. });
  228. }
  229. // 获取热门商品
  230. function getHostProduct() {
  231. if (!isScroll.value) return;
  232. getProductHot(hotPage.value, limit.value).then((res) => {
  233. isScroll.value = res.data.list.length >= limit.value;
  234. hostProduct.value = hostProduct.value.concat(res.data.list);
  235. hotPage.value += 1;
  236. });
  237. }
  238. // 设置热搜值
  239. function setHotSearchValue(val) {
  240. searchValue.value = val;
  241. page.value = 1;
  242. loadend.value = false;
  243. bastList.value = [];
  244. getProductList();
  245. }
  246. // 输入框赋值
  247. function setValue(event) {
  248. searchValue.value = event.detail.value;
  249. }
  250. // 搜索按钮
  251. function searchBut() {
  252. focus.value = false;
  253. if (searchValue.value.length > 0) {
  254. page.value = 1;
  255. loadend.value = false;
  256. bastList.value = [];
  257. uni.showLoading({ title: "正在搜索中" });
  258. getProductList();
  259. uni.hideLoading();
  260. } else {
  261. // 这里假设 $util.Tips 已全局挂载
  262. uni.$u.toast("请输入要搜索的商品");
  263. }
  264. }
  265. // 生命周期
  266. onShow(() => {
  267. getRoutineHotSearch();
  268. getHostProduct();
  269. });
  270. onLoad((options) => {
  271. query.value = options || {};
  272. merchantId.value =
  273. query.value.merchantId ||
  274. appStore.merchantId ||
  275. appStore.userInfo?.merchant?.id ||
  276. "";
  277. if (options && options.cid) {
  278. getProductList();
  279. }
  280. });
  281. onReachBottom(() => {
  282. if (bastList.value.length > 0) {
  283. getProductList();
  284. } else {
  285. getHostProduct();
  286. }
  287. });
  288. </script>
  289. <style lang="scss">
  290. page {
  291. // margin-top: var(--status-bar-height);
  292. background-color: #f9f7f0;
  293. height: 100%;
  294. }
  295. .searchGood .search {
  296. padding-left: 30rpx;
  297. background-color: #fff !important;
  298. }
  299. .searchGood .search {
  300. padding: 16rpx;
  301. }
  302. .searchGood .search .input {
  303. width: 598rpx;
  304. background-color: #f9f7f0;
  305. border-radius: 16rpx;
  306. padding: 0 35rpx;
  307. box-sizing: border-box;
  308. height: 66rpx;
  309. }
  310. .searchGood .search .input input {
  311. width: 472rpx;
  312. font-size: 26rpx;
  313. }
  314. .searchGood .search .input .placeholder {
  315. color: #bbb;
  316. }
  317. .searchGood .search .input .iconfont {
  318. color: #000;
  319. font-size: 35rpx;
  320. }
  321. .searchGood .search .bnt {
  322. width: 120rpx;
  323. text-align: center;
  324. height: 66rpx;
  325. line-height: 66rpx;
  326. font-size: 30rpx;
  327. color: #282828;
  328. }
  329. .searchGood .list {
  330. padding-left: 10rpx;
  331. }
  332. .searchGood .list .item {
  333. font-size: 26rpx;
  334. color: #454545;
  335. padding: 0 21rpx;
  336. height: 60rpx;
  337. border-radius: 30rpx;
  338. line-height: 60rpx;
  339. border: 1rpx solid #aaa;
  340. margin: 0 0 20rpx 20rpx;
  341. }
  342. .searchGood .line {
  343. // border-bottom: 1rpx solid #eee;
  344. margin: 20rpx 30rpx 0 30rpx;
  345. }
  346. .index-product-wrapper {
  347. padding: 0 16rpx;
  348. margin-bottom: 72rpx; /* 为自定义 tabBar 留出空间 */
  349. // min-height: 700rpx;
  350. //background: #fff;
  351. &.on {
  352. min-height: 1500rpx;
  353. }
  354. .list-box {
  355. display: flex;
  356. flex-wrap: wrap;
  357. justify-content: space-between;
  358. .item {
  359. width: 48.99%;
  360. //height: 490rpx;
  361. background-color: #ffffff;
  362. // box-shadow: 0rpx 3rpx 13rpx 0rpx rgba(0, 0, 0, 0.13);
  363. border-radius: 16rpx;
  364. margin-bottom: 16rpx;
  365. overflow: hidden;
  366. display: flex;
  367. flex-direction: column;
  368. justify-content: space-between;
  369. .pictrue {
  370. position: relative;
  371. image {
  372. width: 100%;
  373. height: 330rpx;
  374. }
  375. }
  376. .text-info {
  377. padding: 16rpx 8rpx;
  378. .title {
  379. color: #333;
  380. display: flex;
  381. align-items: center;
  382. justify-content: space-between;
  383. .tip {
  384. width: 61rpx;
  385. height: auto;
  386. font-size: 22rpx;
  387. display: flex;
  388. justify-content: center;
  389. align-items: center;
  390. color: #ffffff;
  391. position: relative;
  392. margin-right: 5rpx;
  393. background-color: #ef4800;
  394. border-radius: 5rpx;
  395. }
  396. .text {
  397. width: 80%;
  398. font-weight: bold;
  399. }
  400. .weight {
  401. background-color: rgba(197, 128, 3, 0.1);
  402. color: #c58003;
  403. font-size: 24rpx;
  404. padding: 8rpx 16rpx;
  405. border-radius: 8rpx;
  406. float: right;
  407. }
  408. }
  409. .bottom-row {
  410. color: $theme-color;
  411. display: flex;
  412. justify-content: space-between;
  413. align-items: center;
  414. font-size: 28rpx;
  415. margin: 10rpx 0 0;
  416. .price {
  417. padding-bottom: 4rpx;
  418. font-weight: 800;
  419. white-space: nowrap;
  420. font-size: 28rpx;
  421. color: #f16327;
  422. }
  423. .sales {
  424. font-size: 22rpx;
  425. white-space: nowrap;
  426. flex-shrink: 0;
  427. color: #999999;
  428. }
  429. }
  430. .merchantInfo {
  431. display: flex;
  432. align-items: center;
  433. height: 40rpx;
  434. margin-top: 16rpx;
  435. }
  436. .merchantLogo {
  437. width: 40rpx;
  438. height: 40rpx;
  439. border-radius: 50%;
  440. margin-right: 8rpx;
  441. }
  442. .merchantName {
  443. font-size: 24rpx;
  444. color: #333;
  445. }
  446. }
  447. }
  448. &.on {
  449. display: flex;
  450. }
  451. }
  452. }
  453. </style>