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.
 
 
 
 
 
 

231 lines
6.1 KiB

  1. <template>
  2. <view class="cu-form-group">
  3. <view class="grid col-4 grid-square flex-sub">
  4. <view
  5. v-for="(item, index) in imgList"
  6. @tap="viewImg(index)"
  7. :key="index"
  8. class="bg-img"
  9. >
  10. <!-- {{item.id?CONFIG('webHost')+item.url:item.url}} -->
  11. <image
  12. v-if="showfile()"
  13. :src="item.id?CONFIG('webHost')+item.url:item.url"
  14. mode="aspectFill"
  15. ></image>
  16. <view v-if="!readonly" @tap.stop="delImg(index)" class="cu-tag bg-red" style="width: 18px; height: 18px; font-size: 24px">
  17. <l-icon
  18. type="close"
  19. style="width: 18px; height: 18px; font-size: 12px"
  20. />
  21. </view>
  22. </view>
  23. <view
  24. v-if="!readonly && imgList.length < Number(number)"
  25. @tap="chooseImg"
  26. class="solids"
  27. >
  28. <l-icon type="cameraadd" />
  29. </view>
  30. </view>
  31. </view>
  32. </template>
  33. <script>
  34. export default {
  35. name: "l-upload",
  36. props: {
  37. number: { default: 1 },
  38. readonly: {},
  39. value: { default: () => [] },
  40. },
  41. data(){
  42. return{
  43. imgList:[],
  44. }
  45. },
  46. methods: {
  47. delImg(index) {
  48. const newList = JSON.parse(JSON.stringify(this.imgList));
  49. newList.splice(index, 1);
  50. this.imgList = newList
  51. this.$emit("update:value", newList);
  52. this.$emit("input", newList);
  53. this.$emit("change");
  54. this.$emit("del");
  55. },
  56. showfile() {
  57. return true;
  58. },
  59. chooseImg() {
  60. uni.chooseImage({
  61. count: (Number(this.number) - this.imgList.length),
  62. sizeType: ["original", "compressed"],
  63. sourceType: ["album", "camera"],
  64. success: (res) => {
  65. let {tempFilePaths,tempFiles} = res
  66. if(!this.validate(tempFiles))return
  67. const newList = JSON.parse(JSON.stringify(this.imgList || [])).concat(
  68. tempFilePaths.map(item=>{
  69. return{
  70. url:item,
  71. id:''
  72. }
  73. })
  74. );
  75. this.imgList = newList
  76. this.$emit("update:value", newList);
  77. this.$emit("input", newList);
  78. this.$emit("change",newList);
  79. this.$emit("add");
  80. },
  81. });
  82. },
  83. uploadImage(){
  84. // 单图上传
  85. return new Promise(async (reslove,reject)=>{
  86. let hasNotUpdatedList = this.imgList.every(item=>item.id)
  87. if(hasNotUpdatedList){
  88. reslove(this.imgList)
  89. }
  90. if(this.imgList.length){
  91. var postData = {
  92. Base64Url: await this.imgToBase64(this.imgList[0].url)
  93. }
  94. this.HTTP_POST('StuInfoFresh/savePhoto', postData, '图片上传失败!').then((data) => {
  95. if (data) {
  96. reslove([{
  97. url:data.Url,
  98. id:data.AnnexesFileId
  99. }])
  100. }else{
  101. reject('图片上传失败!')
  102. }
  103. })
  104. }else{
  105. reslove("")
  106. }
  107. })
  108. },
  109. /**
  110. * @description 控制promise.all并发数量
  111. * @param limit 并发数
  112. * @param array 参数列表
  113. * @param apiFn 执行函数
  114. * @returns {Promise<Awaited<unknown>[]>}
  115. */
  116. async promiseAllLimit(limit, array, apiFn) {
  117. const ret = [] // 用于存放所有的promise实例
  118. const executing = [] // 用于存放目前正在执行的promise
  119. for (const item of array) {
  120. const p = apiFn(item)
  121. ret.push(p)
  122. if (limit <= array.length) {
  123. // then回调中,当这个promise状态变为fulfilled后,将其从正在执行的promise列表executing中删除
  124. const e = p.then(() => executing.splice(executing.indexOf(e), 1))
  125. executing.push(e)
  126. if (executing.length >= limit) {
  127. // 一旦正在执行的promise列表数量等于限制数,就使用Promise.race等待某一个promise状态发生变更,
  128. // 状态变更后,就会执行上面then的回调,将该promise从executing中删除,
  129. // 然后再进入到下一次for循环,生成新的promise进行补充
  130. await Promise.race(executing)
  131. }
  132. }
  133. }
  134. return Promise.all(ret)
  135. },
  136. imgToBase64(url){
  137. return new Promise((resolve,reject)=>{
  138. if(!url){
  139. resolve("")
  140. }
  141. var canvas = document.createElement('canvas'),
  142. ctx = canvas.getContext('2d'),
  143. img = new Image;
  144. img.crossOrigin = 'Anonymous';
  145. img.onload = function () {
  146. canvas.height = img.height;
  147. canvas.width = img.width;
  148. ctx.drawImage(img, 0, 0);
  149. var dataURL = canvas.toDataURL('image/png');
  150. canvas = null;
  151. resolve(dataURL)
  152. };
  153. img.src = url;
  154. })
  155. },
  156. validate(array){
  157. // let type = array.every(item=>{
  158. // return item.type && item.type.substring(0,6) == "image/"
  159. // })
  160. // if(!type){
  161. // this.TOAST('文件类型错误');
  162. // return false
  163. // }
  164. let size = array.every(item=>{
  165. return item.size && item.size <= 100 * 1024 * 1024
  166. })
  167. if(!size){
  168. this.TOAST('文件大小不得超过100M');
  169. return false
  170. }
  171. return true
  172. },
  173. viewImg(index) {
  174. uni.previewImage({
  175. urls: this.imgList.map(item=>item.id?this.CONFIG('webHost')+item.url:item.url),
  176. current: this.imgList[index].id?this.CONFIG('webHost')+this.imgList[index].url:this.imgList[index].url,
  177. });
  178. },
  179. // previewFile() {
  180. // var file = document.querySelector('input[type=file]').files[0];
  181. // var reader = new FileReader();
  182. // // fileReader.readAsDataURL(blob);
  183. // // fileReader.onerror = () => {
  184. // // reject(new Error('blobToBase64 error'));
  185. // // };
  186. // // var encodedData = window.btoa("Hello, world");
  187. // reader.onloadend = function () {
  188. // //$('#PhotoImg').attr('src', reader.result);
  189. // var postData = {
  190. // Base64Url: reader.result
  191. // }
  192. // this.HTTP_POST(config.webapi + "StuInfoFresh/savePhoto", postData, (data) => {
  193. // if (data) {
  194. // $('#Photo').val(data.AnnexesFileId);
  195. // $('#PhotoImg').attr('src', config.web + data.Url);
  196. // } else {
  197. // learun.layer.toast('采集照片信息失败!');
  198. // }
  199. // });
  200. // }
  201. // if (file) {
  202. // reader.readAsDataURL(file);
  203. // }
  204. // },
  205. },
  206. created() {
  207. console.log(123)
  208. this.imgList = JSON.parse(JSON.stringify(this.value.map(item=>{
  209. item.isUploaded = true
  210. return item
  211. })))
  212. // [{url:"",id:""}]
  213. }
  214. };
  215. </script>