index.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. // 响应式数据
  42. const hostProduct = ref([])
  43. const searchValue = ref('')
  44. const focus = ref(true)
  45. const bastList = ref([])
  46. const hotSearchList = ref([])
  47. const limit = ref(8)
  48. const page = ref(1)
  49. const loading = ref(false)
  50. const loadend = ref(false)
  51. const loadTitle = ref('加载更多')
  52. const hotPage = ref(1)
  53. const isScroll = ref(true)
  54. const isbastList = ref(false)
  55. const query = ref({})
  56. // 实时价格处理
  57. const {
  58. realGoldprice, // 黄金实时销售价(基础)
  59. realPtprice, // 铂金实时销售价(基础)
  60. realAgprice, // 白银实时销售价(基础)
  61. } = useRealGoldPrice({});
  62. const calculatedProducts = computed(() => {
  63. // 计算逻辑与原代码一致,但基于响应式数据 products
  64. return bastList.value.map((product) => {
  65. // 1. 将price字符串转为数字
  66. const price = Number(product.price);
  67. // 2. 计算乘积
  68. const total = (price + product.sales) * realGoldprice.value;
  69. // 3. 向上取整到两位小数(先放大100倍取整,再缩小100倍)
  70. const roundedTotal = Math.ceil(total * 100) / 100;
  71. // 4. 格式化保留两位小数
  72. const formattedTotal = roundedTotal.toFixed(2);
  73. const totalLaborCost = Number(product.totalLaborCost);
  74. const additionalAmount = Number(product.additionalAmount);
  75. console.log(totalLaborCost)
  76. console.log(additionalAmount)
  77. const totalPrice = (totalLaborCost+additionalAmount).toFixed(2);
  78. console.log('totalPrice',totalPrice)
  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. }).then(res => {
  103. const list = res.data.list
  104. const isLoadend = list.length < limit.value
  105. // 合并数组
  106. bastList.value = (bastList.value || []).concat(list)
  107. loading.value = false
  108. loadend.value = isLoadend
  109. loadTitle.value = isLoadend ? "我是有底线的" : "加载更多"
  110. page.value += 1
  111. isbastList.value = true
  112. }).catch(() => {
  113. loading.value = false
  114. loadTitle.value = '加载更多'
  115. })
  116. }
  117. // 获取热门商品
  118. function getHostProduct() {
  119. if (!isScroll.value) return
  120. getProductHot(hotPage.value, limit.value).then(res => {
  121. isScroll.value = res.data.list.length >= limit.value
  122. hostProduct.value = hostProduct.value.concat(res.data.list)
  123. hotPage.value += 1
  124. })
  125. }
  126. // 设置热搜值
  127. function setHotSearchValue(val) {
  128. searchValue.value = val
  129. page.value = 1
  130. loadend.value = false
  131. bastList.value = []
  132. getProductList()
  133. }
  134. // 输入框赋值
  135. function setValue(event) {
  136. searchValue.value = event.detail.value;
  137. }
  138. // 搜索按钮
  139. function searchBut() {
  140. focus.value = false
  141. if (searchValue.value.length > 0) {
  142. page.value = 1
  143. loadend.value = false
  144. bastList.value = []
  145. uni.showLoading({ title: '正在搜索中' })
  146. getProductList()
  147. uni.hideLoading()
  148. } else {
  149. // 这里假设 $util.Tips 已全局挂载
  150. uni.$u.toast('请输入要搜索的商品')
  151. }
  152. }
  153. // 生命周期
  154. onShow(() => {
  155. getRoutineHotSearch()
  156. getHostProduct()
  157. })
  158. onLoad((options)=>{
  159. query.value = options || {};
  160. if(options && options.cid){
  161. getProductList()
  162. }
  163. })
  164. onReachBottom(() => {
  165. if (bastList.value.length > 0) {
  166. getProductList()
  167. } else {
  168. getHostProduct()
  169. }
  170. })
  171. </script>
  172. <style lang="scss">
  173. page {
  174. margin-top: var(--status-bar-height);
  175. background-color: #fff !important;
  176. }
  177. .searchGood .search {
  178. padding-left: 30rpx;
  179. background-color: #fff !important;
  180. }
  181. .searchGood .search {
  182. padding-top: 20rpx;
  183. }
  184. .searchGood .search .input {
  185. width: 598rpx;
  186. background-color: #f7f7f7;
  187. border-radius: 16rpx;
  188. padding: 0 35rpx;
  189. box-sizing: border-box;
  190. height: 66rpx;
  191. }
  192. .searchGood .search .input input {
  193. width: 472rpx;
  194. font-size: 26rpx;
  195. }
  196. .searchGood .search .input .placeholder {
  197. color: #bbb;
  198. }
  199. .searchGood .search .input .iconfont {
  200. color: #000;
  201. font-size: 35rpx;
  202. }
  203. .searchGood .search .bnt {
  204. width: 120rpx;
  205. text-align: center;
  206. height: 66rpx;
  207. line-height: 66rpx;
  208. font-size: 30rpx;
  209. color: #282828;
  210. }
  211. .searchGood .title {
  212. font-size: 28rpx;
  213. color: #999;
  214. margin: 50rpx 30rpx 25rpx 30rpx;
  215. }
  216. .searchGood .list {
  217. padding-left: 10rpx;
  218. }
  219. .searchGood .list .item {
  220. font-size: 26rpx;
  221. color: #454545;
  222. padding: 0 21rpx;
  223. height: 60rpx;
  224. border-radius: 30rpx;
  225. line-height: 60rpx;
  226. border: 1rpx solid #aaa;
  227. margin: 0 0 20rpx 20rpx;
  228. }
  229. .searchGood .line {
  230. border-bottom: 1rpx solid #eee;
  231. margin: 20rpx 30rpx 0 30rpx;
  232. }
  233. </style>