|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413 |
- const reqInterface = require("../../../api/models");
- const {
- findidx,
- isValidJson
- } = require("../../../utils/dataHelper");
- Component({
-
- /**
- * 组件的属性列表
- */
- properties: {
- label: {
- type: String,
- value: ''
- },
- dict: {
- type: String,
- value: ''
- },
- list: {
- type: Array,
- value: []
- },
- cols: {
- type: Number,
- value: 1
- },
- value: {
- type: "any",
- value: ""
- },
- transmitData: Array,
- position: {
- type: String,
- value: 'bottom'
- },
- requiredMark: {
- type: Boolean,
- value: true
- },
- activeMode: {
- type: Boolean,
- value: false
- },
- },
-
- observers: {
- 'cols': function (val) {
- let self = this;
- self.setData({
- columns: val
- })
- },
- list(val) {
- let self = this;
- if (val && val.length > 0) {
- self.setData({
- pickerList: val,
- oneValue: val[0].dictKey
- })
- self.triggerEvent('getvalue', {
- val: self.data.oneValue
- })
- }
- },
- 'dict': async function (val) {
- let self = this;
- if (val) {
- if (self.data.columns == 1) {
- self.setData({
- dictionary: val,
- })
- } else {
- self.getData(val)
- }
- }
- },
- 'label': function (val) {
- let self = this;
- self.setData({
- placeholder: '请选择' + val
- })
- },
- 'value': function (val) {
- let self = this;
- // JSON.parse(val)有效
- if ((val && isValidJson(JSON.parse(val))) || (val == 0 && isValidJson(JSON.parse(val)))) {
- if (typeof (JSON.parse(val)) == 'string') {
- self.setData({
- pickerTxt: val ? JSON.parse(val) : '',
- oneValue: val ? JSON.parse(val) : ''
- })
- self.triggerEvent('getvalue', {
- val: self.data.oneValue
- })
- } else {
- if (self.data.pickerList.length > 0) {
- console.log(self.data.pickerList)
- let idx = findidx(self.data.pickerList, val)
- self.data.pickerTxt = (val || val == 0) ? self.data.pickerList[idx].dictValue : ''
- self.setData({
- index: idx,
- pickerTxt: self.data.pickerTxt
- })
- } else {
- if (self.data.dictionary) {
- self.getReq(self.data.dictionary).then(res => {
- self.setData({
- pickerList: res
- })
- let idx = findidx(self.data.pickerList, val)
- self.data.pickerTxt = (val || val == 0) ? self.data.pickerList[idx].dictValue : ''
- self.setData({
- index: idx,
- pickerTxt: self.data.pickerTxt
- })
- })
- }
- }
- }
- } else if ((val && isValidJson(JSON.parse(val))) || (val == 0 && isValidJson(JSON.parse(val)))) {
- if (typeof (JSON.parse(val)) == 'string') {
- self.setData({
- pickerTxt: val ? JSON.parse(val) : ''
- })
- } else {
- self.data.pickerTxt = JSON.parse(val).map(item => item.dictValue ? item.dictValue + '/' : '').filter(part => part);
- if (self.data.pickerTxt.length > 0) {
- self.data.pickerTxt[self.data.pickerTxt.length - 1] = self.data.pickerTxt[self.data.pickerTxt.length - 1].slice(0, -1);
- }
- self.data.pickerTxt = self.data.pickerTxt.join('');
- self.setData({
- pickerTxt: self.data.pickerTxt
- })
- }
- } else {
- self.setData({
- index: 0,
- pickerTxt: ''
- })
- }
- },
- },
-
- lifetimes: {
- attached() {
- let self = this;
- if (self.data.dictionary) {
- self.getReq(self.data.dictionary).then(res => {
- self.setData({
- pickerList: res,
- oneValue: res[0].dictKey
- })
- })
- }
- }
- },
-
- /**
- * 组件的初始数据
- */
- data: {
- styleCss: 'padding: 20rpx 0; line-height: 36rpx font-size: 28rpx; border-bottom: 1rpx dashed #f0f0f0;',
- dictionary: '',
- pickerList: [],
- index: 0,
- oneValue: 0,
- pickerTxt: '',
-
- // 多列, 最多四列吧
- columns: 1,
- totalArray: [],
- firstArray: [],
- secondArray: [],
- thirdArray: [],
- fourthArray: [],
- multiIndex: [],
- multiValue: []
- },
-
- methods: {
- onePickerChange(e) {
- let self = this;
- console.log(e)
- self.setData({
- index: e.detail.value,
- pickerTxt: self.data.pickerList[e.detail.value].dictValue,
- oneValue: self.data.pickerList[e.detail.value].dictKey
- })
- self.triggerEvent('getvalue', {
- val: self.data.oneValue
- })
- },
-
- multiPickerChange(e) {
- let self = this;
- self.data.multiValue = [];
- for (let i = 0; i < self.data.multiIndex.length; i++) {
- self.data.multiValue.push(self.data.pickerList[i][self.data.multiIndex[i]] ? self.data.pickerList[i][self.data.multiIndex[i]].dictKey : 0)
- }
- self.data.pickerTxt = self.data.multiIndex.map((item, i) => {
- let name = self.data.pickerList[i][self.data.multiIndex[i]] ? self.data.pickerList[i][self.data.multiIndex[i]].dictValue : '';
- return name ? name + '/' : '';
- }).filter(part => part);
- if (self.data.pickerTxt.length > 0) {
- self.data.pickerTxt[self.data.pickerTxt.length - 1] = self.data.pickerTxt[self.data.pickerTxt.length - 1].slice(0, -1);
- }
- self.data.pickerTxt = self.data.pickerTxt.join('')
- self.setData({
- multiValue: self.data.multiValue,
- pickerTxt: self.data.pickerTxt
- })
- self.triggerEvent('getvalue', {
- val: self.data.multiValue,
- label: self.data.pickerTxt
- })
- },
-
- // 列改变
- bindMultiPickerColumnChange(e) {
- let self = this;
- // 第几列
- let col = e.detail.column;
- // 第几个下标
- let idx = e.detail.value;
- self.data.multiIndex[col] = idx;
- self.setData({
- multiIndex: self.data.multiIndex
- })
- if (col == 0 && self.data.columns == 2) {
- self.data.multiIndex[1] = 0;
- self.data.pickerList[1] = [];
- self.setData({
- secondArray: [],
- multiIndex: self.data.multiIndex,
- pickerList: self.data.pickerList
- })
- // 滑动第一列,请求数据
- self.getReq(self.data.firstArray[idx].dictKey).then(res => {
- self.setData({
- secondArray: res,
- })
- self.data.pickerList[1] = self.data.secondArray;
- self.setData({
- pickerList: self.data.pickerList
- })
- })
- }
- // 四列
- if (self.data.columns == 4) {
- // 滑动第一列,请求数据
- switch (col) {
- case 0:
- self.data.multiIndex[1] = 0;
- self.data.pickerList[1] = [];
- self.data.multiIndex[2] = 0;
- self.data.pickerList[2] = [];
- self.data.multiIndex[3] = 0;
- self.data.pickerList[3] = [];
- self.setData({
- secondArray: [],
- multiIndex: self.data.multiIndex,
- pickerList: self.data.pickerList
- })
- self.getReq(self.data.firstArray[idx].dictKey).then(res => {
- self.setData({
- secondArray: res,
- })
- self.data.pickerList[1] = self.data.secondArray;
- self.setData({
- pickerList: self.data.pickerList,
- })
- })
- break;
- case 1:
- self.getReq(self.data.secondArray[idx].dictKey).then(res => {
- self.data.multiIndex[2] = 0;
- self.data.pickerList[2] = [];
- self.data.multiIndex[3] = 0;
- self.data.pickerList[3] = [];
- self.setData({
- thirdArray: [],
- multiIndex: self.data.multiIndex,
- pickerList: self.data.pickerList
- })
- if (res) {
- self.setData({
- thirdArray: res,
- })
- self.data.pickerList[2] = self.data.thirdArray;
- self.setData({
- pickerList: self.data.pickerList,
- })
- } else {
- self.setData({
- thirdArray: [],
- })
- self.data.pickerList[2] = self.data.thirdArray;
- self.setData({
- pickerList: self.data.pickerList,
- })
- }
- })
- break;
- case 2:
- self.getReq(self.data.thirdArray[idx].dictKey).then(res => {
- self.data.multiIndex[3] = 0;
- self.data.pickerList[3] = [];
- self.setData({
- fourArray: [],
- multiIndex: self.data.multiIndex,
- pickerList: self.data.pickerList
- })
- if (res) {
- self.setData({
- fourArray: res,
- })
- self.data.pickerList[3] = self.data.fourArray;
- self.setData({
- pickerList: self.data.pickerList,
- })
- } else {
- self.setData({
- fourArray: [],
- })
- self.data.pickerList[3] = self.data.fourArray;
- self.setData({
- pickerList: self.data.pickerList,
- })
- }
- })
- break;
- }
- }
- },
-
- async getData(val) {
- let self = this;
- let res = null,
- res2 = null,
- res3 = null,
- res4 = null;
- res = await self.getReq(val)
- if (self.data.columns == 2) {
- self.setData({
- firstArray: res
- })
- res2 = await self.getReq(res[0].dictKey)
- self.setData({
- secondArray: res2,
- })
- self.data.pickerList[0] = self.data.firstArray;
- self.data.pickerList[1] = self.data.secondArray;
- self.setData({
- pickerList: self.data.pickerList,
- multiIndex: [0, 0]
- })
- } else if (self.data.columns != 1) {
- self.setData({
- firstArray: res
- })
- res2 = await self.getReq(res[0].dictKey)
- self.setData({
- secondArray: res2 ? res2 : []
- })
- if (res2) {
- res3 = await self.getReq(res2[0].dictKey)
- self.setData({
- thirdArray: res3 ? res3 : [],
- })
- } else {
- self.setData({
- thirdArray: [],
- })
- }
- if (res3) {
- res4 = await self.getReq(res3[0].dictKey)
- self.setData({
- fourthArray: res4 ? res4 : [],
- })
- } else {
- self.setData({
- fourthArray: [],
- })
- }
-
- self.data.pickerList[0] = self.data.firstArray;
- self.data.pickerList[1] = self.data.secondArray;
- self.data.pickerList[2] = self.data.thirdArray;
- self.data.pickerList[3] = self.data.fourthArray;
- self.setData({
- pickerList: self.data.pickerList,
- multiIndex: [0, 0, 0, 0]
- })
-
- }
- },
-
- getReq(val) {
- let self = this;
- return new Promise((resolve, reject) => {
- reqInterface.GetBladeSystemDictDictionary({
- code: val
- }).then(res => {
- resolve(res)
- }).catch(error => {
- console.error("GetDictDetail请求错误:", error);
- reject(error);
- });
- })
- }
- }
- })
|