多媒体信息发布平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
5.2 KiB

3 years ago
  1. <template>
  2. <div id="mef-m-authentication-apply">
  3. <div class="apply-upload-item fr noMb">
  4. <div class="photo-upload item-03" :class="{uploaded: userWorkPhoto}" @click="photoUpload('userWorkPhoto')">
  5. <img :src="userWorkPhoto" onerror="this.src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAACnej3aAAAAAXRSTlMAQObYZgAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII='" class="fit-cover-img" />
  6. <span v-if="userWorkPhoto" class="delete-icon" @click.stop="clearPhotoUrl('userWorkPhoto')"></span>
  7. </div>
  8. </div>
  9. </div>
  10. </template>
  11. <script>
  12. /* function getQiniuToken() {
  13. return Promise.resolve({'uptoken': 'xxx'});
  14. } */
  15. // c39eAF6CRLXTPcOp7YZWN47QG-R8GMdJwBtBQ1Eb:vQEKW6kSJvaMFOD6G0Fj1toohjE=:eyJzY29wZSI6ImFpeXVleGluZzoiLCJkZWFkbGluZSI6MTY0NzU3NjY4N30=
  16. /* c39eAF6CRLXTPcOp7YZWN47QG-R8GMdJwBtBQ1Eb:MZKUih1U9m0jKz57RwADJw02a9U=:eyJwZXJzaXN0ZW50UGlwZWxpbmUiOiJhaXl4bGliX3ZpZGVvLXBpcGUiLCJzY29wZSI6ImFpeXVleGluZyIsInBlcnNpc3RlbnRPcHMiOiJhdnRodW1iL20zdTgvbm9Eb21haW4vMS9zZWd0aW1lLzE1L3ZiLzQ0MGsvaGxzS2V5L2Z0em4xOWFpeXhsaWIyMDEvaGxzS2V5VXJsL2h0dHBzOi8vcWluaXUuYWl5eGxpYi5jb20vYWl4eWxpYndvcmQua2V5fHNhdmVhcy8iLCJkZWFkbGluZSI6MTY0NzU3NjcxOX0=*/
  17. import { getQiniuToken } from '@/api/material/material'
  18. export default {
  19. name: 'Upload',
  20. data() {
  21. return {
  22. userWorkPhoto: ''
  23. }
  24. },
  25. mounted() {
  26. },
  27. methods: {
  28. clearPhotoUrl(urlName) {
  29. this[urlName] = ''
  30. },
  31. photoUpload(type) {
  32. const ipt = document.createElement('input')
  33. ipt.setAttribute('type', 'file')
  34. ipt.setAttribute('accept', 'image/*')
  35. ipt.style.width = 0
  36. ipt.style.height = 0
  37. ipt.addEventListener('change', async() => {
  38. const [file] = ipt.files
  39. const base64Img = await this.fileToBase64(file)
  40. const url = await this.uploadBase64(base64Img)
  41. this.userWorkPhoto = url
  42. document.body.removeChild(ipt)
  43. })
  44. document.body.appendChild(ipt)
  45. ipt.click()
  46. },
  47. uploadBase64(base64Img) {
  48. return new Promise((resolve) => {
  49. getQiniuToken()
  50. .then(res => {
  51. const key = 'qiniu.aiyxlib.com' + this.uuid() + '.jpg'
  52. const pic = base64Img.split(',')[1]
  53. // const url = 'https://upload.qiniup.com/' + window.btoa(key) // 非华东空间需要根据注意事项 1 修改上传域名
  54. const url = 'https://upload.qiniup.com/' // 非华东空间需要根据注意事项 1 修改上传域名
  55. const xhr = new XMLHttpRequest()
  56. xhr.onreadystatechange = () => {
  57. if (xhr.readyState == 4) {
  58. resolve(
  59. 'qiniu.aiyxlib.com' + JSON.parse(xhr.responseText).key
  60. )
  61. }
  62. }
  63. xhr.open('POST', url, true)
  64. xhr.setRequestHeader('Content-Type', 'application/octet-stream')
  65. xhr.setRequestHeader('Authorization', 'UpToken ' + res.data)
  66. xhr.send(pic)
  67. // 获取到凭证之后再将文件上传到七牛云空间
  68. })
  69. .catch(console.error)
  70. })
  71. },
  72. fileToBase64(file) {
  73. return new Promise((resolve) => {
  74. const reader = new FileReader()
  75. // 传入一个参数对象即可得到基于该参数对象的文本内容
  76. reader.readAsDataURL(file)
  77. reader.onload = function(e) {
  78. // target.result 该属性表示目标对象的DataURL
  79. resolve(e.target.result)
  80. }
  81. })
  82. },
  83. uuid(len, radix) {
  84. const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
  85. const uuid = []
  86. let i
  87. radix = radix || chars.length
  88. if (len) {
  89. for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix]
  90. } else {
  91. let r
  92. uuid[8] = uuid[13] = uuid[18] = uuid[23] = ''
  93. uuid[14] = '4'
  94. for (i = 0; i < 36; i++) {
  95. if (!uuid[i]) {
  96. r = 0 | Math.random() * 16
  97. uuid[i] = chars[i == 19 ? r & 0x3 | 0x8 : r]
  98. }
  99. }
  100. }
  101. return uuid.join('')
  102. }
  103. }
  104. }
  105. </script>
  106. <style lang="scss" scoped>
  107. #mef-m-authentication-apply{
  108. .apply-upload-item{
  109. width: 132px;
  110. margin-bottom: 30px;
  111. text-align: center;
  112. &.noMb{
  113. margin-bottom: 0;
  114. }
  115. .photo-upload {
  116. width: 132px;
  117. height: 96px;
  118. position: relative;
  119. margin-bottom: 10px;
  120. &.item-03{
  121. // background: top / 100% url(~@/assets/images/authentication/upload_workCard.png)
  122. // no-repeat;
  123. }
  124. &.uploaded{
  125. background: none;
  126. }
  127. img{
  128. width: 85px;
  129. height: 85px;
  130. position: absolute;
  131. left: 50%;
  132. top: 50%;
  133. transform: translate(-50%,-50%);
  134. }
  135. .delete-icon{
  136. width: 16px;
  137. height: 16px;
  138. // background: top / 100% url(~@/assets/images/common/icon-del.png)
  139. // no-repeat;
  140. border-radius: 0;
  141. top: -8px;
  142. right: -8px;
  143. position: absolute;
  144. i {
  145. display: none;
  146. }
  147. }
  148. }
  149. .apply-name{
  150. font-size: 14px;
  151. font-weight: bold;
  152. padding-top: 10px;
  153. text-align: center;
  154. }
  155. }
  156. }
  157. </style>