userCommonSearch.js 3.11 KB
/**
 * Created by moli on 15/5/26.
 */
$(function(){
    var DiliPath = $.extend(DiliPath,
		{
			loginPath : "https://passport.nong12.com/uc/login.aspx",
			registPath : "https://passport.nong12.com/regedit/regedit.aspx",
			homePath  : "http://search.nong12.com/",
			logoOut : "https://passport.nong12.com/uc/loginService?ltype=logout",
			userInfo : "http://user.nong12.com/account/security.html"
		}
	);

    var MethodSearch = {
		searchval : {type : "STG"},
		/* 依据data.dtype 发送ajax请求 */
        http : function(url, success, error, data){
            var parm = {
                type : 'GET',
                url : url,
                dataType : "jsonp",
                cache : false,
                success : function(data){
                    success(data);
                },
                error : function(ero){
                    if(error){error(ero)};
                }
            };
            if(data && data.dtype){
                parm.jsonp = data.dtype;
            }
            if(data && data.call){
                parm.jsonpCallback = data.call;
            }
            $.ajax(parm);
        },
		/* 初始化 需要使用的函数*/
        init : function(){
			this.intSearch();
        },
		intSearch : function(){//初始化搜索
			var keywords = ["搜“黄瓜”试试,大黄瓜小黄瓜应有尽有", "搜“四川”试试,保证多快好省"];
			$(".search-input input").attr("placeholder", keywords[(Math.ceil(Math.random() * 2) - 1)]);
			this.changeSearch();
			this.newAutocomplete();
		},
		changeSearch : function(){//搜索类型切换
			var view = $(".search-type li");
			view.click(function(){
				view.eq(0).text($(this).text()).append('<b class="icon-direction icon-direction-middle"></b>');
				if($(this).text() == "产品"){
					view.eq(1).text("店铺");
					MethodSearch.searchval.type = "STG";
				}else{
					view.eq(1).text("产品");
					MethodSearch.searchval.type = "STORE";
				}
			});
		},
		newAutocomplete : function(){//创建搜索
			$('#autocomplete').autocomplete({
				serviceUrl: DiliPath.homePath + "newTip.html",
				paramName: "keywords",
				dataType: "jsonp",
        jsonp: "callFn",
				appendTo: ".search-input",
				triggerSelectOnValidInput: false,
				extraParams : function(){
					var self = this;
					self.params['type'] = MethodSearch.searchval.type;
				},
				onSelect: function (suggestion) {
					MethodSearch.searchSend();
				}
			});
			$('#autocomplete').keypress(function (event) {
				if (event.which == 13) {
					MethodSearch.searchSend();
				}
			});
			$(".searchForm").click(function () {
				MethodSearch.searchSend();
			});
		},
		searchSend : function(){//搜索结果页面
			var keyword = $("#autocomplete").val();
			keyword = $.trim(keyword);
			keyword = encodeURIComponent(keyword);
			if (jQuery.trim(keyword) == "") {
				var def = $("#autocomplete").attr("placeholder");
				keyword = def.substring(2, def.indexOf("”"));
			}
			var searchtype = MethodSearch.searchval.type == "STG" ? "goods" : "shop";
			window.location.href = DiliPath.homePath + searchtype + ".html"
				+ "?key=" + keyword;
		}
    };

    MethodSearch.init();
});