mirror of
https://github.com/YMFE/yapi.git
synced 2024-12-15 05:10:47 +08:00
Merge branch 'dev' of gitlab.corp.qunar.com:mfe/yapi into dev
This commit is contained in:
commit
a66d1b4aaf
6
chrome_tools/background.html
Normal file
6
chrome_tools/background.html
Normal file
@ -0,0 +1,6 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<body>
|
||||
<script src="background.js"></script>
|
||||
</body>
|
||||
</html>
|
20
chrome_tools/background.js
Normal file
20
chrome_tools/background.js
Normal file
@ -0,0 +1,20 @@
|
||||
'use strict';
|
||||
chrome.runtime.onMessage.addListener(function(request, _ , cb){
|
||||
var data;
|
||||
|
||||
if(request.action === 'get'){
|
||||
data = localStorage.getItem(request.name);
|
||||
console.log(data)
|
||||
if(typeof cb === 'function'){
|
||||
cb(data)
|
||||
}
|
||||
}else if(request.action === 'set'){
|
||||
console.log(request.name, request.value)
|
||||
localStorage.setItem(request.name, request.value);
|
||||
var newdata =data = localStorage.getItem(request.name);
|
||||
console.log(newdata)
|
||||
}
|
||||
|
||||
|
||||
})
|
||||
|
BIN
chrome_tools/icon.png
Normal file
BIN
chrome_tools/icon.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 16 KiB |
75
chrome_tools/index.html
Normal file
75
chrome_tools/index.html
Normal file
@ -0,0 +1,75 @@
|
||||
<html>
|
||||
|
||||
<body>
|
||||
<h1>hello!!!</h1>
|
||||
<input id="file" type="file" name="file" />
|
||||
<button id="upload">upload</button>
|
||||
<script src="yRequest.js"></script>
|
||||
|
||||
<script>
|
||||
yRequest({
|
||||
url: 'http://caibaojian.com/ajax-jsonp.html',
|
||||
method: 'GET',
|
||||
data: {
|
||||
a:1
|
||||
},
|
||||
success: function(res, header){
|
||||
console.log(header)
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
//test get
|
||||
yRequest({
|
||||
url: 'http://127.0.0.1:3000/api',
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
data: {
|
||||
a: 1,
|
||||
b:2,
|
||||
c: {
|
||||
t:1
|
||||
}
|
||||
},
|
||||
success: function(res){
|
||||
console.log(arguments)
|
||||
}
|
||||
})
|
||||
|
||||
//test error get
|
||||
yRequest({
|
||||
url: 'http://127.0.0.1:3000/error_get',
|
||||
method: 'GET',
|
||||
error: function(err, header){
|
||||
console.log(err)
|
||||
}
|
||||
})
|
||||
|
||||
//test upload
|
||||
var file = document.getElementById('file');
|
||||
document.getElementById('upload').addEventListener('click', function(){
|
||||
yRequest({
|
||||
url: 'http://127.0.0.1:3000/upload',
|
||||
method: 'POST',
|
||||
data: {
|
||||
name: 'hello',
|
||||
id: '19'
|
||||
},
|
||||
files: {
|
||||
file: 'file'
|
||||
},
|
||||
success: function(res){
|
||||
alert(res)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
</script>
|
||||
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
3
chrome_tools/jquery-3.1.1.js
vendored
Normal file
3
chrome_tools/jquery-3.1.1.js
vendored
Normal file
File diff suppressed because one or more lines are too long
23
chrome_tools/manifest.json
Normal file
23
chrome_tools/manifest.json
Normal file
@ -0,0 +1,23 @@
|
||||
{
|
||||
"manifest_version": 2,
|
||||
"name": "yapi-request",
|
||||
"description": "这个chrome扩展赋予html页面跨域请求http(s)的能力",
|
||||
"version": "1.0",
|
||||
"browser_action": {
|
||||
"default_icon": "icon.png",
|
||||
"default_popup": "popup.html"
|
||||
},
|
||||
"permissions": [ "tabs", "storage", "webRequest", "webRequestBlocking", "*://*/*" ],
|
||||
"background":
|
||||
{
|
||||
"scripts" : [
|
||||
"background.js"
|
||||
]
|
||||
},
|
||||
"content_scripts": [{
|
||||
"matches": ["http://*/*", "https://*/*"],
|
||||
"js": [
|
||||
"response.js"
|
||||
]
|
||||
}]
|
||||
}
|
34
chrome_tools/popup.html
Normal file
34
chrome_tools/popup.html
Normal file
@ -0,0 +1,34 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<title>yRequest</title>
|
||||
<meta charset="utf8"/>
|
||||
<style>
|
||||
body {font-family: "Segoe UI", "Lucida Grande", Tahoma, sans-serif; font-size: 100%;}
|
||||
.main{width:320px;}
|
||||
*{margin:8px;}
|
||||
.content{line-height: 25px;}
|
||||
.add{line-height:40px;margin-bottom: 20px;}
|
||||
.add input{width:200px;line-height:25px;}
|
||||
.add button{width:70px;line-height:25px;}
|
||||
.del{cursor:pointer}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="main">
|
||||
<h4>允许跨域请求的Host:</h4>
|
||||
<div class="content" id="urls">
|
||||
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<div class="add" id="add">
|
||||
<input />
|
||||
<button class="submit">Add Host</button>
|
||||
</div>
|
||||
<script src="jquery-3.1.1.js"></script>
|
||||
<script src="popup.js"></script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
56
chrome_tools/popup.js
Normal file
56
chrome_tools/popup.js
Normal file
@ -0,0 +1,56 @@
|
||||
function generateHtml(url) {
|
||||
return `
|
||||
<div class="item">
|
||||
<span class="url">${url}</span>
|
||||
<span class="del">✕</span>
|
||||
</div>
|
||||
`
|
||||
}
|
||||
|
||||
|
||||
var key = 'y_request_allow_urls';
|
||||
|
||||
var urls = chrome.runtime.sendMessage({action:'get', name: key}, function(urls){
|
||||
var urlDom = $('#urls');
|
||||
|
||||
if (!urls || $.trim(urls) === '{}') {
|
||||
urls = { 'http://yapi.corp.qunar.com': true ,
|
||||
'127.0.0.1': true,
|
||||
'0.0.0.0' : true
|
||||
};
|
||||
chrome.runtime.sendMessage({action:'set', name: key, value: JSON.stringify(urls)})
|
||||
|
||||
}
|
||||
else urls = JSON.parse(urls);
|
||||
|
||||
for (var url in urls) {
|
||||
urlDom.append(generateHtml(url));
|
||||
}
|
||||
|
||||
$('#add .submit').bind('click', function () {
|
||||
|
||||
var val = $('#add input').val()
|
||||
if (val) urls[val] = true;
|
||||
chrome.runtime.sendMessage({
|
||||
action:'set',
|
||||
name: key,
|
||||
value: JSON.stringify(urls)
|
||||
})
|
||||
|
||||
urlDom.append(generateHtml(val))
|
||||
})
|
||||
|
||||
urlDom.on('click', '.del', function (event) {
|
||||
console.log('del')
|
||||
var p = event.target.parentNode;
|
||||
var url = $(p).find('.url').text();
|
||||
delete urls[url]
|
||||
|
||||
chrome.runtime.sendMessage({
|
||||
action:'set',
|
||||
name: key,
|
||||
value: JSON.stringify(urls)
|
||||
})
|
||||
p.parentNode.removeChild(p)
|
||||
})
|
||||
});
|
167
chrome_tools/response.js
Normal file
167
chrome_tools/response.js
Normal file
@ -0,0 +1,167 @@
|
||||
/*==============common begin=================*/
|
||||
|
||||
var container = 'y-request';
|
||||
var INITSTATUS = 0;
|
||||
var RUNSTATUS = 1;
|
||||
var ENDSTATUS = 2;
|
||||
var localStorageKey = 'y_request_allow_urls'
|
||||
/*==============common end=================*/
|
||||
|
||||
var yRequestDom = document.getElementById(container);
|
||||
|
||||
function handleHeader(headers) {
|
||||
if (!headers) return;
|
||||
var newHeaders = {}, headers = headers.split(/[\r\n]/).forEach(function (header) {
|
||||
var index = header.indexOf(":");
|
||||
var name = header.substr(0, index);
|
||||
var value = header.substr(index + 2);
|
||||
if (name) {
|
||||
newHeaders[name] = value;
|
||||
}
|
||||
|
||||
})
|
||||
return newHeaders;
|
||||
}
|
||||
|
||||
function resFn(res, dom, data) {
|
||||
if (!res) return;
|
||||
var id = dom.getAttribute("_id");
|
||||
var headers = handleHeader(this.getAllResponseHeaders());
|
||||
data.res = {
|
||||
id: id,
|
||||
status: this.status,
|
||||
statusText: this.statusText,
|
||||
header: headers,
|
||||
body: res
|
||||
}
|
||||
|
||||
dom.innerText = JSON.stringify(data);
|
||||
dom.setAttribute('status', ENDSTATUS);
|
||||
}
|
||||
|
||||
function formUrlencode(data) {
|
||||
return Object.keys(data).map(function (key) {
|
||||
return encodeURIComponent(key) + '=' + encodeURIComponent(data[key]);
|
||||
}).join('&')
|
||||
}
|
||||
|
||||
function sendAjax(req, successFn, errorFn) {
|
||||
|
||||
var formDatas;
|
||||
var xhr = new XMLHttpRequest();
|
||||
|
||||
xhr.timeout = req.timeout || 5000;
|
||||
|
||||
req.method = req.method || 'GET';
|
||||
req.async = req.async === false ? false : true;
|
||||
req.headers = req.headers || {};
|
||||
|
||||
if (req.method.toLowerCase() !== 'get') {
|
||||
if (!req.headers['Content-Type'] || req.headers['Content-Type'] == 'application/x-www-form-urlencoded') {
|
||||
req.headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
||||
req.data = formUrlencode(req.data);
|
||||
} else if (req.headers['Content-Type'] === 'multipart/form-data') {
|
||||
formDatas = new FormData();
|
||||
if (req.data) {
|
||||
for (var name in req.data) {
|
||||
formDatas.append(name, req.data[name]);
|
||||
}
|
||||
}
|
||||
if (req.files) {
|
||||
for (var name in req.files) {
|
||||
var files = document.getElementById(req.files[name]).files;
|
||||
if (files.length > 0) {
|
||||
formDatas.append(name, files[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
req.data = formDatas;
|
||||
} else if (typeof req.data === 'object' && req.data) {
|
||||
req.data = JSON.stringify(req.data);
|
||||
}
|
||||
} else {
|
||||
if (req.data) {
|
||||
var getUrl = formUrlencode(req.data);
|
||||
req.url = req.url + '?' + getUrl;
|
||||
req.data = '';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
xhr.open(req.method, req.url, req.async);
|
||||
|
||||
if (req.headers) {
|
||||
for (var name in req.headers) {
|
||||
xhr.setRequestHeader(name, req.headers[name]);
|
||||
}
|
||||
}
|
||||
|
||||
xhr.onload = function (e) {
|
||||
if (this.status == 200) {
|
||||
successFn.call(xhr, this.responseText);
|
||||
} else {
|
||||
errorFn.call(xhr, this.responseText)
|
||||
}
|
||||
};
|
||||
xhr.ontimeout = function (e) {
|
||||
errorFn.call(xhr, 'Error:Request timeout that the time is ' + xhr.timeout)
|
||||
};
|
||||
xhr.onerror = function (e) {
|
||||
errorFn.call(xhr, xhr.statusText)
|
||||
};
|
||||
xhr.upload.onprogress = function (e) { };
|
||||
|
||||
xhr.send(req.data);
|
||||
|
||||
}
|
||||
|
||||
function yResponse() {
|
||||
var reqsDom = yRequestDom.childNodes;
|
||||
if (!reqsDom || reqsDom.length === 0) return;
|
||||
reqsDom.forEach(function (dom) {
|
||||
try {
|
||||
var status = dom.getAttribute("status"), request;
|
||||
|
||||
if (+status === INITSTATUS) {
|
||||
dom.setAttribute("status", RUNSTATUS);
|
||||
var data = JSON.parse(dom.innerText);
|
||||
var req = data.req;
|
||||
|
||||
sendAjax(req, function (res) {
|
||||
resFn.bind(this)(res, dom, data);
|
||||
}, function (err) {
|
||||
resFn.bind(this)(err, dom, data);
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error.message)
|
||||
dom.parentNode.removeChild(dom)
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
function isAllowHost() {
|
||||
chrome.runtime.sendMessage({ action: 'get', name: localStorageKey }, function (res) {
|
||||
res = JSON.parse(res);
|
||||
var flag = false;
|
||||
for (var name in res) {
|
||||
if (location.href.indexOf(name) > -1) {
|
||||
flag = true;
|
||||
}
|
||||
}
|
||||
if (flag && yRequestDom) {
|
||||
console.log('yRequest running...')
|
||||
setInterval(function () {
|
||||
yResponse()
|
||||
}, 100)
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
||||
isAllowHost();
|
||||
|
121
chrome_tools/yRequest.js
Normal file
121
chrome_tools/yRequest.js
Normal file
@ -0,0 +1,121 @@
|
||||
(function (win) {
|
||||
|
||||
/*==============common begin=================*/
|
||||
|
||||
var container = 'y-request';
|
||||
var INITSTATUS = 0;
|
||||
var RUNSTATUS = 1;
|
||||
var ENDSTATUS = 2;
|
||||
|
||||
/*==============common end=================*/
|
||||
|
||||
|
||||
function createNode(tagName, attributes, parentNode) {
|
||||
options = attributes || {};
|
||||
tagName = tagName || 'div';
|
||||
var dom = document.createElement(tagName);
|
||||
for (var attr in attributes) {
|
||||
if (attr === 'id') dom.id = options[attr];
|
||||
else dom.setAttribute(attr, options[attr]);
|
||||
}
|
||||
if (parentNode) parentNode.appendChild(dom);
|
||||
return dom;
|
||||
}
|
||||
|
||||
function getid() {
|
||||
return container + '-' + id++;
|
||||
}
|
||||
|
||||
|
||||
var yRequestDom = createNode('div', { id: container, style: 'display:none' }, document.getElementsByTagName('body')[0]);
|
||||
var yRequestMap = {};
|
||||
var id = 0;
|
||||
var interval;
|
||||
|
||||
|
||||
function yRequest(req) {
|
||||
if (!req) return;
|
||||
if (typeof req === 'string') req = { url: req }
|
||||
|
||||
data = {
|
||||
res: null,
|
||||
req: req
|
||||
}
|
||||
data = JSON.stringify(data, null, 4);
|
||||
var newId = getid();
|
||||
var div = createNode('div', {
|
||||
_id: newId,
|
||||
status: INITSTATUS
|
||||
}, yRequestDom);
|
||||
div.innerText = data;
|
||||
yRequestMap[newId] = {
|
||||
id: newId,
|
||||
status: INITSTATUS,
|
||||
success: function (res, header, data) {
|
||||
if (typeof req.success === 'function') {
|
||||
req.success(res, header, data);
|
||||
}
|
||||
},
|
||||
error: function (error, header, data) {
|
||||
if (typeof req.error === 'function') {
|
||||
req.error(error, header, data)
|
||||
}
|
||||
}
|
||||
}
|
||||
monitor();
|
||||
}
|
||||
|
||||
|
||||
|
||||
function monitor() {
|
||||
if (interval) return;
|
||||
interval = setInterval(function () {
|
||||
var queueDom = yRequestDom.childNodes;
|
||||
if (!queueDom || queueDom.length === 0) {
|
||||
interval = clearInterval(interval);
|
||||
}
|
||||
|
||||
try {
|
||||
for (var i = 0; i < queueDom.length; i++) {
|
||||
try {
|
||||
var dom = queueDom[i];
|
||||
if (+dom.getAttribute('status') === ENDSTATUS) {
|
||||
var text = dom.innerText;
|
||||
if (text) {
|
||||
var data = JSON.parse(dom.innerText);
|
||||
var id = dom.getAttribute('_id');
|
||||
var res = data.res;
|
||||
if (res.status === 200) {
|
||||
yRequestMap[id].success(res.body, res.header, data);
|
||||
} else {
|
||||
yRequestMap[id].error(res.statusText, res.header, data);
|
||||
}
|
||||
dom.parentNode.removeChild(dom);
|
||||
} else {
|
||||
dom.parentNode.removeChild(dom);
|
||||
}
|
||||
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err.message);
|
||||
dom.parentNode.removeChild(dom);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err.message);
|
||||
interval = clearInterval(interval);
|
||||
}
|
||||
|
||||
|
||||
}, 50)
|
||||
}
|
||||
|
||||
win.yRequest = yRequest;
|
||||
if (typeof define == 'function' && define.amd) {
|
||||
define('yRequest', [], function () {
|
||||
return yRequest;
|
||||
});
|
||||
}
|
||||
|
||||
})(window)
|
||||
|
@ -4,6 +4,7 @@
|
||||
"data": {
|
||||
"uid": 101,
|
||||
"email": "admin@admin.com",
|
||||
"username": "admin",
|
||||
"add_time": 1499762848,
|
||||
"up_time": 1499762848
|
||||
}
|
||||
|
@ -4,6 +4,7 @@
|
||||
"data": {
|
||||
"uid": 139,
|
||||
"email": "18500803805@163.com",
|
||||
"username": "18500803805",
|
||||
"add_time": 1499768413,
|
||||
"up_time": 1499768413,
|
||||
"role": "member"
|
||||
|
@ -42,6 +42,7 @@ class userController extends baseController{
|
||||
this.setLoginCookie(result._id, result.passsalt)
|
||||
|
||||
return ctx.body = yapi.commons.resReturn({
|
||||
username: username,
|
||||
uid: result._id,
|
||||
email: result.email,
|
||||
add_time: result.add_time,
|
||||
@ -172,7 +173,7 @@ class userController extends baseController{
|
||||
}
|
||||
|
||||
async resetPassword(ctx){
|
||||
|
||||
|
||||
}
|
||||
|
||||
setLoginCookie(uid, passsalt){
|
||||
@ -225,6 +226,9 @@ class userController extends baseController{
|
||||
add_time: yapi.commons.time(),
|
||||
up_time: yapi.commons.time()
|
||||
}
|
||||
if(!data.username){
|
||||
data.username = data.email.substr(0, data.email.indexOf('@'));
|
||||
}
|
||||
try{
|
||||
let user = await userInst.save(data);
|
||||
this.setLoginCookie(user._id, user.passsalt)
|
||||
@ -232,6 +236,7 @@ class userController extends baseController{
|
||||
ctx.body = yapi.commons.resReturn({
|
||||
uid: user._id,
|
||||
email: user.email,
|
||||
username: user.username,
|
||||
add_time: user.add_time,
|
||||
up_time: user.up_time,
|
||||
role: 'member'
|
||||
|
@ -129,6 +129,7 @@ var userController = function (_baseController) {
|
||||
this.setLoginCookie(result._id, result.passsalt);
|
||||
|
||||
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn({
|
||||
username: username,
|
||||
uid: result._id,
|
||||
email: result.email,
|
||||
add_time: result.add_time,
|
||||
@ -518,11 +519,15 @@ var userController = function (_baseController) {
|
||||
add_time: _yapi2.default.commons.time(),
|
||||
up_time: _yapi2.default.commons.time()
|
||||
};
|
||||
_context8.prev = 13;
|
||||
_context8.next = 16;
|
||||
|
||||
if (!data.username) {
|
||||
data.username = data.email.substr(0, data.email.indexOf('@'));
|
||||
}
|
||||
_context8.prev = 14;
|
||||
_context8.next = 17;
|
||||
return userInst.save(data);
|
||||
|
||||
case 16:
|
||||
case 17:
|
||||
user = _context8.sent;
|
||||
|
||||
this.setLoginCookie(user._id, user.passsalt);
|
||||
@ -530,6 +535,7 @@ var userController = function (_baseController) {
|
||||
ctx.body = _yapi2.default.commons.resReturn({
|
||||
uid: user._id,
|
||||
email: user.email,
|
||||
username: user.username,
|
||||
add_time: user.add_time,
|
||||
up_time: user.up_time,
|
||||
role: 'member'
|
||||
@ -538,21 +544,21 @@ var userController = function (_baseController) {
|
||||
to: params.email,
|
||||
contents: '\u6B22\u8FCE\u6CE8\u518C\uFF0C\u60A8\u7684\u8D26\u53F7 ' + params.email + ' \u5DF2\u7ECF\u6CE8\u518C\u6210\u529F'
|
||||
});
|
||||
_context8.next = 25;
|
||||
_context8.next = 26;
|
||||
break;
|
||||
|
||||
case 22:
|
||||
_context8.prev = 22;
|
||||
_context8.t0 = _context8['catch'](13);
|
||||
case 23:
|
||||
_context8.prev = 23;
|
||||
_context8.t0 = _context8['catch'](14);
|
||||
|
||||
ctx.body = _yapi2.default.commons.resReturn(null, 401, _context8.t0.message);
|
||||
|
||||
case 25:
|
||||
case 26:
|
||||
case 'end':
|
||||
return _context8.stop();
|
||||
}
|
||||
}
|
||||
}, _callee8, this, [[13, 22]]);
|
||||
}, _callee8, this, [[14, 23]]);
|
||||
}));
|
||||
|
||||
function reg(_x9) {
|
||||
|
Loading…
Reference in New Issue
Block a user