| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622 | export default {  props: {    localdata: {      type: [Array, Object],      default () {        return []      }    },    spaceInfo: {      type: Object,      default () {        return {}      }    },    collection: {      type: String,      default: ''    },    action: {      type: String,      default: ''    },    field: {      type: String,      default: ''    },    orderby: {      type: String,      default: ''    },    where: {      type: [String, Object],      default: ''    },    pageData: {      type: String,      default: 'add'    },    pageCurrent: {      type: Number,      default: 1    },    pageSize: {      type: Number,      default: 500    },    getcount: {      type: [Boolean, String],      default: false    },    getone: {      type: [Boolean, String],      default: false    },    gettree: {      type: [Boolean, String],      default: false    },    manual: {      type: Boolean,      default: false    },    value: {      type: [Array, String, Number],      default () {        return []      }    },    modelValue: {      type: [Array, String, Number],      default () {        return []      }    },    preload: {      type: Boolean,      default: false    },    stepSearh: {      type: Boolean,      default: true    },    selfField: {      type: String,      default: ''    },    parentField: {      type: String,      default: ''    },    multiple: {      type: Boolean,      default: false    },    map: {      type: Object,      default () {        return {          text: "text",          value: "value"        }      }    }  },  data() {    return {      loading: false,      errorMessage: '',      loadMore: {        contentdown: '',        contentrefresh: '',        contentnomore: ''      },      dataList: [],      selected: [],      selectedIndex: 0,      page: {        current: this.pageCurrent,        size: this.pageSize,        count: 0      }    }  },  computed: {    isLocalData() {      return !this.collection.length;    },    isCloudData() {      return this.collection.length > 0;    },    isCloudDataList() {      return (this.isCloudData && (!this.parentField && !this.selfField));    },    isCloudDataTree() {      return (this.isCloudData && this.parentField && this.selfField);    },    dataValue() {      let isModelValue = Array.isArray(this.modelValue) ? (this.modelValue.length > 0) : (this.modelValue !== null ||        this.modelValue !== undefined);      return isModelValue ? this.modelValue : this.value;    },    hasValue() {      if (typeof this.dataValue === 'number') {        return true      }      return (this.dataValue != null) && (this.dataValue.length > 0)    }  },  created() {    this.$watch(() => {      var al = [];      ['pageCurrent',        'pageSize',        'spaceInfo',        'value',        'modelValue',        'localdata',        'collection',        'action',        'field',        'orderby',        'where',        'getont',        'getcount',        'gettree'      ].forEach(key => {        al.push(this[key])      });      return al    }, (newValue, oldValue) => {      let needReset = false      for (let i = 2; i < newValue.length; i++) {        if (newValue[i] != oldValue[i]) {          needReset = true          break        }      }      if (newValue[0] != oldValue[0]) {        this.page.current = this.pageCurrent      }      this.page.size = this.pageSize      this.onPropsChange()    })    this._treeData = []  },  methods: {    onPropsChange() {      this._treeData = [];    },    // 填充 pickview 数据    async loadData() {      if (this.isLocalData) {        this.loadLocalData();      } else if (this.isCloudDataList) {        this.loadCloudDataList();      } else if (this.isCloudDataTree) {        this.loadCloudDataTree();      }    },    // 加载本地数据    async loadLocalData() {      this._treeData = [];      this._extractTree(this.localdata, this._treeData);      let inputValue = this.dataValue;      if (inputValue === undefined) {        return;      }      if (Array.isArray(inputValue)) {        inputValue = inputValue[inputValue.length - 1];        if (typeof inputValue === 'object' && inputValue[this.map.value]) {          inputValue = inputValue[this.map.value];        }      }      this.selected = this._findNodePath(inputValue, this.localdata);    },    // 加载 Cloud 数据 (单列)    async loadCloudDataList() {      if (this.loading) {        return;      }      this.loading = true;      try {        let response = await this.getCommand();        let responseData = response.result.data;        this._treeData = responseData;        this._updateBindData();        this._updateSelected();        this.onDataChange();      } catch (e) {        this.errorMessage = e;      } finally {        this.loading = false;      }    },    // 加载 Cloud 数据 (树形)    async loadCloudDataTree() {      if (this.loading) {        return;      }      this.loading = true;      try {        let commandOptions = {          field: this._cloudDataPostField(),          where: this._cloudDataTreeWhere()        };        if (this.gettree) {          commandOptions.startwith = `${this.selfField}=='${this.dataValue}'`;        }        let response = await this.getCommand(commandOptions);        let responseData = response.result.data;        this._treeData = responseData;        this._updateBindData();        this._updateSelected();        this.onDataChange();      } catch (e) {        this.errorMessage = e;      } finally {        this.loading = false;      }    },    // 加载 Cloud 数据 (节点)    async loadCloudDataNode(callback) {      if (this.loading) {        return;      }      this.loading = true;      try {        let commandOptions = {          field: this._cloudDataPostField(),          where: this._cloudDataNodeWhere()        };        let response = await this.getCommand(commandOptions);        let responseData = response.result.data;        callback(responseData);      } catch (e) {        this.errorMessage = e;      } finally {        this.loading = false;      }    },    // 回显 Cloud 数据    getCloudDataValue() {      if (this.isCloudDataList) {        return this.getCloudDataListValue();      }      if (this.isCloudDataTree) {        return this.getCloudDataTreeValue();      }    },    // 回显 Cloud 数据 (单列)    getCloudDataListValue() {      // 根据 field's as value标识匹配 where 条件      let where = [];      let whereField = this._getForeignKeyByField();      if (whereField) {        where.push(`${whereField} == '${this.dataValue}'`)      }      where = where.join(' || ');      if (this.where) {        where = `(${this.where}) && (${where})`      }      return this.getCommand({        field: this._cloudDataPostField(),        where      }).then((res) => {        this.selected = res.result.data;        return res.result.data;      });    },    // 回显 Cloud 数据 (树形)    getCloudDataTreeValue() {      return this.getCommand({        field: this._cloudDataPostField(),        getTreePath: {          startWith: `${this.selfField}=='${this.dataValue}'`        }      }).then((res) => {        let treePath = [];        this._extractTreePath(res.result.data, treePath);        this.selected = treePath;        return treePath;      });    },    getCommand(options = {}) {      /* eslint-disable no-undef */      let db = uniCloud.database(this.spaceInfo)      const action = options.action || this.action      if (action) {        db = db.action(action)      }      const collection = options.collection || this.collection      db = db.collection(collection)      const where = options.where || this.where      if (!(!where || !Object.keys(where).length)) {        db = db.where(where)      }      const field = options.field || this.field      if (field) {        db = db.field(field)      }      const orderby = options.orderby || this.orderby      if (orderby) {        db = db.orderBy(orderby)      }      const current = options.pageCurrent !== undefined ? options.pageCurrent : this.page.current      const size = options.pageSize !== undefined ? options.pageSize : this.page.size      const getCount = options.getcount !== undefined ? options.getcount : this.getcount      const getTree = options.gettree !== undefined ? options.gettree : this.gettree      const getOptions = {        getCount,        getTree      }      if (options.getTreePath) {        getOptions.getTreePath = options.getTreePath      }      db = db.skip(size * (current - 1)).limit(size).get(getOptions)      return db    },    _cloudDataPostField() {      let fields = [this.field];      if (this.parentField) {        fields.push(`${this.parentField} as parent_value`);      }      return fields.join(',');    },    _cloudDataTreeWhere() {      let result = []      let selected = this.selected      let parentField = this.parentField      if (parentField) {        result.push(`${parentField} == null || ${parentField} == ""`)      }      if (selected.length) {        for (var i = 0; i < selected.length - 1; i++) {          result.push(`${parentField} == '${selected[i].value}'`)        }      }      let where = []      if (this.where) {        where.push(`(${this.where})`)      }      if (result.length) {        where.push(`(${result.join(' || ')})`)      }      return where.join(' && ')    },    _cloudDataNodeWhere() {      let where = []      let selected = this.selected;      if (selected.length) {        where.push(`${this.parentField} == '${selected[selected.length - 1].value}'`);      }      where = where.join(' || ');      if (this.where) {        return `(${this.where}) && (${where})`      }      return where    },    _getWhereByForeignKey() {      let result = []      let whereField = this._getForeignKeyByField();      if (whereField) {        result.push(`${whereField} == '${this.dataValue}'`)      }      if (this.where) {        return `(${this.where}) && (${result.join(' || ')})`      }      return result.join(' || ')    },    _getForeignKeyByField() {      let fields = this.field.split(',');      let whereField = null;      for (let i = 0; i < fields.length; i++) {        const items = fields[i].split('as');        if (items.length < 2) {          continue;        }        if (items[1].trim() === 'value') {          whereField = items[0].trim();          break;        }      }      return whereField;    },    _updateBindData(node) {      const {        dataList,        hasNodes      } = this._filterData(this._treeData, this.selected)      let isleaf = this._stepSearh === false && !hasNodes      if (node) {        node.isleaf = isleaf      }      this.dataList = dataList      this.selectedIndex = dataList.length - 1      if (!isleaf && this.selected.length < dataList.length) {        this.selected.push({          value: null,          text: "请选择"        })      }      return {        isleaf,        hasNodes      }    },    _updateSelected() {      let dl = this.dataList      let sl = this.selected      let textField = this.map.text      let valueField = this.map.value      for (let i = 0; i < sl.length; i++) {        let value = sl[i].value        let dl2 = dl[i]        for (let j = 0; j < dl2.length; j++) {          let item2 = dl2[j]          if (item2[valueField] === value) {            sl[i].text = item2[textField]            break          }        }      }    },    _filterData(data, paths) {      let dataList = []      let hasNodes = true      dataList.push(data.filter((item) => {        return (item.parent_value === null || item.parent_value === undefined || item.parent_value === '')      }))      for (let i = 0; i < paths.length; i++) {        let value = paths[i].value        let nodes = data.filter((item) => {          return item.parent_value === value        })        if (nodes.length) {          dataList.push(nodes)        } else {          hasNodes = false        }      }      return {        dataList,        hasNodes      }    },    _extractTree(nodes, result, parent_value) {      let list = result || []      let valueField = this.map.value      for (let i = 0; i < nodes.length; i++) {        let node = nodes[i]        let child = {}        for (let key in node) {          if (key !== 'children') {            child[key] = node[key]          }        }        if (parent_value !== null && parent_value !== undefined && parent_value !== '') {          child.parent_value = parent_value        }        result.push(child)        let children = node.children        if (children) {          this._extractTree(children, result, node[valueField])        }      }    },    _extractTreePath(nodes, result) {      let list = result || []      for (let i = 0; i < nodes.length; i++) {        let node = nodes[i]        let child = {}        for (let key in node) {          if (key !== 'children') {            child[key] = node[key]          }        }        result.push(child)        let children = node.children        if (children) {          this._extractTreePath(children, result)        }      }    },    _findNodePath(key, nodes, path = []) {      let textField = this.map.text      let valueField = this.map.value      for (let i = 0; i < nodes.length; i++) {        let node = nodes[i]        let children = node.children        let text = node[textField]        let value = node[valueField]        path.push({          value,          text        })        if (value === key) {          return path        }        if (children) {          const p = this._findNodePath(key, children, path)          if (p.length) {            return p          }        }        path.pop()      }      return []    }  }}
 |