一、封裝異步請求:
1. 為什么要封裝?
2. 封裝的思路
export default (params) => {uni
.showLoading({title
: "加載中"})return new Promise((resolve, reject) => {wx
.request({...params
,success(res) {resolve(res
)},fail(err) {reject(err
)},complete() {uni
.hideLoading()}})})
}
二、處理時間moment.js
http://momentjs.cn/
三、封裝手勢滑動組件
3.1 實現思路
3.2 關鍵代碼
<template
><view @touchstart
="handleTouchStart"@touchend
="handleTouchEnd">學習觸屏事件
</view
>
</template
><script
>export default {data() {return {startTime
: 0, startX
: 0, startY
: 0}},methods
: {handleTouchStart (event) {console
.log("handleTouchStart 手指按下屏幕")this.startTime
= Date
.now()this.startX
= event
.changedTouches
[0].clientX
this.startY
= event
.changedTouches
[0].clientY
},handleTouchEnd (event) {console
.log("handleTouchEnd 手指離開屏幕")const endTime
= Date
.now()const endX
= event
.changedTouches
[0].clientX
const endY
= event
.changedTouches
[0].clientY
if(endTime
- this.startTime
> 2000) {return;}let direction
= ""if(Math
.abs(endX
-this.startX
) > 10) {direction
= endX
- this.startX
> 0 ? "right" : "left"} else {return;}console
.log(direction
)}}}
</script
><style
>view
{width
: 100%;height
: 500rpx
;background
-color
: aqua
;}
</style
>
3.3 實現滑動手勢組件 SwiperAction
swiperAction組件代碼:
<template
><view
><view
>swiperAction
</view
><view@touchstart
="handleTouchStart"@touchend
="handleTouchEnd"><slot
></slot
></view
></view
>
</template
><script
>export default {data() {return {startTime
: 0, startX
: 0, startY
: 0}},methods
: {handleTouchStart (event) {console
.log("handleTouchStart 手指按下屏幕")this.startTime
= Date
.now()this.startX
= event
.changedTouches
[0].clientX
this.startY
= event
.changedTouches
[0].clientY
},handleTouchEnd (event) {console
.log("handleTouchEnd 手指離開屏幕")const endTime
= Date
.now()const endX
= event
.changedTouches
[0].clientX
const endY
= event
.changedTouches
[0].clientY
if(endTime
- this.startTime
> 2000) {return;}let direction
= ""if(Math
.abs(endX
-this.startX
) > 10) {direction
= endX
- this.startX
> 0 ? "right" : "left"} else {return;}console
.log(direction
)this.$emit("swiperAction", {direction
})}}}
</script
><style lang
="scss"></style
>
在imgDetail頁面中使用:
通過改變imgIndex來切換圖片
<template
><view
><!-- 用戶信息開始
--><view
class="user_info"><view
class="user_icon"><image
:src
="imgDetail.user.avatar" mode
="widthFix"></image
></view
><view
class="user_desc"><view
class="user_name">{{ imgDetail
.user
.name
}}</view
><view
class="user_time">{{ imgDetail
.cnTime
}}</view
></view
></view
><!-- 用戶信息結束
--><!-- 高清大圖開始
--><view
class="high_img"><swiper
-action @swiperAction
="handleSwiperAcion"><image
:src
="imgDetail.img" mode
="widthFix"></image
></swiper
-action
></view
><!-- 高清大圖結束
--><!-- 點贊 開始
--><view
class="user_rank"><view
class="rank"><text
class="iconfont icon-dianzan">{{ imgDetail
.rank
}}</text
></view
><view
class="user_collect"><text
class="iconfont icon-shoucang">收藏
</text
></view
></view
><!-- 點贊 結束
--><!-- 最新評論 開始
--><view
class="comment_hot"><view
class="comment_title"><text
class="iconfont icon-hot1"></text
><text
class="comment_text">最新評論
</text
></view
><view
class="comment_list"><view
class="comment_item" :key
="item.id" v
-for="item in comment"><!-- 用戶信息
--><view
class="comment_user"><!-- 用戶頭像
--><view
class="user_icon"><image
:src
="item.user.avatar" mode
="widthFix"></image
><!-- 用戶名稱
--><view
class="user_name"><view
class="user_nickname">{{item
.user
.name
}}</view
><view
class="user_time">{{item
.cnTime
}}</view
></view
><!-- 用戶徽章
--><view
class="user_badge"><image v
-for="item2 in item.user.title" :key
="item2.icon" :src
="item2.icon" mode
=""></image
></view
></view
></view
><!-- 評論數據
--><view
class="comment_desc"><view
class="comment_content">{{ item
.content
}}</view
><view
class="comment_like"><text
class="iconfont icon-dianzan">{{ item
.size
}}</text
></view
></view
></view
></view
></view
><!-- 最新評論 結束
--></view
>
</template
><script
>import moment
from "moment"moment
.locale("zh-cn")import swiperAction
from "@/components/swiperAction.vue"export default {data() {return {imgDetail
: {},hot
: [], comment
: [], imgIndex
: 0 }},components
: {swiperAction
},onLoad() {console
.log(getApp().globalData
.imgList
)const {imgList
,imgIndex
} = getApp().globalData
this.imgIndex
= imgIndex
this.getData()},methods
: {getComments(id) {this.request({url
: `http://157.122.54.189:9088/image/v2/wallpaper/wallpaper/${id}/comment`}).then((res) => {console
.log(res
)this.hot
= res
.data
.res
.hot
this.comment
= res
.data
.res
.commentres
.data
.res
.comment
.forEach(v => {v
.cnTime
= moment(v
.atime
*1000).fromNow()})})},getData() {const {imgList
} = getApp().globalData
this.imgDetail
= imgList
[this.imgIndex
]this.imgDetail
.cnTime
= moment(this.imgDetail
.atime
* 1000).fromNow()this.getComments(this.imgDetail
.id
)},handleSwiperAcion (e) {console
.log(e
)const {imgList
} = getApp().globalData
if (e
.direction
=== "left" && this.imgIndex
< imgList
.length
-1) {this.imgIndex
++this.getData()} else if (e
.direction
=== "right" && this.imgIndex
> 0) {this.imgIndex
--this.getData()} else {uni
.showToast({title
: "沒有數據啦",icon
: "none"})}}}}
</script
><style lang
="scss" scoped
>.user_info
{display
: flex
;align
-items
: center
;padding
: 20rpx
;.user_icon
{padding
: 0 20rpx
;image
{width
: 88rpx
;border
-radius
: 50%;}}.user_desc
{.user_name
{color
: #
000000;font
-weight
: 600;}.user_time
{color
: #
CCCCCC;font
-size
: 24rpx
;padding
: 10rpx
0;}}}.user_rank
{display
: flex
;height
: 80rpx
;border
-bottom
: 5rpx solid #
EEEEEE;.rank
{display
: flex
;justify
-content
: center
;align
-items
: center
;flex
: 1;.iconfont
{}}.user_collect
{display
: flex
;justify
-content
: center
;align
-items
: center
;flex
: 1;.iconfont
{}}}.comment_hot
{.comment_title
{padding
: 15rpx
;.iconfont
{color
: red
;font
-size
: 40rpx
;}.comment_text
{font
-weight
: 600;font
-size
: 28rpx
;color
: #
666666;margin
-left
: 10rpx
;}}.comment_list
{.comment_item
{border
-bottom
: 15rpx solid #
EEEEEE;.comment_user
{display
: flex
;padding
: 20rpx
10rpx
;.user_icon
{display
: flex
;width
: 35%;justify
-content
: center
;align
-items
: center
;image
{border
-radius
: 50%;width
: 40%;height
: 90%;}}.user_name
{flex
: 1;margin
-left
: 10rpx
;.user_nickname
{color
: #
777;}.user_time
{color
: #ccc
;font
-size
: 24rpx
;padding
: 5rpx
;}}.user_badge
{image
{width
: 40rpx
;height
: 40rpx
;}}}.comment_desc
{display
: flex
;padding
: 10rpx
0;.comment_content
{flex
: 1;padding
-left
: 15%;color
: #
000000;}.comment_like
{text
-align
: right
;margin
-right
: 15rpx
;.icon
-dianzan
{}}}}}}
</style
>
四、實現下載圖片到本地功能
總結
以上是生活随笔為你收集整理的一、uniapp项目(封装异步请求、moment.js时间处理、封装手势滑动组件、下载图片到本地)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。