upload.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <template>
  2. <view>
  3. <!--#ifdef APP-PLUS-->
  4. <view class="lz-status_bar">
  5. <view class="lz-top_view"></view>
  6. </view>
  7. <!--#endif-->
  8. <view class="kaoshi-head">
  9. <view class="kaoshi-head-top">
  10. <view class="kaoshi-head-left" @tap="$navigateBack">
  11. <view class="icon icon-zuojiantou"></view>
  12. </view>
  13. <view class="kaoshi-head-m">头像</view>
  14. </view>
  15. </view>
  16. <view>
  17. <sunui-upbasic :upImgConfig="upImgBasic" @onUpImg="upBasicData" @onImgDel="delImgInfo" ref="uImage">
  18. </sunui-upbasic>
  19. <button type="primary" @tap="uImageTap">提交</button>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. import sunuiUpbasic from '@/src/components/sunui-upimg/sunui-upimg-basic.vue'
  25. import {
  26. mapState
  27. } from 'vuex'
  28. export default {
  29. data() {
  30. return {
  31. basicArr: [],
  32. // 基础版配置
  33. upImgBasic: {
  34. // 后端图片接口地址
  35. basicConfig: {
  36. url: 'http://rrf.quanyouyisheng.com/api/common/upload',
  37. },
  38. // 是否开启提示(提醒上传图片的数量)
  39. // tips: false,
  40. // 是否开启notli(开启的话就是选择完直接上传,关闭的话当count满足数量时才上传)
  41. notli: false,
  42. // 图片数量
  43. count: 1,
  44. // 相机来源(相机->camera,相册->album,两者都有->all,默认all)
  45. sourceType: 'camera',
  46. // 是否压缩上传照片(仅小程序生效)
  47. sizeType: true,
  48. // 上传图片背景修改
  49. upBgColor: '#E8A400',
  50. // 上传icon图标颜色修改(仅限于iconfont)
  51. upIconColor: '#fff',
  52. // 上传svg图标名称
  53. // upSvgIconName: 'icon-card',
  54. // 上传文字描述(仅限四个字)
  55. // upTextDesc: '上传证书',
  56. // 删除按钮位置(left,right,bleft,bright),默认右上角
  57. delBtnLocation: '',
  58. // 是否隐藏添加图片
  59. // isAddImage: false,
  60. // 是否隐藏删除图标
  61. // isDelIcon: false,
  62. // 删除图标定义背景颜色
  63. // delIconColor: '',
  64. // 删除图标字体颜色
  65. // delIconText: '',
  66. // 上传图标替换(+),是个http,https图片地址(https://www.playsort.cn/right.png)
  67. iconReplace: '',
  68. header: {}
  69. }
  70. };
  71. },
  72. computed: {
  73. ...mapState(['subject', 'userinfo']),
  74. },
  75. components: {
  76. sunuiUpbasic
  77. },
  78. onLoad() {
  79. this.upImgBasic.basicConfig.url = this.upImgBasic.basicConfig.url + '?tokne=' + this.userinfo.token;
  80. this.upImgBasic.header = {
  81. token: this.userinfo.token
  82. };
  83. },
  84. methods: {
  85. // 手动上传图片(适用于表单等上传) -2019/05/10增加
  86. uImageTap() {
  87. this.$refs.uImage.uploadimage(this.upImgBasic);
  88. },
  89. // 删除图片 -2019/05/12(本地图片进行删除)
  90. async delImgInfo(e) {
  91. console.log('你删除的图片地址为:', e, this.basicArr.splice(e.index, 1));
  92. },
  93. // 基础版
  94. async upBasicData(e) {
  95. console.log('===>', e);
  96. // 上传图片数组
  97. let arrImg = [];
  98. for (let i = 0, len = e.length; i < len; i++) {
  99. try {
  100. if (e[i].path_server != "") {
  101. await arrImg.push(e[i].path_server.split(','));
  102. }
  103. } catch (err) {
  104. console.log('上传失败...');
  105. }
  106. }
  107. // 图片信息保存到data数组
  108. this.basicArr = arrImg;
  109. // 可以根据长度来判断图片是否上传成功. 2019/4/11新增
  110. if (arrImg.length == this.upImgBasic.count) {
  111. uni.showToast({
  112. title: `上传成功`,
  113. icon: 'none'
  114. });
  115. }
  116. },
  117. // 获取上传图片basic
  118. getUpImgInfoBasic() {
  119. console.log('后端转成一维数组:', this.basicArr.join().split(','));
  120. }
  121. }
  122. }
  123. </script>
  124. <style>
  125. </style>