详情小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
3.6 KiB

5 years ago
5 years ago
5 years ago
  1. <template>
  2. <view class="content">
  3. <!-- 文本输入 -->
  4. <textarea class="textarea" placeholder="这一刻的想法…" v-model="content"></textarea>
  5. <!-- 上传图片 -->
  6. <view class="my-images">
  7. <view class="my-image-item" @click="uploadImage" v-if="image_list.length < 6">
  8. <text class="lf-iconfont icon-jia my-image-item-after"></text>
  9. </view>
  10. <view class="my-image-item" v-for="(item, index) in image_list" :key="index" @click="lookImage(index)">
  11. <image :src="item" mode="aspectFill"></image>
  12. <view class="remove-image" @click.stop="removeInage(index)">
  13. <text class="lf-iconfont icon-shanchu"></text>
  14. </view>
  15. </view>
  16. </view>
  17. <!-- 发表按钮 -->
  18. <button class="my-btn" hover-class="lf-opacity">发表</button>
  19. </view>
  20. </template>
  21. <script>
  22. export default {
  23. data(){
  24. return {
  25. image_list: [],
  26. image_count: 6,
  27. content: ''
  28. }
  29. },
  30. watch: {
  31. // #ifdef MP-WEIXIN
  32. content(val){
  33. if(String(val).length){
  34. // 监听微信小程序页面返回,弹出询问框
  35. wx.enableAlertBeforeUnload({
  36. message: '您已输入文字,但未发表,离开后已输入的文字将清空,确定离开?',
  37. complete: result => {
  38. console.log("result", result);
  39. }
  40. })
  41. }else{
  42. wx.disableAlertBeforeUnload(); // 关闭监听
  43. }
  44. }
  45. // #endif
  46. },
  47. onLoad(){
  48. },
  49. methods: {
  50. // 上传凭证图片
  51. uploadImage(){
  52. let current_count = this.image_count - this.image_list.length;
  53. if(current_count == 0) return;
  54. uni.chooseImage({
  55. count: current_count,
  56. complete: result => {
  57. if(result.errMsg == "chooseImage:fail cancel"){
  58. return; // 取消选择图片
  59. }
  60. let tempFiles = result.tempFiles;
  61. let image_list = [];
  62. let overstep = false;
  63. tempFiles.map(item => {
  64. // 上传的图片需小于10MB
  65. if(item.size < 10464788){
  66. image_list.push(item.path);
  67. }else{
  68. overstep = true;
  69. }
  70. })
  71. if(overstep){
  72. uni.showModal({
  73. title: '温馨提示',
  74. content: '您上传的图片含有超出10M大小的限制,请优化大小后再上传!',
  75. showCancel: false
  76. })
  77. }
  78. this.image_list.push(...image_list);
  79. }
  80. })
  81. },
  82. // 预览图片
  83. lookImage(current){
  84. if(this.image_list.length <= 0) return;
  85. this.$u.throttle(() => {
  86. uni.previewImage({
  87. urls: this.image_list,
  88. current: current
  89. });
  90. }, 500);
  91. },
  92. // 移除图片
  93. removeInage(current){
  94. this.image_list.splice(current, 1);
  95. }
  96. }
  97. }
  98. </script>
  99. <style lang="scss" scoped="scoped">
  100. .content{
  101. width: 750rpx;
  102. height: max-content;
  103. box-sizing: border-box;
  104. padding: 30rpx 32rpx;
  105. .textarea{
  106. width: 100%;
  107. height: 160rpx;
  108. padding: 10rpx 0;
  109. }
  110. }
  111. .my-images{
  112. display: flex;
  113. flex-wrap: wrap;
  114. margin-top: 30rpx;
  115. margin-bottom: 18rpx;
  116. .my-image-item{
  117. width: 220rpx;
  118. height: 220rpx;
  119. background: #DDDDDD;
  120. position: relative;
  121. margin-right: 12rpx;
  122. &:nth-child(3n){
  123. margin-right: 0rpx;
  124. }
  125. &:nth-child(n+4){
  126. margin-top: 12rpx;
  127. }
  128. image{
  129. width: 100%;
  130. height: 100%;
  131. }
  132. .remove-image{
  133. position: absolute;
  134. right: -4rpx;
  135. top: -18rpx;
  136. color: #e74c3c;
  137. font-size: 40rpx;
  138. padding: 8rpx;
  139. }
  140. }
  141. .my-image-item-after{
  142. position: absolute;
  143. width: 100%;
  144. height: 100%;
  145. display: flex;
  146. justify-content: center;
  147. align-items: center;
  148. font-size: 60rpx;
  149. color: #999999;
  150. }
  151. }
  152. .my-btn{
  153. margin-top: 30rpx;
  154. background-color: #E21196;
  155. color: #FFFFFF;
  156. }
  157. /deep/.textarea-placeholder{
  158. color: #999999;
  159. }
  160. </style>