Commit 5135e8c3 by 杨铁龙
parents 9124b947 86f18628
...@@ -13,20 +13,20 @@ ...@@ -13,20 +13,20 @@
</template> </template>
<script lang="ts"> <script lang="ts">
import { Component } from "vue-property-decorator"; import { Component } from "vue-property-decorator";
import BaseMessage from "./index"; import BaseMessage from "./index";
import VideoPlayerIcon from "./video-player-icon.vue"; import VideoPlayerIcon from "./video-player-icon.vue";
@Component({ components: { VideoPlayerIcon } }) @Component({ components: { VideoPlayerIcon } })
export default class Index extends BaseMessage { export default class Index extends BaseMessage {
mounted() { mounted() {
this.buildMessageUrl(); this.buildMessageUrl();
} }
} }
</script> </script>
<style lang="less" scoped> <style lang="less" scoped>
.video-message { .video-message {
height: 160px; height: 160px;
width: 200px; width: 200px;
background-color: #000 !important; background-color: #000 !important;
......
export * from "./upload"
\ No newline at end of file
...@@ -7,3 +7,63 @@ export async function uploadFile(file: File, uploading?: (p: number) => void) { ...@@ -7,3 +7,63 @@ export async function uploadFile(file: File, uploading?: (p: number) => void) {
}); });
return `${sdk.global.baseUrl}${url}`; return `${sdk.global.baseUrl}${url}`;
} }
export const enum UploadType {
Default,
Image,
Camera
}
/**
* x
* @param type UploadType 默认值 UploadType.Default
* @param size 可不传 单位M
* @returns
*/
export function chooseFileAndUpload(type = UploadType.Default, size?: number) {
return chooseFile(type).then(r => {
const sdk = Chat.getSdk();
return sdk.uploadFileV2(r)
})
}
/**
*
* @param type UploadType
* @param size 可不传 单位M
* @returns
*/
export function chooseFile(type = UploadType.Default, size?: number) {
return new Promise<File>((resolve, reject) => {
const target = document.createElement('input');
target.setAttribute('type', 'file');
// 添加这个属性,就可以唤起相机的功能
(type === UploadType.Camera) && target.setAttribute('capture', 'camera');
// 这里如果不加属性 accept 是 "image/*" 或者 "video/*",就默认打开摄像头,既可以拍照也可以录像
(type === UploadType.Image) && target.setAttribute('accept', 'image/*');
target.setAttribute('style', 'display:none');
// 监听改变事件
target.addEventListener('change', (e: Event) => {
// 拿到文件对象
if (e && e.target) {
const t = e.target as HTMLInputElement;
const { files } = t;
if (files) {
// 返回的是一个文件对象
if (size && files[0].size >= size * 1024 * 1024) {
reject(`上传的${type === UploadType.Default ? "文件" : "图片"}太大了~`);
return;
}
resolve(files[0]);
setTimeout(() => target.remove(), 200);
return;
}
}
reject(new Error('系统不支持'));
setTimeout(() => target.remove(), 200);
});
document.body.appendChild(target);
// 这里是模拟点击了input控件
target.click();
});
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment