mobilemode.api.basic_wev8.js 3.38 KB
define("mApi/basic", ["mUtil"], function (mUtil) {
	return {
		ajax: function (url, data, callback, cfg) {
			return mUtil.ajax(url, data, callback, cfg);
		},
		openUrl: function (url, callback, opts) {
			var Mobilebone = require("mobilebone");

			opts = opts || {};

			if(mUtil.isObject(callback)) {
				opts = callback;
			}

			if(url.indexOf("/mobilemode/appHomepageView.jsp") == 0) {
				var simpleUrl,
					index = url.indexOf("&");
				if(index != -1) {
					queryString = url.substring(index + 1);
					simpleUrl = url.substring(0, index);
				} else {
					simpleUrl = url;
					queryString = "";
				}
				var appHomepageId = simpleUrl.replace("/mobilemode/appHomepageView.jsp?appHomepageId=", "");
				if(!appHomepageId) {
					mUtil.getLabel(5466, "未找到相关页面", function (msg) {
						Mobile_NS.alert(msg);
					});
					return;
				}

				var idTarget = mUtil.getPageId(appHomepageId);

				var options = $.extend({
					reload: true,
					formdata: queryString
				}, opts);
				var self_page = document.querySelector(".in." + Mobilebone.classPage);
				if(self_page == null) return;
				var eleTargetPage = document.getElementById(idTarget);
				if(eleTargetPage) {
					Mobilebone.transition(eleTargetPage, self_page, false, options);
				}
			} else if(callback === true || mUtil.isFunction(callback) || url.indexOf("ajax=true") != -1) {
				Mobilebone.ajax($.extend({
					url: url,
					success: function () {
						if(mUtil.isFunction(callback)) {
							callback.call();
						}
					}
				}, opts));
			} else {
				location.href = url;
			}
		},
		getParameter: function (name, pageid) {
			var pageParam;
			var v;

			pageid = pageid || mUtil.getCurrentPageId();
			pageParam = mUtil.getPageParam(pageid);
			v = pageParam[name];

			if(v == null || typeof (v) == "undefined") {
				v = "";
			}

			return v;
		},
		getGlobalVar: function (name) {
			var gvars = mUtil.getGlobalVars();
			return gvars[name] || "";
		},
		getCurrUser: function () {
			return mUtil.getCurrUser();
		},
		getWechatUserInfo: function () {
			return mUtil.getWechatUserInfo();
		},
		getCurrentPageId: function () {
			return mUtil.getCurrentPageId();
		},
		backPage: function () {
			mUtil.back();
		},
		eval: function (code) {
			if(mUtil.isString(code)) {
				try { code = decodeURIComponent(code); } catch(e) { }
			}
			mUtil.eval(code);
		},
		login: function (loginid, password, callback) {
			mUtil.ajax("/mobilemode/mobile/verifyLogin2.jsp", { "loginid": loginid, "password": password }, function (d) {
				d = typeof d === 'string' ? JSON.parse(d) : d;
				if(d.message == "1") {
					mUtil.setSessionKey(d.sessionkey);
					$.extend(__meta__.user, d.user);
				}
				callback && callback(d);
			}, { type: "POST" });
		},
		callApi: function (path, data, type, success, cfg) {
			cfg = cfg || {};

			// (path, success)
			if(mUtil.isFunction(data)) {
				success = data;
				data = null;
			}
			var isType = mUtil.isString(data) && ~["get", "post"].indexOf(data.toLowerCase().trim());
			// (path, data, success, [cfg])
			if (!isType && mUtil.isFunction(type)) {
				cfg = success || {};
				success = type;
				type = "GET";
			} else if (isType) {
				// (path, type, success)
				cfg = success || {};
				success = type;
				type = data;
				data = null;
			}

			cfg.url = mUtil.completeUrl("/mobilemode/api" + path);
			cfg.data = data;
			cfg.type = type;
			cfg.success = success;

			return $.ajax(cfg);
		}
	};
});