index.vue 12 KB

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