mtl-js-sdk
Version:
178 lines (147 loc) • 5.66 kB
JavaScript
"use strict";
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
define(["../ucg/config.js", "./defaultData"], function (config, defaultData) {
var readUCGConfig = config.readUCGConfig;
var DEF_PATH = "/maserver/umserver/core";
function getUrl(object, suffix) {
var appCode = object.appId;
var UCGConfig = appCode ? readUCGConfig({
appcode: appCode
}) || {} : {};
var _object$isHttps = object.isHttps,
isHttps = _object$isHttps === void 0 ? UCGConfig.isHttps || false : _object$isHttps,
_object$port = object.port,
port = _object$port === void 0 ? UCGConfig.port || "" : _object$port,
_object$host = object.host,
host = _object$host === void 0 ? UCGConfig.host || "" : _object$host,
_object$url = object.url,
url = _object$url === void 0 ? DEF_PATH : _object$url,
_object$_suffix = object._suffix,
_suffix = _object$_suffix === void 0 ? suffix : _object$_suffix;
if (url.indexOf("http") != -1) {
var _url = "".concat(url).concat(_suffix);
return _url;
} else {
var http = isHttps ? "https://" : "http://";
var _port = port ? ":" + port : "";
var _url2 = host ? "".concat(http).concat(host).concat(_port).concat(url).concat(_suffix) : "".concat(url).concat(_suffix);
return _url2;
}
}
function getTerminalType() {
var terminalType = "";
var platform = mtl.platform;
switch (platform) {
case "android":
terminalType = "Android";
break;
case "ios":
terminalType = "IOS";
break;
case "wx":
terminalType = "WX";
break;
case "dd":
terminalType = "DD";
break;
case "h5":
terminalType = "h5";
break;
case "upesn":
terminalType = "upesn";
break;
default:
break;
}
return terminalType;
}
function onCommon(func, object) {
var promise = new Promise(func);
promise.then(function (res) {
var resData = res.data.data;
var code = resData.code;
if (code == 1) {
var resultctx = resData["resultctx"];
object.success && object.success(resultctx);
object.complete && object.complete(resultctx);
} else {
var err = {
code: code,
msg: resData.msg
};
object.fail && object.fail(err);
object.complete && object.complete(err);
}
})["catch"](function (err) {
object.fail && object.fail(err);
object.complete && object.complete(err);
});
}
function callAction(object) {
var _object$tp = object.tp,
tp = _object$tp === void 0 ? "none" : _object$tp,
_object$viewid = object.viewid,
viewid = _object$viewid === void 0 ? "" : _object$viewid,
_object$action = object.action,
action = _object$action === void 0 ? "" : _object$action,
appId = object.appId,
serviceId = object.serviceId,
params = object.params;
var requestData = {
deviceinfo: _objectSpread({}, defaultData.deviceinfo, {
os: getTerminalType(),
style: getTerminalType()
}),
appcontext: _objectSpread({}, defaultData.appcontext, {
appid: appId
}),
serviceid: serviceId || defaultData.serviceid,
servicecontext: _objectSpread({}, defaultData.servicecontext, {
params: params,
appid: appId,
viewid: viewid,
actionname: action
})
};
var dataForm = new FormData();
dataForm.append("tp", tp);
dataForm.append("data", JSON.stringify(requestData));
onCommon(function (resolve, reject) {
var headers = object.headers || {}; // headers.token = token;
var _url = getUrl(object, "");
console.log(_url); // 调用网络请求,请求登录接口
mtl.request({
url: _url,
method: "POST",
// 默认值
data: dataForm,
headers: _objectSpread({
"Content-Type": "application/json",
os: "h5"
}, headers),
complete: function complete(res) {
resolve(res);
},
fail: function fail(err) {
reject(err);
}
});
}, object);
}
function setConfig(object) {
config.writeUCGConfig(object);
}
function setAppId(object) {
localStorage.setItem("__MAapplicationid", object["applicationid"]);
}
function getAppId() {
return localStorage.getItem("__MAapplicationid");
}
return {
getTerminalType: getTerminalType,
callAction: callAction,
setConfig: setConfig
};
});