1.封裝一個(gè)js文件msgSdk.js
注意:需要修改這個(gè)請(qǐng)求地址? apiServiceAddress
;(function () {
if (window.msgSdk) {
return
}
var msgSdk = (function () {
var m_msgSdk = this
var apiServiceAddress="http://172.12.14.5:8000"
this.I_SendHTTPRequest = function (msgApiUrl, methodType,option) {
let oPromise = new Promise(async function (resolve, reject) {
let url=apiServiceAddress+msgApiUrl
$.ajax({
url: url, // 請(qǐng)求的URL
method: methodType, // 請(qǐng)求方法,可以是GET、POST、PUT、DELETE等
data: {...option}, // 發(fā)送到服務(wù)器的數(shù)據(jù)
success: function(data) {
// 請(qǐng)求成功時(shí)的回調(diào)函數(shù)
resolve(data)
},
error: function(jqXHR, textStatus, errorThrown) {
// 請(qǐng)求失敗時(shí)的回調(diào)函數(shù)
reject(errorThrown)
}
});
})
return oPromise
}
// 站內(nèi)
this.M_inStation = function (options) {
let oPromise = new Promise((resolve, reject) => {
this.m_ISAPIProtocol
.station(options)
.then(
() => {
resolve()
},
oError => {
reject(oError)
}
)
})
return oPromise
}
// 小程序
this.M_miniProject = function (options) {
let oPromise = new Promise((resolve, reject) => {
this.m_ISAPIProtocol
.miniProject(options)
.then(
() => {
resolve()
},
oError => {
reject(oError)
}
)
})
return oPromise
}
// 公眾號(hào)
this.M_officialAccount = function (options) {
let oPromise = new Promise((resolve, reject) => {
this.m_ISAPIProtocol
.officialAccount(options)
.then(
() => {
resolve()
},
oError => {
reject(oError)
}
)
})
return oPromise
}
// app
this.M_App = function (options) {
let oPromise = new Promise((resolve, reject) => {
this.m_ISAPIProtocol
.App(options)
.then(
() => {
resolve()
},
oError => {
reject(oError)
}
)
})
return oPromise
}
// 短信
this.M_textMessage = function (options) {
let oPromise = new Promise((resolve, reject) => {
this.m_ISAPIProtocol
.textMessage(options)
.then(
() => {
resolve()
},
oError => {
reject(oError)
}
)
})
return oPromise
}
// 企業(yè)微信
this.M_weCom = function (options) {
let oPromise = new Promise((resolve, reject) => {
this.m_ISAPIProtocol
.weCom(options)
.then(
() => {
resolve()
},
oError => {
reject(oError)
}
)
})
return oPromise
}
var ISAPIProtocol = function () {}
// 站內(nèi)消息
ISAPIProtocol.prototype.station = function (options) {
return m_msgSdk.I_SendHTTPRequest("/ISAPI/Security/userCheck?format=json","get",options )
}
// 小程序
ISAPIProtocol.prototype.miniProject = function (options) {
return m_msgSdk.I_SendHTTPRequest("/ISAPI/Security/userCheck?format=json","get",options )
}
// 公眾號(hào)
ISAPIProtocol.prototype.officialAccount = function (options) {
return m_msgSdk.I_SendHTTPRequest("/ISAPI/Security/userCheck?format=json","get",options )
}
// app
ISAPIProtocol.prototype.App = function (options) {
return m_msgSdk.I_SendHTTPRequest("/ISAPI/Security/userCheck?format=json","get",options )
}
// 短信
ISAPIProtocol.prototype.textMessage = function (options) {
return m_msgSdk.I_SendHTTPRequest("/ISAPI/Security/userCheck?format=json","get",options )
}
// 企業(yè)微信
ISAPIProtocol.prototype.weCom = function (options) {
return m_msgSdk.I_SendHTTPRequest("/ISAPI/Security/weCom?weCom","get",options )
}
m_ISAPIProtocol = new ISAPIProtocol()
return this
})()
var NS = (window.msgSdk = msgSdk)
NS.version = '1.0.0'
})(this)
if ('object' === typeof exports && typeof module !== 'undefined') {
} else if ('function' === typeof define && define.amd) {
define(function () {
return msgSdk
})
} else if ('function' === typeof define && define.cmd) {
define(function (require, exports, module) {
module.exports = msgSdk
})
} else {
}
2.在index.html中引入msgSdk.js文件和jquery文件文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-859661.html
<script src="./static/js/jquery-1.7.1.min.js"></script>
<script src="./static/js/msgSdk.js"></script>
3.在頁(yè)面中調(diào)用文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-859661.html
mounted() {
let oDeviceInfo = {
IP: "http://666",
Port: "8000",
Auth: "95484",
}
msgSdk.M_weCom(oDeviceInfo).then(
(data) => {
console.log(data,"data");
},
(error) => {
console.log(error,"error");
}
);
}
到了這里,關(guān)于js封裝SDK 在VUE、小程序、公眾號(hào)直接調(diào)用js調(diào)用后端接口(本文以vue項(xiàng)目為例)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!