index.vue 10 KB

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