index.vue 8.8 KB

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