main.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. function ajax(opt) {
  2. opt = opt || {};
  3. opt.method = opt.method.toUpperCase() || 'POST';
  4. opt.url = opt.url || '';
  5. opt.async = opt.async || false;
  6. opt.data = opt.data || null;
  7. opt.success = opt.success || function() {};
  8. var xmlHttp = null;
  9. if (XMLHttpRequest) {
  10. xmlHttp = new XMLHttpRequest();
  11. } else {
  12. xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
  13. }
  14. var params = [];
  15. for (var key in opt.data) {
  16. params.push(key + '=' + opt.data[key]);
  17. }
  18. var postData = params.join('&');
  19. if (opt.method.toUpperCase() === 'POST') {
  20. xmlHttp.open(opt.method, opt.url, opt.async);
  21. xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8');
  22. xmlHttp.send(postData);
  23. } else if (opt.method.toUpperCase() === 'GET') {
  24. xmlHttp.open(opt.method, opt.url + '?' + postData, opt.async);
  25. xmlHttp.send(null);
  26. }
  27. if (opt.async) {
  28. xmlHttp.onreadystatechange = function() {
  29. if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
  30. // 有传入success回调就执行
  31. opt.success(JSON.parse(xmlHttp.responseText));
  32. }
  33. }
  34. } else {
  35. if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
  36. opt.success(JSON.parse(xmlHttp.responseText));
  37. }
  38. }
  39. };
  40. function wxShare(that){
  41. //自定义微信分享
  42. var localUrl = encodeURIComponent(location.href.split('#')[0]);
  43. ajax({
  44. method: 'GET',
  45. url: 'https://logic.uqchat.cn/logic/signature',
  46. data: {
  47. url: localUrl
  48. },
  49. async: false,
  50. success: function(res) {
  51. //console.log(res)
  52. if(res.code == 200){
  53. //通过微信config接口注入配置
  54. wx.config({
  55. debug: false, // 默认为false 为true的时候是调试模式,会打印出日志
  56. appId: res.data.appid,
  57. timestamp: res.data.timestamp,
  58. nonceStr: res.data.noncestr,
  59. signature: res.data.signature,
  60. jsApiList: [
  61. 'checkJsApi',
  62. 'onMenuShareTimeline',
  63. 'onMenuShareAppMessage',
  64. 'onMenuShareQQ',
  65. 'onMenuShareWeibo'
  66. ]
  67. });
  68. //配置自定义分享内容
  69. window.share_config = {
  70. 'share': {
  71. 'imgUrl': 'https://logic.uqchat.cn/partTimeJob/logo.png',
  72. 'desc': '玩游戏,月入10000元+,日赚500元+,自由无压力的兼职副业,名额只限100人', // 这是分享展示的摘要
  73. 'title': '【友期APP】高薪招聘兼职游戏陪玩师,轻松月入10000元+', // 这是分享展示卡片的标题
  74. 'link': location.href, // 这里是分享的网址
  75. 'success': function(rr) {
  76. console.log('成功' + JSON.stringify(rr))
  77. },
  78. 'cancel': function(tt) {
  79. console.log('失败' + JSON.stringify(tt));
  80. }
  81. }
  82. };
  83. wx.ready(function() {
  84. wx.onMenuShareAppMessage(share_config.share); // 微信好友
  85. wx.onMenuShareTimeline(share_config.share); // 微信朋友圈
  86. wx.onMenuShareQQ(share_config.share); // QQ
  87. });
  88. }else{
  89. console.log(res)
  90. }
  91. }
  92. });
  93. }
  94. wxShare();