wxAuthorized.js 3.6 KB

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