diff --git a/src/components/AppIcon/index.vue b/src/components/AppIcon/index.vue index 3ed3e97..11f4f52 100644 --- a/src/components/AppIcon/index.vue +++ b/src/components/AppIcon/index.vue @@ -1,54 +1,65 @@ - + + diff --git a/src/directives/default-img.js b/src/directives/default-img.js new file mode 100644 index 0000000..8529798 --- /dev/null +++ b/src/directives/default-img.js @@ -0,0 +1,59 @@ +import defaultAvatar from '/src/assets/images/dio.jpg' +import defaultBackground from '/src/assets/images/cute.jpg' +import { getStrColor } from '../utils/process' + +/** + * 项目难点标记:检测图片存在,如果不存在则用用户的昵称的首字符作为头像,绘制svg + * 检测图片是否存在 + * @param url + */ +const imageIsExist = function (url) { + return new Promise((resolve) => { + let img = new Image() + img.src = url + img.onload = () => { + if (img.complete === true) { + resolve(true) + img = null + } + } + img.onerror = () => { + resolve(false) + img = null + } + }) +} + +function genSvgImg(text, color, size = 36) { + text = text.substring(0, 1) + color = encodeURIComponent(color) + return `data:image/svg+xml;utf8, + + + ${text} +` +} + +/** + * 当图片加载失败时,显示默认图片 + * 参数可选:'avatar' | 'background' | string + * + */ +export default async function defaultImg(el, binding) { + // 需要显示默认图片(当图片原本的src属性有错时)的类型 + const { value, modifiers } = binding + // 图片原本的src + const realURL = el.src + // 当原本图片不存在时,根据参数返回不同的图片url + const exist = await imageIsExist(realURL) + if (exist) return // 图片可正常加载,不做任何处理 + if (value) { + el.setAttribute('src', genSvgImg(value, getStrColor(value))) + } else if (modifiers.avatar) { + el.setAttribute('src', defaultAvatar) + } else if (modifiers.background) { + el.setAttribute('src', defaultBackground) + } else { + el.remove() // 什么都不加 v-default-img 时 移除图片 + } +} diff --git a/src/layout/index.vue b/src/layout/index.vue index 53af195..cfffd88 100644 --- a/src/layout/index.vue +++ b/src/layout/index.vue @@ -1,6 +1,6 @@