当前位置:   article > 正文

uniapp小程序上传pdf文件

uniapp小程序上传pdf文件

  1. <template>
  2. <view class="mainInnBox">
  3. <view class="formBox">
  4. <!-- 注意,如果需要兼容微信小程序,最好通过setRules方法设置rules规则 -->
  5. <u-form :model="form" ref="uForm" :rules="rules">
  6. <u-form-item label="故障车辆" labelWidth="80px" prop="licensePlate" borderBottom>
  7. <u--input
  8. :placeholder="vehOptions.licensePlate"
  9. placeholder-style="color: #333;"
  10. disabledColor="#ffffff"
  11. border="none"
  12. inputAlign="right"
  13. disabled
  14. readonly
  15. />
  16. </u-form-item>
  17. <u-form-item label="发生时间" labelWidth="80px" prop="occurreTime" borderBottom @click="showDate = true">
  18. <!-- <span class="redStar">*</span> -->
  19. <u--input
  20. v-model.trim="form.occurreTime"
  21. placeholder="请选择发生时间"
  22. disabledColor="#ffffff"
  23. border="none"
  24. inputAlign="right"
  25. disabled
  26. readonly
  27. />
  28. </u-form-item>
  29. <u-form-item label="事件类型" labelWidth="80px" prop="typeName" borderBottom @click="showSelect = true">
  30. <!-- <span class="redStar">*</span> -->
  31. <u-input
  32. v-model="form.typeName"
  33. placeholder="请选择事件类型"
  34. disabledColor="#ffffff"
  35. border="none"
  36. inputAlign="right"
  37. disabled
  38. readonly
  39. >
  40. <!-- <template v-show="form.publishCycle" slot="suffix">
  41. <span></span>
  42. </template> -->
  43. </u-input>
  44. </u-form-item>
  45. <view class="form_label">描述</view>
  46. <u-form-item label=" " labelWidth="0" labelPosition="top" prop="description" borderBottom>
  47. <u--textarea
  48. v-model.trim="form.description"
  49. border="none"
  50. placeholder="请输入描述..."
  51. placeholderStyle="#999999"
  52. maxlength="100"
  53. height="50"
  54. :count="false"
  55. borderBottom
  56. />
  57. </u-form-item>
  58. <view class="form_label">附件文件</view>
  59. <view class="form_label_tip">文件大小不大于10M,支持PDF</view>
  60. <!-- 上传文件展示 -->
  61. <view class="uploadContent">
  62. <view class="uploadFileBox" v-if="pdfInfo.length!=0">
  63. <view class="uploadTexts" @click="jump(pdfInfo[0].url)">
  64. {{pdfInfo[0].name}}
  65. </view>
  66. <u-icon name="close" @click="deleteFile()"></u-icon>
  67. </view>
  68. <view v-else class="uploadChoose" @click="selectFile()">
  69. <u-icon name="plus"></u-icon>
  70. </view>
  71. </view>
  72. <!-- 上传文件按钮 -->
  73. </u-form>
  74. </view>
  75. <view class="btnBox">
  76. <view class="btn" @click="submitFunc">提交</view>
  77. </view>
  78. <view>
  79. <!-- 发生时间 -->
  80. <u-datetime-picker :show="showDate" v-model="datetime" mode="datetime" @cancel="closeDate" @confirm="sureDate"></u-datetime-picker>
  81. <!-- 事件类型 -->
  82. <u-picker :show="showSelect" :columns="columnsSelect" keyName="label" @cancel="closeSelect" @confirm="confirmSelect"></u-picker>
  83. </view>
  84. <u-modal
  85. :show="successModalShow"
  86. confirmText="理赔记录"
  87. cancelText="返回首页"
  88. @confirm="confirmFunc"
  89. @cancel="cancelFunc"
  90. :showConfirmButton="true"
  91. :showCancelButton="true"
  92. confirmColor="#ffffff"
  93. cancelColor="#333"
  94. >
  95. <view class="slot-content">
  96. <u-icon name="checkmark-circle-fill" color="#70b603" size="28" label="上报成功" labelPos="bottom" labelSize="16px" labelColor="#333"></u-icon>
  97. <view style="text-align: center;padding: 30rpx 0 0; font-size: 24rpx;">出险信息已上报</view>
  98. </view>
  99. </u-modal>
  100. </view>
  101. </template>
  102. <script>
  103. import { getToken } from '@/assets/scripts/auth'
  104. export default {
  105. data() {
  106. return {
  107. imgUrl: this.$imgUrl,
  108. recordId: '', // 保险记录id
  109. vehOptions: {},
  110. showDate: false, // 发生时间选择
  111. datetime: Number(new Date()),
  112. showSelect: false, // 事件类型选择
  113. columnsSelect: [
  114. [{label: '出险', value: 1},{label: '维修', value: 2}, {label: '理赔', value: 3}]
  115. ],
  116. form: {
  117. occurreTime: '',
  118. typeName: '',
  119. description: '',
  120. },
  121. rules: {
  122. occurreTime: [
  123. { required: true, message: '请选择发生时间', trigger: ['change']},
  124. ],
  125. typeName: [
  126. { required: true, message: '请选择事件类型', trigger: ['change']},
  127. ],
  128. description: [
  129. { required: false, message: '请输入描述', trigger: ['blur', 'change']},
  130. { min: 1, max: 100, message: '长度在100个字符之间'},
  131. ],
  132. },
  133. btnStatus: false,
  134. successModalShow: false,
  135. pdfInfo: []
  136. }
  137. },
  138. onShow() {
  139. },
  140. onLoad(option) {
  141. // 点击理赔记录-上报--跳转过来。
  142. console.log(option)
  143. this.recordId = option.recordId
  144. this.vehOptions = option
  145. },
  146. onReady() {
  147. this.$nextTick(()=>{
  148. //如果需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则。
  149. this.$refs.uForm.setRules(this.rules)
  150. })
  151. },
  152. methods: {
  153. // 发生时间选择
  154. closeDate () {
  155. this.showDate = false
  156. },
  157. sureDate (e) {
  158. // console.log(e, this.value1)
  159. this.form.occurreTime = uni.$u.timeFormat(e.value, 'yyyy-mm-dd hh:MM:ss')
  160. this.$refs.uForm.clearValidate('occurreTime')
  161. this.showDate = false
  162. },
  163. // 事件类型选择
  164. closeSelect() {
  165. this.showSelect = false
  166. },
  167. confirmSelect(e) {
  168. // console.log(e)
  169. this.form.type = e.value[0].value
  170. this.form.typeName = e.value[0].label
  171. this.$refs.uForm.clearValidate('typeName')
  172. this.showSelect = false
  173. },
  174. // 上传pdf
  175. selectFile(){
  176. // console.log('111', this.pdfInfo)
  177. if(this.pdfInfo.length != 0){ // this.pdfInfo 要求不可重复上传
  178. this.$toast('如果重新上传请先删除已有的附件~')
  179. return
  180. }
  181. let that = this
  182. uni.chooseMessageFile({
  183. count: 1, //最多可以选择的文件个数,可以 1
  184. type: 'file', //所选的文件的类型,具体看官方文档
  185. extension: ['.pdf'], //文件类型, '.docx''.doc',
  186. success (res) {
  187. // console.log('上传', res)
  188. // // tempFilePath可以作为img标签的src属性显示图片
  189. const tempFilePaths = res.tempFiles[0].path
  190. let filename = res.tempFiles[0].name; //用于页面显示的名字
  191. // console.log(filename)
  192. // 这一步判断可以省略,如果需求没有格式要求的话
  193. if(filename.indexOf(".pdf")==-1){
  194. that.$toast('暂时仅支持pdf格式的文件')
  195. return
  196. } else if (res.tempFiles[0].size > (10 * 1024 * 1024)) { //这里限制了文件的大小和具体文件类型,如果不限制文件类型则去掉'|| filename.indexOf(".pdf") == -1'
  197. that.$toast('文件大小不能超过10MB')
  198. // wx.showToast({
  199. // title: '文件大小不能超过10MB',
  200. // icon: "none",
  201. // duration: 2000,
  202. // mask: true
  203. // })
  204. } else {
  205. // console.log("开始上传")
  206. uni.uploadFile({
  207. url: uni.$u.http.config.baseURL + 'file/upload', // '这里是您后台提供文件上传的API':上传的路径
  208. filePath: tempFilePaths, //刚刚在data保存的文件路径
  209. name: 'file', //后台获取的凭据
  210. formData:{ //如果是需要带参数,请在formData里面添加,不需要就去掉这个就可以的
  211. fileGroup: 'leasContract'
  212. },
  213. header: {
  214. 'Content-Type': 'multipart/form-data',
  215. 'Authorization': 'Bearer ' + getToken(),
  216. },
  217. success: (uploadFileRes) => {
  218. // console.log(uploadFileRes)
  219. if (uploadFileRes.errMsg === 'uploadFile:ok') {
  220. let result = JSON.parse(uploadFileRes.data)
  221. // console.log('=====', result)
  222. that.pdfInfo.push({name: filename, url: result.data.previewUrl})
  223. that.$forceUpdate() //有时候页面渲染不上,这里强制刷新
  224. if (result.code === 200 && result.headImg) {
  225. this.$toast('保存成功')
  226. }
  227. }
  228. }
  229. })
  230. // console.log('上传到服务器')
  231. }
  232. },
  233. fail: (err) => {
  234. console.log(err, 'err');
  235. that.$forceUpdate()
  236. }
  237. })
  238. },
  239. // 删除pdf
  240. deleteFile() {
  241. this.pdfInfo = []
  242. },
  243. // 预览pdf
  244. jump(linkUrl) {
  245. // console.log("发送跳转页面地址112:" + linkUrl)
  246. if(linkUrl){
  247. let linkUrlNew = encodeURIComponent(linkUrl)
  248. // console.log("发送跳转页面地址111:" + linkUrlNew )
  249. uni.navigateTo({
  250. url: '/subPackages/home/claim/index?url='+ linkUrlNew
  251. })
  252. }
  253. },
  254. // 提交
  255. submitFunc() {
  256. if (this.btnStatus) {
  257. return
  258. }
  259. let that = this
  260. // 限制用户多次触发
  261. this.btnStatus = true
  262. that.$refs.uForm.validate().then(res => {
  263. let params = {
  264. recordId: that.recordId,
  265. occurreTime: that.form.occurreTime,
  266. type: that.form.type,
  267. description: that.form.description
  268. }
  269. // 附件pdf
  270. if(this.pdfInfo.length>0) {
  271. params.attachment = this.pdfInfo[0].picUrl
  272. params.attachmentName = this.pdfInfo[0].name
  273. }
  274. console.log('提交的表单', params)
  275. uni.showLoading({
  276. title: '提交中'
  277. })
  278. this.$http.post('/mobile/leaseContract/insurance/claim', params).then((res) => {
  279. if (res.code === 200) {
  280. // console.log(res)
  281. uni.hideLoading()
  282. this.successModalShow = true
  283. setTimeout(function() {
  284. that.btnStatus = false
  285. }, 1100)
  286. }
  287. })
  288. .catch((error) => {
  289. console.log(error)
  290. uni.hideLoading()
  291. this.$toast(error.msg)
  292. // 填好提交,但是接口报错,这里要释放按钮限制
  293. that.btnStatus = false
  294. })
  295. }).catch(errors => {
  296. // uni.$u.toast('校验失败')
  297. // 没有填写信息,就点击了提交按钮,校验不通过,然后填好信息后,再点击提交
  298. that.btnStatus = false
  299. })
  300. },
  301. // 提交成功后的弹窗
  302. cancelFunc () {
  303. this.successModalShow = false
  304. // uni.switchTab({ url: '/pages/index' })
  305. uni.redirectTo({ url: '/pages/index' })
  306. // uni.navigateBack()
  307. },
  308. confirmFunc () {
  309. this.successModalShow = false
  310. let params = {
  311. from: 'addform',
  312. id: this.vehOptions.vehicleId,
  313. vin: this.vehOptions.vin,
  314. licensePlate: this.vehOptions.licensePlate
  315. }
  316. uni.redirectTo({ url: '/subPackages/home/record/claim' + uni.$u.queryParams(params)})
  317. },
  318. }
  319. }
  320. </script>
  321. <style scoped lang="scss">
  322. .mainInnBox {
  323. height: 100vh;
  324. padding-top: 18rpx;
  325. padding-bottom: calc(18rpx + constant(safe-area-inset-bottom));
  326. padding-bottom: calc(18rpx + env(safe-area-inset-bottom));
  327. background: #FFFFFF;
  328. border-top: 20rpx solid #EDF1F5;
  329. .formBox {
  330. flex: 1;
  331. // background-color: #fff;
  332. padding: 0 48rpx 150rpx;
  333. .item {
  334. display: flex;
  335. flex-direction: row;
  336. padding: 28rpx 0;
  337. border-bottom: 1rpx solid #EDF1F5;
  338. position: relative;
  339. .label {
  340. font-family: PingFangSC, PingFang SC;
  341. font-weight: 400;
  342. font-size: 28rpx;
  343. color: #666666;
  344. text-align: left;
  345. font-style: normal;
  346. margin-right: 40rpx;
  347. }
  348. .inBox {
  349. flex: 1;
  350. display: flex;
  351. align-items: center;
  352. justify-content: flex-end;
  353. .input {
  354. text-align: right;
  355. color: #212121;
  356. font-family: PingFangSC, PingFang SC;
  357. font-weight: 400;
  358. font-size: 28rpx;
  359. }
  360. }
  361. &.block {
  362. flex-direction: column;
  363. border: 0;
  364. padding: 28rpx 0 0 0;
  365. .inBox {
  366. flex: 1;
  367. display: flex;
  368. align-items: center;
  369. justify-content: flex-start;
  370. border-bottom: 1rpx solid #EDF1F5;
  371. padding: 0 0 24rpx 0;
  372. .input {
  373. text-align: left;
  374. color: #212121;
  375. }
  376. }
  377. }
  378. .dateBox {
  379. position: absolute;
  380. left: 0;
  381. right: 0;
  382. top: 0;
  383. bottom: 0;
  384. z-index: 999;
  385. }
  386. .tip {
  387. font-family: PingFangSC, PingFang SC;
  388. font-weight: 400;
  389. font-size: 28rpx;
  390. color: #999999;
  391. font-style: normal;
  392. margin: 16rpx 0 20rpx 0;
  393. }
  394. .update {
  395. width: 136rpx;
  396. height: 136rpx;
  397. background: #FFFFFF;
  398. border-radius: 12rpx;
  399. border: 2rpx dashed #126DCC;
  400. }
  401. }
  402. }
  403. .form_label {
  404. color: #303133;
  405. font-size: 30rpx;
  406. padding-top: 20rpx;
  407. }
  408. .form_label_tip {
  409. font-weight: 400;
  410. font-size: 28rpx;
  411. color: #999999;
  412. }
  413. .btnBox {
  414. position: fixed;
  415. left: 0;
  416. right: 0;
  417. bottom: 0;
  418. z-index: 1;
  419. padding-top: 32rpx;
  420. padding-bottom: calc(32rpx + constant(safe-area-inset-bottom));
  421. padding-bottom: calc(32rpx + env(safe-area-inset-bottom));
  422. .btn {
  423. width: 600rpx;
  424. height: 80rpx;
  425. background: #4095FF;
  426. box-shadow: 0rpx -4rpx 20rpx 0rpx rgba(0,0,0,0.06);
  427. border-radius: 12rpx;
  428. margin: 0 auto;
  429. border-radius: 12rpx;
  430. font-family: PingFangSC, PingFang SC;
  431. font-weight: 500;
  432. font-size: 32rpx;
  433. color: #FFFFFF;
  434. line-height: 80rpx;
  435. letter-spacing: 2px;
  436. text-align: center;
  437. font-style: normal;
  438. }
  439. }
  440. }
  441. .uploadContent {
  442. padding-top: 20rpx;
  443. .uploadFileBox {
  444. display: flex;
  445. justify-content: space-between;
  446. background: #eeeeee;
  447. padding: 18rpx 30rpx;
  448. border-radius: 4rpx;
  449. }
  450. .uploadChoose {
  451. width: 140rpx;
  452. height: 140rpx;
  453. background: #EDF1F5;
  454. display: flex;
  455. align-items: center;
  456. justify-content: center;
  457. }
  458. }
  459. </style>

 pdf.vue

  1. <template>
  2. <!-- <view>kkkk</view> -->
  3. <!-- <web-view src="https://www.baidu.com/"></web-view> -->
  4. <web-view :src="toUrl"></web-view>
  5. </template>
  6. <script>
  7. // import { getToken } from '@/assets/scripts/auth'
  8. export default {
  9. data() {
  10. return {
  11. toUrl: '' // http://112.17.37.24:6090/web/country_6_wechart/stealOil_heatmap.html/?token=' + getToken() + '&httpUrl=' +
  12. }
  13. },
  14. onLoad (option) {
  15. // console.log(option)
  16. this.toUrl = decodeURIComponent(option.url)
  17. }
  18. }
  19. </script>
  20. <style>
  21. </style>

 pages.json

  1. {
  2. // 如果您是通过uni_modules形式引入uView,可以忽略此配置
  3. "easycom": {
  4. "^u-(.*)": "@/uni_modules/uview-ui/components/u-$1/u-$1.vue"
  5. },
  6. "pages": [ //pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/collocation/pages
  7. {
  8. "path": "pages/index",
  9. "style": {
  10. "navigationStyle": "custom",
  11. "enablePullDownRefresh": true
  12. }
  13. },
  14. {
  15. "path": "pages/login/login",
  16. "style": {
  17. "navigationStyle": "custom",
  18. "enablePullDownRefresh": false
  19. }
  20. },
  21. {
  22. "path": "pages/home/index",
  23. "style": {
  24. "navigationStyle": "custom",
  25. "enablePullDownRefresh": true
  26. }
  27. },
  28. {
  29. "path": "pages/vehicles/index",
  30. "style": {
  31. "navigationStyle": "custom"
  32. }
  33. },
  34. {
  35. "path": "pages/user/index",
  36. "style": {
  37. "navigationBarTitleText": "我的"
  38. }
  39. },
  40. {
  41. "path": "pages/warn/index",
  42. "style": {
  43. "navigationBarTitleText": "报警"
  44. }
  45. }
  46. ],
  47. "subPackages": [
  48. {
  49. "root": "subPackages",
  50. "pages": [
  51. {
  52. "path": "home/claim/index",
  53. "style": {
  54. "navigationBarTitleText": "出险上报",
  55. "enablePullDownRefresh": false
  56. }
  57. },
  58. {
  59. "path": "home/claim/pdf",
  60. "style": {
  61. "navigationBarTitleText": "预览PDF",
  62. "enablePullDownRefresh": false
  63. }
  64. },
  65. ]
  66. }
  67. ],
  68. "globalStyle": {
  69. "navigationBarTextStyle": "black",
  70. "navigationBarTitleText": "",
  71. "navigationBarBackgroundColor": "#FFF",
  72. "backgroundColor": "#FFF",
  73. "enablePullDownRefresh": false,
  74. "onReachBottomDistance": 100
  75. }
  76. }

wx.chooseMessageFile 使用小程序API,要登录小程序管理后台,设置用户隐私协议:设置--基本信息--服务内容声明。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/844845
推荐阅读
相关标签
  

闽ICP备14008679号