index.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <template>
  2. <view>
  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'></text>
  7. <input type='text' confirm-type="search" :value='searchValue' :focus="focus" placeholder='点击搜索商品' placeholder-class='placeholder'
  8. @input="setValue" @confirm="searchBut" />
  9. </view>
  10. <view class='bnt' @tap='searchBut'>搜索</view>
  11. </view>
  12. <!-- <view class='title'>热门搜索</view>-->
  13. <!-- <view class='list acea-row'>-->
  14. <!-- <block v-for="(item, index) in hotSearchList" :key="index">-->
  15. <!-- <view class='item' @tap='setHotSearchValue(item.title)'>{{ item.title }}</view>-->
  16. <!-- </block>-->
  17. <!-- </view>-->
  18. <view class='line'></view>
  19. <goodList :bastList="calculatedProducts" v-if="bastList.length > 0"></goodList>
  20. <view class='loadingicon acea-row row-center-wrapper' v-if="bastList.length > 0">
  21. <text class='loading iconfont icon-jiazai' :hidden='loading == false'></text>{{ loadTitle }}
  22. </view>
  23. </view>
  24. <view class='noCommodity'>
  25. <view class='pictrue' v-if="bastList.length == 0 && isbastList">
  26. <image :src="HTTP_REQUEST_URL_IMG+'noSearch.png'"></image>
  27. </view>
  28. <!-- <recommend :hostProduct='hostProduct' v-if="bastList.length == 0"></recommend>-->
  29. </view>
  30. </view>
  31. </template>
  32. <script setup>
  33. import {computed, ref} from 'vue'
  34. import { onShow, onReachBottom,onLoad } from '@dcloudio/uni-app'
  35. import { getSearchKeyword, getProductslist, getProductHot } from '@/api/store.js'
  36. import goodList from '@/components/goodList'
  37. import recommend from '@/components/recommend'
  38. import { HTTP_REQUEST_URL_IMG } from "@/config/app";
  39. // 获取实时金价
  40. import useRealGoldPrice from "@/hooks/useRealGoldPrice";
  41. import { useAppStore } from "@/stores/app";
  42. const appStore = useAppStore();
  43. // 响应式数据
  44. const hostProduct = ref([])
  45. const searchValue = ref('')
  46. const focus = ref(true)
  47. const bastList = ref([])
  48. const hotSearchList = ref([])
  49. const limit = ref(8)
  50. const page = ref(1)
  51. const loading = ref(false)
  52. const loadend = ref(false)
  53. const loadTitle = ref('加载更多')
  54. const hotPage = ref(1)
  55. const isScroll = ref(true)
  56. const isbastList = ref(false)
  57. const query = ref({})
  58. const merchantId = ref('')
  59. // 实时价格处理
  60. const {
  61. realGoldprice, // 黄金实时销售价(基础)
  62. realPtprice, // 铂金实时销售价(基础)
  63. realAgprice, // 白银实时销售价(基础)
  64. } = useRealGoldPrice({});
  65. const calculatedProducts = computed(() => {
  66. // 计算逻辑与原代码一致,但基于响应式数据 products
  67. return bastList.value.map((product) => {
  68. // 1. 将price字符串转为数字
  69. const price = Number(product.price);
  70. // 2. 计算乘积
  71. const total = (price + product.sales) * realGoldprice.value;
  72. // 3. 向上取整到两位小数(先放大100倍取整,再缩小100倍)
  73. const roundedTotal = Math.ceil(total * 100) / 100;
  74. // 4. 格式化保留两位小数
  75. const formattedTotal = roundedTotal.toFixed(2);
  76. const totalLaborCost = Number(product.totalLaborCost);
  77. const additionalAmount = Number(product.additionalAmount);
  78. const totalPrice = (totalLaborCost+additionalAmount).toFixed(2);
  79. return {
  80. ...product,
  81. calculatedTotal: formattedTotal, // 新增计算结果字段
  82. totalPrice
  83. };
  84. });
  85. });
  86. // 获取热搜
  87. function getRoutineHotSearch() {
  88. getSearchKeyword().then(res => {
  89. hotSearchList.value = res.data
  90. })
  91. }
  92. // 获取商品列表
  93. function getProductList() {
  94. if (loadend.value || loading.value) return
  95. loading.value = true
  96. loadTitle.value = ''
  97. getProductslist({
  98. ...query.value,
  99. keyword: searchValue.value,
  100. page: page.value,
  101. limit: limit.value,
  102. merchantId:merchantId.value,
  103. }).then(res => {
  104. const list = res.data.list
  105. const isLoadend = list.length < limit.value
  106. // 合并数组
  107. bastList.value = (bastList.value || []).concat(list)
  108. loading.value = false
  109. loadend.value = isLoadend
  110. loadTitle.value = isLoadend ? "我是有底线的" : "加载更多"
  111. page.value += 1
  112. isbastList.value = true
  113. }).catch(() => {
  114. loading.value = false
  115. loadTitle.value = '加载更多'
  116. })
  117. }
  118. // 获取热门商品
  119. function getHostProduct() {
  120. if (!isScroll.value) return
  121. getProductHot(hotPage.value, limit.value).then(res => {
  122. isScroll.value = res.data.list.length >= limit.value
  123. hostProduct.value = hostProduct.value.concat(res.data.list)
  124. hotPage.value += 1
  125. })
  126. }
  127. // 设置热搜值
  128. function setHotSearchValue(val) {
  129. searchValue.value = val
  130. page.value = 1
  131. loadend.value = false
  132. bastList.value = []
  133. getProductList()
  134. }
  135. // 输入框赋值
  136. function setValue(event) {
  137. searchValue.value = event.detail.value;
  138. }
  139. // 搜索按钮
  140. function searchBut() {
  141. focus.value = false
  142. if (searchValue.value.length > 0) {
  143. page.value = 1
  144. loadend.value = false
  145. bastList.value = []
  146. uni.showLoading({ title: '正在搜索中' })
  147. getProductList()
  148. uni.hideLoading()
  149. } else {
  150. // 这里假设 $util.Tips 已全局挂载
  151. uni.$u.toast('请输入要搜索的商品')
  152. }
  153. }
  154. // 生命周期
  155. onShow(() => {
  156. getRoutineHotSearch()
  157. getHostProduct();
  158. })
  159. onLoad((options)=>{
  160. query.value = options || {};
  161. merchantId.value = appStore.merchantId||appStore.userInfo?.merchant?.id||'';
  162. if(options && options.cid){
  163. getProductList()
  164. }
  165. })
  166. onReachBottom(() => {
  167. if (bastList.value.length > 0) {
  168. getProductList()
  169. } else {
  170. getHostProduct()
  171. }
  172. })
  173. </script>
  174. <style lang="scss">
  175. page {
  176. margin-top: var(--status-bar-height);
  177. background-color: #fff !important;
  178. }
  179. .searchGood .search {
  180. padding-left: 30rpx;
  181. background-color: #fff !important;
  182. }
  183. .searchGood .search {
  184. padding-top: 20rpx;
  185. }
  186. .searchGood .search .input {
  187. width: 598rpx;
  188. background-color: #f7f7f7;
  189. border-radius: 16rpx;
  190. padding: 0 35rpx;
  191. box-sizing: border-box;
  192. height: 66rpx;
  193. }
  194. .searchGood .search .input input {
  195. width: 472rpx;
  196. font-size: 26rpx;
  197. }
  198. .searchGood .search .input .placeholder {
  199. color: #bbb;
  200. }
  201. .searchGood .search .input .iconfont {
  202. color: #000;
  203. font-size: 35rpx;
  204. }
  205. .searchGood .search .bnt {
  206. width: 120rpx;
  207. text-align: center;
  208. height: 66rpx;
  209. line-height: 66rpx;
  210. font-size: 30rpx;
  211. color: #282828;
  212. }
  213. .searchGood .title {
  214. font-size: 28rpx;
  215. color: #999;
  216. margin: 50rpx 30rpx 25rpx 30rpx;
  217. }
  218. .searchGood .list {
  219. padding-left: 10rpx;
  220. }
  221. .searchGood .list .item {
  222. font-size: 26rpx;
  223. color: #454545;
  224. padding: 0 21rpx;
  225. height: 60rpx;
  226. border-radius: 30rpx;
  227. line-height: 60rpx;
  228. border: 1rpx solid #aaa;
  229. margin: 0 0 20rpx 20rpx;
  230. }
  231. .searchGood .line {
  232. border-bottom: 1rpx solid #eee;
  233. margin: 20rpx 30rpx 0 30rpx;
  234. }
  235. </style>