goods-list.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. <template>
  2. <view :class="!empty ? 'padding-top-big' : '' ">
  3. <!-- 空白页 -->
  4. <use-empty v-if="empty" e-style="round" e-type="search" tip="搜索数据为空" btn-tip="重新搜索" height="70vh" :auto="false"
  5. @goto="tosearch"></use-empty>
  6. <!-- 列表区 -->
  7. <view v-else>
  8. <!-- 筛选区 -->
  9. <view class="navbar pos-f w-full dflex bg-main" :style="{ position: headerPosition }">
  10. <view class="nav-item dflex-c flex1 pos-r h-full" :class="{active: filterIndex === 0}"
  11. @click="navbarClick(0)">
  12. 综合排序
  13. </view>
  14. <!-- <view class="nav-item dflex-c flex1 pos-r h-full" :class="{active: filterIndex === 1}"
  15. @click="navbarClick(1)">
  16. 销量优先
  17. </view> -->
  18. <view class="nav-item dflex-c flex1 pos-r h-full" :class="{active: filterIndex === 2}"
  19. @click="navbarClick(2)">
  20. <text>价格</text>
  21. <view class="">
  22. <view class="iconfont iconjiantou02 ft-dark dflex-c"
  23. :class="{active: priceOrder === 1 && filterIndex === 2}"></view>
  24. <view class="iconfont iconjiantou ft-dark dflex-c"
  25. :class="{active: priceOrder === 2 && filterIndex === 2}"></view>
  26. </view>
  27. </view>
  28. </view>
  29. <!-- 商品列表区 -->
  30. <view class="goods-list">
  31. <view class="list dflex-b dflex dflex-wrap-w w-full" style="padding-top: 60px;">
  32. <view class="item bg-main border-radius-sm padding-bottom-sm" v-for="(item, index) in goodsDatas"
  33. :key="index" @click="togoods(item)">
  34. <view class="image-wrapper">
  35. <image mode="aspectFill" :lazy-load="true" v-if="((item.imgs).indexOf(',')) != -1"
  36. :src="((item.imgs).substring(0, ((item.imgs).indexOf(','))))"></image>
  37. <image mode="aspectFill" :lazy-load="true" v-else
  38. :src="item.imgs"></image>
  39. </view>
  40. <text class="title clamp padding-sm">{{ item.name }}</text>
  41. <view class="padding-left-sm dflex-b">
  42. <text class="price">{{ item.price }}</text>
  43. <text class="ft-dark margin-right-sm fs-xs">
  44. <template v-if="item.state=='1'">已售 {{item.saleCnt }}</template>
  45. <template v-if="item.state=='0'">已下架</template>
  46. <template v-if="item.state=='2'">审核中</template>
  47. </text>
  48. </view>
  49. </view>
  50. </view>
  51. </view>
  52. <!-- 上拉加载更多 -->
  53. <use-loadmore :type="loadmoreType"></use-loadmore>
  54. </view>
  55. <!-- 置顶 -->
  56. <use-totop ref="usetop"></use-totop>
  57. <!-- 03. 猜你想要 -->
  58. <use-hot-goods v-if="empty" title-type="round" title="猜你想要"></use-hot-goods>
  59. </view>
  60. </template>
  61. <script>
  62. import {
  63. searchadd
  64. } from '../../utils/api_goods.js'
  65. import {
  66. goodslistlimit
  67. } from '../../utils/api_home.js'
  68. import useEmpty from '../../components/use-empty/use-empty.vue'
  69. import useLoadmore from '../../components/use-loadmore/use-loadmore.vue'
  70. import useTotop from '../../components/use-totop/use-totop.vue'
  71. import useHotGoods from '../../components/use-hot-goods/use-hot-goods.vue'
  72. export default {
  73. components:{
  74. useEmpty,
  75. useLoadmore,
  76. useTotop,
  77. useHotGoods
  78. },
  79. data() {
  80. return {
  81. empty: false,
  82. headerPosition: "fixed",
  83. // 0综合排序 1销量优先 2价格排序
  84. filterIndex: 0,
  85. // 1价格从低到高 2价格从高到低
  86. priceOrder: 0,
  87. // 商品数据
  88. goodsDatas: [],
  89. // 加载更多状态
  90. loadmoreType: 'more',
  91. // 请求数据
  92. reqdata: {
  93. page: 1,
  94. rows: 8,
  95. sidx: 'sort',
  96. sord: 'asc'
  97. },
  98. scrollTop: 0,
  99. pan:0,//0是搜索,1是热门,2是限时
  100. leiId:'',//类别id
  101. leiLevel:'',//类别level
  102. keyword:'',//关键字
  103. now_date:'',//当前时间
  104. };
  105. },
  106. watch: {
  107. // 显示空白页
  108. goodsDatas(e) {
  109. let empty = e.length === 0;
  110. if (this.empty !== empty) {
  111. this.empty = empty;
  112. }
  113. }
  114. },
  115. onPageScroll(e) {
  116. // 兼容iOS端下拉时顶部漂移
  117. if (e.scrollTop >= 0) {
  118. this.headerPosition = "fixed";
  119. } else {
  120. this.headerPosition = "absolute";
  121. }
  122. // this.scrollTop = e.scrollTop
  123. this.$refs.usetop.change(e.scrollTop);
  124. },
  125. //下拉刷新
  126. onPullDownRefresh() {
  127. this.loadData('refresh');
  128. },
  129. //加载更多
  130. onReachBottom() {
  131. this.loadData();
  132. },
  133. onLoad(options) {
  134. this.getNowDate()
  135. var userId=options.userId || ''
  136. var searchCnt=options.searchCnt || 1
  137. var createBy=options.createBy || ''
  138. var createTime=decodeURIComponent(options.createTime) || this.now_date
  139. this.leiId=options.cid || ''
  140. this.leiLevel=options.level || ''
  141. this.keyword=decodeURIComponent(options.keyword) || ''
  142. let title = '搜索列表';
  143. if (options && options.hot) {
  144. title = '热门推荐';
  145. this.pan=1
  146. } else if (options && options.limited) {
  147. title = '限时精选';
  148. this.pan=2
  149. }
  150. uni.setNavigationBarTitle({
  151. title: title
  152. })
  153. for (let key in options) {
  154. this.reqdata[key] = decodeURIComponent(options[key]);
  155. }
  156. var data={
  157. "id": 0,
  158. "userId": '',
  159. "keyword": this.keyword,
  160. "searchCnt": searchCnt,
  161. "createBy": createBy,
  162. "createTime": this.now_date,
  163. "updateTime": this.now_date,
  164. "updateBy": ""
  165. }
  166. var headers={
  167. 'Content-Type': 'application/json; charset=utf-8',
  168. }
  169. //新增历史搜索
  170. searchadd(data,headers).then((res) => {
  171. if(res.success){
  172. }
  173. })
  174. this.loadData();
  175. },
  176. methods: {
  177. //获取当前时间
  178. getNowDate() {
  179. var _this = this;
  180. // this.timer = setInterval(function() {
  181. var aData = new Date();
  182. var month = aData.getMonth() < 9 ? "0" + (aData.getMonth() + 1) : aData.getMonth() + 1;
  183. var date = aData.getDate() <= 9 ? "0" + aData.getDate() : aData.getDate();
  184. var date2 = aData.getDate() <= 9 ? "0" + (aData.getDate()-1) : (aData.getDate()-1);
  185. var Hour = aData.getHours() <= 9 ? "0" + (aData.getHours()) : aData.getHours();
  186. var Miunte = aData.getMinutes() <= 9 ? "0" + (aData.getMinutes()) : aData.getMinutes();
  187. var Seconds = aData.getSeconds() <= 9 ? "0" + (aData.getSeconds()) : aData.getSeconds();
  188. // console.log(aData.getTime())
  189. _this.now_date = aData.getFullYear() + "-" + month + "-" + date + ' '+ Hour +":"+ Miunte +":"+ Seconds;
  190. // console.log(aData.getFullYear() + "-" + month + "-" + date2)昨天
  191. // }, 86400000);
  192. },
  193. // 加载商品,下拉刷新|上拉加载
  194. loadData(type = 'add', loading) {
  195. if (this.loadmoreType === 'loading') {
  196. // 防止重复加载
  197. return;
  198. }
  199. if (loading == 1 || type == 'refresh') {
  200. // 从首页开始加载
  201. this.reqdata.page = 1;
  202. }
  203. // 没有更多直接返回
  204. if (type === 'add') {
  205. if (this.loadmoreType === 'nomore') {
  206. return;
  207. }
  208. // 加载中
  209. this.loadmoreType = 'loading';
  210. } else {
  211. // 更多
  212. this.loadmoreType = 'more'
  213. }
  214. if (this.pan==2){
  215. //限时精选
  216. let _self = this;
  217. _self.goodsDatas=[]
  218. var data='?limited=1&orderField='+_self.reqdata.sidx+'&order='+_self.reqdata.sord
  219. goodslistlimit(data).then((res) => {
  220. if (res.success) {
  221. var total=res.data.totalCount
  222. data='?limited=1&pageSize='+total+'&orderField='+_self.reqdata.sidx+'&order='+_self.reqdata.sord
  223. goodslistlimit(data).then((res) => {
  224. if (res.success) {
  225. res.data.list.forEach(data => {
  226. _self.goodsDatas.push(data)
  227. })
  228. }else{
  229. _self.$message.warning('没有符合条件的数据!')
  230. }
  231. })
  232. }
  233. })
  234. }else if (this.pan==1){
  235. let _self = this;
  236. _self.goodsDatas=[]
  237. var data='?hot=1'+'&orderField='+_self.reqdata.sidx+'&order='+_self.reqdata.sord
  238. // 热门推荐
  239. goodslistlimit(data).then((res) => {
  240. if (res.success) {
  241. var total=res.data.totalCount
  242. data='?hot=1&pageSize='+total+'&orderField='+_self.reqdata.sidx+'&order='+_self.reqdata.sord
  243. goodslistlimit(data).then((res) => {
  244. if (res.success) {
  245. res.data.list.forEach(data => {
  246. _self.goodsDatas.push(data)
  247. })
  248. }else{
  249. _self.$message.warning('没有符合条件的数据!')
  250. }
  251. })
  252. }
  253. })
  254. }
  255. let _self = this;
  256. //根据类别
  257. var data='?categoryId='+_self.leiId+'&categoryLevel='+_self.leiLevel
  258. +'&curPage='+_self.reqdata.page+'&pageSize='+_self.reqdata.rows
  259. +'&goodsName='+_self.keyword+'&orderField='+_self.reqdata.sidx+'&order='+_self.reqdata.sord
  260. goodslistlimit(data).then((res) => {
  261. if (res.success) {
  262. if (res.data) {
  263. if (loading == 1 || type == 'refresh') {
  264. _self.goodsDatas = [];
  265. }
  266. let _datas = [];
  267. res.data.list.forEach((row) => {
  268. if(row.state === '1'){
  269. _datas.push(row);
  270. }
  271. });
  272. _self.goodsDatas = [..._self.goodsDatas, ..._datas];
  273. if (res.data.list.length >= _self.reqdata.rows) {
  274. _self.reqdata.page++;
  275. _self.loadmoreType = 'more'
  276. } else {
  277. _self.loadmoreType = 'nomore'
  278. }
  279. } else {
  280. _self.loadmoreType = 'nomore'
  281. }
  282. }
  283. if (_self.goodsDatas.length === 0) {
  284. _self.empty = true;
  285. }
  286. if (loading == 1) {
  287. uni.hideLoading()
  288. } else if (type == 'refresh') {
  289. uni.stopPullDownRefresh();
  290. }
  291. })
  292. },
  293. // 点击筛选
  294. navbarClick(index) {
  295. //
  296. if (this.filterIndex === index && index !== 2) {
  297. return;
  298. }
  299. this.filterIndex = index;
  300. if (index === 2) {
  301. this.priceOrder = this.priceOrder === 1 ? 2 : 1;
  302. } else {
  303. this.priceOrder = 0;
  304. }
  305. if (this.filterIndex == 0) {
  306. // 综合排序
  307. this.reqdata.sidx = 'sort';
  308. this.reqdata.sord = 'asc';
  309. } else if (this.filterIndex == 1) {
  310. // 销量优先
  311. this.reqdata.sidx = 'saleCnt';
  312. this.reqdata.sord = 'desc';
  313. } else if (this.filterIndex == 2) {
  314. // 价格排序
  315. this.reqdata.sidx = 'price';
  316. if (this.priceOrder == 1) {
  317. // 降序
  318. this.reqdata.sord = 'desc';
  319. } else if (this.priceOrder == 2) {
  320. // 升序
  321. this.reqdata.sord = 'asc';
  322. }
  323. }
  324. uni.pageScrollTo({
  325. duration: 300,
  326. scrollTop: 0
  327. })
  328. this.loadData('refresh', 1);
  329. uni.showLoading({
  330. title: '正在加载'
  331. })
  332. },
  333. // 搜索页
  334. tosearch() {
  335. if (this.$api.pages().length > 1) {
  336. uni.navigateBack();
  337. return;
  338. }
  339. this.$api.tosearch();
  340. },
  341. // 商品详情
  342. togoods(options) {
  343. this.$api.togoods({
  344. id: options.id
  345. });
  346. }
  347. },
  348. }
  349. </script>
  350. <style lang="scss">
  351. page {
  352. background-color: $page-color-base;
  353. }
  354. .navbar {
  355. top: var(--window-top);
  356. left: 0;
  357. height: 100rpx;
  358. box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, .06);
  359. z-index: 10;
  360. .nav-item {
  361. font-size: 30rpx;
  362. &.active {
  363. &:after {
  364. content: '';
  365. position: absolute;
  366. left: 50%;
  367. bottom: 0;
  368. transform: translateX(-50%);
  369. width: 120rpx;
  370. height: 0;
  371. border-bottom: 4rpx solid $base-color;
  372. }
  373. }
  374. }
  375. .iconfont {
  376. width: 30rpx;
  377. height: 14rpx;
  378. font-size: 20rpx;
  379. line-height: 1;
  380. margin-left: 4rpx;
  381. }
  382. }
  383. .goods-list {
  384. .list {
  385. padding: 0 3vw 20rpx;
  386. }
  387. .item {
  388. width: 46vw;
  389. overflow: hidden;
  390. margin-top: 2vw;
  391. &:nth-child(2n) {
  392. margin-left: 1vw;
  393. }
  394. &:nth-child(2n + 1) {
  395. margin-right: 1vw;
  396. }
  397. }
  398. .image-wrapper {
  399. width: 100%;
  400. height: 300rpx;
  401. overflow: hidden;
  402. image {
  403. width: 100%;
  404. height: 100%;
  405. opacity: 1;
  406. }
  407. }
  408. }
  409. </style>