Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

可以将request文件转化为配置的形式,并且在这一层做模型的parse和dispose #69

Closed
PLA31001 opened this issue Mar 23, 2021 · 3 comments

Comments

@PLA31001
Copy link

PLA31001 commented Mar 23, 2021

import Ajax from '../common/ajax';
import Utils from '../common/utils'
import Login from '../model/login/Login'

/**
  * 根据schema转化数据
  */
function convert(schema, data, fun){
    if (Array.isArray(schema)) {
        return (data || []).map(i => schema[0][fun](i));
    }
    if (schema instanceof Model ) {
        return schema[fun](data);
    }
    if (typeof schema == 'object') {
        let obj = {};
        for (const key of Object.keys(schema)) {
            obj[key] = convert(schema[key], data[key], fun);
        }
        return obj;
    }
    return data;
}

function gen(conf){
    let obj = {};
    // 遍历模块
    for (const moduleName of Object.keys(conf)) {
        let module = conf[moduleName];
        let newModule = obj[moduleName] = {};
        // 遍历模块下的每项
        for (const moduleItemName of Object.keys(module)) {
            let item = module[moduleItemName];
            // 如果是个funnction直接赋值
            if (typeof item == 'function') {
                newModule[moduleItemName] = item;
                continue;
            }
            // 解析配置
            let urlSchema = item.url || '';
            let reqSchema = item.request;
            let respSchema = item.response;
            let method = item.method == 'post' ? 'postJson' : (item.method || 'get');

            newModule[moduleItemName] = async function({ params, query, request}) {
                params = params || {};
                query = query || {};
                request = request || {};
                // 替换url中的变量
                let url = urlSchema
                    .split('/')
                    .map(i => i.indexOf(':') == 0 ? params[i.split(':')[1]] + '': i )
                    .join('/');
                url = Object.keys(query).length > 0 ? Utils.setUrlQuery(url, query) : url;
                request = reqSchema ? convert(reqSchema, request, 'dispose') : request;
                let res = await Ajax[method](url, request);
                if (res.ok && res.data && respSchema) {
                   res.data = convert(respSchema, res.data, 'parse');
                }
                return res;
            }
        }
        obj[moduleName] = newModule;
    }
    return obj;
}

export  default gen({

    Login: {
      login: {
          method: 'post',
          url: '/login',
          request: Login
      }
    }
});

@MrPsCHen
Copy link

MrPsCHen commented Apr 1, 2021

我觉得 是不是可以 由后台提供配置接口,通知前端怎么去那数据怎么去解析数据
request本质,起到桥梁的作用,如果能再封装一层
将配置前后端 接口规范统一,可以大大减少去处理接口数据逻辑业务了
思路:
1.前端规范api清单接口
2.解析清单数据
3.通过方法驱动request 取数据
4.根据清单配置渲染前端组件

@PLA31001
Copy link
Author

PLA31001 commented Apr 1, 2021

我觉得 是不是可以 由后台提供配置接口,通知前端怎么去那数据怎么去解析数据
思路:

我认为parse和dispose是前后端数据的中间层,使后端数据源的数据对前端组件,界面是透明的。是与前端强相关的。可能不同的前端应用的parse和dispose是不一样的。后端应该是稳定的,前端应该是根据业务逻辑变化的。

@vvpvvp
Copy link
Member

vvpvvp commented Feb 5, 2022

如果这样,很多逻辑就强耦合了,其实在业务逻辑处理中,并不适合。

@vvpvvp vvpvvp closed this as completed Feb 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants