addPlan.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <template>
  2. <view class="plan">
  3. <u-form :model="form" ref="uForm">
  4. <u-form-item label="名称:" prop="title" label-position="top">
  5. <u-input v-model="form.title" placeholder="请输入名称" :border="true" />
  6. </u-form-item>
  7. <u-form-item label="描述:" prop="description" label-position="top">
  8. <view class="box">
  9. <jinEdit placeholder="请输入描述" @editOk="editOk" :uploadFileUrl="config.uploadUrl"></jinEdit>
  10. </view>
  11. </u-form-item>
  12. <u-form-item label="创业计划书:" prop="planBookUrl" label-position="top">
  13. <view class="upload">
  14. <view class="upload-box" @click="uploadFile">
  15. <text>+上传文件,格式:pdf</text>
  16. </view>
  17. <view class="file-list">
  18. <view class="file-list-item" v-for="(item, index) in fileList" :key="index">
  19. <view>{{ item.fileName }}</view>
  20. <view class="close">
  21. <u-icon name="close" size="20" color="#9B9B9B" @click="delFile(index)"></u-icon>
  22. </view>
  23. </view>
  24. </view>
  25. </view>
  26. </u-form-item>
  27. </u-form>
  28. <view class="plan-bottom">
  29. <view class="plan-bottom-box" @click="submitPlan">提交</view>
  30. </view>
  31. <u-toast ref="uToast" />
  32. </view>
  33. </template>
  34. <script>
  35. import jinEdit from '../../../components/jin-edit/jin-edit.vue';
  36. export default {
  37. components: {
  38. jinEdit
  39. },
  40. data() {
  41. return {
  42. form: {
  43. title: '',
  44. description: '',
  45. planBookUrl: '',
  46. planBookName: ''
  47. },
  48. rules: {
  49. title: [
  50. {
  51. required: true,
  52. message: '请输入名称',
  53. trigger: ['change', 'blur'],
  54. }
  55. ],
  56. description: [
  57. {
  58. required: true,
  59. message: '请输入描述',
  60. trigger: ['change', 'blur'],
  61. }
  62. ],
  63. planBookUrl: [
  64. {
  65. required: true,
  66. message: '请上传企业计划书',
  67. trigger: ['change', 'blur'],
  68. }
  69. ]
  70. },
  71. fileList: []
  72. }
  73. },
  74. mounted() {
  75. // this.$nextTick(function() {
  76. // const input = document.createElement('input')
  77. // input.style.width = "100%";
  78. // input.type = 'file' //添加file类型
  79. // // input.multiple = 'multiple' // 可以选择多个
  80. // input.accept = ".pdf" // 限制只能上传PDF文件,可以不限制,能上传任何类型
  81. // input.style.height = "100%";
  82. // input.style.position = "absolute";
  83. // input.style.top = "0";
  84. // input.style.right = "0";
  85. // input.style.opacity = "0";
  86. // input.style.overflow = "hidden"; //防止注意input 元素溢出
  87. // input.id = 'file';
  88. // var _this = this;
  89. // setTimeout(() => {
  90. // this.$refs.input.$el.appendChild(
  91. // input
  92. // ); // 这里可能会报$el找不到错误,所以加了延时器,报错的原因是this.$refs.input没有找到为null,所以需要等页面结构加载完后再将其添加进去
  93. // input.onchange = (event) => { // 点击上传选择文件
  94. // var file = event.target.files;
  95. // if (file.length > 0) {
  96. // file.forEach(item => { // 因为后台限制,只能一个一个文件的上传,所以当选择多个文件后,需要遍历一个一个调用上传接口
  97. // _this.uploadAPI(item); // 上传图片
  98. // })
  99. // }
  100. // }
  101. // }, 1000)
  102. // })
  103. },
  104. onReady() {
  105. this.$refs.uForm.setRules(this.rules);
  106. },
  107. methods: {
  108. /**
  109. * 富文本返回值
  110. * @param {Object} res
  111. */
  112. editOk(res) {
  113. this.form.description = res?.html
  114. },
  115. /**
  116. * 上传文件
  117. */
  118. uploadFile() {
  119. uni.chooseFile({
  120. count: 1,
  121. extension: ['.pdf'],
  122. success: async (res) => {
  123. var tempFilePaths = res.tempFilePaths;
  124. uni.showLoading({
  125. title: '正在上传中...'
  126. })
  127. for (let temp of tempFilePaths) {
  128. // 图片上传服务器
  129. await uni.uploadFile({
  130. url: this.config.uploadUrl,
  131. filePath: temp,
  132. success: res => {
  133. const data = JSON.parse(res.data).data
  134. if (data.suffix === 'pdf') {
  135. this.fileList = [{ ...data }]
  136. this.form.planBookUrl = data.url
  137. this.form.planBookName = data.fileName
  138. } else {
  139. uni.showToast({
  140. title: '只能上传pdf文件',
  141. duration: 2000
  142. })
  143. }
  144. uni.hideLoading()
  145. },
  146. fail: err => {
  147. uni.hideLoading()
  148. }
  149. });
  150. }
  151. }
  152. });
  153. },
  154. uploadAPI(path) {
  155. uni.showLoading({
  156. title: '上传中'
  157. })
  158. var _this = this
  159. var fData = new FormData();
  160. fData.append("file", path);
  161. var xhr = new XMLHttpRequest();
  162. var surl = this.config.uploadUrl; // 文件上传地址
  163. xhr.open("POST", surl, true);
  164. xhr.onload = function(e) {
  165. let response = JSON.parse(e.currentTarget.response)
  166. let file = response.data.url; //获取服务端传过来的文件地址
  167. };
  168. xhr.onreadystatechange = () => {
  169. if (xhr.readyState == 4 && xhr.status == 200) { //上传后台成功
  170. uni.hideLoading()
  171. const res = JSON.parse(xhr.responseText)
  172. _this.fileList.push(res.data); // 上传成功后放进fileList数组用于展示
  173. } else {
  174. uni.hideLoading()
  175. }
  176. }
  177. // 这里设置请求头,做的时候遇到一个问题,明明上传的是FormData,可是在请求中变成了request payload,后台需要的是FormData,解决方法,设置enctype为multipart/form-data,不要设置Content-Type,切记直接不设置Content-Type
  178. xhr.setRequestHeader('enctype', "multipart/form-data");
  179. xhr.send(fData)
  180. },
  181. /**
  182. * 删除文件
  183. * @param { Number } index
  184. */
  185. delFile(index) {
  186. this.fileList.splice(index, 1)
  187. },
  188. submitPlan() {
  189. this.$refs.uForm.validate(valid => {
  190. if (valid) {
  191. this.$u.api.entrepreneurship.addEntrepreneurshipApi(this.form).then(res => {
  192. if (res.code === 200) {
  193. this.$refs.uToast.show({
  194. title: '提交成功!',
  195. type: 'success',
  196. url: 'pages/entrepreneurshipGuidelines/entrepreneurshipGuidelines'
  197. })
  198. } else {
  199. this.$refs.uToast.show({
  200. title: res.msg,
  201. type: 'error'
  202. })
  203. }
  204. })
  205. }
  206. });
  207. }
  208. }
  209. }
  210. </script>
  211. <style lang="scss" scoped>
  212. .plan {
  213. padding: 34rpx 30rpx;
  214. font-family: PingFangSC-Regular, PingFang SC;
  215. .box {
  216. border: solid 1px #dcdfe6;
  217. }
  218. .upload {
  219. width: 100%;
  220. display: flex;
  221. flex-direction: column;
  222. &-box {
  223. width: 100%;
  224. height: 59rpx;
  225. line-height: 59rpx;
  226. text-align: center;
  227. border: solid 1px #D1D1D1;
  228. border-radius: 5rpx;
  229. color: #9B9B9B;
  230. font-size: 28rpx;
  231. position: relative;
  232. }
  233. .file-list {
  234. margin-top: 30rpx;
  235. .file-list-item {
  236. height: 59rpx;
  237. line-height: 59rpx;
  238. display: flex;
  239. justify-content: center;
  240. border: dashed 1px #0091FF;
  241. color: #008FFF;
  242. font-size: 28rpx;
  243. position: relative;
  244. .close {
  245. position: absolute;
  246. right: 30rpx;
  247. top: -2rpx;
  248. }
  249. }
  250. }
  251. }
  252. .plan-bottom {
  253. position: fixed;
  254. bottom: 0;
  255. width: calc(100% - 60rpx);
  256. height: 166rpx;
  257. background-color: #fff;
  258. display: flex;
  259. justify-content: center;
  260. &-box {
  261. background-color: #709078;
  262. width: 100%;
  263. height: 80rpx;
  264. line-height: 80rpx;
  265. margin-top: 26rpx;
  266. text-align: center;
  267. font-size: 30rpx;
  268. color: #fff;
  269. }
  270. }
  271. }
  272. </style>