index.vue 13 KB

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