helper.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. (function(global,factory){
  2. typeof define == 'function' && define('helper',[],factory.bind(window));
  3. global['$h'] = factory();
  4. })(this,function(){
  5. var $h = $h || {};
  6. $h._errorMsgOptions = {offset: '80%',anim: 2,time:1200,shadeClose:true,shade:'0.1'};
  7. $h.returnErrorMsg = function(msg,fn){
  8. $h.pushMsg(msg,fn);
  9. return false;
  10. };
  11. $h._loadIndex = null;
  12. $h.pushMsg = function(msg,fn){
  13. requirejs(['layer'],function(layer){
  14. layer.msg(msg.toString(),$h._errorMsgOptions,fn);
  15. });
  16. };
  17. $h.pushMsgOnceStatus = false;
  18. $h.pushMsgOnce = function(msg,fn){
  19. if($h.pushMsgOnceStatus) return ;
  20. $h.pushMsgOnceStatus = true;
  21. $h.pushMsg(msg,function(){
  22. fn && fn();
  23. $h.pushMsgOnceStatus = false;
  24. });
  25. }
  26. /**
  27. * 底部浮动框
  28. * @param msg
  29. */
  30. $h._promptStyle='<style id="_loading_bounce_style"> #_loading_bounce._prompt_hide{animation:down-hide .25s ease-in; animation-fill-mode: forwards; } #_loading_bounce{z-index: 998;position:fixed;bottom:0;background:#fff;width:100%;height:60px;box-shadow:0 1px 15px rgba(0,0,0,0.17);animation:up-show .25s ease-in}@keyframes up-show{0%{transform:translateY(60px)}100%{transform:translateY(0px)}}@keyframes down-hide{0%{transform:translateY(0px)}100%{transform:translateY(60px)}}#_loading_bounce ._double-container{height: 100%;display: table;position: absolute;width: 30%;left: 44%;} #_loading_bounce ._double-container .double-text{display: table-cell;vertical-align: middle;font-size: 12px;}.double-bounce1,.double-bounce2{width:50px;height:50px;border-radius:50%;background-color:#67CF22;opacity:.6;position:absolute;top:50%;margin-top:-25px;left:26%;-webkit-animation:bounce 2.0s infinite ease-in-out;-moz-animation:bounce 2.0s infinite ease-in-out;-o-animation:bounce 2.0s infinite ease-in-out;animation:bounce 2.0s infinite ease-in-out}.double-bounce2{-webkit-animation-delay:-1.0s;-moz-animation-delay:-1.0s;-o-animation-delay:-1.0s;animation-delay:-1.0s}@keyframes bounce{0%,100%{transform:scale(0.0)}50%{transform:scale(1.0)}}</style>';
  31. $h._promptHtml = '<div id="_loading_bounce" class="_prompt_loading"><div class="mop-css-1 double-bounce"><div class="double-bounce1"></div><div class="double-bounce2"></div></div><div class="_double-container"><span class="double-text">请稍等片刻...</span></div></div>';
  32. $h.prompt = function(msg){
  33. var $body = $('body'),$prompt = $($h._promptHtml);
  34. if(!$body.find('#_loading_bounce_style').length)
  35. $body.append($h._promptStyle);
  36. $prompt.find('.double-text').text(msg);
  37. $body.append($prompt);
  38. };
  39. $h.promptClear = function() {
  40. var that = $('._prompt_loading');
  41. that.addClass('_prompt_hide');
  42. setTimeout(function(){
  43. that.remove();
  44. },250)
  45. };
  46. $h.load = function(){
  47. if($h._loadIndex !== null) $h.loadClear();
  48. requirejs(['layer'],function(layer) {
  49. $h._loadIndex = layer.load(2, {shade: 0.3});
  50. });
  51. };
  52. $h.loadFFF = function(){
  53. if($h._loadIndex !== null) $h.loadClear();
  54. requirejs(['layer'],function(layer) {
  55. $h._loadIndex = layer.load(1, {shade: [0.1,'#fff']});
  56. });
  57. };
  58. $h.loadClear = function(){
  59. requirejs(['layer'],function(layer){
  60. setTimeout(function(){
  61. layer.close($h._loadIndex);
  62. },250);
  63. });
  64. };
  65. $h.uploadFile = function (name,url,successFn,errorFn) {
  66. $.ajaxFileUpload({
  67. url: url,
  68. type: 'post',
  69. secureuri: false, //一般设置为false
  70. fileElementId: name, // 上传文件的id、name属性名
  71. dataType: 'json', //返回值类型,一般设置为json、application/json
  72. success:successFn,
  73. error: errorFn
  74. });
  75. };
  76. $h.ajaxUploadFile = function (name,url,fnGroup) {
  77. fnGroup.start && fnGroup.start();
  78. $.ajaxFileUpload({
  79. url: url,
  80. type: 'POST',
  81. secureuri: false, //一般设置为false
  82. fileElementId: name, // 上传文件的id、name属性名
  83. dataType: 'JSON', //返回值类型,一般设置为json、application/json
  84. success:function(res,status){
  85. fnGroup.success && fnGroup.success(res,status);
  86. fnGroup.end && fnGroup.end(res,status);
  87. // var fileInput = $("#"+name),html = fileInput.prop('outerHTML'),p = fileInput.parent();
  88. // fileInput.remove();
  89. // p.append(html);
  90. },
  91. error: function(res,status){
  92. fnGroup.error && fnGroup.error(res,status);
  93. fnGroup.end && fnGroup.end(res,status);
  94. }
  95. });
  96. };
  97. //除法函数,用来得到精确的除法结果
  98. //说明:javascript的除法结果会有误差,在两个浮点数相除的时候会比较明显。这个函数返回较为精确的除法结果。
  99. //调用:_h.Div(arg1,arg2)
  100. //返回值:arg1除以arg2的精确结果
  101. $h.div = function(arg1,arg2){
  102. var t1=0,t2=0,r1,r2;
  103. try{t1=arg1.toString().split(".")[1].length;}catch(e){}
  104. try{t2=arg2.toString().split(".")[1].length;}catch(e){}
  105. with(Math){
  106. r1=Number(arg1.toString().replace(".",""));
  107. r2=Number(arg2.toString().replace(".",""));
  108. return (r1/r2)*pow(10,t2-t1);
  109. }
  110. };
  111. //乘法函数,用来得到精确的乘法结果
  112. //说明:javascript的乘法结果会有误差,在两个浮点数相乘的时候会比较明显。这个函数返回较为精确的乘法结果。
  113. //调用:_h.Mul(arg1,arg2)
  114. //返回值:arg1乘以arg2的精确结果
  115. $h.Mul = function(arg1,arg2) {
  116. var m=0,s1=arg1.toString(),s2=arg2.toString();
  117. try{m+=s1.split(".")[1].length}catch(e){}
  118. try{m+=s2.split(".")[1].length}catch(e){}
  119. return Number(s1.replace(".","")) * Number(s2.replace(".","")) / Math.pow(10,m);
  120. };
  121. //加法函数,用来得到精确的加法结果
  122. //说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显。这个函数返回较为精确的加法结果。
  123. //调用:_h.Add(arg1,arg2)
  124. //返回值:arg1加上arg2的精确结果
  125. $h.Add = function(arg1,arg2){
  126. var r1,r2,m;
  127. try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
  128. try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
  129. m=Math.pow(10,Math.max(r1,r2));
  130. return (arg1*m+arg2*m)/m;
  131. };
  132. //加法函数,用来得到精确的减法结果
  133. //说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显。这个函数返回较为精确的减法结果。
  134. //调用:_h.Sub(arg1,arg2)
  135. //返回值:arg1减去arg2的精确结果
  136. $h.Sub = function(arg1,arg2){
  137. var r1,r2,m,n;
  138. try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
  139. try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
  140. m=Math.pow(10,Math.max(r1,r2));
  141. //动态控制精度长度
  142. n=(r1>=r2)?r1:r2;
  143. return ((arg1*m-arg2*m)/m).toFixed(n);
  144. };
  145. $h.cookie = function(key,val,time) {
  146. if(val == undefined){
  147. return _helper.getCookie(key);
  148. }else if(val == null){
  149. return _helper.delCookie(key);
  150. }else{
  151. return _helper.setCookie(key,val,time);
  152. }
  153. };
  154. //操作cookie
  155. $h.setCookie = function(key,val,time){//设置cookie方法
  156. var date=new Date(); //获取当前时间
  157. if(!time) time = 1; //将date设置为n天以后的时间
  158. date.setTime(date.getTime()+time*24*3600*1000); //格式化为cookie识别的时间
  159. document.cookie=key + "=" + val +";expires="+date.toGMTString(); //设置cookie
  160. };
  161. $h.getCookie = function(key) {//获取cookie方法
  162. /*获取cookie参数*/
  163. var getCookie = document.cookie.replace(/;[ ]/g, ";"); //获取cookie,并且将获得的cookie格式化,去掉空格字符
  164. var arrCookie = getCookie.split(";"); //将获得的cookie以"分号"为标识 将cookie保存到arrCookie的数组中
  165. var tips; //声明变量tips
  166. for (var i = 0; i < arrCookie.length; i++) { //使用for循环查找cookie中的tips变量
  167. var arr = arrCookie[i].split("="); //将单条cookie用"等号"为标识,将单条cookie保存为arr数组
  168. if (key == arr[0]) { //匹配变量名称,其中arr[0]是指的cookie名称,如果该条变量为tips则执行判断语句中的赋值操作
  169. tips = arr[1]; //将cookie的值赋给变量tips
  170. break; //终止for循环遍历
  171. }
  172. }
  173. return tips;
  174. };
  175. $h.delCookie = function(key){ //删除cookie方法
  176. var date = new Date(); //获取当前时间
  177. date.setTime(date.getTime()-10000); //将date设置为过去的时间
  178. document.cookie = key + "=v; expires =" +date.toGMTString();//设置cookie
  179. };
  180. $pushCookie = function(key){
  181. var data = $h.getCookie(key);
  182. $h.delCookie(key);
  183. return data;
  184. };
  185. $h.getParmas = function getUrlParam(name) {
  186. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
  187. var r = window.location.search.substr(1).match(reg); //匹配目标参数
  188. if (r != null) return decodeURI(r[2]); return null; //返回参数值
  189. };
  190. $h.U = function(opt){
  191. var m = opt.m || 'wap',c = opt.c || 'public_api', a = opt.a || 'index',q = opt.q || '',p = opt.p || {},_params = '';
  192. _params = Object.keys(p).map(function(key){
  193. return key+'/'+p[key];
  194. }).join('/');
  195. return "/"+m+"/"+c+"/"+a+(_params == '' ? '' : '/'+_params)+(q == '' ? '' : '?'+q);
  196. };
  197. $h.isLogin = function(){
  198. return !!$h.getCookie('is_login');
  199. };
  200. return $h;
  201. });