351 lines
8.7 KiB
Vue
Raw Normal View History

2024-04-28 10:10:03 +08:00
<template>
<view>
<template v-if="!readonly">
<view v-if="formProps.expanding && dataOptions.length === 0" style="color:#E79467;">无选项😢请检查设置</view>
<!-- 多选 -->
<template v-else-if="formProps.multiple">
<uni-data-checkbox v-if="formProps.expanding" :map="optionsKeyMap" multiple v-model="__value"
:localdata="dataOptions" />
<multiple-picker v-slot="{show}" v-else index="name" v-model="__value" :options="dataOptions">
<click-input :value="selectVal" :placeholder="formProps.placeholder || '请选择'" @click="show" />
</multiple-picker>
</template>
<!-- 单选 -->
<template v-else>
2024-06-07 16:48:09 +08:00
<!-- title == '司机姓名' -->
2024-04-28 10:10:03 +08:00
<uni-data-checkbox v-if="formProps.expanding" :map="optionsKeyMap" v-model="__value"
:localdata="dataOptions" />
2024-06-19 11:25:04 +08:00
<picker class="picker" v-else-if="title == '司机姓名'" @cancel="onCancel" @change="ok2" mode="selector" ref="pickerBottom"
2024-06-07 16:48:09 +08:00
:value="index" range-key="name" :range="dataOptions2">
<uni-search-bar v-if="isSearch" bgColor="#fff" v-model="searchValue" class="search" radius="5"
placeholder="请输入" clearButton="auto" cancelButton="none" @input="searchChange" />
<click-input @click="isSearchFn()" :value="(modelValue || []).length > 0 ? dataOptions[index]:null"
index="name" :placeholder="formProps.placeholder || '请选择'" />
</picker>
2024-06-19 11:25:04 +08:00
<picker v-else @change="ok" mode="selector" :value="index" range-key="name" :range="dataOptions">
2024-05-09 00:00:15 +08:00
<click-input :value="(modelValue || []).length > 0 ? dataOptions[index]:null" index="name"
2024-04-28 10:10:03 +08:00
:placeholder="formProps.placeholder || '请选择'" />
</picker>
</template>
</template>
<!-- 只读模式 -->
<template v-else>
2024-04-30 00:30:46 +08:00
<!-- <click-input disabled v-if="formProps.multiple" :value="modelValue.length > 0 ? dataOptions[index]:null"
index="name" /> -->
2024-06-07 16:48:09 +08:00
<click-input disabled v-if="formProps.multiple" :value="(modelValue || []).length > 0 ? dataOptions:null"
index="name" />
2024-04-28 10:10:03 +08:00
<click-input disabled v-else :value="selectVal" />
</template>
</view>
</template>
<script setup>
2024-06-07 16:48:09 +08:00
import {
computed,
onMounted,
ref,
watch
} from 'vue'
import {
$nEmpty
} from '@/utils/tool.js'
2024-04-28 10:10:03 +08:00
import ClickInput from '@/components/ClickInput.vue'
import MultiplePicker from '@/components/common/MultiplePicker.vue'
const props = defineProps({
formProps: {
type: Object,
default: () => {
return {}
}
},
modelValue: {
type: Array,
default: () => {
return []
}
},
2024-06-07 16:48:09 +08:00
readonly: Boolean,
title: String,
2024-04-28 10:10:03 +08:00
})
const _value = computed({
get() {
return props.modelValue || []
},
set(val) {
emits('update:modelValue', val)
}
})
const selectVal = computed(() => {
return dataOptions.value.filter(v => (props.modelValue || []).indexOf(v.value) > -1).map(v => v.name)
})
const __value = computed({
get() {
return props.formProps.multiple ? _value.value : _value.value[0]
},
set(val) {
if (props.formProps.multiple) {
emits('update:modelValue', val)
} else {
emits('update:modelValue', [val])
}
}
})
2024-06-07 16:48:09 +08:00
const emits = defineEmits(['update:modelValue', 'resize'])
2024-04-28 10:10:03 +08:00
const loading = ref(false)
const dataOptions = ref([])
2024-06-07 16:48:09 +08:00
const dataOptions2 = ref([])
2024-04-28 10:10:03 +08:00
let preHandlerFuc = null
let aftHandlerFuc = null
2024-06-07 16:48:09 +08:00
const optionsKeyMap = {
text: 'name',
value: 'value'
}
2024-04-28 10:10:03 +08:00
const index = ref(0)
function loadOptionsData() {
try {
if (props.formProps.fixed) {
2024-06-07 16:48:09 +08:00
dataOptions.value = props.formProps.options;
2024-04-28 10:10:03 +08:00
} else {
doRequest(dataOptions.value)
}
} catch (e) {
console.log(e)
}
}
2024-06-07 16:48:09 +08:00
watch(() => dataOptions, () => {
dataOptions2.value = dataOptions.value;
}, {
deep: true
});
2024-04-28 10:10:03 +08:00
onMounted(() => {
loadOptionsData()
})
2024-06-07 16:48:09 +08:00
function resizeCollapse() {
2024-05-18 00:52:06 +08:00
setTimeout(() => emits('resize'), 800)
}
2024-06-07 16:48:09 +08:00
2024-04-28 10:10:03 +08:00
function ok(e) {
index.value = e.detail.value
__value.value = dataOptions.value[index.value].value
2024-05-18 00:52:06 +08:00
resizeCollapse();
2024-04-28 10:10:03 +08:00
}
2024-06-07 16:48:09 +08:00
function ok2(e) {
const id = dataOptions2.value[e.detail.value].value;
const findIndex = dataOptions.value.findIndex(item => item.value == id);
// alert(findIndex)
if (findIndex > -1) {
index.value = findIndex;
__value.value = dataOptions.value[findIndex].value;
resizeCollapse();
isSearch.value = false;
}
}
// 搜索查询
const isSearch = ref(false);
const searchValue = ref("");
2024-06-19 11:25:04 +08:00
const searchBottom = ref(0);
2024-06-07 16:48:09 +08:00
function searchChange(e) {
console.log(e);
// ,调模糊查询然后 把返回的结果传给this.list数组
let findList = dataOptions.value.filter(item => item.name.includes(e))
dataOptions2.value = findList
if (e == '') {
dataOptions2.value = dataOptions.value
}
}
const onCancel = () => {
isSearch.value = false;
}
const isSearchFn = () => {
2024-06-19 11:25:04 +08:00
//.box获取class为box的元素如果使用的id= 'box' 则使用'#box'
// uni.createSelectorQuery().in(this).select('.picker').boundingClientRect(data => {
// console.log(JSON.stringify(data))
// console.log(data.height / 100 * window.innerHeight,window.innerHeight)
// console.log(uni.getSystemInfoSync().windowHeight, uni.getSystemInfoSync().windowWidth)
// uni.getSystemInfo({
// success: function (res) {
// console.log(res.screenHeight);
// console.log(res.windowHeight);
// console.log(data.height / 100 * res.screenHeight)
// // data.height / 100 * res.screenHeight - 22
// // searchBottom.value = (45 / 8) + 238;
// }
// });
// // (data.height / 100 * window.innerHeight) * 1.48
// }).exec()
2024-06-07 16:48:09 +08:00
setTimeout(() => {
isSearch.value = true;
}, 500)
}
2024-04-28 10:10:03 +08:00
function doRequest(options) {
const http = props.formProps.http || {}
if (http.url && http.method) {
const params = {
url: http.url,
method: http.method,
headers: {
'content-type': http.contentType === 'JSON' ? 'application/json' :
'application/x-www-form-urlencoded',
...coverParams(http.headers || [])
},
params: {},
data: http.contentType === 'JSON' ? JSON.parse(http.data || '{}') : coverParams(http.params || []),
}
preHandler(params, http.preHandler)
if (http.contentType !== 'JSON') {
2024-06-07 16:48:09 +08:00
params.data = {
...params.data,
...params.params
}
2024-04-28 10:10:03 +08:00
}
loading.value = true
uni.request({
...params,
timeout: 20000,
header: params.headers,
withCredentials: true,
dataType: 'json',
success: (rsp) => {
loading.value = false
2024-05-18 20:34:50 +08:00
// console.log('获取到数据: ', JSON.stringify(rsp))
2024-04-28 10:10:03 +08:00
const ops = aftHandler(rsp, http.aftHandler)
options.push(...(ops || []))
},
fail: (err) => {
loading.value = false
uni.showToast({
icon: 'none',
title: '请求http数据源发生异常:' + JSON.stringify(err)
})
}
})
}
}
//数组转对象
function coverParams(args, isForm = false) {
const params = {};
if (Array.isArray(args)) {
args.forEach(arg => {
if ($nEmpty(arg.name)) {
params[arg.name] = arg.value
}
})
}
return params
}
//前置处理
function preHandler(params, script) {
if (script) {
if (!preHandlerFuc) {
preHandlerFuc = new Function('ctx', `${script}\n preHandler(ctx)`)
}
try {
preHandlerFuc(params)
} catch (e) {
console.log(e)
}
}
}
//后置处理
function aftHandler(rsp, aftHandler) {
2024-04-30 00:30:46 +08:00
if (aftHandler) {
2024-04-28 10:10:03 +08:00
return toFunc(rsp, aftHandler)
} else {
//新格式,带字段设置
if (aftHandler.isJs) {
return toFunc(rsp, aftHandler.js)
} else {
//取值进行转,拿到数组
const dataArr = getData(rsp, ifEmGet(aftHandler.rule.source, 'data'));
const name = ifEmGet(aftHandler.rule.name, 'name')
const value = ifEmGet(aftHandler.rule.value, 'value')
return (dataArr || []).map(v => {
return {
name: v[name],
value: v[value],
}
})
}
}
}
function ifEmGet(v, dv) {
return $nEmpty(v) ? v : dv
}
function getData(obj, path) {
// 将路径字符串分割成数组
const keys = path.split('.');
// 初始化结果为传入的对象
let result = obj;
// 逐层查找属性值
for (const key of keys) {
if (result.hasOwnProperty(key)) {
result = result[key];
} else {
return undefined;
}
}
return result;
}
function toFunc(rsp, script) {
if (script) {
if (!aftHandlerFuc) {
aftHandlerFuc = new Function('rsp', `${script}\n return aftHandler(rsp)`)
}
try {
return aftHandlerFuc(rsp) || []
} catch (e) {
console.log(e)
}
}
return []
}
</script>
2024-06-07 19:31:29 +08:00
<style>
2024-06-19 14:56:32 +08:00
/* :deep(.uni-picker-custom), .uni-picker-custom {
2024-06-07 19:31:29 +08:00
height: 480rpx;
2024-06-19 14:56:32 +08:00
} */
2024-06-07 19:31:29 +08:00
</style>
2024-06-07 16:48:09 +08:00
<style scoped>
.search {
position: fixed;
2024-06-19 11:25:04 +08:00
/* bottom: 465rpx; */
2024-06-19 14:56:32 +08:00
bottom: 242px;
/* bottom: 400rpx; */
2024-06-07 16:48:09 +08:00
left: 50%;
transform: translateX(-50%);
z-index: 9999;
2024-06-19 11:25:04 +08:00
padding: 0;
2024-06-07 16:48:09 +08:00
}
2024-06-19 11:25:04 +08:00
2024-06-07 16:48:09 +08:00
.search /deep/ .uni-searchbar__box {
width: 400rpx;
border: 2rpx solid #f5f5f5;
}
2024-04-28 10:10:03 +08:00
</style>