feat: 支持自定义域名邮箱登录

This commit is contained in:
gaoxiaolin.gao 2018-05-15 16:48:34 +08:00
parent 99a5446f46
commit 7e5ef96867
4 changed files with 172 additions and 160 deletions

View File

@ -1,3 +1,11 @@
### v1.3.16
* 支持自定义域名邮箱登录
#### Bug Fixed
* postman headers 为 null 时报错
### v1.3.15
* 增强跨域请求安全性,只允许 YApi 网站进行跨域请求
* 优化文档

View File

@ -100,7 +100,7 @@ class Login extends Component {
rules: [{
required: true,
message: '请输入正确的email!',
pattern: /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
pattern: /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{1,})+$/
}]
})(
<Input style={changeHeight} prefix={<Icon type="user" style={{ fontSize: 13 }} />} placeholder="Email" />

View File

@ -90,7 +90,7 @@ class Reg extends Component {
rules: [{
required: true,
message: '请输入email!',
pattern: /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
pattern: /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{1,})+$/
}]
})(
<Input style={changeHeight} prefix={<Icon type="mail" style={{ fontSize: 13 }} />} placeholder="Email" />

View File

@ -1,40 +1,39 @@
import {message} from 'antd'
import { message } from 'antd';
import URL from 'url';
import _ from 'underscore';
const GenerateSchema = require('generate-schema/src/schemas/json.js');
import { json_parse } from '../../common/utils.js'
function postman(importDataModule){
import { json_parse } from '../../common/utils.js';
function postman(importDataModule) {
var folders = [];
function parseUrl(url){
return URL.parse(url)
function parseUrl(url) {
return URL.parse(url);
}
function checkInterRepeat(interData){
function checkInterRepeat(interData) {
let obj = {};
let arr = [];
for(let item in interData){
for (let item in interData) {
// console.log(interData[item].url + "-" + interData[item].method);
if(!obj[interData[item].url + "-" + interData[item].method+ "-"+interData[item].method]){
if (!obj[interData[item].url + '-' + interData[item].method + '-' + interData[item].method]) {
arr.push(interData[item]);
obj[interData[item].url + "-" + interData[item].method+ "-"+interData[item].method] = true;
obj[
interData[item].url + '-' + interData[item].method + '-' + interData[item].method
] = true;
}
}
return arr;
}
function handleReq_query(query){
function handleReq_query(query) {
let res = [];
if(query&&query.length){
for(let item in query){
if (query && query.length) {
for (let item in query) {
res.push({
name: query[item].key,
desc: query[item].description,
example: query[item].value,
// example: query[item].value,
value: query[item].value,
required: query[item].enabled ? '1' : '0'
});
@ -42,10 +41,10 @@ function postman(importDataModule){
}
return res;
}
function handleReq_headers(headers){
function handleReq_headers(headers) {
let res = [];
if(headers&&headers.length){
for(let item in headers){
if (headers && headers.length) {
for (let item in headers) {
res.push({
name: headers[item].key,
desc: headers[item].description,
@ -56,44 +55,44 @@ function postman(importDataModule){
}
return res;
}
function handleReq_body_form(body_form){
function handleReq_body_form(body_form) {
let res = [];
if(body_form&&body_form.length){
for(let item in body_form){
if (body_form && body_form.length) {
for (let item in body_form) {
res.push({
name: body_form[item].key,
example: body_form[item].value,
// example: body_form[item].value,
value: body_form[item].value,
type: body_form[item].type,
required: body_form[item].enabled ? '1': '0',
required: body_form[item].enabled ? '1' : '0',
desc: body_form[item].description
});
}
}
return res;
}
function handlePath(path){
function handlePath(path) {
path = parseUrl(path).pathname;
path = decodeURIComponent(path);
if(!path) return '';
if (!path) return '';
path = path.replace(/\{\{.*\}\}/g, '');
if(path[0] != "/"){
path = "/" + path;
if (path[0] != '/') {
path = '/' + path;
}
return path;
}
function run(res){
try{
function run(res) {
try {
res = JSON.parse(res);
let interData = res.requests;
let interfaceData = {apis: [], cats: []};
interData = checkInterRepeat.bind(this)(interData);
let interfaceData = { apis: [], cats: [] };
interData = checkInterRepeat.bind(this)(interData);
if (res.folders && Array.isArray(res.folders)) {
res.folders.forEach(tag => {
interfaceData.cats.push({
@ -103,147 +102,156 @@ function postman(importDataModule){
});
}
if(_.find(res.folders,item => item.collectionId === res.id)){
folders = res.folders
}
if(interData && interData.length){
for(let item in interData){
if (_.find(res.folders, item => item.collectionId === res.id)) {
folders = res.folders;
}
if (interData && interData.length) {
for (let item in interData) {
let data = importPostman.bind(this)(interData[item]);
interfaceData.apis.push(data);
}
}
return interfaceData;
}catch(e){
message.error("文件格式必须为JSON");
} catch (e) {
message.error('文件格式必须为JSON');
}
}
function importPostman(data, key){
let reflect = {//数据字段映射关系
title: "name",
path: "url",
method: "method",
desc: "description",
req_query: "queryParams",
req_headers: "headerData",
req_params: "",
req_body_type: "dataMode",
req_body_form: "data",
req_body_other: "rawModeData",
res_body: "text",
res_body_type: "language"
function importPostman(data, key) {
let reflect = {
//数据字段映射关系
title: 'name',
path: 'url',
method: 'method',
desc: 'description',
req_query: 'queryParams',
req_headers: 'headerData',
req_params: '',
req_body_type: 'dataMode',
req_body_form: 'data',
req_body_other: 'rawModeData',
res_body: 'text',
res_body_type: 'language'
};
let allKey = ["title","path","catname","method","desc","req_query","req_headers","req_body_type","req_body_form","req_body_other","res"];
let allKey = [
'title',
'path',
'catname',
'method',
'desc',
'req_query',
'req_headers',
'req_body_type',
'req_body_form',
'req_body_other',
'res'
];
key = key || allKey;
let res = {};
for(let item in key){
item = key[item];
if(item === "req_query"){
res[item] = handleReq_query.bind(this)(data[reflect[item]]);
}else if(item === "req_headers"){
res[item] = handleReq_headers.bind(this)(data[reflect[item]]);
}else if(item === "req_body_form"){
res[item] = handleReq_body_form.bind(this)(data[reflect[item]]);
}else if(item === "req_body_type"){
if(data[reflect[item]] === 'urlencoded' || data[reflect[item]] === 'params'){
res[item] = 'form';
}else{
if(data.headers.indexOf('application/json')>-1){
res[item] = "json";
}else{
res[item] = "raw";
try {
for (let item in key) {
item = key[item];
if (item === 'req_query') {
res[item] = handleReq_query.bind(this)(data[reflect[item]]);
} else if (item === 'req_headers') {
res[item] = handleReq_headers.bind(this)(data[reflect[item]]);
} else if (item === 'req_body_form') {
res[item] = handleReq_body_form.bind(this)(data[reflect[item]]);
} else if (item === 'req_body_type') {
if (data[reflect[item]] === 'urlencoded' || data[reflect[item]] === 'params') {
res[item] = 'form';
} else {
if (_.isString(data.headers) && data.headers.indexOf('application/json') > -1) {
res[item] = 'json';
} else {
res[item] = 'raw';
}
}
} else if (item === 'req_body_other') {
if (_.isString(data.headers) && data.headers.indexOf('application/json') > -1) {
res.req_body_is_json_schema = true;
res[item] = transformJsonToSchema(data[reflect[item]]);
} else {
res[item] = data[reflect[item]];
}
} else if (item === 'path') {
res[item] = handlePath.bind(this)(data[reflect[item]]);
if (res[item] && res[item].indexOf('/:') > -1) {
let params = res[item].substr(res[item].indexOf('/:') + 2).split('/:');
// res[item] = res[item].substr(0,res[item].indexOf("/:"));
let arr = [];
for (let i in params) {
arr.push({
name: params[i],
desc: ''
});
}
res['req_params'] = arr;
}
} else if (item === 'title') {
let path = handlePath.bind(this)(data[reflect['path']]);
if (data[reflect[item]].indexOf(path) > -1) {
res[item] = path;
if (res[item] && res[item].indexOf('/:') > -1) {
res[item] = res[item].substr(0, res[item].indexOf('/:'));
}
} else {
res[item] = data[reflect[item]];
}
} else if (item === 'catname') {
let found = folders.filter(item => {
return item.id === data.folder;
});
res[item] = found && Array.isArray(found) && found.length > 0 ? found[0].name : null;
} else if (item === 'res') {
let response = handleResponses(data['responses']);
if (response) {
(res['res_body'] = response['res_body']),
(res['res_body_type'] = response['res_body_type']);
}
}
} else if(item === 'req_body_other') {
if(data.headers.indexOf('application/json')>-1){
res.req_body_is_json_schema = true
res[item] = transformJsonToSchema(data[reflect[item]])
} else {
res[item] = data[reflect[item]];
}
}
else if(item === "path"){
res[item] = handlePath.bind(this)(data[reflect[item]]);
if(res[item] && res[item].indexOf("/:") > -1){
let params = res[item].substr(res[item].indexOf("/:")+2).split("/:");
// res[item] = res[item].substr(0,res[item].indexOf("/:"));
let arr = [];
for(let i in params){
arr.push({
name: params[i],
desc: ""
});
}
res["req_params"] = arr;
}
}else if(item === "title"){
let path = handlePath.bind(this)(data[reflect["path"]]);
if(data[reflect[item]].indexOf(path) > -1){
res[item] = path;
if(res[item] && res[item].indexOf("/:") > -1){
res[item] = res[item].substr(0,res[item].indexOf("/:"));
}
}else{
res[item] = data[reflect[item]];
}
} else if(item === 'catname'){
let found = folders.filter(item => {
return item.id === data.folder
})
res[item] = found && Array.isArray(found) && found.length>0 ? found[0].name : null;
}
else if(item === 'res') {
let response = handleResponses(data['responses'])
if(response) {
res['res_body'] = response['res_body'],
res['res_body_type'] = response['res_body_type']
}
}
else{
res[item] = data[reflect[item]];
}
} catch (err) {
console.log(err.message);
message.error(`${err.message}, 导入的postman格式有误`)
}
return res;
}
const handleResponses = (data) => {
if(data&&data.length){
const handleResponses = data => {
if (data && data.length) {
let res = data[0];
let response = {};
response['res_body_type'] = res.language === 'json' ? 'json' : 'raw'
response['res_body_type'] = res.language === 'json' ? 'json' : 'raw';
// response['res_body'] = res.language === 'json' ? transformJsonToSchema(res.text): res.text;
if(res.language === 'json') {
response['res_body_is_json_schema'] = true
response['res_body'] = transformJsonToSchema(res.text)
if (res.language === 'json') {
response['res_body_is_json_schema'] = true;
response['res_body'] = transformJsonToSchema(res.text);
} else {
response['res_body'] = res.text
response['res_body'] = res.text;
}
return response;
}
return null
}
return null;
};
const transformJsonToSchema = (json) => {
json = json || {}
let jsonData = json_parse(json)
const transformJsonToSchema = json => {
json = json || {};
let jsonData = json_parse(json);
jsonData = GenerateSchema(jsonData);
let schemaData = JSON.stringify(jsonData)
return schemaData
}
if(!importDataModule || typeof importDataModule !== 'object'){
let schemaData = JSON.stringify(jsonData);
return schemaData;
};
if (!importDataModule || typeof importDataModule !== 'object') {
console.error('obj参数必需是一个对象');
return null;
}
@ -252,13 +260,9 @@ function postman(importDataModule){
name: 'Postman',
run: run,
desc: '注意只支持json格式数据'
}
};
}
module.exports = function(){
this.bindHook('import_data', postman)
}
module.exports = function() {
this.bindHook('import_data', postman);
};