wxAuthorized.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import {
  2. wechatConfig,
  3. share
  4. } from '@/api/wx';
  5. import store from '../store';
  6. // 保存wx对象
  7. // #ifdef H5
  8. let weixinObj = require('jweixin-module');
  9. // #endif
  10. // 保存分享数据
  11. let shareData = '';
  12. // 保存注册返回appId数据
  13. let appId = '';
  14. //微信登录
  15. /**
  16. * @param {string} 当前页面地址信息
  17. */
  18. export function loginWinxin() {
  19. let pages, page, path;
  20. try {
  21. pages = getCurrentPages();
  22. // 获取跳转前页面
  23. page = pages[pages.length - 1];
  24. // 获取跳转前路由地址
  25. path = page.route;
  26. } catch (e) {
  27. console.log(e);
  28. }
  29. // 判断是否在登录页
  30. if (path != 'pages/redirect/redirect') {
  31. setTimeout((e) => {
  32. weixinObj.ready(() => {
  33. console.log('weixinObj.ready');
  34. // 调用
  35. try {
  36. console.log(shareData);
  37. weixinSq(shareData, path);
  38. } catch (e) {
  39. console.log(e);
  40. //TODO handle the exception
  41. }
  42. });
  43. weixinObj.error((e) => {
  44. console.log(e);
  45. })
  46. }, 10)
  47. }
  48. };
  49. // 微信授权登录
  50. function weixinSq(data, path) {
  51. // 微信授权后跳转页面
  52. try {
  53. let ul = encodeURIComponent(store.state.baseURL + store.state.urlFile + '/pages/redirect/redirect');
  54. console.log(ul, "ul")
  55. // 打开微信授权页面
  56. let url =
  57. 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
  58. appId +
  59. '&redirect_uri=' +
  60. ul +
  61. '&response_type=code&scope=snsapi_userinfo&state=' +
  62. new Date().getTime() +
  63. '#wechat_redirect';
  64. window.location.href = url;
  65. console.log(url, "url")
  66. } catch (e) {
  67. console.log(e);
  68. }
  69. };
  70. // 微信注册
  71. export function weixindata(config) {
  72. //注册微信信息
  73. let weixinObj = require('jweixin-module');
  74. // 保存微信对象到全局仓库
  75. store.commit('setWeiChatObj', weixinObj)
  76. wechatConfig({
  77. url: window.location.href
  78. })
  79. .then(({
  80. data
  81. }) => {
  82. // 保存appId
  83. store.commit('setWeiChatInfo', data)
  84. appId = data.appId
  85. // 微信信息配置
  86. weixinObj.config({
  87. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  88. appId: data.appId, // 必填,企业号的唯一标识,此处填写企业号corpid
  89. timestamp: data.timestamp, // 必填,生成签名的时间戳
  90. nonceStr: data.nonceStr, // 必填,生成签名的随机串
  91. signature: data.signature, // 必填,签名,见附录1
  92. jsApiList: data.jsApiList // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
  93. });
  94. weixinObj.ready((e) => {
  95. if (shareData == '') {
  96. share({}).then(({
  97. data
  98. }) => {
  99. shareData = data.data
  100. shareLoad(config)
  101. });
  102. } else {
  103. shareLoad(config)
  104. }
  105. })
  106. })
  107. .catch(e => {
  108. console.log(e);
  109. });
  110. }
  111. // 加载分享数据
  112. function shareLoad(config) {
  113. let mess;
  114. if (config) {
  115. mess = {
  116. link: config.link, // 分享链接
  117. imgUrl: config.imgUrl,
  118. desc: config.desc,
  119. title: config.title,
  120. success: config.success,
  121. fail: config.fail
  122. }
  123. } else {
  124. console.log(window.location.href, 55)
  125. let url = window.location.href + '?spread=' + store.state.userInfo.uid;
  126. url = url.replace(/[\?,&]{0,1}from=singlemessage/g, '');
  127. mess = {
  128. title: shareData.title,
  129. link: url, // 分享链接
  130. imgUrl: shareData.img, // 分享图标
  131. desc: shareData.synopsis,
  132. success: function() {
  133. // uni.showModal({
  134. // title: '分享',
  135. // content: '分享成功',
  136. // showCancel: false,
  137. // success: res => {
  138. // console.log('分享成功回调接口');
  139. // },
  140. // fail: () => {},
  141. // complete: () => {}
  142. // });
  143. }
  144. }
  145. }
  146. // 获取仓库数据
  147. // 分享好友
  148. weixinObj.updateAppMessageShareData(mess);
  149. // 分享朋友圈
  150. weixinObj.updateTimelineShareData(mess)
  151. }
  152. export default {
  153. weixinObj,
  154. shareData,
  155. appId
  156. }