publish.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <template>
  2. <view class="page" @touchstart="touchStart" @touchend="touchEnd">
  3. <form>
  4. <view class="uni-textarea">
  5. <textarea placeholder="这一刻的想法..." v-model="input_content" />
  6. </view>
  7. <view class="uni-list list-pd">
  8. <view class="uni-list-cell cell-pd">
  9. <view class="uni-uploader">
  10. <view class="uni-uploader-head">
  11. <view class="uni-uploader-title"></view>
  12. <view class="uni-uploader-info">{{imageList.length}}/9</view>
  13. </view>
  14. <view class="uni-uploader-body">
  15. <view class="uni-uploader__files">
  16. <block v-for="(image,index) in imageList" :key="index">
  17. <view class="uni-uploader__file" style="position: relative;">
  18. <image class="uni-uploader__img" mode="aspectFill" :src="image" :data-src="image" @tap="previewImage"></image>
  19. <view class="close-view" @click="close(index)">×</view>
  20. </view>
  21. </block>
  22. <view class="uni-uploader__input-box" v-show="imageList.length < 9">
  23. <view class="uni-uploader__input" @tap="chooseImage"></view>
  24. </view>
  25. </view>
  26. </view>
  27. </view>
  28. </view>
  29. </view>
  30. <view class="footer">
  31. <tui-button @click="publish">页面主操作</tui-button>
  32. </view>
  33. </form>
  34. </view>
  35. </template>
  36. <script>
  37. import image from '@/common/image.js';
  38. import tuiButton from "@/components/thorui/tui-button/tui-button"
  39. var sourceType = [
  40. ['camera'],
  41. ['album'],
  42. ['camera', 'album']
  43. ]
  44. var sizeType = [
  45. ['compressed'],
  46. ['original'],
  47. ['compressed', 'original']
  48. ]
  49. export default {
  50. components: {
  51. tuiButton,
  52. },
  53. data() {
  54. return {
  55. // title: 'choose/previewImage',
  56. input_content:'',
  57. imageList: [],
  58. sourceTypeIndex: 2,
  59. sourceType: ['拍照', '相册', '拍照或相册'],
  60. sizeTypeIndex: 2,
  61. sizeType: ['压缩', '原图', '压缩或原图'],
  62. countIndex: 8,
  63. count: [1, 2, 3, 4, 5, 6, 7, 8, 9],
  64. //侧滑返回start
  65. startX: 0, //点击屏幕起始位置
  66. movedX: 0, //横向移动的距离
  67. endX: 0, //接触屏幕后移开时的位置
  68. //end
  69. }
  70. },
  71. onUnload() {
  72. this.imageList = [],
  73. this.sourceTypeIndex = 2,
  74. this.sourceType = ['拍照', '相册', '拍照或相册'],
  75. this.sizeTypeIndex = 2,
  76. this.sizeType = ['压缩', '原图', '压缩或原图'],
  77. this.countIndex = 8;
  78. },
  79. methods: {
  80. async publish(){
  81. if (!this.input_content) {
  82. uni.showModal({ content: '内容不能为空,请输入内容', showCancel: false, });
  83. return;
  84. }
  85. uni.showLoading({title:'发布中'});
  86. var location = await this.getLocation();//位置信息,可删除,主要想记录一下异步转同步处理
  87. var images = [];
  88. for(var i = 0,len = this.imageList.length; i < len; i++){
  89. var image_obj = {name:'image-'+i,uri:this.imageList[i]};
  90. images.push(image_obj);
  91. }
  92. uni.uploadFile({//该上传仅为示例,可根据自己业务修改或封装,注意:统一上传可能会导致服务器压力过大
  93. url: 'moment/moments', //仅为示例,非真实的接口地址
  94. files:images,//有files时,会忽略filePath和name
  95. filePath: '',
  96. name: '',
  97. formData: {//后台以post方式接收
  98. 'user_id':'1',//自己系统中的用户id
  99. 'text': this.input_content,//moment文字部分
  100. 'longitude':location.longitude,//经度
  101. 'latitude':location.latitude//纬度
  102. },
  103. success: (uploadFileRes) => {
  104. uni.hideLoading();
  105. uni.showToast({
  106. icon:'success',
  107. title:"发布成功"
  108. })
  109. uni.navigateBack({//可根据实际情况使用其他路由方式
  110. delta:1
  111. });
  112. },
  113. fail: (e) => {
  114. console.log("e: " + JSON.stringify(e));
  115. uni.hideLoading();
  116. uni.showToast({
  117. icon:'none',
  118. title:"发布失败,请检查网络"
  119. })
  120. }
  121. });
  122. },
  123. getLocation(){//h5中可能不支持,自己选择
  124. return new Promise((resolve, reject) => {
  125. uni.getLocation({
  126. type: 'wgs84',
  127. success: function (res) {
  128. resolve(res);
  129. },
  130. fail: (e) => {
  131. reject(e);
  132. }
  133. });
  134. } )
  135. },
  136. close(e){
  137. this.imageList.splice(e,1);
  138. },
  139. chooseImage: async function() {
  140. if (this.imageList.length === 9) {
  141. let isContinue = await this.isFullImg();
  142. console.log("是否继续?", isContinue);
  143. if (!isContinue) {
  144. return;
  145. }
  146. }
  147. uni.chooseImage({
  148. sourceType: sourceType[this.sourceTypeIndex],
  149. sizeType: sizeType[this.sizeTypeIndex],
  150. count: this.imageList.length + this.count[this.countIndex] > 9 ? 9 - this.imageList.length : this.count[this.countIndex],
  151. success: (res) => {
  152. // #ifdef APP-PLUS
  153. //提交压缩,因为使用了H5+ Api,所以自定义压缩目前仅支持APP平台
  154. var compressd = cp_images=> {
  155. this.imageList = this.imageList.concat(cp_images)//压缩后的图片路径
  156. }
  157. image.compress(res.tempFilePaths,compressd);
  158. // #endif
  159. // #ifndef APP-PLUS
  160. this.imageList = this.imageList.concat(res.tempFilePaths)//非APP平台不支持自定义压缩,暂时没有处理,可通过uni-app上传组件的sizeType属性压缩
  161. // #endif
  162. }
  163. })
  164. },
  165. isFullImg: function() {
  166. return new Promise((res) => {
  167. uni.showModal({
  168. content: "已经有9张图片了,是否清空现有图片?",
  169. success: (e) => {
  170. if (e.confirm) {
  171. this.imageList = [];
  172. res(true);
  173. } else {
  174. res(false)
  175. }
  176. },
  177. fail: () => {
  178. res(false)
  179. }
  180. })
  181. })
  182. },
  183. previewImage: function(e) {
  184. var current = e.target.dataset.src
  185. uni.previewImage({
  186. current: current,
  187. urls: this.imageList
  188. })
  189. },
  190. touchStart: function(e) {
  191. this.startX = e.mp.changedTouches[0].pageX;
  192. },
  193. touchEnd: function(e) {
  194. this.endX = e.mp.changedTouches[0].pageX;
  195. if (this.endX - this.startX > 200) {
  196. uni.navigateBack();
  197. }
  198. }
  199. }
  200. }
  201. </script>
  202. <style scoped>
  203. @import url("@/common/uni.css");
  204. .footer {
  205. margin-top: 80upx;
  206. padding: 0 20rpx;
  207. }
  208. .cell-pd {
  209. padding: 20upx 30upx;
  210. }
  211. .uni-textarea {
  212. width: auto;
  213. padding: 20upx 25upx;
  214. line-height: 1.6;
  215. height: 150upx;
  216. }
  217. .uni-list::before {
  218. height: 0;
  219. }
  220. .uni-list:after {
  221. height: 0;
  222. }
  223. .list-pd {
  224. margin-top: 0;
  225. }
  226. .close-view{
  227. text-align: center;
  228. line-height:30upx;
  229. height: 35upx;
  230. width: 35upx;
  231. background: #ef5350;
  232. color: #FFFFFF;
  233. position: absolute;
  234. top: 1upx;
  235. right: 1upx;
  236. font-size: 35upx;
  237. border-radius: 8upx;
  238. }
  239. .page {
  240. width: 750upx;
  241. height: 100%;
  242. }
  243. </style>