FormImg.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <template>
  2. <!-- 图片上传 -->
  3. <div class="select-box-img" :class="isFlex?'selectFlex':''">
  4. <div class="select-title">
  5. <span :class="isRedDot?'':'isRedDot'">*</span>
  6. {{name}}
  7. </div>
  8. <div class="home">
  9. <div class="">
  10. <el-upload :class="{
  11. showUpload:fileList.length>=limit?true:false,
  12. borderRadius:uploadBorderRadius == '50%',
  13. }" action="" :multiple="limit>1" :accept="accept ? accept : '.jpg,.png,.jpeg,.gif'"
  14. :list-type="listType?listType:'picture-card'" :limit='limit' :on-remove="handleRemove"
  15. :http-request='httpRequest' :file-list="fileList" :disabled='disabled' :auto-upload="true">
  16. <el-button v-if="listType=='text'" size="small" type="primary">点击上传</el-button>
  17. <i v-else slot="default" class="el-icon-plus"></i>
  18. </el-upload>
  19. </div>
  20. <div v-show="showMsg" class="errorMsg">{{msg}}</div>
  21. </div>
  22. </div>
  23. </template>
  24. <script>
  25. import {
  26. Loading
  27. } from 'element-ui';
  28. import oss from "ali-oss"
  29. import {
  30. GetSts
  31. } from '@/api/conmmon.js'
  32. export default {
  33. props: ['name', 'isRedDot', 'isFlex', 'value', 'isCheck', 'createOss',
  34. 'limit', 'witdh', 'dataType', 'listType', 'accept', 'disabled', 'uploadBorderRadius'
  35. ],
  36. data() {
  37. return {
  38. aliOss: '',
  39. msg: '',
  40. showMsg: false,
  41. showUpButton: false, //true隐藏 false显示上传按钮
  42. fileList: [],
  43. fileUrl: [],
  44. }
  45. },
  46. mounted() {
  47. console.log(this.value);
  48. if (this.value) {
  49. if (this.limit === 1) {
  50. this.fileList.push({
  51. url: this.value
  52. })
  53. } else {
  54. this.fileList = this.value
  55. }
  56. }
  57. this.setName(this.name)
  58. if (!this.createOss) {
  59. this.setNewOss()
  60. }
  61. },
  62. watch: {
  63. fileList(val) {
  64. this.verification()
  65. },
  66. value(val) {
  67. if (val) {
  68. if (typeof val === 'string') {
  69. let list = val.split(';');
  70. list.forEach((url) => {
  71. this.setFileUrl(url);
  72. })
  73. } else {
  74. val.forEach((url) => {
  75. if (typeof url === 'string') {
  76. this.setFileUrl(url);
  77. } else {
  78. this.setFileUrl(url.url);
  79. }
  80. })
  81. }
  82. }
  83. },
  84. },
  85. methods: {
  86. setFileUrl(url) {
  87. let isAdd = true;
  88. this.fileList.forEach((v, k) => {
  89. if (v.url === url) {
  90. isAdd = false;
  91. }
  92. })
  93. if (isAdd) {
  94. this.fileList.push({
  95. name: this.getFileName(url),
  96. url: url
  97. });
  98. }
  99. console.log(this.fileList);
  100. },
  101. getFileName(o) {
  102. let pos = o.lastIndexOf("\\");
  103. return o.substring(pos + 1);
  104. },
  105. async setNewOss() {
  106. let obj = await GetSts()
  107. obj = obj.data
  108. let client = new oss({
  109. region: "oss-cn-chengdu",
  110. accessKeyId: obj.AccessKeyId,
  111. accessKeySecret: obj.AccessKeySecret,
  112. stsToken: obj.SecurityToken,
  113. refreshSTSToken: async () => {
  114. let obj = await GetSts()
  115. obj = obj.data
  116. return {
  117. accessKeyId: obj.accessKeyId,
  118. accessKeySecret: obj.accessKeySecret,
  119. stsToken: obj.stsToken,
  120. }
  121. },
  122. refreshSTSTokenInterval: 900000,
  123. bucket: "jhnewshop",
  124. })
  125. this.aliOss = client
  126. },
  127. // 文件列表移除文件时的钩子
  128. handleRemove(file, fileList) {
  129. this.fileUrl.forEach((v, i) => {
  130. if (v == file.url) {
  131. this.fileUrl.splice(i, 1)
  132. }
  133. })
  134. this.fileList = fileList
  135. },
  136. httpRequest({
  137. file
  138. }) {
  139. let imgName = `${Date.parse(new Date())}/${file.name}`
  140. Loading.service({
  141. lock: true,
  142. text: '图片上传中',
  143. background: 'rgba(0, 0, 0, 0.7)'
  144. })
  145. this.aliOss.put(imgName, file).then((res) => {
  146. if (res.res && res.res.status == 200) {
  147. // console.log(`阿里云OSS上传文件成功回调`, res, url, name);
  148. Loading.service().close()
  149. this.popFun('成功', '上传成功', 'success')
  150. file.url = res.url;
  151. this.fileUrl.push(res.url)
  152. if (this.dataType === 1) {
  153. this.$emit('input', this.fileUrl)
  154. } else {
  155. this.$emit('input', this.fileUrl.join(';'))
  156. }
  157. console.log(this.fileList);
  158. }
  159. }).catch((err) => {
  160. Loading.service().close()
  161. this.popFun('提示', '上传失败', 'error')
  162. })
  163. },
  164. popFun(title, msg, type) {
  165. this.$notify({
  166. title: title,
  167. message: msg,
  168. type: type
  169. });
  170. },
  171. // 处理name结尾的冒号
  172. setName(name) {
  173. if (this.name) {
  174. if (name.indexOf(':') != -1) {
  175. this.title = name.substr(0, name.length - 1)
  176. } else {
  177. this.title = name
  178. }
  179. }
  180. },
  181. verification() {
  182. if (this.isCheck) {
  183. this.showMsg = true
  184. if (this.fileList.length < 1) {
  185. this.msg = "请上传图片";
  186. return false
  187. }
  188. this.showMsg = false
  189. return true
  190. } else {
  191. return true
  192. }
  193. },
  194. alertMsg() {
  195. if (this.isCheck) {
  196. if (this.verification) {
  197. this.$notify.error({
  198. title: '提示',
  199. message: this.msg
  200. });
  201. }
  202. }
  203. },
  204. },
  205. }
  206. </script>