index.vue 10 KB

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