main.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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(){
  41. //自定义微信分享
  42. var localUrl = encodeURIComponent(location.href.split('#')[0]);
  43. ajax({
  44. method: 'GET',
  45. url: 'https://logic.test.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. 'updateAppMessageShareData',
  67. 'updateTimelineShareData'
  68. ]
  69. });
  70. //配置自定义分享内容
  71. window.share_config = {
  72. 'share': {
  73. 'imgUrl': 'https://logic.test.uqchat.cn/partTimeJob/logo.png',
  74. 'desc': '测试有奖,丰富的聊天室玩法,欢迎有实力的公会入驻体验', // 这是分享展示的摘要
  75. 'title': '友期陪玩app,开测啦', // 这是分享展示卡片的标题
  76. 'link': location.href, // 这里是分享的网址
  77. 'success': function(rr) {
  78. console.log('成功' + JSON.stringify(rr))
  79. },
  80. 'cancel': function(tt) {
  81. console.log('失败' + JSON.stringify(tt));
  82. }
  83. }
  84. };
  85. wx.ready(function() {
  86. // wx.onMenuShareAppMessage(share_config.share); // 微信好友
  87. // wx.onMenuShareTimeline(share_config.share); // 微信朋友圈
  88. wx.updateAppMessageShareData(share_config.share); // 微信好友
  89. wx.updateTimelineShareData(share_config.share); // 微信朋友圈
  90. });
  91. }else{
  92. console.log(res)
  93. }
  94. }
  95. });
  96. }
  97. wxShare();