index.vue 9.6 KB

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