123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741 |
- var mobile = {};
- var dictMap = {};
- var paramMap = {};
- var user={};
- var company="";
- var shareUser="";
- $(function() {
- pushHistory();
- });
- function pushHistory() {
- window.addEventListener("popstate", function(e) {
- self.location.reload();
- }, false);
- var state = {
- title : "",
- url : "#"
- };
- window.history.replaceState(state, "", "#");
- };
- (function() {
- if (typeof WeixinJSBridge == "object" && typeof WeixinJSBridge.invoke == "function") {
- handleFontSize();
- } else {
- if (document.addEventListener) {
- document.addEventListener("WeixinJSBridgeReady", handleFontSize, false);
- } else if (document.attachEvent) {
- //IE浏览器,非W3C规范
- document.attachEvent("onWeixinJSBridgeReady", handleFontSize);
- }
- }
- function handleFontSize() {
- // 设置网页字体为默认大小
- WeixinJSBridge.invoke('setFontSizeCallback', { 'fontSize' : 0 });
- // 重写设置网页字体大小的事件
- WeixinJSBridge.on('menu:setfont', function() {
- WeixinJSBridge.invoke('setFontSizeCallback', { 'fontSize' : 0 });
- });
- }
- })();
- $(document).ready(function () {
- if(window.history.length<2){
- //$(".mui-action-back").hide();
- }
- addLog();
- getTicket();
- $(".home-btn").on("tap",function(){
- location.href=base+"/mobile/index.jsp";
- });
- });
- function addLog(){
- if(mobile.getUrlVars("company")){
- company = mobile.getUrlVars("company");
- sessionStorage.setItem("company",mobile.getUrlVars("company"));
- }else if(sessionStorage.getItem("company")){
- company = sessionStorage.getItem("company");
- }
- if(mobile.getUrlVars("shareUser")){
- shareUser = mobile.getUrlVars("shareUser");
- sessionStorage.setItem("shareUser",mobile.getUrlVars("shareUser"));
- }else if(sessionStorage.getItem("shareUser")){
- shareUser = sessionStorage.getItem("shareUser");
- }
- var url = location.href.replace('#','');
- if (user){
- if(url.indexOf("?")!=-1){
- if(url.indexOf("shareUser")!=-1){
- url = mobile.changeUrlParam(url,"shareUser",user.openid);
- }else{
- url+="&shareUser="+user.openid;
- }
- }else{
- url+="?shareUser="+user.openid;
- }
- }
- if(company){
- if(url.indexOf("?")!=-1){
- url+="&company="+company;
- }else{
- url+="?company="+company;
- }
- }
-
- share.link = url;
- var page = window.location.pathname;
- var id = mobile.getUrlVars("id");
- mobile.ajaxPost(base+"/userViewLog/save",{id:id,page:page,company:company,shareUser:shareUser},function(json){},"");
- }
- //获取微信js参数
- function getTicket(){
- mobile.ajaxPost(base+"/wechat/getTicket",{url:location.href.replace('#','')},function(data){
- wx.config({
- debug: false,
- appId:data.appId,
- timestamp: data.timestamp,
- nonceStr: data.nonceStr,
- signature: data.signature,
- jsApiList: [
- 'onMenuShareTimeline',
- 'onMenuShareAppMessage',
- 'getLocation',
- 'openLocation'
- ]
- });
- },"");
- }
- //分享
- wx.ready(function(){
- wx.onMenuShareTimeline({
- title: share.title, // 分享标题
- link: share.link, // 分享链接
- imgUrl: share.imgUrl, // 分享图标
- success: function () {
- if(typeof share.callback=='function'){
- share.callback('1');
- }
- saveShareLog("10");
- },
- cancel: function () {
- alert('取消分享');
- }
- });
-
- wx.onMenuShareAppMessage({
- title: share.title, // 分享标题
- desc: share.desc, // 分享描述
- link: share.link, // 分享链接
- imgUrl: share.imgUrl, // 分享图标
- type: 'link', // 分享类型,music、video或link,不填默认为link
- success: function () {
- if(typeof share.callback=='function'){
- share.callback('2');
- }
- saveShareLog("20");
- },
- cancel: function () {
- alert('取消分享');
- }
- });
- });
- function saveShareLog(type) {
- var page = window.location.pathname;
- var id = mobile.getUrlVars("id");
- mobile.ajaxPost(base+"/userShareLog/save",{id:id,page:page,type:type},function(json){},"");
- }
- /**
- * 获取购物车数量
- */
- mobile.getCartNum = function () {
- mobile.ajaxPost(base+'/userCart/getNum',{},function(json){
- if(json.data!=0){
- $('.mui-bar-tab .icon-gouwuche .mui-badge').html(json.data);
- $('.mui-bar-tab .icon-gouwuche .mui-badge').removeClass('mui-hidden');
- }else{
- $('.mui-bar-tab .icon-gouwuche .mui-badge').addClass('mui-hidden');
- }
- });
- };
- /**
- * flag 当子窗口取不到是否不取父窗口的值,默认取父窗口
- */
- mobile.getUrlVars = function (name, flag) {
- var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
- var r = window.location.search.substr(1).match(reg);
- //modify by zhengwei
- if (flag) {
- if (r == null) {
- r = window.parent.location.search.substr(1).match(reg);
- }
- }
- if (r != null) return decodeURI(r[2]);
- return null;
- };
- /**
- * form赋值
- */
- mobile.toForm = function (data) {
- if(data){
- $.each(data,function(key,value){
- if($('#'+key)){
- $('#'+key).val(mobile.nullToSpace(value));
- }
- });
- }
- }
- //把null转为空
- mobile.nullToSpace = function (param) {
- if (param == null || typeof param == 'undefined') {
- param = '';
- }
- return param;
- }
- //把null转为0
- mobile.nullToZore = function (param) {
- if (param == null || typeof param == 'undefined') {
- param = 0;
- }
- return param;
- }
- /**
- * 增加天数
- */
- mobile.addDate = function(date,days){
- var a = new Date(date)
- a = a.valueOf();
- a = a + days * 24 * 60 * 60 * 1000
- a = new Date(a)
- return a;
- }
- /**
- * 获取星期
- */
- mobile.getWeek = function(date){
- var str = "星期";
- var week = date.getDay();
- switch (week) {
- case 0 :
- str += "日";
- break;
- case 1 :
- str += "一";
- break;
- case 2 :
- str += "二";
- break;
- case 3 :
- str += "三";
- break;
- case 4 :
- str += "四";
- break;
- case 5 :
- str += "五";
- break;
- case 6 :
- str += "六";
- break;
- }
- return str;
- }
- /**
- * 检查手机号码
- */
- mobile.checkMobile = function (phone){
- var reg= /(^1[3|4|5|7|8|9][0-9]{9}$)/;
- return reg.test(phone);
- }
- //ajax post
- mobile.ajaxPost = function (url, params, callback,msg) {
- var index=null;
- $.ajax({
- type:"POST",
- url:url,
- data:params,
- dataType:"json",
- timeout:60000,
- beforeSend:function(){
- if(msg!=""){
- if(msg){
- index=layer.open({type: 3,shadeClose:false,content: msg});
- }else{
- index=layer.open({type: 3,shadeClose:false,content: '加载中...'});
- }
- }
- },
- success:function(data){
- if(msg!=""){
- layer.close(index);
- }
- if(typeof callback=='function'){
- callback(data);
- }
- },
- error:function(XMLHttpRequest, textStatus, errorThrown){
- if(msg!=""){
- layer.close(index);
- }
- switch (XMLHttpRequest.status){
- case(500):
- layer.open({content: "服务异常",time: 3});
- break;
- case(408):
- layer.open({content: "请求超时",time: 3});
- break;
- case(0):
- layer.open({content: "网络连接异常",time: 3});
- break;
- default:
- layer.open({content: "请求错误,请稍后再试",time: 3});
- }
-
- },
- complete: function (XHR, TS) { XHR = null; }
- });
- };
- //保留两位小数
- mobile.toDecimal = function (x) {
- var f = parseFloat(x);
- if (isNaN(f)) {
- return;
- }
- f = Math.round(x * 100) / 100;
- return f;
- }
- mobile.getWeekDay = function (){
- var str = "";
- var week = new Date().getDay();
- if (week == 0) {
- str = "星期日";
- } else if (week == 1) {
- str = "星期一";
- } else if (week == 2) {
- str = "星期二";
- } else if (week == 3) {
- str = "星期三";
- } else if (week == 4) {
- str = "星期四";
- } else if (week == 5) {
- str = "星期五";
- } else if (week == 6) {
- str = "星期六";
- }
- return str;
- }
- //日期格式化
- Date.prototype.format = function (fmt) {
- var o = {
- "M+": this.getMonth() + 1, //月份
- "d+": this.getDate(), //日
- "h+": this.getHours(), //小时
- "m+": this.getMinutes(), //分
- "s+": this.getSeconds(), //秒
- "q+": Math.floor((this.getMonth() + 3) / 3), //季度
- "S": this.getMilliseconds() //毫秒
- };
- if (/(y+)/.test(fmt))
- fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
- for (var k in o)
- if (new RegExp("(" + k + ")").test(fmt))
- fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
- return fmt;
- }
- //数组移除
- Array.prototype.removeAt = function (Index) {
- if (isNaN(Index) || Index > this.length) {
- return false;
- }
- for (var i = 0, n = 0; i < this.length; i++) {
- if (this[i] != this[Index]) {
- this[n++] = this[i]
- }
- }
- this.length -= 1
- }
- //数组是否包含某项
- Array.prototype.contains = function (needle) {
- for (i in this) {
- if (this[i] == needle)
- return true;
- }
- return false;
- }
- /**
- * 根据字典类型获取items
- * @param d_key
- * @returns
- */
- mobile.getDictMap = function (d_key) {
- if (dictMap[d_key]) {
- return dictMap[d_key];
- }
- };
- /**
- * 根据系统参数
- * @param code
- * @returns
- */
- mobile.getParam = function (code) {
- if (paramMap[code]) {
- return paramMap[code];
- } else {
- return "";
- }
- };
- /**
- * 根据代码和字典类型获取下拉选项的名称item{value:'1',label:'测试'}
- * @param d_key
- * @param code
- * @returns
- */
- mobile.getDictVal = function (key, code) {
- var items = dictMap[key];
- if (items && items.length > 0) {
- for (var i = 0; i < items.length; i++) {
- var item = items[i];
- if (item.dict_code == code) {
- return item.dict_name;
- break;
- }
- }
- }
- return "";
- }
- mobile.fileUploader = function(filePicker,callback,params){
-
- //添加删除事件结束
- var imgUploadUrl = base+'/common/upload';
- if(typeof params!='undefined'){
- imgUploadUrl = imgUploadUrl+"?"+params;
- }
- var opt={
- auto: true,
- // swf文件路径
- swf: base+'/static/frame/webuploader/Uploader.swf',
- // 文件接收服务端。
- server : imgUploadUrl,
- // 内部根据当前运行是创建,可能是input元素,也可能是flash.
- pick : {
- id : '#'+filePicker,
- multiple : false
- },
- //fileSingleSizeLimit: 5*1024*1024, //最大5M
- // 不压缩image, 默认如果是jpeg,文件上传前会压缩一把再上传!
- resize : false,
- multiple : false,
- duplicate :true
- };
- if(typeof options!='undefined'){
- opt = $.extend(opt,options);
- }
-
- var uploader = WebUploader.create(opt);
-
- // 文件上传过程中提示。
- uploader.on( 'uploadProgress', function( file, percentage ) {
- //layer.msg
- });
-
- uploader.on( 'uploadError', function( file ) {
- layer.msg("上传失败");
- uploader.reset();
- });
-
- uploader.onError = function( code ) {
- switch(code){
- case 'F_EXCEED_SIZE':
- layer.msg('文件大小超出最大值');
- break;
- case 'Q_EXCEED_NUM_LIMIT':
- layer.msg('超过最大上传数量');
- break;
- case 'Q_EXCEED_SIZE_LIMIT':
- layer.msg('文件大小超出最大值');
- break;
- case 'Q_TYPE_DENIED':
- layer.msg('文件类型不允许');
- break;
- default:
- layer.msg('未知错误:'+code);
- break;
- }
- };
-
- //如果服务器端返回的code不为0,那么也是没有成功,会触发uploadError事件
- uploader.on("uploadAccept", function(file,data){
- if(!data.success){
- return false;
- }
- });
-
-
- // 文件上传成功,给item添加成功class, 用样式标记上传成功。
- uploader.on( 'uploadSuccess', function(file,data) {
- if(typeof callback=='function'){
- callback(data);
- }
- uploader.reset();
- });
-
- // 完成上传完了,成功或者失败,先删除进度条。
- uploader.on( 'uploadComplete', function(file,data) {
-
- });
- }
- /**
- * 加载图片
- * @param fileList 容器ID
- * @param data 数组类型或者字符串
- * @param width 图片宽度
- * @param height 图片高度
- */
- mobile.loadImage = function(fileList,data,width,height,editFlag){
- if(!width){//如果没有设置,默认为100px
- width=100;
- }
- if(!height){
- height=100;
- }
- var $list = $('#'+fileList);
- var arr=[];
- if(typeof(data) == "string"){
- var $li = $(
- '<div class="file-item thumbnail">' +
- '<img src="'+data+'" style="width:'+width+'px;height:'+height+'px;">' +
- '</div>'
- );
- if(editFlag){
- var $btns = $('<div class="file-panel" style="height:0px;">' +
- '<span class="cancel">删除</span>' +
- '</div>').appendTo( $li );
- }
- if(data.indexOf("flag=true")==-1){
- data +="&flag=true";
- }
- arr.push(data);
- $list.append( $li );
- }else{
- $.each(data,function(id,item){
- if(typeof(item) == "string"){
- var $li = $(
- '<div class="file-item thumbnail">' +
- '<img src="'+item+'" style="width:'+width+'px;height:'+height+'px;">' +
- '</div>'
- );
- if(item.indexOf("flag=true")==-1){
- item +="&flag=true";
- }
- arr.push(item);
- }else{
- var $li = $(
- '<div class="file-item thumbnail">' +
- '<img src="'+item.imgUrl+'" style="width:'+width+'px;height:'+height+'px;">' +
- '<div class="info">"'+item.imgName+'"</div>' +
- '</div>'
- );
- if(item.imgUrl.indexOf("flag=true")==-1){
- item.imgUrl +="&flag=true";
- }
- arr.push(item.imgUrl);
- }
- if(editFlag){
- var $btns = $('<div class="file-panel" style="height:0px;">' +
- '<span class="cancel">删除</span>' +
- '</div>').appendTo( $li );
- }
- $list.append( $li );
- });
- }
- $list.find("img").on('click',function(){
- web.showImage(arr,$(this).parent().index());
- });
- }
- /**
- * 上传图片
- * @param fileList 容器ID
- * @param filePicker 上传按钮ID
- * @param callback 返回方法
- * @param options 自定义上传
- * @param params 参数
- */
- mobile.imageUploader = function(fileList,filePicker,callback,options,params){
-
- $('#'+fileList).on('mouseover','.file-item',function(){
- $(this).find('.file-panel').css('height','30px');
- });
- $('#'+fileList).on('mouseout','.file-item',function(){
- $(this).find('.file-panel').css('height','0');
- });
- $('#'+fileList).on('click','.cancel',function(){
- $(this).parents('.file-item').remove();
- });
- //添加删除事件结束
- var imgUploadUrl = base+'/common/upload';
- if(typeof params!='undefined'){
- imgUploadUrl = imgUploadUrl+"?"+params;
- }
-
- var opt={
- auto: true,
- // swf文件路径
- swf: base+'/static/frame/webuploader/Uploader.swf',
- // 文件接收服务端。
- server : imgUploadUrl,
- // 内部根据当前运行是创建,可能是input元素,也可能是flash.
- pick : {
- id : '#'+filePicker,
- multiple : true
- },
- fileSingleSizeLimit: 5*1024*1024, //最大5M
- // 不压缩image, 默认如果是jpeg,文件上传前会压缩一把再上传!
- resize : false,
- duplicate :true,
- fileNumLimit:10,
- accept : {
- title : 'Images',
- extensions : 'jpg,jpeg,png',
- mimeTypes : 'image/jpg,image/jpeg,image/png'
- }
- };
- if(typeof options!='undefined'){
- opt = $.extend(opt,options);
- }
-
- if(!opt.thumbnailWidth){//如果没有设置,默认为100px
- opt.thumbnailWidth=100;
- }
- if(!opt.thumbnailHeight){
- opt.thumbnailHeight=100;
- }
-
- var $list = $('#'+fileList);
- var uploader = WebUploader.create(opt);
-
- uploader.on( 'fileQueued', function( file ) {
- if($list.find('.image-item').length>=opt.maxLength){
- layer.open({content:'最多上传'+opt.maxLength+'张图片',time:3});
- uploader.cancelFile( file );
- return;
- }
- var $li = $(
- '<div class="image-item">'+
- '<div class="image-close">X</div>'+
- '</div>'
- );
- $list.append( $li );
- // 创建缩略图
- // 如果为非图片文件,可以不用调用此方法。
- // thumbnailWidth x thumbnailHeight 为 100 x 100
- uploader.makeThumb( file, function( error, src ) {
- if ( error ) {
- $img.replaceWith('<span>不能预览</span>');
- return;
- }
- $li.css("background-image","url("+src+")");
- }, opt.thumbnailWidth, opt.thumbnailHeight);
- });
-
- // 文件上传过程中创建进度条实时显示。
- uploader.on( 'uploadProgress', function( file, percentage ) {
- var $li = $( '#'+file.id );
- var $percent = $li.find('.progress span');
- // 避免重复创建
- if ( !$percent.length ) {
- $percent = $('<p class="progress"><span></span></p>')
- .appendTo( $li )
- .find('span');
- }
- $percent.css( 'width', percentage * 100 + '%' );
- });
-
- uploader.on( 'uploadError', function( file ,reason) {
- var $li = $( '#'+file.id );
- var $error = $li.find('div.error');
- // 避免重复创建
- if ( !$error.length ) {
- $error = $('<div class="error"></div>').appendTo( $li );
- }
- $error.text('上传失败');
- });
-
- uploader.onError = function( code ) {
- switch(code){
- case 'F_EXCEED_SIZE':
- layer.msg('文件大小超出最大值');
- break;
- case 'Q_EXCEED_NUM_LIMIT':
- layer.msg('超过最大上传数量');
- break;
- case 'Q_EXCEED_SIZE_LIMIT':
- layer.msg('文件大小超出最大值');
- break;
- case 'Q_TYPE_DENIED':
- layer.msg('文件类型不允许');
- break;
- default:
- layer.msg('未知错误:'+code);
- break;
- }
- };
-
- //如果服务器端返回的code不为0,那么也是没有成功,会触发uploadError事件
- uploader.on("uploadAccept", function(file,data){
- if(!data.success){
- return false;
- }
- });
-
-
- // 文件上传成功,给item添加成功class, 用样式标记上传成功。
- uploader.on( 'uploadSuccess', function(file,data) {
- if(typeof callback=='function'){
- callback(data);
- }
- });
-
- // 完成上传完了,成功或者失败,先删除进度条。
- uploader.on( 'uploadComplete', function(file,data) {
- $( '#'+file.id ).find('.progress').remove();
- });
- }
- /**
- * 上传图片
- * @param fileList 容器ID
- * @param filePicker 上传按钮ID
- * @param callback 返回方法
- * @param options 自定义上传
- * @param params 参数
- */
- mobile.tip=function(obj,msg,icon,top){
- if(!top){
- top="50%"
- }
- if(!icon){
- icon="icon-zanwushuju"
- }
- $(""+obj+"").append('<div class="center" style="top:'+top+'">'+
- '<div class="mui-icon iconfont '+icon+' fs44 mb10"></div><div class="fs14">'+msg+'</div></div>');
- }
- mobile.changeUrlParam=function (url, param, val){
- var pattern = param+'=([^&]*)';
- var replaceText = param+'='+val;
- return url.match(pattern) ? url.replace(eval('/('+ param+'=)([^&]*)/gi'), replaceText) : (url.match('[\?]') ? url+'&'+replaceText : url+'?'+replaceText);
- }
|