前端维护属性库-获取

2023-11-12  本文已影响0人  Hasan_hs

使用方法

store.get(store.orderStatus,'5-1','')
import { isNumber } from 'lodash';
import { defineStore } from 'pinia';
const attribute = defineStore({
  id: 'attribute',
  state: () => ({
    orderStatus:[
        {
            name:"交易取消",
            value:0
        },{
            name:"已付款",
            value:1
        },{
            name:"待付款",
            value:2
        },{
            name:"待收货",
            value:3
        },{
            name:"已收货",
            value:4
        },{
            name:"售后中",
            value:5,
            items:[
                {
                    name:"售后中(退货退款)",
                    value:1
                },
                {
                    name:"售后中(仅退款)",
                    value:2,
                }
            ]
        },{
            name:"交易取消",
            value:6
        },{
            name:"交易取消",
            value:7
        },{
            name:"交易取消",
            value:8
        }
    ]
  }),

  actions: {
    /**
     * 获取-返回属性Obj,value和name不能同时传入
     * @param {*} lists 赛选元数据
     * @param {*} value 赛选依赖项
     * @param {*} name 根据名字赛选
     * @returns 
     */
    get(lists,value,name = '占位'){
        value = (isNumber(value) ? value.toString() : value).split("-");
        if(!lists || (value.length == 0 && name != '占位')){
            uni.showToast({
                icon:'none',
                title: "获取属性传参数错误",
            })
            return;
        }
        //优先使用value值获取
        if(value.length != 0){
            let newValue = this.findList(lists,value[0],'value');
            if(newValue.items){
                if(value.length == 1){
                    return newValue;
                }else{
                    value = value.splice(1).join('-');
                    return this.get(newValue.items,value,name);
                }
                
            }else{
                return newValue;
            }
        }else{
            return this.findList(lists,name,'name')
        }
    },
    findList(lists,value,key){
        return lists.find((e)=>{
            return e[key] == value;
        })
    }
  },
  persist: {
    enabled: false
  },
});

export default attribute;



上一篇下一篇

猜你喜欢

热点阅读