index.vue 14 KB

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