增加网络请求方法

This commit is contained in:
zhl 2019-02-26 14:37:51 +08:00
parent 5e5da2a214
commit 28e154d61c
5 changed files with 244 additions and 0 deletions

View File

@ -1,3 +1,4 @@
var http = require('./utils/http');
cc.Class({
extends: cc.Component,
@ -57,6 +58,13 @@ cc.Class({
onLoad () {
let self = this;
http.get('http://192.168.100.228:7456/res/import/4d/4d364e42-855d-4e28-abe4-0221d8b6c47b.json', {} )
.then(res => {
console.log(res);
})
.catch(err => {
console.error(err);
});
this.partsY = [0, 1560, 2920, 3666, 4686];
this.mainScrollContent = cc.instantiate(this.mainScrollPrefab);
this.scrollContent.addChild(this.mainScrollContent);

View File

@ -0,0 +1,39 @@
let Promise = require('./zpromise');
const get = (url, header) => {
header = header || {};
return Ajax(url, 'GET', {}, header)
};
const post = (url, data, header) => {
header = header || {};
header['Content-Type'] = 'application/json';
return Ajax(url, 'POST', data, header)
};
const Ajax = (url, method, data, header) => {
header = header || {};
return new Promise((resolve, reject) => {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 ){
if(xhr.status >= 200 && xhr.status < 400) {
resolve(xhr.responseText);
}else{
reject(xhr.statusText);
}
}
};
xhr.open(method, url, true);
for(var key in header) {
xhr.setRequestHeader(key, header[key]);
}
xhr.send(data);
});
};
export default {
get,
post
}

View File

@ -0,0 +1,9 @@
{
"ver": "1.0.5",
"uuid": "595b8b81-d761-483f-929c-5a82671028d9",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}

View File

@ -0,0 +1,179 @@
try {
module.exports = Promise
} catch (e) {}
function Promise(executor) {
var self = this
self.status = 'pending'
self.onResolvedCallback = []
self.onRejectedCallback = []
function resolve(value) {
if (value instanceof Promise) {
return value.then(resolve, reject)
}
setTimeout(function() { // 异步执行所有的回调函数
if (self.status === 'pending') {
self.status = 'resolved'
self.data = value
for (var i = 0; i < self.onResolvedCallback.length; i++) {
self.onResolvedCallback[i](value)
}
}
})
}
function reject(reason) {
setTimeout(function() { // 异步执行所有的回调函数
if (self.status === 'pending') {
self.status = 'rejected'
self.data = reason
for (var i = 0; i < self.onRejectedCallback.length; i++) {
self.onRejectedCallback[i](reason)
}
}
})
}
try {
executor(resolve, reject)
} catch (reason) {
reject(reason)
}
}
function resolvePromise(promise2, x, resolve, reject) {
var then
var thenCalledOrThrow = false
if (promise2 === x) {
return reject(new TypeError('Chaining cycle detected for promise!'))
}
if (x instanceof Promise) {
if (x.status === 'pending') { //because x could resolved by a Promise Object
x.then(function(v) {
resolvePromise(promise2, v, resolve, reject)
}, reject)
} else { //but if it is resolved, it will never resolved by a Promise Object but a static value;
x.then(resolve, reject)
}
return
}
if ((x !== null) && ((typeof x === 'object') || (typeof x === 'function'))) {
try {
then = x.then //because x.then could be a getter
if (typeof then === 'function') {
then.call(x, function rs(y) {
if (thenCalledOrThrow) return
thenCalledOrThrow = true
return resolvePromise(promise2, y, resolve, reject)
}, function rj(r) {
if (thenCalledOrThrow) return
thenCalledOrThrow = true
return reject(r)
})
} else {
resolve(x)
}
} catch (e) {
if (thenCalledOrThrow) return
thenCalledOrThrow = true
return reject(e)
}
} else {
resolve(x)
}
}
Promise.prototype.then = function(onResolved, onRejected) {
var self = this
var promise2
onResolved = typeof onResolved === 'function' ? onResolved : function(v) {
return v
}
onRejected = typeof onRejected === 'function' ? onRejected : function(r) {
throw r
}
if (self.status === 'resolved') {
return promise2 = new Promise(function(resolve, reject) {
setTimeout(function() { // 异步执行onResolved
try {
var x = onResolved(self.data)
resolvePromise(promise2, x, resolve, reject)
} catch (reason) {
reject(reason)
}
})
})
}
if (self.status === 'rejected') {
return promise2 = new Promise(function(resolve, reject) {
setTimeout(function() { // 异步执行onRejected
try {
var x = onRejected(self.data)
resolvePromise(promise2, x, resolve, reject)
} catch (reason) {
reject(reason)
}
})
})
}
if (self.status === 'pending') {
// 这里之所以没有异步执行是因为这些函数必然会被resolve或reject调用而resolve或reject函数里的内容已是异步执行构造函数里的定义
return promise2 = new Promise(function(resolve, reject) {
self.onResolvedCallback.push(function(value) {
try {
var x = onResolved(value)
resolvePromise(promise2, x, resolve, reject)
} catch (r) {
reject(r)
}
})
self.onRejectedCallback.push(function(reason) {
try {
var x = onRejected(reason)
resolvePromise(promise2, x, resolve, reject)
} catch (r) {
reject(r)
}
})
})
}
}
Promise.prototype.catch = function(onRejected) {
return this.then(null, onRejected)
}
Promise.deferred = Promise.defer = function() {
var dfd = {}
dfd.promise = new Promise(function(resolve, reject) {
dfd.resolve = resolve
dfd.reject = reject
})
return dfd
}
/**
* new Promise(function(resolve, reject) {
* resolve(42)
* })
*.then(function(value) {
* // "Big ERROR!!!"
* return Promise.stop()
* })
* .catch()
* .then()
* .then()
* .catch()
* .then()
* */
Promise.cancel = Promise.stop = function() {
return new Promise(function(){})
}

View File

@ -0,0 +1,9 @@
{
"ver": "1.0.5",
"uuid": "545affd6-f2b6-4f69-84ec-351a4fd985ea",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}