common_utils.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. function hasAllKeys(keyList, object) {
  2. for (let index in keyList) {
  3. if (!object.hasOwnProperty(keyList[index])) {
  4. return false
  5. }
  6. }
  7. return true
  8. }
  9. function urlParamsParse(str) {
  10. let params = {};
  11. let paramStr = str || window.location.search.slice(1);
  12. if (paramStr.length <= 0) {
  13. return params
  14. }
  15. paramStr.replace(/([^=&]+)=([^&]*)/g, function (m, key, value) {
  16. params[decodeURIComponent(key)] = decodeURIComponent(value);
  17. });
  18. return params;
  19. }
  20. //通用Request
  21. function commonRequest(type, url, params, func) {
  22. params = params ? params : {};
  23. if (type.toUpperCase() != 'GET') {
  24. params = JSON.stringify(params)
  25. }
  26. let request = $.ajax({
  27. type: type,
  28. url: url,
  29. contentType: 'application/json',
  30. data: params,
  31. success: function (data) {
  32. func(data);
  33. },
  34. error: function (err) {
  35. func(err);
  36. }
  37. });
  38. return request
  39. }
  40. function buildProposalData(proposalData) {
  41. let params = urlParamsParse();
  42. if (location.hash) {
  43. proposalData.code = location.hash.substr(1);
  44. }
  45. if (params.track_code) {
  46. proposalData.track_code = params.track_code;
  47. }
  48. proposalData.path = window.location.pathname;
  49. proposalData.href = window.location.href;
  50. let referer = $.cookie('gearSourceReferer');
  51. if (referer && proposalData.href != referer) {
  52. proposalData.recent_referer = referer
  53. }
  54. if (document.referrer) {
  55. if (document.referrer.indexOf("ad.toutiao") != -1) {
  56. proposalData.recent_referer = document.referrer;
  57. proposalData.source = "头条"
  58. } else if (document.referrer.indexOf("www.baidu.com") != -1) {
  59. proposalData.recent_referer = document.referrer;
  60. proposalData.source = "百度"
  61. }
  62. }
  63. return proposalData
  64. }
  65. function commonSubmitProposal(proposalData, func) {
  66. if (hasAllKeys(['name', 'phone', 'description'], proposalData)) {
  67. let subData = buildProposalData(proposalData);
  68. let url = '/projects/proposal/submit';
  69. commonRequest('POST', url, subData, func)
  70. } else {
  71. func({result: false, message: "姓名、联系方式、需求描述为必填"});
  72. }
  73. }
  74. function isPhone() {
  75. console.log(navigator.userAgent)
  76. let userAgentInfo = navigator.userAgent;
  77. let Agents = ["Android", "iPhone",
  78. "SymbianOS", "Windows Phone",
  79. "iPad", "iPod"];
  80. let flag = false;
  81. for (let v = 0; v < Agents.length; v++) {
  82. if (userAgentInfo.indexOf(Agents[v]) > 0) {
  83. flag = true;
  84. break;
  85. }
  86. }
  87. return flag
  88. }
  89. function isMobile() {
  90. return isPhone()
  91. }
  92. // 拨打电话
  93. function callTal() {
  94. if (isPhone()) {
  95. window.location.href = "tel:4006-988-819";
  96. }
  97. }
  98. $(function () {
  99. setSourceReferer()
  100. });
  101. //Referer的设置
  102. function setSourceReferer() {
  103. let params = urlParamsParse();
  104. if (params.hasOwnProperty('source')) {
  105. let referer = $.cookie('gearSourceReferer');
  106. if (!(params.source != 'baidu' && referer && referer.indexOf("source=baidu") != -1)) {
  107. $.cookie('gearSourceReferer', window.location.href, {expires: 1});
  108. }
  109. }
  110. }
  111. //微信分享
  112. function wxShare(fn){
  113. let params = window.location.href;
  114. let url = '/api/wx_sign_data';
  115. commonRequest('GET',url,{url:params},fn)
  116. }