addPlan.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 ${vuex_token}`
  132. },
  133. success: res => {
  134. const data = JSON.parse(res.data).data
  135. if (data.suffix === 'pdf') {
  136. this.fileList = [{
  137. ...data
  138. }]
  139. this.form.planBookUrl = data.url
  140. this.form.planBookName = data.fileName
  141. } else {
  142. uni.showToast({
  143. title: '只能上传pdf文件',
  144. duration: 2000
  145. })
  146. }
  147. uni.hideLoading()
  148. },
  149. fail: err => {
  150. uni.hideLoading()
  151. }
  152. });
  153. }
  154. }
  155. });
  156. },
  157. uploadAPI(path) {
  158. uni.showLoading({
  159. title: '上传中'
  160. })
  161. var _this = this
  162. var fData = new FormData();
  163. fData.append("file", path);
  164. var xhr = new XMLHttpRequest();
  165. var surl = this.config.uploadUrl; // 文件上传地址
  166. xhr.open("POST", surl, true);
  167. xhr.onload = function(e) {
  168. let response = JSON.parse(e.currentTarget.response)
  169. let file = response.data.url; //获取服务端传过来的文件地址
  170. };
  171. xhr.onreadystatechange = () => {
  172. if (xhr.readyState == 4 && xhr.status == 200) { //上传后台成功
  173. uni.hideLoading()
  174. const res = JSON.parse(xhr.responseText)
  175. _this.fileList.push(res.data); // 上传成功后放进fileList数组用于展示
  176. } else {
  177. uni.hideLoading()
  178. }
  179. }
  180. // 这里设置请求头,做的时候遇到一个问题,明明上传的是FormData,可是在请求中变成了request payload,后台需要的是FormData,解决方法,设置enctype为multipart/form-data,不要设置Content-Type,切记直接不设置Content-Type
  181. xhr.setRequestHeader('enctype', "multipart/form-data");
  182. xhr.send(fData)
  183. },
  184. /**
  185. * 删除文件
  186. * @param { Number } index
  187. */
  188. delFile(index) {
  189. this.fileList.splice(index, 1)
  190. },
  191. submitPlan() {
  192. this.$refs.uForm.validate(valid => {
  193. if (valid) {
  194. this.$u.api.entrepreneurship.addEntrepreneurshipApi(this.form).then(res => {
  195. if (res.code === 200) {
  196. this.$refs.uToast.show({
  197. title: '提交成功!',
  198. type: 'success',
  199. url: 'pages/entrepreneurshipGuidelines/entrepreneurshipGuidelines'
  200. })
  201. } else {
  202. this.$refs.uToast.show({
  203. title: res.msg,
  204. type: 'error'
  205. })
  206. }
  207. })
  208. }
  209. });
  210. }
  211. }
  212. }
  213. </script>
  214. <style lang="scss" scoped>
  215. .plan {
  216. padding: 34rpx 30rpx;
  217. font-family: PingFangSC-Regular, PingFang SC;
  218. .box {
  219. border: solid 1px #dcdfe6;
  220. }
  221. .upload {
  222. width: 100%;
  223. display: flex;
  224. flex-direction: column;
  225. &-box {
  226. width: 100%;
  227. height: 59rpx;
  228. line-height: 59rpx;
  229. text-align: center;
  230. border: solid 1px #D1D1D1;
  231. border-radius: 5rpx;
  232. color: #9B9B9B;
  233. font-size: 28rpx;
  234. position: relative;
  235. }
  236. .file-list {
  237. margin-top: 30rpx;
  238. .file-list-item {
  239. height: 59rpx;
  240. line-height: 59rpx;
  241. display: flex;
  242. justify-content: center;
  243. border: dashed 1px #0091FF;
  244. color: #008FFF;
  245. font-size: 28rpx;
  246. position: relative;
  247. .close {
  248. position: absolute;
  249. right: 30rpx;
  250. top: -2rpx;
  251. }
  252. }
  253. }
  254. }
  255. .plan-bottom {
  256. position: fixed;
  257. bottom: 0;
  258. width: calc(100% - 60rpx);
  259. height: 166rpx;
  260. background-color: #fff;
  261. display: flex;
  262. justify-content: center;
  263. &-box {
  264. background-color: #709078;
  265. width: 100%;
  266. height: 80rpx;
  267. line-height: 80rpx;
  268. margin-top: 26rpx;
  269. text-align: center;
  270. font-size: 30rpx;
  271. color: #fff;
  272. }
  273. }
  274. }
  275. </style>