金诚优选前端代码
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.

236 lines
5.4 KiB

4 years ago
  1. <template>
  2. <view>
  3. <lf-nav title="发表" :showIcon="true" :backInquiry="backInquiry"></lf-nav>
  4. <view class="content">
  5. <!-- 文本输入 -->
  6. <textarea class="textarea" placeholder="这一刻的想法…" v-model="content"></textarea>
  7. <!-- 上传图片 -->
  8. <view class="my-images">
  9. <view class="my-image-item" @click="uploadImage" v-if="image_list.length < 6">
  10. <text class="lf-iconfont icon-jia my-image-item-after"></text>
  11. </view>
  12. <view class="my-image-item" v-for="(item, index) in image_list" :key="index" @click="lookImage(index)">
  13. <image :src="item" mode="aspectFill"></image>
  14. <view class="remove-image" @click.stop="removeInage(index)">
  15. <text class="lf-iconfont icon-shanchu"></text>
  16. </view>
  17. </view>
  18. </view>
  19. <!-- 发表按钮 -->
  20. <button class="my-btn" hover-class="lf-opacity" @click="send()">发表</button>
  21. </view>
  22. </view>
  23. </template>
  24. <script>
  25. export default {
  26. data(){
  27. return {
  28. image_list: [],
  29. image_count: 6,
  30. content: '',
  31. backInquiry: false,
  32. uploadImg: []
  33. }
  34. },
  35. watch: {
  36. content(val){
  37. if(String(val).length){
  38. this.backInquiry = true;
  39. }else{
  40. this.backInquiry = false;
  41. }
  42. }
  43. },
  44. onLoad(){
  45. },
  46. methods: {
  47. send() {
  48. if(!this.content) {
  49. this.$msg('请输入文字内容!');
  50. return
  51. }
  52. if(this.uploadImg.length == 0) {
  53. this.$msg('请上传图片!');
  54. return
  55. }
  56. this.$http
  57. .post({
  58. api: 'api/discover/create',
  59. data: {
  60. images: this.uploadImg,
  61. content: this.content
  62. },
  63. header: {
  64. Authorization: this.$cookieStorage.get('user_token')
  65. },
  66. })
  67. .then(res => {
  68. if (res.data.code == 200) {
  69. if (res.data.status) {
  70. this.$msg('发布成功').then(() => {
  71. this.$toBack();
  72. })
  73. } else {
  74. wx.showModal({
  75. content: res.data.message || '发布失败,请稍后尝试!',
  76. showCancel: false
  77. });
  78. }
  79. } else {
  80. wx.showModal({
  81. content: res.data.message || '发布失败,请稍后尝试!',
  82. showCancel: false
  83. });
  84. }
  85. wx.hideLoading();
  86. })
  87. .catch(() => {
  88. wx.hideLoading();
  89. wx.showModal({
  90. content: '请求失败',
  91. showCancel: false
  92. });
  93. });
  94. },
  95. uploadDiscover(img) {
  96. uni.uploadFile({
  97. header: {
  98. Authorization: this.$cookieStorage.get('user_token')
  99. },
  100. url: this.$config.GLOBAL.baseUrl+'api/discover/upload_image',
  101. filePath: img,
  102. fileType: 'image',
  103. name: 'image',
  104. success: res => {
  105. var result = JSON.parse(res.data).data.url;
  106. this.uploadImg.push(result)
  107. console.log('上传后的图片路径',this.uploadImg)
  108. },
  109. fail: err => {
  110. console.log(err)
  111. }
  112. })
  113. },
  114. // 上传凭证图片
  115. uploadImage(){
  116. let current_count = this.image_count - this.image_list.length;
  117. if(current_count == 0) return;
  118. uni.chooseImage({
  119. count: 1,
  120. complete: result => {
  121. if(result.errMsg == "chooseImage:fail cancel"){
  122. return; // 取消选择图片
  123. }
  124. let tempFiles = result.tempFiles;
  125. let image_list = [];
  126. let overstep = false;
  127. tempFiles.map(item => {
  128. // 上传的图片需小于10MB
  129. if(item.size < 1000000){
  130. image_list.push(item.path);
  131. }else{
  132. overstep = true;
  133. }
  134. })
  135. if(overstep){
  136. uni.showModal({
  137. title: '温馨提示',
  138. content: '您上传的图片含有超出10M大小的限制,请优化大小后再上传!',
  139. showCancel: false
  140. })
  141. }
  142. this.image_list.push(...image_list);
  143. this.uploadDiscover(tempFiles[0].path)
  144. console.log('当前选择的图片列表',this.image_list);
  145. }
  146. })
  147. },
  148. // 预览图片
  149. lookImage(current){
  150. if(this.image_list.length <= 0) return;
  151. this.$u.throttle(() => {
  152. uni.previewImage({
  153. urls: this.image_list,
  154. current: current
  155. });
  156. }, 500);
  157. },
  158. // 移除图片
  159. removeInage(current){
  160. this.image_list.splice(current, 1);
  161. this.uploadImg.splice(current, 1);
  162. console.log('删除的图片列表',this.uploadImg);
  163. }
  164. }
  165. }
  166. </script>
  167. <style lang="scss" scoped="scoped">
  168. .content{
  169. width: 750rpx;
  170. height: max-content;
  171. box-sizing: border-box;
  172. padding: 30rpx 32rpx;
  173. .textarea{
  174. width: 100%;
  175. height: 160rpx;
  176. }
  177. }
  178. .my-images{
  179. display: flex;
  180. flex-wrap: wrap;
  181. margin-top: 30rpx;
  182. margin-bottom: 18rpx;
  183. .my-image-item{
  184. width: 220rpx;
  185. height: 220rpx;
  186. background: #DDDDDD;
  187. position: relative;
  188. margin-right: 12rpx;
  189. &:nth-child(3n){
  190. margin-right: 0rpx;
  191. }
  192. &:nth-child(n+4){
  193. margin-top: 12rpx;
  194. }
  195. image{
  196. width: 100%;
  197. height: 100%;
  198. }
  199. .remove-image{
  200. position: absolute;
  201. right: -4rpx;
  202. top: -18rpx;
  203. color: #e74c3c;
  204. font-size: 40rpx;
  205. padding: 8rpx;
  206. }
  207. }
  208. .my-image-item-after{
  209. position: absolute;
  210. width: 100%;
  211. height: 100%;
  212. display: flex;
  213. justify-content: center;
  214. align-items: center;
  215. font-size: 60rpx;
  216. color: #999999;
  217. }
  218. }
  219. .my-btn{
  220. margin-top: 30rpx;
  221. background-color: #15716E;
  222. color: #FFFFFF;
  223. }
  224. /deep/.textarea-placeholder{
  225. color: #999999;
  226. line-height: 1;
  227. vertical-align: middle;
  228. }
  229. </style>