communityPublish.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <template>
  2. <view class="container">
  3. <view class="top-box">
  4. <u--textarea border='none' v-model="textContent" count placeholder="写下自己的感受分享给更多人"></u--textarea>
  5. <view class="upload-box">
  6. <image class="upd-img" v-for="item of fileList" :src="item" mode="aspectFill"></image>
  7. <view class="upload-item" @click="handlerUploadImg" v-if="fileList.length < 9">
  8. <image class="img" src="@/static/QR57a.jpg" mode=""></image>
  9. </view>
  10. </view>
  11. </view>
  12. <view class="item-box" @click="handlerGetLocation">
  13. <view class="item-left">
  14. <u-icon name="map" size='22'></u-icon>
  15. <span class="fs-bold">你在哪里</span>
  16. <span class="left-text">(越详细越容易被推荐噢)</span>
  17. </view>
  18. <view class="item-right">
  19. <u-icon name="arrow-right"></u-icon>
  20. </view>
  21. </view>
  22. <view class="btn-box">
  23. <button class="btn" @click="handlerPublishBtn">发布</button>
  24. </view>
  25. </view>
  26. </template>
  27. <script>
  28. import {
  29. addClientContent
  30. } from '@/api/client/community.js';
  31. export default {
  32. data() {
  33. return {
  34. textContent: '',
  35. fileList: [],
  36. userId: null,
  37. uploadCount: 0
  38. }
  39. },
  40. mounted() {
  41. this.userId = this.$store.state.user.userMessage.id
  42. },
  43. methods: {
  44. // 获取当前定位
  45. handlerGetLocation() {
  46. wx.getLocation({
  47. type: 'gcj02',
  48. success(res) {
  49. console.log("@@@@res", res);
  50. },
  51. fail(error) {
  52. console.log("error", error);
  53. }
  54. })
  55. },
  56. // 点击发布按钮
  57. handlerPublishBtn() {
  58. let params = {
  59. textContent: this.textContent,
  60. status: 0, // 状态:0-正常;1-屏蔽
  61. type: 'PURE_TEXT', // 动态类型:1-纯文字;2-图片;3-视频 允许值: PURE_TEXT, PHOTO, VIDEO
  62. location: '山东潍坊', // 定位
  63. userId: this.userId,
  64. }
  65. if (this.fileList.length != 0) {
  66. params.type = 'PHOTO'
  67. params.list = []
  68. this.fileList.map((rs, idx) => {
  69. params.list.push({
  70. resourceKey: rs,
  71. sort: idx
  72. })
  73. })
  74. }
  75. addClientContent(params).then(res => {
  76. if (res.code == 200) {
  77. uni.showToast({
  78. title: '发布成功',
  79. icon: 'none'
  80. })
  81. this.textContent = ''
  82. setTimeout(() => {
  83. uni.navigateBack(-1)
  84. }, 1500)
  85. } else {
  86. uni.showToast({
  87. title: res.msg,
  88. icon: 'none'
  89. })
  90. return
  91. }
  92. })
  93. },
  94. // 上传头像
  95. async handlerUploadImg() {
  96. try {
  97. const res = await uni.showActionSheet({
  98. itemList: ['拍照', '从相册选择'],
  99. });
  100. if (res.tapIndex === 0) {
  101. // 用户选择拍照
  102. this.takePhoto();
  103. } else if (res.tapIndex === 1) {
  104. // 用户选择从相册选择
  105. this.chooseImage();
  106. }
  107. } catch (error) {
  108. console.error(error);
  109. }
  110. },
  111. // 拍照
  112. takePhoto() {
  113. uni.chooseImage({
  114. sourceType: ['camera'],
  115. count: 9 - this.uploadCount,
  116. success: res => {
  117. const tempFilePaths = res.tempFilePaths;
  118. // 调用上传图片的方法
  119. tempFilePaths.map(rs => {
  120. this.uploadAvatar(rs);
  121. })
  122. },
  123. fail: error => {
  124. console.error(error);
  125. },
  126. });
  127. },
  128. //从相册中选择
  129. chooseImage() {
  130. uni.chooseImage({
  131. sourceType: ['album'],
  132. count: 9 - this.uploadCount,
  133. success: res => {
  134. const tempFilePaths = res.tempFilePaths;
  135. tempFilePaths.map(rs => {
  136. this.uploadAvatar(rs);
  137. })
  138. // 调用上传图片的方法
  139. },
  140. fail: error => {
  141. console.error(error);
  142. },
  143. });
  144. },
  145. // 上传头像
  146. uploadAvatar(filePath) {
  147. // 在这里实现上传头像的逻辑,将filePath作为参数传入
  148. this.fileList.push(filePath)
  149. this.uploadCount = this.fileList.length
  150. console.log('@@@filePath', this.fileList);
  151. // this.queryParams.avatar = filePath;
  152. this.$forceUpdate(); // 手动触发组件的重新渲染
  153. },
  154. }
  155. }
  156. </script>
  157. <style lang="scss" scoped>
  158. .container {
  159. position: relative;
  160. background-color: #F5F7F9;
  161. height: 100vh;
  162. .top-box,
  163. .item-box {
  164. padding: 20rpx;
  165. background-color: #fff;
  166. border-radius: 20rpx;
  167. .upload-box {
  168. margin-top: 20rpx;
  169. display: flex;
  170. flex-wrap: wrap;
  171. .upd-img {
  172. width: 140rpx;
  173. height: 140rpx;
  174. border-radius: 20rpx;
  175. margin: 10rpx;
  176. }
  177. .upload-item {
  178. width: 140rpx;
  179. height: 140rpx;
  180. border: 2rpx solid #000;
  181. border-radius: 20rpx;
  182. display: flex;
  183. flex-direction: column;
  184. justify-content: center;
  185. margin: 10rpx;
  186. .img {
  187. width: 80rpx;
  188. height: 80rpx;
  189. margin: 0 auto;
  190. }
  191. }
  192. }
  193. }
  194. .item-box {
  195. display: flex;
  196. justify-content: space-between;
  197. align-items: center;
  198. margin-top: 20rpx;
  199. .item-left {
  200. display: flex;
  201. align-items: center;
  202. }
  203. }
  204. .btn-box {
  205. position: absolute;
  206. bottom: 0;
  207. background-color: #fff;
  208. height: 100rpx;
  209. padding: 20rpx;
  210. width: 95%;
  211. display: flex;
  212. justify-content: center;
  213. .btn {
  214. width: 80%;
  215. border-radius: 20rpx;
  216. font-size: 28rpx;
  217. color: #000;
  218. background-color: #FECF4C;
  219. height: 80rpx;
  220. line-height: 80rpx;
  221. }
  222. }
  223. .fs-bold {
  224. font-weight: bold;
  225. font-size: 28rpx;
  226. color: #000;
  227. margin-left: 10rpx;
  228. }
  229. .left-text {
  230. color: #a6a7a8;
  231. font-size: 28rpx;
  232. margin-left: 10rpx;
  233. }
  234. }
  235. </style>