common.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * 登录
  3. * @param url
  4. */
  5. function login(url){
  6. $.toast('请先登录','text');
  7. setTimeout(function(){
  8. location.href = url;
  9. }, 500);
  10. }
  11. /**
  12. * 获取地址参数
  13. * @param name
  14. * @returns {string}
  15. */
  16. function getParam(name) {
  17. return location.href.match(new RegExp('[?#&]' + name + '=([^?#&]+)', 'i')) ? decodeURI(RegExp.$1) : '';
  18. }
  19. /**
  20. * 小数点2位非四舍五入
  21. * @param num
  22. * @param decimal
  23. * @returns {string}
  24. */
  25. function moneyFormat(num, decimal) {
  26. num = num.toString()
  27. let index = num.indexOf('.')
  28. if (index !== -1) {
  29. num = num.substring(0, decimal + index + 1)
  30. } else {
  31. num = num.substring(0)
  32. }
  33. return parseFloat(num).toFixed(decimal)
  34. }
  35. /**
  36. * 获取唯一字符串
  37. */
  38. function getKey() {
  39. var s = [];
  40. var hexDigits = "0123456789abcdef";
  41. for (var i = 0; i < 36; i++) {
  42. s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  43. }
  44. s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
  45. s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
  46. s[8] = s[13] = s[18] = s[23] = "-";
  47. var uuid = s.join("");
  48. return uuid;
  49. }
  50. /**
  51. * 错误提示
  52. * @param msg
  53. * @param url
  54. */
  55. function showAlert(msg, url){
  56. url = typeof(url) != 'undefined'? url : '/';
  57. $.modal({
  58. title: "错误提示",
  59. autoClose: false,
  60. text: msg,
  61. buttons: [
  62. {
  63. text: "退出登录",
  64. onClick: function () {
  65. location.href = '/weixin/login/logout';
  66. }
  67. },
  68. {
  69. text: "确定",
  70. onClick: function () {
  71. location.href = url;
  72. }
  73. }
  74. ]
  75. });
  76. }