123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- <template>
- <view class="lsj-file" :style="[getStyles]">
- <view ref="lsj" class="hFile" :style="[getStyles]" @click="onClick">
- <slot><view class="defview" :style="[getStyles]">附件上传</view></slot>
- </view>
- </view>
- </template>
- <script>
- import {LsjFile} from './LsjFile.js'
- export default {
- name: 'Lsj-upload',
- props: {
-
- debug: {type: Boolean,default: false},
-
- instantly: {type: Boolean,default: false},
-
- option: {type: Object,default: ()=>{}},
-
- size: { type: Number, default: 10 },
-
- count: { type: Number, default: 9 },
-
- formats: { type: String, default:''},
-
- accept: {type: String,default: ''},
-
-
-
-
-
- wxFileType: { type: String, default: 'all' },
-
- childId: { type: String, default: 'lsjUpload' },
-
- width: { type: String, default: '100%' },
-
- height: { type: String, default: '80rpx' },
-
-
- top: { type: [String, Number], default: '' },
- left: { type: [String, Number], default: '' },
- bottom: { type: [String, Number], default: '' },
- right: { type: [String, Number], default: '' },
- xmtype:{ type: [String, Number], default: '' },
-
- position: {
- type: String,
-
- default: 'absolute',
-
-
- default: 'static',
-
- },
- },
- data() {
- return {
-
- }
- },
- watch: {
- option(v) {
-
- this.lsjFile&&this.show();
-
- }
- },
- updated() {
-
- if (this.isShow) {
- this.lsjFile&&this.show();
- }
-
- },
- computed: {
- getStyles() {
- let styles = {
- width: this.width,
- height: this.height
- }
- if (this.position == 'absolute') {
- styles['top'] = this.top
- styles['bottom'] = this.bottom
- styles['left'] = this.left
- styles['right'] = this.right
- styles['position'] = 'fixed'
- }
- return styles
- }
- },
- mounted() {
- this._size = 0;
- let WEBID = this.childId + new Date().getTime();
- this.lsjFile = new LsjFile({
- id: WEBID,
- debug: this.debug,
- width: this.width,
- height: this.height,
- option: this.option,
- instantly: this.instantly,
-
- prohibited: {
-
- size: this.size,
-
- formats: this.formats,
-
- accept: this.accept,
- count: this.count
- },
- onchange: this.onchange,
- onprogress: this.onprogress,
- });
- this.create();
-
- },
- beforeDestroy() {
-
- this.lsjFile.dom.close();
-
- },
- methods: {
- setFiles(array) {
- if (array instanceof Map) {
- for (let [key, item] of array) {
- item['progress'] = 100;
- item['type'] = 'success';
- this.lsjFile.files.set(key,item);
- }
- }
- else if (Array.isArray(array)) {
- array.forEach(item=>{
- if (item.name) {
- item['progress'] = 100;
- item['type'] = 'success';
- this.lsjFile.files.set(item.name,item);
- }
- });
- }
- this.onchange(this.lsjFile.files);
- },
- setData() {
- this.lsjFile&&this.lsjFile.setData(...arguments);
- },
- getDomStyles(callback) {
-
- let view = uni
- .createSelectorQuery()
- .in(this)
- .select('.lsj-file')
- view.fields(
- {
- size: true,
- rect: true
- },
- ({ height, width, top, left, right, bottom }) => {
- uni.createSelectorQuery()
- .selectViewport()
- .scrollOffset(({ scrollTop }) => {
- return callback({
- top: parseInt(top) + parseInt(scrollTop) + 'px',
- left: parseInt(left) + 'px',
- width: parseInt(width) + 'px',
- height: parseInt(height) + 'px'
- })
- })
- .exec()
- }
- ).exec()
-
-
- const dom = weex.requireModule('dom')
- dom.getComponentRect(this.$refs.lsj, ({ size: { height, width, top, left, right, bottom } }) => {
- return callback({
- top: parseInt(top) + 'px',
- left: parseInt(left) + 'px',
- width: parseInt(width) + 'px',
- height: parseInt(height) + 'px',
- right: parseInt(right) + 'px',
- bottom: parseInt(bottom) + 'px'
- })
- })
-
- },
- show() {
- if (this._size && (this._size >= this.count)) {
- return;
- }
- this.isShow = true;
-
- this.lsjFile&&this.getDomStyles(styles => {
- this.lsjFile.dom.setStyle(styles)
- });
-
-
- this.lsjFile.dom.style.display = 'inline'
-
- },
- hide() {
- this.isShow = false;
-
- this.lsjFile&&this.lsjFile.dom.setStyle({
- top: '-100px',
- left:'0px',
- width: '1px',
- height: '100px',
- });
-
-
- this.lsjFile.dom.style.display = 'none'
-
- },
-
- upload(name) {
- this.lsjFile&&this.lsjFile.upload(name);
- },
-
- onchange(files) {
- this.$emit('getFile',this.xmtype)
- this.$emit('change',files);
- this._size = files.size;
- return files.size >= this.count ? this.hide() : this.show();
- },
-
- onprogress(item,end=false) {
- this.$emit('progress',item);
- if (end) {
- setTimeout(()=>{
- this.$emit('uploadEnd',item);
- },0);
- }
- },
-
- clear(name) {
- this.lsjFile.clear(name);
- },
-
- create() {
-
- let path = '/uni_modules/lsj-upload/hybrid/html/uploadFile.html';
- let dom = this.lsjFile.create(path);
-
- this.$refs.lsj.$el.appendChild(dom);
-
-
- this.show();
-
-
- dom.setStyle({position: this.position});
- dom.loadURL(path);
- setTimeout(()=>{
-
- plus.webview.currentWebview().append(dom);
-
-
- this.$root.$scope.$getAppWebview().append(dom);
-
- this.show();
- },300)
-
- },
-
- onClick() {
- if (this._size >= this.count) {
- this.toast(`只允许上传${this.count}个文件`);
- return;
- }
-
-
- if (!this.isShow) {return;}
- let count = this.count - this._size;
- this.lsjFile.chooseMessageFile(this.wxFileType,count);
-
- },
- toast(msg) {
- uni.showToast({
- title: msg,
- icon: 'none'
- });
- }
- }
- }
- </script>
- <style scoped>
- .lsj-file {
- display: inline-block;
- }
- .defview {
- background-color: #007aff;
- color: #fff;
- border-radius: 10rpx;
- display: flex;
- align-items: center;
- justify-content: center;
- font-size: 28rpx;
- }
- .hFile {
- position: relative;
- overflow: hidden;
- }
- </style>
|