index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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 = [
  162. {
  163. id: 0,
  164. name: "全部",
  165. },
  166. ].concat(re.data || []);
  167. }
  168. });
  169. },
  170. onPullDownRefresh() {
  171. // 如果请求中,不允许请求,
  172. if (this.isReqing) return false;
  173. // 初始化页码为1
  174. this.requestParam.page = 1;
  175. // 是否是最后一页
  176. this.isLast = false;
  177. // 设置请求中
  178. this.isReqing = true;
  179. // 请求列表
  180. this.$http.request("api/product/get_list", this.requestParam).then((re) => {
  181. // 设置非请求中
  182. this.isReqing = false;
  183. // 成功结果
  184. if (re.code == "success") {
  185. if (re.data.last_page <= this.requestParam.page) this.isLast = true;
  186. this.productList = re.data.data;
  187. }
  188. });
  189. uni.stopPullDownRefresh();
  190. },
  191. onReachBottom() {
  192. // 如果页码是0,避免第一页重复
  193. if (this.requestParam.page < 1) return;
  194. // 最后一页不再请求
  195. if (this.isLast) return;
  196. // 请求中,不再请求
  197. if (this.isReqing) return;
  198. // 增加一页
  199. this.requestParam.page = this.requestParam.page + 1;
  200. // 设置请求中
  201. this.isReqing = true;
  202. // 请求列表
  203. this.$http.request("api/product/get_list", this.requestParam).then((re) => {
  204. // 设置非请求中
  205. this.isReqing = false;
  206. // 成功结果
  207. if (re.code == "success") {
  208. // 最后一页
  209. if (re.data.last_page <= this.requestParam.page) this.isLast = true;
  210. // 追加数据
  211. this.productList.push(...re.data.data);
  212. }
  213. });
  214. },
  215. methods: {
  216. // 处理点击tab
  217. onTabClick(index) {
  218. this.requestParam.type_id = index;
  219. // 如果请求中,不允许请求,
  220. this.searchOpen();
  221. },
  222. searchChange(e) {
  223. // 如果没有搜索词
  224. if (!this.requestParam.name) {
  225. this.searchOpen();
  226. }
  227. },
  228. searchOpen() {
  229. // 请求中,不再请求
  230. if (this.isReqing) return;
  231. // 是否是最后一页
  232. this.isLast = false;
  233. // 初始化页码为1
  234. this.requestParam.page = 1;
  235. // 设置请求中
  236. this.isReqing = true;
  237. // 请求列表
  238. this.$http.request("api/product/get_list", this.requestParam).then((re) => {
  239. // 设置非请求中
  240. this.isReqing = false;
  241. // 成功结果
  242. if (re.code == "success") {
  243. this.productList = re.data.data;
  244. if (re.data.data.length && re.data.last_page >= this.requestParam.page) this.isLast = true;
  245. }
  246. });
  247. },
  248. toDetail(item) {
  249. uni.navigateTo({
  250. url: "/pages/product/index?product_id=" + item.id,
  251. });
  252. },
  253. navLottery(url) {
  254. // 没有路径,不跳转
  255. if (!url) return;
  256. // 判断是不是小程序链接
  257. if (url.includes("http")) {
  258. // 转码
  259. let link_url = encodeURIComponent(url);
  260. // 跳转到webview
  261. uni.redirectTo({
  262. url: `/pages/webview/index?link_url=${link_url}`,
  263. });
  264. } else {
  265. // 跳转到webview
  266. uni.navigateTo({
  267. url: url,
  268. });
  269. }
  270. },
  271. },
  272. };
  273. </script>
  274. <style lang="less">
  275. .search_fixed {
  276. top: var(--window-top);
  277. left: 0rpx;
  278. width: 750rpx;
  279. display: block;
  280. position: fixed;
  281. margin: 0rpx auto;
  282. padding: 20rpx 0rpx;
  283. background-color: #ffffff;
  284. .search_box {
  285. width: 750rpx;
  286. height: 60rpx;
  287. display: block;
  288. position: relative;
  289. .city_name {
  290. float: left;
  291. width: 80rpx;
  292. height: 60rpx;
  293. display: block;
  294. font-size: 24rpx;
  295. overflow: hidden;
  296. margin-left: 35rpx;
  297. line-height: 60rpx;
  298. white-space: nowrap;
  299. text-overflow: ellipsis;
  300. }
  301. .city_name.uncheck {
  302. color: #e03519;
  303. }
  304. .search_input {
  305. z-index: 0;
  306. float: left;
  307. width: 510rpx;
  308. height: 56rpx;
  309. display: block;
  310. font-size: 24rpx;
  311. padding-left: 20rpx;
  312. position: relative;
  313. border-top-left-radius: 40rpx;
  314. border-bottom-left-radius: 40rpx;
  315. border: 2rpx solid #dddddd;
  316. }
  317. .search_btn {
  318. top: 0rpx;
  319. z-index: 9;
  320. left: 610rpx;
  321. color: #ffffff;
  322. position: absolute;
  323. display: block;
  324. width: 120rpx;
  325. height: 60rpx;
  326. font-size: 24rpx;
  327. margin: 0rpx 0rpx;
  328. padding: 0rpx 0rpx;
  329. line-height: 60rpx;
  330. border-radius: 40rpx;
  331. background-color: #e03519;
  332. }
  333. }
  334. }
  335. .banner_box {
  336. width: 680rpx;
  337. display: block;
  338. overflow: hidden;
  339. margin: 0rpx auto;
  340. margin-top: 120rpx;
  341. .banner_list {
  342. display: block;
  343. width: 680rpx;
  344. height: 382rpx;
  345. line-height: 382rpx;
  346. text-align: center;
  347. .banner_swiper {
  348. display: block;
  349. width: 680rpx;
  350. height: 382rpx;
  351. line-height: 382rpx;
  352. text-align: center;
  353. .image {
  354. width: 680rpx;
  355. height: 382rpx;
  356. }
  357. }
  358. }
  359. }
  360. .product_box {
  361. display: block;
  362. overflow: hidden;
  363. margin: 20rpx auto;
  364. padding: 0rpx 35rpx;
  365. .navBar {
  366. display: flex;
  367. height: 100rpx;
  368. .navItem {
  369. display: flex;
  370. align-items: center;
  371. justify-content: center;
  372. border-bottom: 4rpx solid transparent;
  373. width: max-content;
  374. margin: 0 10rpx;
  375. .navItemTitle {
  376. }
  377. }
  378. .active {
  379. color: #e03519;
  380. }
  381. }
  382. .product_list {
  383. display: block;
  384. overflow: hidden;
  385. margin: 0rpx auto;
  386. .product_item {
  387. float: left;
  388. width: 320rpx;
  389. height: 520rpx;
  390. display: block;
  391. overflow: hidden;
  392. margin: 20rpx 0rpx;
  393. margin-right: 40rpx;
  394. background-color: #ffffff;
  395. border-radius: 20rpx;
  396. .product_image {
  397. width: 320rpx;
  398. height: 320rpx;
  399. }
  400. .product_name {
  401. height: 80rpx;
  402. font-size: 30rpx;
  403. line-height: 40rpx;
  404. overflow: hidden;
  405. margin: 10rpx 0rpx;
  406. padding: 0rpx 10rpx;
  407. text-overflow: ellipsis;
  408. .regiment_title {
  409. background-color: red;
  410. color: #f9f9f9;
  411. }
  412. }
  413. .product_spec {
  414. height: 30rpx;
  415. color: #999999;
  416. font-size: 24rpx;
  417. line-height: 30rpx;
  418. padding: 0rpx 10rpx;
  419. overflow: hidden;
  420. white-space: nowrap;
  421. text-overflow: ellipsis;
  422. }
  423. .stock_price {
  424. color: #dddddd;
  425. font-size: 20rpx;
  426. overflow: hidden;
  427. line-height: 30rpx;
  428. padding: 0rpx 10rpx;
  429. .product_price {
  430. float: left;
  431. color: red;
  432. font-size: 30rpx;
  433. line-height: 60rpx;
  434. .product_market {
  435. font-size: 24rpx;
  436. color: #999999;
  437. line-height: 30rpx;
  438. vertical-align: top;
  439. text-decoration: line-through;
  440. }
  441. }
  442. .product_stock {
  443. float: right;
  444. font-size: 20rpx;
  445. line-height: 60rpx;
  446. }
  447. }
  448. }
  449. .product_item:nth-child(even) {
  450. margin-right: 0rpx;
  451. }
  452. }
  453. }
  454. </style>