index.vue 2.62 KB
<!-- author cuibo -->
<template>
	<c-select v-model="model" v-bind="$attrs" placeholder="请选择币种" filterable v-bind:disabled="isDisable" @change="handleChange"
	          popper-class="m-currency-dropdown">
		<el-option v-for="item in codes" :key="item.value" :label="item.label" :value="item.value" :disabled="disabledList.indexOf(item.value)!== -1">
		</el-option>
	</c-select>
</template>

<script>
	import QueryRequest from "../../model/QueryRequest";
	import Currency from "../../model/Currency";
	import Select from "../Select"

	export default {
		components: {
			'c-select': Select
		},
		props: {
			value: {
				type: [String, Array, Number],
				default: undefined
			},
			disabledList: {
				type: Array,
				default: function () {
					return []
				}
			},
			disabled: {
				type: Boolean,
				default: false
			},
			isOften: {
				type: Boolean,
				default: true
			},
		},
		computed: {
			model: {
				get() {
					return this.value;
				},
				set(newVal) {
					this.$emit("input", newVal);
				}
			},
			isDisable: {
				get() {
					return this.disabled
				}
			},
		},
		data: function() {
			return {
				codes: [],
				list: [],
				currencyMap: {}
			}
		},
		created() {
			this.handleSearch();
		},
		methods: {
			listCurs(){
				return Promise.resolve(null)
			},
			handleSearch() {
				var params = new QueryRequest().data;
				params.current = 0;
				params.size = 0;
				params.data = new Currency().data;
				// console.log('this.isOften', this.isOften)
				let key = 'curCache'
				if (this.isOften === true) {
					//查询条件——是常用币种
					params.data.isOften = 'Y'
					key = key + 'Y'
				} else {
					params.data = {};
				}
				let promise;
				if (!window[key] || new Date() - window[key].lastTime > 30000) //30s内不超时
				{
					promise = listCurs(params)
					window[key] = {
						lastTime: new Date() - 0,
						promise
					}
				} else {
					promise = window[key].promise
				}

				promise
					.then(res => {
						if (res.code === SUCCESS) {
							this.list = res.data;
							console.log('list', this.list)
							for (var i = 0; i < this.list.length; i++) {
								this.codes.push({
									value: this.list[i].currCode,
									label: this.list[i].currSymbol + '-' + this.list[i].currCnName
								});
								//利用map遍历list
								this.currencyMap[this.list[i].currCode] = JSON.parse(JSON.stringify(this.list[i]))
							}

						}
					})
					.catch(error => {
						console.log(error);
					});
			},
			handleChange(value) {
				//根据value在currencyMap中找到list中的币种对象 返回
				this.$emit('update-currency', this.currencyMap[value])
			}
		}
	}
</script>