PicsUploader.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <template>
  2. <div style="width: 100%;">
  3. <div class="photoPart">
  4. <input type="file" class="photoChoose" id="photoChoose" accept="image/png,image/jpg,image/gif,image/JPEG" style="display: none;"/>
  5. <label for="photoChoose">
  6. <img id="photo" :src="photoSrc">
  7. </label>
  8. <p>点击上传头像</p>
  9. </div>
  10. <div class="certificatePart">
  11. <input type="file" class="certificateChoose" id="certificateChoose" accept="image/png,image/jpg,image/gif,image/JPEG" style="display: none;"/>
  12. <label for="certificateChoose">
  13. <img id="certificate" :src="certificateSrc">
  14. </label>
  15. <p>点击上传营业资格证</p>
  16. </div>
  17. </div>
  18. </template>
  19. <script>
  20. export default {
  21. name: 'picsUploader',
  22. data () {
  23. return {
  24. photoSrc: '/static/pic/defaultPhoto.jpg',
  25. certificateSrc: '/static/pic/defaultLackPic.png'
  26. }
  27. },
  28. mounted () {
  29. const that = this
  30. $('#photoChoose').change(function (e) {
  31. const file = e.target.files[0]
  32. const item = {
  33. name: file.name,
  34. size: file.size,
  35. file: file
  36. }
  37. that.html5Reader(file, item)
  38. that.submit(item, 'p')
  39. })
  40. $('#certificateChoose').change(function (e) {
  41. const file = e.target.files[0]
  42. const item = {
  43. name: file.name,
  44. size: file.size,
  45. file: file
  46. }
  47. that.html5Reader(file, item)
  48. that.submit(item, 'c')
  49. })
  50. },
  51. methods: {
  52. html5Reader (file, item) {
  53. const reader = new FileReader()
  54. reader.onload = e => {
  55. this.$set(item, 'src', e.target.result)
  56. }
  57. reader.readAsDataURL(file)
  58. },
  59. timestamp: function () {
  60. const time = new Date()
  61. const y = time.getFullYear()
  62. const m = time.getMonth() + 1
  63. const d = time.getDate()
  64. const h = time.getHours()
  65. const mm = time.getMinutes()
  66. const s = time.getSeconds()
  67. const ms = time.getMilliseconds()
  68. return (
  69. '' +
  70. y +
  71. this.Add0(m) +
  72. this.Add0(d) +
  73. this.Add0(h) +
  74. this.Add0(mm) +
  75. this.Add0(s) +
  76. this.Add0(ms)
  77. )
  78. },
  79. Add0: function (m) {
  80. return m < 10 ? '0' + m : m
  81. },
  82. submit (file, value) {
  83. const that = this
  84. // console.log(that.photoSrc);
  85. const OSS = require('ali-oss')
  86. const client = new OSS({
  87. region: 'oss-cn-hangzhou',
  88. accessKeyId: 'LTAIL4RFw3fPAweH',
  89. accessKeySecret: 'xOw4hzztNsCPm5LtJoWVwsvSOFl8IB',
  90. bucket: 'njuhzl'
  91. })
  92. const f = file.file
  93. console.log(f)
  94. const Name = f.name
  95. console.log(Name)
  96. const suffix = Name.substr(Name.indexOf('.'))
  97. const obj = this.timestamp()
  98. const storeAs = 'photos/' + obj + suffix // 路径+时间戳+后缀名
  99. console.log(storeAs)
  100. client
  101. .multipartUpload(storeAs, f)
  102. .then(function (result) {
  103. const data = result.res.requestUrls[0]
  104. const url = data.split('?uploadId')[0]
  105. console.log(url)
  106. if (value === 'p') {
  107. that.photoSrc = url
  108. } else {
  109. that.certificateSrc = url
  110. }
  111. })
  112. .catch(function (err) {
  113. console.log(err)
  114. })
  115. }
  116. }
  117. }
  118. </script>
  119. <style scoped>
  120. .photoPart{
  121. position: absolute;
  122. right: 15%;
  123. top: 120px;
  124. }
  125. #photo{
  126. width: 120px;
  127. height: 120px;
  128. cursor: pointer;
  129. }
  130. .certificatePart{
  131. margin-top: 50px;
  132. margin-right: 50px;
  133. }
  134. #certificate{
  135. width: 400px;
  136. height: 250px;
  137. cursor: pointer;
  138. }
  139. </style>