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

265 lines
6.1 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
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 < 9">
  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. p_list: [],
  34. tempFilesList: []
  35. }
  36. },
  37. watch: {
  38. content(val){
  39. if(String(val).length){
  40. this.backInquiry = true;
  41. }else{
  42. this.backInquiry = false;
  43. }
  44. }
  45. },
  46. onLoad(){
  47. },
  48. methods: {
  49. send() {
  50. if(!this.content) {
  51. this.$msg('请输入文字内容!');
  52. return
  53. }
  54. if(this.tempFilesList.length == 0) {
  55. this.$msg('请上传图片!');
  56. return
  57. }
  58. let p_list = [];
  59. this.tempFilesList.forEach((item,index) => {
  60. p_list.push(this.uploadDiscover(item.path))
  61. })
  62. this.p_list = p_list;
  63. Promise.all(this.p_list).then(result => {
  64. this.$http
  65. .post({
  66. api: 'api/discover/create',
  67. data: {
  68. images: result,
  69. content: this.content
  70. },
  71. header: {
  72. Authorization: this.$cookieStorage.get('user_token')
  73. },
  74. })
  75. .then(res => {
  76. if (res.data.code == 200) {
  77. if (res.data.status) {
  78. this.content = '';
  79. this.image_list = [];
  80. this.$msg('发布成功',{duration: 2500}).then(() => {
  81. this.$toBack();
  82. })
  83. } else {
  84. wx.showModal({
  85. content: res.data.message || '发布失败,请稍后尝试!',
  86. showCancel: false
  87. });
  88. }
  89. } else {
  90. wx.showModal({
  91. content: res.data.message || '发布失败,请稍后尝试!',
  92. showCancel: false
  93. });
  94. }
  95. wx.hideLoading();
  96. })
  97. .catch(() => {
  98. wx.hideLoading();
  99. wx.showModal({
  100. content: '请求失败',
  101. showCancel: false
  102. });
  103. });
  104. })
  105. },
  106. uploadDiscover(img) {
  107. console.log('=========',img)
  108. return new Promise((resolve, reject) => {
  109. uni.uploadFile({
  110. header: {
  111. Authorization: this.$cookieStorage.get('user_token')
  112. },
  113. url: this.$config.GLOBAL.baseUrl+'api/discover/upload_image',
  114. filePath: img,
  115. fileType: 'image',
  116. name: 'image',
  117. success: res => {
  118. var result = JSON.parse(res.data).data.url;
  119. resolve(result);
  120. },
  121. fail: err => {
  122. console.log(err)
  123. reject(err)
  124. }
  125. })
  126. })
  127. },
  128. // 上传凭证图片
  129. uploadImage(){
  130. let current_count = this.image_count - this.image_list.length;
  131. if(current_count == 0) return;
  132. uni.chooseImage({
  133. count: current_count,
  134. complete: result => {
  135. if(result.errMsg == "chooseImage:fail cancel"){
  136. return; // 取消选择图片
  137. }
  138. let tempFiles = result.tempFiles;
  139. let image_list = [];
  140. let overstep = false;
  141. tempFiles.map(item => {
  142. // 上传的图片需小于10MB
  143. if(item.size < 1000000){
  144. image_list.push(item.path);
  145. }else{
  146. overstep = true;
  147. }
  148. })
  149. if(overstep){
  150. uni.showModal({
  151. title: '温馨提示',
  152. content: '您上传的图片含有超出10M大小的限制,请优化大小后再上传!',
  153. showCancel: false
  154. })
  155. }
  156. this.image_list.push(...image_list);
  157. this.tempFilesList.push(...tempFiles);
  158. }
  159. })
  160. },
  161. // 预览图片
  162. lookImage(current){
  163. if(this.image_list.length <= 0) return;
  164. this.$u.throttle(() => {
  165. uni.previewImage({
  166. urls: this.image_list,
  167. current: current
  168. });
  169. }, 500);
  170. },
  171. // 移除图片
  172. removeInage(current){
  173. uni.showModal({
  174. title: '提示',
  175. content: '即将取消上传这张图片,请确认?',
  176. success: e => {
  177. if (!e.confirm) return;
  178. this.image_list.splice(current, 1);
  179. this.uploadImg.splice(current, 1);
  180. this.tempFilesList.splice(current, 1);
  181. }
  182. });
  183. }
  184. }
  185. }
  186. </script>
  187. <style lang="scss" scoped="scoped">
  188. .content{
  189. width: 750rpx;
  190. height: max-content;
  191. box-sizing: border-box;
  192. padding: 30rpx 32rpx;
  193. .textarea{
  194. width: 100%;
  195. height: 160rpx;
  196. }
  197. }
  198. .my-images{
  199. display: flex;
  200. flex-wrap: wrap;
  201. margin-top: 30rpx;
  202. margin-bottom: 18rpx;
  203. .my-image-item{
  204. width: 220rpx;
  205. height: 220rpx;
  206. background: #DDDDDD;
  207. position: relative;
  208. margin-right: 12rpx;
  209. &:nth-child(3n){
  210. margin-right: 0rpx;
  211. }
  212. &:nth-child(n+4){
  213. margin-top: 12rpx;
  214. }
  215. image{
  216. width: 100%;
  217. height: 100%;
  218. }
  219. .remove-image{
  220. position: absolute;
  221. right: 0;
  222. top: 0;
  223. width: 50rpx;
  224. height: 50rpx;
  225. display: flex;
  226. align-items: center;
  227. justify-content: center;
  228. color: #e74c3c;
  229. font-size: 40rpx;
  230. background-color: rgba(0,0,0,0.4);
  231. }
  232. }
  233. .my-image-item-after{
  234. position: absolute;
  235. width: 100%;
  236. height: 100%;
  237. display: flex;
  238. justify-content: center;
  239. align-items: center;
  240. font-size: 60rpx;
  241. color: #999999;
  242. }
  243. }
  244. .my-btn{
  245. margin-top: 30rpx;
  246. background-color: #15716E;
  247. color: #FFFFFF;
  248. }
  249. /deep/.textarea-placeholder{
  250. color: #999999;
  251. line-height: 1;
  252. vertical-align: middle;
  253. }
  254. </style>