Skip to content
导航栏

处理器yao.component.selectoptions修正

当在脚本环境中使用处理器yao.component.selectoptions时,无法获取正确数据。

:也可以不修正代码,换一个处理方式,直接调用处理器models.<mode_id>.get的方式

比如有以下代码,是在 jsapi 中调用处理器yao.component.selectoptions。如果不调用Yao框架代码,无法筛选条件wheres无法正确运行。

js
function Test() {
  const param = {
    key: 'search',
    label: 'name',
    model: 'material.category',
    search: '',
    value: 'id',
    wheres: '{"column":"id","op":"ne","value":"4"}',
  };

  const data = Search(param);
  console.log(data);
}

function Search(param) {
  if (param.search == '') return [];

  // 使用字符串数组
  const wheres_str = [
    param.wheres,
    JSON.stringify({
      column: 'name',
      op: 'match',
      value: param.search,
    }),
  ];

  // 使用对象数组
  const wheres_object = [
    JSON.parse(param.wheres),
    {
      column: 'name',
      op: 'match',
      value: param.search,
    },
  ];

  param.wheres = wheres_object;
  const values = Process('yao.component.SelectOptions', param);
  return values;
}

尝试修正如下,增加对]interface{}的判断与处理。

处理传入条件对象数组:

https://github.com/wwsheng009/yao/commit/f6637e9dc29e2e1e418d0c84c37577e10f51ab9d

处理应付传入单个条件对象:

https://github.com/wwsheng009/yao/commit/347f491d6c2da3d3e00d4d235cb2c2c898633c5f

go

	case []interface{}:
		for _, line := range input {
			if data, ok := line.(string); ok {
				where := gou.QueryWhere{}
				err := jsoniter.Unmarshal([]byte(data), &where)
				if err != nil {
					exception.New("query.wheres error %s", 400, err.Error()).Throw()
				}
				wheres = append(wheres, where)
			} else {
				data, err := jsoniter.Marshal(line)
				if err != nil {
					exception.New("query.wheres error: %s", 400, err).Throw()
				}
				where := gou.QueryWhere{}
				err = jsoniter.Unmarshal([]byte(data), &where)
				if err != nil {
					exception.New("query.wheres error %s", 400, err.Error()).Throw()
				}
				wheres = append(wheres, where)
			}
		}
    case interface{}:
		data, err := jsoniter.Marshal(input)
		if err != nil {
			exception.New("query.wheres error: %s", 400, err).Throw()
		}
		where := gou.QueryWhere{}
		err = jsoniter.Unmarshal([]byte(data), &where)
		if err != nil {
			exception.New("query.wheres error %s", 400, err.Error()).Throw()
		}
		wheres = append(wheres, where)