index.vue 13 KB

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