define(['zepto', 'md5'], function($, md5) {
	/*
	 *微信服务号通用方法
	 */

	//设置请求头
	var setHeader = function() {
		var headers = {};
		headers['apptype'] = 'wechat_lung';
		headers['user-os'] = 'wechat';
		headers['token'] = localStorage.wxLungToken;
		return headers;
	}

	//获取url上的参数
	var getRequest = function(strParame) {
		var args = new Object();
		var query = window.location.search.substring(1);
		var pairs = query.split("&");
		for (var i = 0; i < pairs.length; i++) {
			var pos = pairs[i].indexOf('=');
			if (pos == -1) continue;
			var argname = pairs[i].substring(0, pos);
			var value = pairs[i].substring(pos + 1);
			value = decodeURIComponent(value);
			args[argname] = value;
		}
		args[strParame] = args[strParame] || "";
		return args[strParame];
	}

	//页面跳转
	var pushWindow = function(urlParame) {
		var ts = new Date().getTime(); //时间戳
		if (urlParame.indexOf('?') == -1) {
			window.location.href = urlParame + '?ts=' + ts;
		} else {
			window.location.href = urlParame + '&ts=' + ts;
		}
	}

	//loading效果
	var loading = function(isOpen) {
		var a = $('.mod');
		if (a.attr("has")) {
			if (isOpen == 1) {
				$('.mod').show();
			} else {
				setTimeout(function() {
					$('.mod').hide();
				}, 1000)
			}
		} else {
			var html = '<div class="mod" has="true"><div class="cssload-spin-box"></div></div>';
			$('body').append(html);
			if (isOpen == 1) {
				$('.mod').show();
			} else {
				setTimeout(function() {
					$('.mod').hide();
				}, 1000)
			}
		}
	}

	//toast 提示
	var toast = function(msgParame) {
		//设置消息框提示时间
		var time = msgParame.time;
		if (time == undefined || time == '') {
			time = 2000;
		}
		//初始化消息框元素
		var el = document.createElement("div");
		el.setAttribute("class", "web-toast");
		el.innerHTML = msgParame.message;
		document.body.appendChild(el);
		el.classList.add("fadeIn");
		setTimeout(function() {
			el.classList.remove("fadeIn");
			el.classList.add("fadeOut");
			/* 监听动画结束，移除提示信息元素 */
			el.addEventListener("animationend", function() {
				document.body.removeChild(el);
			});
			el.addEventListener("webkitAnimationEnd", function() {
				document.body.removeChild(el);
			});
		}, time);
	}

	//pop提示
	var winPop = function(msg) { //用于pop提示效果的方法，提示文字为参数如$.winPop("加载失败")；
		var b = $("body");
		b.append("<div class='winPop'>" + msg + "</div>");
		var a = setTimeout(function() {
			var a = $(".winPop");
			a.remove();
			clearTimeout(a);
		}, 3000);
	}

	//请求函数
	var submitAjax = function(opts) {
		//请求参数
		var ajaxParame = opts.data || '';
		var params = {};
		if (ajaxParame != "") {
			for (var i in ajaxParame) {
				params[i] = ajaxParame[i];
			}
		}
		//是否异步请求
		var async = true;
		if (opts.async != undefined) {
			async = opts.async;
		}
		//设置contentType
		var type = "application/x-www-form-urlencoded";
		if (opts.contentType == "application/json") {
			type = "application/json";
			params = JSON.stringify(params);
		}
		$.ajax({
			url: opts.url,
			type: opts.type,
			headers: setHeader(),
			async: opts.async,
			contentType: type,
			data: params,
			success: function(res) {
				opts.success(res);
			},
			error: function(res) {
				opts.error(res);
			}
		})
	}


	//模块暴露
	return {
		setHeader: setHeader,
		getRequest: getRequest,
		pushWindow: pushWindow,
		loading: loading,
		toast: toast,
		winPop: winPop,
		submitAjax: submitAjax
	}
})
