wxAuthorized.js 4.1 KB

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