addPlan.vue 7.7 KB

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