goods-list.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 useTotop from '../../components/use-totop/use-totop.vue'
  70. import useHotGoods from '../../components/use-hot-goods/use-hot-goods.vue'
  71. export default {
  72. components:{
  73. useEmpty,
  74. useTotop,
  75. useHotGoods
  76. },
  77. data() {
  78. return {
  79. empty: false,
  80. headerPosition: "fixed",
  81. // 0综合排序 1销量优先 2价格排序
  82. filterIndex: 0,
  83. // 1价格从低到高 2价格从高到低
  84. priceOrder: 0,
  85. // 商品数据
  86. goodsDatas: [],
  87. // 加载更多状态
  88. loadmoreType: 'more',
  89. // 请求数据
  90. reqdata: {
  91. page: 1,
  92. rows: 8,
  93. sidx: 'sort',
  94. sord: 'asc'
  95. },
  96. scrollTop: 0,
  97. pan:0,//0是搜索,1是热门,2是限时
  98. leiId:'',//类别id
  99. leiLevel:'',//类别level
  100. keyword:'',//关键字
  101. now_date:'',//当前时间
  102. };
  103. },
  104. watch: {
  105. // 显示空白页
  106. goodsDatas(e) {
  107. let empty = e.length === 0;
  108. if (this.empty !== empty) {
  109. this.empty = empty;
  110. }
  111. }
  112. },
  113. onPageScroll(e) {
  114. // 兼容iOS端下拉时顶部漂移
  115. if (e.scrollTop >= 0) {
  116. this.headerPosition = "fixed";
  117. } else {
  118. this.headerPosition = "absolute";
  119. }
  120. // this.scrollTop = e.scrollTop
  121. this.$refs.usetop.change(e.scrollTop);
  122. },
  123. //下拉刷新
  124. onPullDownRefresh() {
  125. this.loadData('refresh');
  126. },
  127. //加载更多
  128. onReachBottom() {
  129. this.loadData();
  130. },
  131. onLoad(options) {
  132. console.log(options,decodeURIComponent(options.keyword),'pop')
  133. this.getNowDate()
  134. var userId=options.userId || ''
  135. var searchCnt=options.searchCnt || 1
  136. var createBy=options.createBy || ''
  137. var createTime=decodeURIComponent(options.createTime) || this.now_date
  138. this.leiId=options.cid || ''
  139. this.leiLevel=options.level || ''
  140. this.keyword=decodeURIComponent(options.keyword) || ''
  141. let title = '搜索列表';
  142. if (options && options.hot) {
  143. title = '热门推荐';
  144. this.pan=1
  145. } else if (options && options.limited) {
  146. title = '限时精选';
  147. this.pan=2
  148. }
  149. uni.setNavigationBarTitle({
  150. title: title
  151. })
  152. for (let key in options) {
  153. this.reqdata[key] = decodeURIComponent(options[key]);
  154. }
  155. var data={
  156. "id": 0,
  157. "userId": '',
  158. "keyword": this.keyword,
  159. "searchCnt": searchCnt,
  160. "createBy": createBy,
  161. "createTime": this.now_date,
  162. "updateTime": this.now_date,
  163. "updateBy": ""
  164. }
  165. var headers={
  166. 'Content-Type': 'application/json; charset=utf-8',
  167. }
  168. //新增历史搜索
  169. searchadd(data,headers).then((res) => {
  170. if(res.success){
  171. }
  172. })
  173. this.loadData();
  174. },
  175. methods: {
  176. //获取当前时间
  177. getNowDate() {
  178. var _this = this;
  179. // this.timer = setInterval(function() {
  180. var aData = new Date();
  181. var month = aData.getMonth() < 9 ? "0" + (aData.getMonth() + 1) : aData.getMonth() + 1;
  182. var date = aData.getDate() <= 9 ? "0" + aData.getDate() : aData.getDate();
  183. var date2 = aData.getDate() <= 9 ? "0" + (aData.getDate()-1) : (aData.getDate()-1);
  184. var Hour = aData.getHours() <= 9 ? "0" + (aData.getHours()) : aData.getHours();
  185. var Miunte = aData.getMinutes() <= 9 ? "0" + (aData.getMinutes()) : aData.getMinutes();
  186. var Seconds = aData.getSeconds() <= 9 ? "0" + (aData.getSeconds()) : aData.getSeconds();
  187. // console.log(aData.getTime())
  188. _this.now_date = aData.getFullYear() + "-" + month + "-" + date + ' '+ Hour +":"+ Miunte +":"+ Seconds;
  189. // console.log(aData.getFullYear() + "-" + month + "-" + date2)昨天
  190. // }, 86400000);
  191. },
  192. // 加载商品,下拉刷新|上拉加载
  193. loadData(type = 'add', loading) {
  194. if (this.loadmoreType === 'loading') {
  195. // 防止重复加载
  196. return;
  197. }
  198. if (loading == 1 || type == 'refresh') {
  199. // 从首页开始加载
  200. this.reqdata.page = 1;
  201. }
  202. // 没有更多直接返回
  203. if (type === 'add') {
  204. if (this.loadmoreType === 'nomore') {
  205. return;
  206. }
  207. // 加载中
  208. // this.loadmoreType = 'loading';
  209. } else {
  210. // 更多
  211. this.loadmoreType = 'more'
  212. }
  213. if (this.pan==2){
  214. //限时精选
  215. let _self = this;
  216. _self.goodsDatas=[]
  217. var data='?limited=1&state=1&orderField='+_self.reqdata.sidx+'&order='+_self.reqdata.sord
  218. goodslistlimit(data).then((res) => {
  219. if (res.success) {
  220. var total=res.data.totalCount
  221. data='?limited=1&state=1&pageSize='+total+'&curPage='+_self.reqdata.page+'&orderField='+_self.reqdata.sidx+'&order='+_self.reqdata.sord
  222. goodslistlimit(data).then((res) => {
  223. if (res.success) {
  224. res.data.list.forEach(data => {
  225. _self.goodsDatas.push(data)
  226. })
  227. }else{
  228. _self.$message.warning('没有符合条件的数据!')
  229. }
  230. })
  231. }
  232. })
  233. }else if (this.pan==1){
  234. let _self = this;
  235. _self.goodsDatas=[]
  236. var data='?hot=1'+'&state=1&orderField='+_self.reqdata.sidx+'&order='+_self.reqdata.sord
  237. // 热门推荐
  238. goodslistlimit(data).then((res) => {
  239. if (res.success) {
  240. var total=res.data.totalCount
  241. data='?hot=1&state=1&pageSize='+total+'&curPage='+_self.reqdata.page+'&orderField='+_self.reqdata.sidx+'&order='+_self.reqdata.sord
  242. goodslistlimit(data).then((res) => {
  243. if (res.success) {
  244. res.data.list.forEach(data => {
  245. _self.goodsDatas.push(data)
  246. })
  247. }else{
  248. _self.$message.warning('没有符合条件的数据!')
  249. }
  250. })
  251. }
  252. })
  253. }else{
  254. let _self = this;
  255. //根据类别
  256. var data='?categoryId='+_self.leiId+'&state=1&categoryLevel='+_self.leiLevel
  257. +'&curPage='+_self.reqdata.page+'&pageSize='+_self.reqdata.rows
  258. +'&orderField='+_self.reqdata.sidx+'&order='+_self.reqdata.sord+'&goodsName='+_self.keyword
  259. goodslistlimit(data).then((res) => {
  260. if (res.success) {
  261. if (res.data) {
  262. if (loading == 1 || type == 'refresh') {
  263. _self.goodsDatas = [];
  264. }
  265. let _datas = [];
  266. res.data.list.forEach((row) => {
  267. if(row.state === '1'){
  268. _datas.push(row);
  269. }
  270. });
  271. _self.goodsDatas = [..._self.goodsDatas, ..._datas];
  272. if (res.data.list.length >= _self.reqdata.rows) {
  273. _self.reqdata.page++;
  274. _self.loadmoreType = 'more'
  275. } else {
  276. _self.loadmoreType = 'nomore'
  277. }
  278. } else {
  279. _self.loadmoreType = 'nomore'
  280. }
  281. }
  282. if (_self.goodsDatas.length === 0) {
  283. _self.empty = true;
  284. }
  285. if (loading == 1) {
  286. uni.hideLoading()
  287. } else if (type == 'refresh') {
  288. uni.stopPullDownRefresh();
  289. }
  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. @import url('/packageShang/components/iconfont/iconfont.css');
  352. @import url('/packageShang/common/common.scss');
  353. page {
  354. background-color: $page-color-base;
  355. }
  356. .navbar {
  357. top: var(--window-top);
  358. left: 0;
  359. height: 100rpx;
  360. box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, .06);
  361. z-index: 10;
  362. .nav-item {
  363. font-size: 30rpx;
  364. &.active {
  365. &:after {
  366. content: '';
  367. position: absolute;
  368. left: 50%;
  369. bottom: 0;
  370. transform: translateX(-50%);
  371. width: 120rpx;
  372. height: 0;
  373. border-bottom: 4rpx solid $base-color;
  374. }
  375. }
  376. }
  377. .iconfont {
  378. width: 30rpx;
  379. height: 14rpx;
  380. font-size: 20rpx;
  381. line-height: 1;
  382. margin-left: 4rpx;
  383. }
  384. }
  385. .goods-list {
  386. .list {
  387. padding: 0 3vw 20rpx;
  388. }
  389. .item {
  390. width: 46vw;
  391. overflow: hidden;
  392. margin-top: 2vw;
  393. &:nth-child(2n) {
  394. margin-left: 1vw;
  395. }
  396. &:nth-child(2n + 1) {
  397. margin-right: 1vw;
  398. }
  399. }
  400. .image-wrapper {
  401. width: 100%;
  402. height: 300rpx;
  403. overflow: hidden;
  404. image {
  405. width: 100%;
  406. height: 100%;
  407. opacity: 1;
  408. }
  409. }
  410. }
  411. </style>