addPlan.vue 7.3 KB

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