index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <template>
  2. <view>
  3. <!-- 轮播图 -->
  4. <view class="banner_box">
  5. <view class="banner_list" v-if="bannerList.length">
  6. <swiper class="banner_swiper" :autoplay="true">
  7. <swiper-item v-for="(item, index) in bannerList" :key="index">
  8. <image :src="item.thumb" class="image" @click="navLottery(item.link_url)"></image>
  9. </swiper-item>
  10. </swiper>
  11. </view>
  12. </view>
  13. <view class="product_box">
  14. <!-- 导航条.tab太多的时候需要使用scrollview -->
  15. <scroll-view class="navBar" scroll-x="true" enable-flex>
  16. <view v-for="(item, index) in tabs" :key="index" class="navItem">
  17. <view class="navItem" :class="requestParam.type_id == item.id ? 'active' : ''" @click="onTabClick(item.id)">
  18. <view class="navItemTitle">{{ item.name }}</view>
  19. </view>
  20. </view>
  21. </scroll-view>
  22. <view class="to_bottom" v-if="!productList.length"> -----还没有产品啦-----</view>
  23. <!-- 产品列表 -->
  24. <view class="product_list">
  25. <!-- Vue3 项目部分小程序端事件延迟或调用失败 在执行事件的元素上添加 data-eventsync="true" 属性以解决此问题 -->
  26. <view @click="toDetail(item)" data-eventsync="true" class="product_item" v-for="(item, index) in productList" :key="index">
  27. <view style="width: 100%; display: flex; justify-content: center">
  28. <image class="product_image" :src="item.thumb" mode="heightFix"></image>
  29. </view>
  30. <view class="product_name">
  31. <text v-if="item.promo_title" class="regiment_title">{{ item.promo_title }}</text>
  32. <text v-if="item.regiment_title" class="regiment_title">{{ item.regiment_title }}</text>
  33. <text>{{ item.name }}</text></view
  34. >
  35. <view class="product_spec"
  36. ><text>{{ item.spec }}</text></view
  37. >
  38. <view class="stock_price">
  39. <view class="product_price" v-if="isShowPrice">
  40. <text>¥{{ item.price }} </text>
  41. </view>
  42. <view class="product_stock">剩{{ item.stock }}个</view>
  43. </view>
  44. </view>
  45. </view>
  46. </view>
  47. <view class="search_fixed">
  48. <view class="search_box">
  49. <view class="city_name">深圳市</view>
  50. <!-- <navigator url="/pages/user/info" v-if="toSelectedCity" class="city_name uncheck">选城市</navigator> -->
  51. <input class="search_input" type="text" v-model="requestParam.name" @input="searchChange" placeholder="请输入产品名称搜索" />
  52. <button class="search_btn" @click.stop="searchOpen()" data-eventsync="true">搜索</button>
  53. </view>
  54. </view>
  55. <view class="to_bottom" v-if="isLast"> -----{{ !productList.length && toSelectedCity ? "请选择您的城市" : "到底啦" }}-----</view>
  56. </view>
  57. </template>
  58. <script>
  59. export default {
  60. data() {
  61. return {
  62. // 轮播图
  63. bannerList: [],
  64. // 产品列表
  65. productList: [],
  66. // 请求参数
  67. requestParam: {
  68. name: "",
  69. page: 1,
  70. type_id: 0,
  71. },
  72. // 是否最后一页
  73. isLast: false,
  74. // 是否请求中
  75. isReqing: false,
  76. // 是否显示价格
  77. isShowPrice: false,
  78. // 城市名称
  79. cityName: "选城市",
  80. // 选择城市
  81. toSelectedCity: false,
  82. tabs: [
  83. {
  84. id: 0,
  85. name: "全部",
  86. },
  87. ],
  88. tabIndex: 0,
  89. };
  90. },
  91. onLoad() {
  92. // #ifdef MP-WEIXIN
  93. //分享按钮
  94. uni.showShareMenu({
  95. withShareTicket: true,
  96. menus: ["shareAppMessage", "shareTimeline"],
  97. });
  98. // #endif
  99. },
  100. onReady() {
  101. // 请求参数
  102. this.requestParam.name = "";
  103. // 请求参数
  104. this.requestParam.page = 1;
  105. // 是否是最后一页
  106. this.isLast = false;
  107. // 设置请求中
  108. this.isReqing = true;
  109. // 请求
  110. this.$http.request("api/product/get_list", this.requestParam).then((re) => {
  111. // 设置非请求中
  112. this.isReqing = false;
  113. // 成功结果
  114. if (re.code == "success") {
  115. if (re.data.last_page <= this.requestParam.page) this.isLast = true;
  116. this.productList = re.data.data;
  117. }
  118. });
  119. },
  120. onShareAppMessage(obj) {
  121. // 获取分享信息
  122. let shareList = getApp().globalData.shareList;
  123. // 获取分享信息
  124. let shareObj = {
  125. title: "学生征订",
  126. path: "/pages/index/index",
  127. imageUrl: "https://baokan-mp.oss-cn-shenzhen.aliyuncs.com/static/20250213-142749.png",
  128. };
  129. // 循环列表
  130. for (let i in shareList) {
  131. if (shareList[i].pages == "pages/index/index") {
  132. shareObj.path = shareList[i].path ? shareList[i].path : shareObj.path;
  133. shareObj.title = shareList[i].title ? shareList[i].title : shareObj.title;
  134. shareObj.imageUrl = shareList[i].image_url ? shareList[i].image_url : shareObj.imageUrl;
  135. }
  136. }
  137. // 返回分享信息
  138. return shareObj;
  139. },
  140. onShow() {
  141. // 是否显示价格
  142. this.isShowPrice = this.$checkAccess.checkShowPrice();
  143. // 城市名
  144. this.cityName = this.$checkAccess.getCity();
  145. // 选城市
  146. (this.cityName = this.cityName ? this.cityName : "选城市"),
  147. // 登录并且未选择城市,才可以选择城市
  148. (this.toSelectedCity = !this.$checkAccess.getCity() ? true : false);
  149. // 没有数据的话,或者请求中,不允许刷新
  150. if (this.isReqing) return;
  151. // 获取列表
  152. this.$http.request("/api/banner/get_list").then((re) => {
  153. if (re.code === "success") {
  154. this.bannerList = re.data;
  155. }
  156. });
  157. // 获取类型
  158. this.$http.request("/api/product/get_type").then((re) => {
  159. if (re.code === "success") {
  160. // re.data.unshift();
  161. this.tabs = this.tabs.concat(re.data || []);
  162. }
  163. });
  164. },
  165. onPullDownRefresh() {
  166. // 如果请求中,不允许请求,
  167. if (this.isReqing) return false;
  168. // 初始化页码为1
  169. this.requestParam.page = 1;
  170. // 是否是最后一页
  171. this.isLast = false;
  172. // 设置请求中
  173. this.isReqing = true;
  174. // 请求列表
  175. this.$http.request("api/product/get_list", this.requestParam).then((re) => {
  176. // 设置非请求中
  177. this.isReqing = false;
  178. // 成功结果
  179. if (re.code == "success") {
  180. if (re.data.last_page <= this.requestParam.page) this.isLast = true;
  181. this.productList = re.data.data;
  182. }
  183. });
  184. uni.stopPullDownRefresh();
  185. },
  186. onReachBottom() {
  187. // 如果页码是0,避免第一页重复
  188. if (this.requestParam.page < 1) return;
  189. // 最后一页不再请求
  190. if (this.isLast) return;
  191. // 请求中,不再请求
  192. if (this.isReqing) return;
  193. // 增加一页
  194. this.requestParam.page = this.requestParam.page + 1;
  195. // 设置请求中
  196. this.isReqing = true;
  197. // 请求列表
  198. this.$http.request("api/product/get_list", this.requestParam).then((re) => {
  199. // 设置非请求中
  200. this.isReqing = false;
  201. // 成功结果
  202. if (re.code == "success") {
  203. // 最后一页
  204. if (re.data.last_page <= this.requestParam.page) this.isLast = true;
  205. // 追加数据
  206. this.productList.push(...re.data.data);
  207. }
  208. });
  209. },
  210. methods: {
  211. // 处理点击tab
  212. onTabClick(index) {
  213. this.requestParam.type_id = index;
  214. // 如果请求中,不允许请求,
  215. this.searchOpen();
  216. },
  217. searchChange(e) {
  218. // 如果没有搜索词
  219. if (!this.requestParam.name) {
  220. this.searchOpen();
  221. }
  222. },
  223. searchOpen() {
  224. // 请求中,不再请求
  225. if (this.isReqing) return;
  226. // 是否是最后一页
  227. this.isLast = false;
  228. // 初始化页码为1
  229. this.requestParam.page = 1;
  230. // 设置请求中
  231. this.isReqing = true;
  232. // 请求列表
  233. this.$http.request("api/product/get_list", this.requestParam).then((re) => {
  234. // 设置非请求中
  235. this.isReqing = false;
  236. // 成功结果
  237. if (re.code == "success") {
  238. this.productList = re.data.data;
  239. if (re.data.data.length && re.data.last_page >= this.requestParam.page) this.isLast = true;
  240. }
  241. });
  242. },
  243. toDetail(item) {
  244. uni.navigateTo({
  245. url: "/pages/product/index?product_id=" + item.id,
  246. });
  247. },
  248. navLottery(url) {
  249. // 没有路径,不跳转
  250. if (!url) return;
  251. // 判断是不是小程序链接
  252. if (url.includes("http")) {
  253. // 转码
  254. let link_url = encodeURIComponent(url);
  255. // 跳转到webview
  256. uni.redirectTo({
  257. url: `/pages/webview/index?link_url=${link_url}`,
  258. });
  259. } else {
  260. // 跳转到webview
  261. uni.navigateTo({
  262. url: url,
  263. });
  264. }
  265. },
  266. },
  267. };
  268. </script>
  269. <style lang="less">
  270. .search_fixed {
  271. top: var(--window-top);
  272. left: 0rpx;
  273. width: 750rpx;
  274. display: block;
  275. position: fixed;
  276. margin: 0rpx auto;
  277. padding: 20rpx 0rpx;
  278. background-color: #ffffff;
  279. .search_box {
  280. width: 750rpx;
  281. height: 60rpx;
  282. display: block;
  283. position: relative;
  284. .city_name {
  285. float: left;
  286. width: 80rpx;
  287. height: 60rpx;
  288. display: block;
  289. font-size: 24rpx;
  290. overflow: hidden;
  291. margin-left: 35rpx;
  292. line-height: 60rpx;
  293. white-space: nowrap;
  294. text-overflow: ellipsis;
  295. }
  296. .city_name.uncheck {
  297. color: #e03519;
  298. }
  299. .search_input {
  300. z-index: 0;
  301. float: left;
  302. width: 510rpx;
  303. height: 56rpx;
  304. display: block;
  305. font-size: 24rpx;
  306. padding-left: 20rpx;
  307. position: relative;
  308. border-top-left-radius: 40rpx;
  309. border-bottom-left-radius: 40rpx;
  310. border: 2rpx solid #dddddd;
  311. }
  312. .search_btn {
  313. top: 0rpx;
  314. z-index: 9;
  315. left: 610rpx;
  316. color: #ffffff;
  317. position: absolute;
  318. display: block;
  319. width: 120rpx;
  320. height: 60rpx;
  321. font-size: 24rpx;
  322. margin: 0rpx 0rpx;
  323. padding: 0rpx 0rpx;
  324. line-height: 60rpx;
  325. border-radius: 40rpx;
  326. background-color: #e03519;
  327. }
  328. }
  329. }
  330. .banner_box {
  331. width: 680rpx;
  332. display: block;
  333. overflow: hidden;
  334. margin: 0rpx auto;
  335. margin-top: 120rpx;
  336. .banner_list {
  337. display: block;
  338. width: 680rpx;
  339. height: 382rpx;
  340. line-height: 382rpx;
  341. text-align: center;
  342. .banner_swiper {
  343. display: block;
  344. width: 680rpx;
  345. height: 382rpx;
  346. line-height: 382rpx;
  347. text-align: center;
  348. .image {
  349. width: 680rpx;
  350. height: 382rpx;
  351. }
  352. }
  353. }
  354. }
  355. .product_box {
  356. display: block;
  357. overflow: hidden;
  358. margin: 20rpx auto;
  359. padding: 0rpx 35rpx;
  360. .navBar {
  361. display: flex;
  362. height: 100rpx;
  363. .navItem {
  364. display: flex;
  365. align-items: center;
  366. justify-content: center;
  367. border-bottom: 4rpx solid transparent;
  368. width: max-content;
  369. margin: 0 10rpx;
  370. .navItemTitle {
  371. }
  372. }
  373. .active {
  374. color: #e03519;
  375. }
  376. }
  377. .product_list {
  378. display: block;
  379. overflow: hidden;
  380. margin: 0rpx auto;
  381. .product_item {
  382. float: left;
  383. width: 320rpx;
  384. height: 520rpx;
  385. display: block;
  386. overflow: hidden;
  387. margin: 20rpx 0rpx;
  388. margin-right: 40rpx;
  389. background-color: #ffffff;
  390. border-radius: 20rpx;
  391. .product_image {
  392. width: 320rpx;
  393. height: 320rpx;
  394. }
  395. .product_name {
  396. height: 80rpx;
  397. font-size: 30rpx;
  398. line-height: 40rpx;
  399. overflow: hidden;
  400. margin: 10rpx 0rpx;
  401. padding: 0rpx 10rpx;
  402. text-overflow: ellipsis;
  403. .regiment_title {
  404. background-color: red;
  405. color: #f9f9f9;
  406. }
  407. }
  408. .product_spec {
  409. height: 30rpx;
  410. color: #999999;
  411. font-size: 24rpx;
  412. line-height: 30rpx;
  413. padding: 0rpx 10rpx;
  414. overflow: hidden;
  415. white-space: nowrap;
  416. text-overflow: ellipsis;
  417. }
  418. .stock_price {
  419. color: #dddddd;
  420. font-size: 20rpx;
  421. overflow: hidden;
  422. line-height: 30rpx;
  423. padding: 0rpx 10rpx;
  424. .product_price {
  425. float: left;
  426. color: red;
  427. font-size: 30rpx;
  428. line-height: 60rpx;
  429. .product_market {
  430. font-size: 24rpx;
  431. color: #999999;
  432. line-height: 30rpx;
  433. vertical-align: top;
  434. text-decoration: line-through;
  435. }
  436. }
  437. .product_stock {
  438. float: right;
  439. font-size: 20rpx;
  440. line-height: 60rpx;
  441. }
  442. }
  443. }
  444. .product_item:nth-child(even) {
  445. margin-right: 0rpx;
  446. }
  447. }
  448. }
  449. </style>