photo.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <template>
  2. <view>
  3. <view class="list-box">
  4. <view
  5. v-for="(item,index) in photoList"
  6. :key="index"
  7. :class="{'active':item.active}"
  8. :data-index="index"
  9. @tap="previewPhoto"
  10. >
  11. <image :src="item.url" mode="aspectFill" lazy-load="true"></image>
  12. <view>第 {{item.index+1}} 张图片</view>
  13. </view>
  14. </view>
  15. <view class="load">{{loadTxt}}</view>
  16. </view>
  17. </template>
  18. <script>
  19. export default {
  20. data() {
  21. return {
  22. pageNum:1,
  23. posts:[],
  24. photoList:[],
  25. rows:10,
  26. page:1,
  27. isGet:true,
  28. loadTxt:"",
  29. $url:''
  30. };
  31. },
  32. methods:{
  33. /* 获取照片 */
  34. getPhoto(){
  35. if(!this.isGet){
  36. return;
  37. }
  38. this.isGet=false;
  39. new Promise((success,error)=>{
  40. /* 第一页弹出加载层 */
  41. if(this.page==1){
  42. uni.showLoading({
  43. title:'加载中',
  44. mask:true
  45. })
  46. }else{
  47. this.loadTxt="正在加载中";
  48. }
  49. /* 无真实图片请求接口,由 setTimeout 模拟异步过程 */
  50. setTimeout(()=>{
  51. /* 拼接图片路径字符串 */
  52. let joinUrlStr=(num)=>{
  53. return {
  54. index:num,
  55. url:`https://liangxin.zthymaoyi.com/upload/default.jpg`
  56. }
  57. }
  58. let list=[];
  59. for(let i=0;i<10;i++){
  60. list.push(joinUrlStr((this.page-1)*this.rows+i))
  61. }
  62. success(list);
  63. },500);
  64. }).then((res)=>{
  65. if(this.page==1){
  66. uni.hideLoading();
  67. }
  68. this.photoList=[...this.photoList,...res];
  69. this.showImages();
  70. })
  71. },
  72. /* 显示照片 */
  73. showImages(){
  74. let index=(this.page-1)*this.rows;
  75. let show=()=>{
  76. if(index<this.photoList.length){
  77. this.$set(this.photoList[index],"active",true);
  78. index++;
  79. }else{
  80. clearInterval(interval);
  81. this.loadTxt="上拉加载更多";
  82. this.page++;
  83. this.isGet=true;
  84. }
  85. }
  86. let interval=setInterval(()=>{
  87. show();
  88. },100);
  89. },
  90. /* 预览照片 */
  91. previewPhoto(e){
  92. let index=e.currentTarget.dataset.index;
  93. let list=this.photoList.map((item,index)=>{
  94. return item.url;
  95. });
  96. uni.previewImage({
  97. current:list[index], /* 传 Number H5 端出现不兼容 */
  98. urls: list
  99. });
  100. },
  101. getPhotos(){
  102. this.$socket.queryPostsReq(this.userData.user.operId, this.pageNum, res => {
  103. this.posts = res.response.data.filter(p=>p.urls!='');
  104. console.log(this.posts)
  105. });
  106. },
  107. openBigImg(current, imgList){
  108. for(let index in imgList){
  109. if(!imgList[index]){
  110. imgList.splice(index, 1)
  111. }else{
  112. imgList[index] = this.$url + imgList[index]
  113. }
  114. }
  115. uni.previewImage({
  116. current,
  117. urls: imgList,
  118. // #ifndef MP-WEIXIN
  119. indicator: 'number'
  120. // #endif
  121. });
  122. }
  123. },
  124. filters:{
  125. format: function (e) {
  126. // 获取js 时间戳
  127. let time = new Date().getTime();
  128. // 去掉 js 时间戳后三位
  129. time = parseInt((time - e) / 1000);
  130. // 存储转换值
  131. let s;
  132. if (time < 60 * 10) {
  133. // 十分钟内
  134. return '刚刚';
  135. } else if (time < 60 * 60 && time >= 60 * 10) {
  136. // 超过十分钟少于1小时
  137. s = Math.floor(time / 60);
  138. return s + '分钟前';
  139. } else if (time < 60 * 60 * 24 && time >= 60 * 60) {
  140. // 超过1小时少于24小时
  141. s = Math.floor(time / 60 / 60);
  142. return s + '小时前';
  143. } else if (time < 60 * 60 * 24 * 3 && time >= 60 * 60 * 24) {
  144. // 超过1天少于3天内
  145. s = Math.floor(time / 60 / 60 / 24);
  146. return s + '天前';
  147. } else {
  148. // 超过3天
  149. var date = new Date(e);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
  150. var Y = date.getFullYear() + '-';
  151. var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
  152. var D = date.getDate()>10?date.getDate(): '0'+date.getDate() + ' ';
  153. var h = date.getHours() + ':';
  154. var m = date.getMinutes()>10?date.getMinutes():'0'+ date.getMinutes() + ':';
  155. var ss = date.getSeconds();
  156. return Y+M+D+h+m+ss;
  157. }
  158. },
  159. formatData: function (e) {
  160. var date = new Date(e);
  161. //时间戳为10位需*1000,时间戳为13位的话不需乘1000
  162. var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1);
  163. return M;
  164. }
  165. },
  166. mounted() {
  167. this.getPhoto();
  168. }
  169. }
  170. </script>
  171. <style lang="scss">
  172. page {
  173. background-color: #eee;
  174. }
  175. .list-box {
  176. display: flex;
  177. flex-direction: row;
  178. flex-wrap: wrap;
  179. justify-content: space-between;
  180. align-items: flex-start;
  181. align-content: flex-start;
  182. padding: 20upx 20upx 0 20upx;
  183. line-height: 30upx;
  184. font-size: 28upx;
  185. color: #333;
  186. &>view {
  187. background-color: #fff;
  188. width: 345upx;
  189. padding: 20upx;
  190. margin-bottom: 20upx;
  191. box-sizing: border-box;
  192. opacity: 0;
  193. transform:translateY(40upx);
  194. transition: all 0.3s ease-in-out 0s;
  195. &.active{
  196. opacity: 1;
  197. transform:translateY(0);
  198. }
  199. }
  200. image{
  201. width: 100%;
  202. height: 300upx;
  203. margin-bottom: 10upx;
  204. }
  205. }
  206. .load{
  207. line-height: 80upx;
  208. text-align: center;
  209. font-size: 24upx;
  210. color: #999;
  211. padding-bottom: 20rpx;
  212. }
  213. </style>