index.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <template>
  2. <div class="home">
  3. <div class="image"
  4. v-for="item,index in imgUrl"
  5. :key="index">
  6. <div class="title">图片展示区域:</div>
  7. <img :src="item"
  8. class="inner-img" />
  9. <input type="text"
  10. class="url-block"
  11. disabled
  12. placeholder="默认使用url"
  13. :value="item">
  14. </div>
  15. <el-upload class="upload"
  16. action=""
  17. accept=".png,.jpg,.jpeg"
  18. :on-success="handleSuccess"
  19. :http-request="uploadFile"
  20. :show-file-list="false"
  21. :multiple="false"
  22. :file-list="fileList">
  23. <el-button size="small"
  24. class="update-button"
  25. type="primary">点击上传</el-button>
  26. </el-upload>
  27. </div>
  28. </template>
  29. <script>
  30. const OSS = require('ali-oss');
  31. const client = new OSS({
  32. accessKeyId: 'LTAI5tAmkA2tR9t5hXWm7ru1', // 查看你自己的阿里云KEY
  33. accessKeySecret: 'Gy5zuTqh7x6OpKWdKfxZLuQjPEF2vG', // 查看自己的阿里云KEYSECRET
  34. bucket: 'we-spa', // 你的 OSS bucket 名称
  35. region: 'oss-cn-shenzhen', // bucket 所在地址
  36. // cname: true // 开启自定义域名上传
  37. // endpoint:"file.xxxx.live" // 自己的域名
  38. });
  39. export default {
  40. props: {
  41. msg: String
  42. },
  43. data () {
  44. return {
  45. fileList: [],
  46. imgUrl: [
  47. ]
  48. }
  49. },
  50. methods: {
  51. async uploadFile (options) {
  52. try {
  53. let file = options.file; // 拿到 file
  54. console.log(file.name, 'file.name.substr(0,file.name');
  55. const size = file.size / 1024
  56. console.log(size, 'file.size');
  57. if (size > 2000) {
  58. this.$notify.warning({
  59. title: '警告',
  60. message: '大小必须小于M'
  61. })
  62. return
  63. }
  64. let fileName = file.name.substr(file.name.lastIndexOf('.'), file.name.length - 1)
  65. let date = new Date().getTime()
  66. let fileNames = `${date}${fileName}` // 拼接文件名,保证唯一,这里使用时间戳+原文件名
  67. // 上传文件,这里是上传到OSS的 uploads文件夹下
  68. client.put('total_picture/' + fileNames, file).then(res => {
  69. if (res.res.statusCode === 200) {
  70. options.onSuccess(res)
  71. } else {
  72. options.onError("上传失败")
  73. }
  74. })
  75. } catch (e) {
  76. options.onError("上传失败")
  77. }
  78. },
  79. // 上传成功回调函数
  80. handleSuccess (res) {
  81. if (res) {
  82. this.imgUrl.push(res.url)
  83. }
  84. }
  85. }
  86. }
  87. </script>
  88. <style>
  89. .home {
  90. padding: 8px;
  91. position: relative;
  92. }
  93. .title {
  94. text-align: left;
  95. font-size: 13px;
  96. width: 95%;
  97. padding-left: 10px;
  98. background-color: #ffffff;
  99. color: #999999;
  100. }
  101. .image {
  102. margin-top: 20px;
  103. text-align: center;
  104. }
  105. .url-block {
  106. margin-top: 20px;
  107. width: 100%;
  108. height: 50px;
  109. padding-left: 10px;
  110. border: 1px solid rgb(15, 15, 16);
  111. overflow-x: auto;
  112. }
  113. .inner-img {
  114. max-width: 95%;
  115. height: auto;
  116. display: block;
  117. margin: 10px auto;
  118. }
  119. .el-upload {
  120. margin-top: 20px;
  121. width: 100%;
  122. }
  123. .update-button {
  124. width: 80%;
  125. height: 40px;
  126. color: #ffffff;
  127. background-color: #00a2ff;
  128. border-radius: 10px;
  129. margin: 0 auto;
  130. text-align: center;
  131. }
  132. </style>