index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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()) {
  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. uni.getStorage({
  139. key: "followPopupTime",
  140. success: (res) => {
  141. if (!res.data || new Date().getTime() - res.data > 1000 * 60 * 60 * 24) {
  142. uni.setStorage({
  143. key: "followPopupTime",
  144. data: new Date().getTime(),
  145. });
  146. this.$refs.addFollow.open("center");
  147. }
  148. },
  149. fail: (err) => {
  150. this.$refs.addFollow.open("center");
  151. },
  152. });
  153. }
  154. this.isManager = this.$checkAccess.isManager();
  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.requestParam.name = "";
  165. // 请求参数
  166. this.requestParam.page = 1;
  167. // 是否是最后一页
  168. this.isLast = false;
  169. // 设置请求中
  170. this.isReqing = true;
  171. // 请求
  172. this.$http.request("api/product/get_list", this.requestParam).then((re) => {
  173. // 设置非请求中
  174. this.isReqing = false;
  175. // 成功结果
  176. if (re.code == "success") {
  177. if (re.data.last_page <= this.requestParam.page) this.isLast = true;
  178. this.productList = re.data.data;
  179. }
  180. });
  181. },
  182. onPullDownRefresh() {
  183. // 如果请求中,不允许请求,
  184. if (this.isReqing) return false;
  185. // 初始化页码为1
  186. this.requestParam.page = 1;
  187. // 是否是最后一页
  188. this.isLast = false;
  189. // 设置请求中
  190. this.isReqing = true;
  191. // 请求列表
  192. this.$http.request("api/product/get_list", this.requestParam).then((re) => {
  193. // 设置非请求中
  194. this.isReqing = false;
  195. // 成功结果
  196. if (re.code == "success") {
  197. if (re.data.last_page <= this.requestParam.page) this.isLast = true;
  198. this.productList = re.data.data;
  199. }
  200. });
  201. uni.stopPullDownRefresh();
  202. },
  203. onReachBottom() {
  204. // 如果页码是0,避免第一页重复
  205. if (this.requestParam.page < 1) return;
  206. // 最后一页不再请求
  207. if (this.isLast) return;
  208. // 请求中,不再请求
  209. if (this.isReqing) return;
  210. // 增加一页
  211. this.requestParam.page = this.requestParam.page + 1;
  212. // 设置请求中
  213. this.isReqing = true;
  214. // 请求列表
  215. this.$http.request("api/product/get_list", this.requestParam).then((re) => {
  216. // 设置非请求中
  217. this.isReqing = false;
  218. // 成功结果
  219. if (re.code == "success") {
  220. // 最后一页
  221. if (re.data.last_page <= this.requestParam.page) this.isLast = true;
  222. // 追加数据
  223. this.productList.push(...re.data.data);
  224. }
  225. });
  226. },
  227. methods: {
  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. closePopup() {
  278. this.$refs.addFollow.close();
  279. //存key以及时间
  280. uni.setStorage({
  281. key: "followPopupTime",
  282. data: new Date().getTime(),
  283. });
  284. },
  285. },
  286. };
  287. </script>
  288. <style lang="less">
  289. .search_fixed {
  290. top: var(--window-top);
  291. left: 0rpx;
  292. width: 750rpx;
  293. display: block;
  294. position: fixed;
  295. margin: 0rpx auto;
  296. padding: 20rpx 0rpx;
  297. background-color: #ffffff;
  298. .search_box {
  299. width: 750rpx;
  300. height: 60rpx;
  301. display: block;
  302. position: relative;
  303. .city_name {
  304. float: left;
  305. width: 80rpx;
  306. height: 60rpx;
  307. display: block;
  308. font-size: 24rpx;
  309. overflow: hidden;
  310. margin-left: 35rpx;
  311. line-height: 60rpx;
  312. white-space: nowrap;
  313. text-overflow: ellipsis;
  314. }
  315. .city_name.uncheck {
  316. color: #e03519;
  317. }
  318. .search_input {
  319. z-index: 0;
  320. float: left;
  321. width: 510rpx;
  322. height: 56rpx;
  323. display: block;
  324. font-size: 24rpx;
  325. padding-left: 20rpx;
  326. position: relative;
  327. border-top-left-radius: 40rpx;
  328. border-bottom-left-radius: 40rpx;
  329. border: 2rpx solid #dddddd;
  330. }
  331. .search_btn {
  332. top: 0rpx;
  333. z-index: 9;
  334. left: 610rpx;
  335. color: #ffffff;
  336. position: absolute;
  337. display: block;
  338. width: 120rpx;
  339. height: 60rpx;
  340. font-size: 24rpx;
  341. margin: 0rpx 0rpx;
  342. padding: 0rpx 0rpx;
  343. line-height: 60rpx;
  344. border-radius: 40rpx;
  345. background-color: #e03519;
  346. }
  347. }
  348. }
  349. .banner_box {
  350. width: 680rpx;
  351. display: block;
  352. overflow: hidden;
  353. margin: 0rpx auto;
  354. margin-top: 120rpx;
  355. .banner_list {
  356. display: block;
  357. width: 680rpx;
  358. height: 382rpx;
  359. line-height: 382rpx;
  360. text-align: center;
  361. .banner_swiper {
  362. display: block;
  363. width: 680rpx;
  364. height: 382rpx;
  365. line-height: 382rpx;
  366. text-align: center;
  367. .image {
  368. width: 680rpx;
  369. height: 382rpx;
  370. }
  371. }
  372. }
  373. }
  374. .product_box {
  375. display: block;
  376. overflow: hidden;
  377. margin: 20rpx auto;
  378. padding: 0rpx 35rpx;
  379. .product_list {
  380. display: block;
  381. overflow: hidden;
  382. margin: 0rpx auto;
  383. .product_item {
  384. float: left;
  385. width: 320rpx;
  386. height: 520rpx;
  387. display: block;
  388. overflow: hidden;
  389. margin: 20rpx 0rpx;
  390. margin-right: 40rpx;
  391. background-color: #ffffff;
  392. border-radius: 20rpx;
  393. .product_image {
  394. width: 320rpx;
  395. height: 320rpx;
  396. }
  397. .product_name {
  398. height: 80rpx;
  399. font-size: 30rpx;
  400. line-height: 40rpx;
  401. overflow: hidden;
  402. margin: 10rpx 0rpx;
  403. padding: 0rpx 10rpx;
  404. text-overflow: ellipsis;
  405. .regiment_title {
  406. background-color: red;
  407. color: #f9f9f9;
  408. }
  409. }
  410. .product_spec {
  411. height: 30rpx;
  412. color: #999999;
  413. font-size: 24rpx;
  414. line-height: 30rpx;
  415. padding: 0rpx 10rpx;
  416. overflow: hidden;
  417. white-space: nowrap;
  418. text-overflow: ellipsis;
  419. }
  420. .stock_price {
  421. color: #dddddd;
  422. font-size: 20rpx;
  423. overflow: hidden;
  424. line-height: 30rpx;
  425. padding: 0rpx 10rpx;
  426. .product_price {
  427. float: left;
  428. color: red;
  429. font-size: 30rpx;
  430. line-height: 60rpx;
  431. .product_market {
  432. font-size: 24rpx;
  433. color: #999999;
  434. line-height: 30rpx;
  435. vertical-align: top;
  436. text-decoration: line-through;
  437. }
  438. }
  439. .product_stock {
  440. float: right;
  441. font-size: 20rpx;
  442. line-height: 60rpx;
  443. }
  444. }
  445. }
  446. .product_item:nth-child(even) {
  447. margin-right: 0rpx;
  448. }
  449. }
  450. }
  451. </style>