1、 http.js
import '../js/jquery-3.3.1.min.js' //这个地方居然要如此引用
import _url from './config.js'
import util from '../utils/util.js'
import '../js/jquery-3.3.1.min.js' //这个地方居然要如此引用
export default {
/**
* 传入 headers data
*/
post: function (headers, jsonstr, url) {
return new Promise((resolve, reject) => {
$.ajax({
headers: headers,
type: 'POST',
url: url,
// async: false, // 同步
data: jsonstr
}).then(res => {
resolve(res.data)
}).catch(err => {
reject(err.data)
})
});
},
//----------------------------------------------
request_inner: function (is_hash, json, url) {
if (url == null) {
url = _url.obj
}
let headers = { 'Content-Type': 'application/x-www-form-urlencoded' }
var jsonstr = JSON.stringify(json)
if (is_hash) {
let loginid = util.getStorage('loginid')
let hash = util.sha1(jsonstr + loginid)
headers.token = hash
}
console.log(jsonstr)
// loading.showLoading()
return new Promise((resolve, reject) => {
this.post(headers, jsonstr, url)
.then(res => {
console.log('res =', res)
//if (res.statusCode === 200 && res.data != null) { }
if (res != null) {
if (res.s != null && res.s != 0) {
console.log('出错时')
reject(res) //出错时
} else {
console.log('正确时')
resolve(res) //正确时
}
}
// loading.hideLoading()
})
.catch(err => {
console.log('catch', err)
reject(err)
// loading.hideLoading()
})
});
},
request_pub: function (json, url) {
return this.request_inner(false, json, url)
},
request: function (json, url) {
return this.request_inner(true, json, url)
},
}
2、sdk.js
import _url from './config.js'
import http from './http.js'
export default {
Api1: function (p1,p2,p3) {
var json = {
p1,p2,p3
}
return http.request(json, _url.url1)
}
}
3、html中使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<!-- <script type="module" src="./sdk/sdk.js"></script> -->
</head>
<body>
<script type="module" >
import enums from "./sdk/enums.js";
import sdk from "./sdk/sdk.js";
console.log(enums.enums.lus);
sdk.Api1('admin', '123456', 'veri')
</script>
</body>
</html>