wxAuthorized.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // import {
  2. // wechatConfig,
  3. // share
  4. // } from '@/api/wx';
  5. // 加载日志
  6. import * as log from './log.js'
  7. import {
  8. isAndroid
  9. } from './platform.js'
  10. import store from '../store';
  11. // 保存wx对象
  12. import weixinObj from "@/plugin/jweixin-module/index.js";
  13. // 保存分享数据
  14. let shareData = '';
  15. // 保存注册返回appId数据
  16. let appId = '';
  17. //保存路由对象
  18. let router = '';
  19. //微信登录
  20. /**
  21. * @param {string} 当前页面地址信息
  22. */
  23. export async function loginWinxin() {
  24. console.log('1');
  25. let pages, page, path;
  26. if (!router) {
  27. router = await setRouter();
  28. }
  29. try {
  30. pages = getCurrentPages();
  31. // 获取跳转前页面
  32. page = pages[pages.length - 1];
  33. // 获取跳转前路由地址
  34. path = page.route;
  35. } catch (e) {
  36. console.log(e);
  37. //TODO handle the exception
  38. }
  39. // 判断是否在登录页
  40. if (path != 'pages/redirect/redirect') {
  41. log.addLog('开始注册微信')
  42. weixindata().then(() => {
  43. // 调用
  44. try {
  45. weixinSq();
  46. } catch (e) {
  47. console.log(e);
  48. //TODO handle the exception
  49. }
  50. });
  51. }
  52. };
  53. // 微信授权登录
  54. function weixinSq() {
  55. // 微信授权后跳转页面
  56. try {
  57. // 判断是否真实路由模式
  58. const type = router.mode === "history"?'':'/#'
  59. let ul = encodeURIComponent(store.state.baseURL + store.state.urlFile +type+ '/pages/redirect/redirect');
  60. // 打开微信授权页面
  61. let url =
  62. 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
  63. appId +
  64. '&redirect_uri=' +
  65. ul +
  66. '&response_type=code&scope=snsapi_userinfo&state=' +
  67. new Date().getTime() +
  68. '#wechat_redirect';
  69. window.location.href = url;
  70. } catch (e) {
  71. console.log(e);
  72. //TODO handle the exception
  73. }
  74. };
  75. // 判断分享调用方法
  76. export function shareLoad(config) {
  77. console.log('开始调用分享')
  78. try {
  79. weixindata().then((e) => {
  80. // 判断有无自定义数据
  81. if (config) {
  82. shareFun(config)
  83. } else {
  84. // 判断是否已经缓存了默认数据
  85. if (shareData) {
  86. shareFun()
  87. } else {
  88. // 请求获取默认数据
  89. share({}).then(({
  90. data
  91. }) => {
  92. shareData = data.data
  93. shareFun()
  94. });
  95. }
  96. }
  97. })
  98. } catch (e) {
  99. console.log('报错', e)
  100. //TODO handle the exception
  101. }
  102. }
  103. // 配置分享数据
  104. function shareFun(config) {
  105. try {
  106. console.log('再付分享内容', config);
  107. let mess;
  108. if (config) {
  109. mess = {
  110. link: config.link, // 分享链接
  111. imgUrl: config.imgUrl,
  112. desc: config.desc,
  113. title: config.title,
  114. success: config.success || function(e) {
  115. console.log(e);
  116. },
  117. fail: config.fail || function(e) {
  118. console.log(e);
  119. }
  120. }
  121. } else {
  122. const userInfo = uni.getStorageSync('userInfo')
  123. console.log(userInfo);
  124. const url = window.location.href + '?spread=' + userInfo.uid;
  125. url = url.replace(/[\?,&]{0,1}from=singlemessage/g, '');
  126. mess = {
  127. title: shareData.title,
  128. link: url, // 分享链接
  129. imgUrl: shareData.img, // 分享图标
  130. desc: shareData.synopsis,
  131. success: function() {
  132. // uni.showModal({
  133. // title: '分享',
  134. // content: '分享成功',
  135. // showCancel: false,
  136. // success: res => {
  137. // console.log('分享成功回调接口');
  138. // },
  139. // fail: () => {},
  140. // complete: () => {}
  141. // });
  142. }
  143. }
  144. }
  145. // 获取仓库数据
  146. // 分享好友
  147. weixinObj.updateAppMessageShareData(mess);
  148. // 分享朋友圈
  149. weixinObj.updateTimelineShareData(mess)
  150. } catch (e) {
  151. console.log(e);
  152. //TODO handle the exception
  153. }
  154. }
  155. // 保存路由对象
  156. export function setRouter(route) {
  157. return new Promise((ok, err) => {
  158. router = getApp().$router;
  159. console.log(router,'开始数据');
  160. if (!router) {
  161. const set = setInterval(() => {
  162. router = getApp().$router;
  163. console.log(router,'返回数据');
  164. if (router) {
  165. console.log(router,'结束');
  166. clearInterval(set)
  167. ok(router)
  168. }
  169. }, 100);
  170. }else{
  171. console.log(router,'成功');
  172. ok(router)
  173. }
  174. })
  175. }
  176. export default {
  177. weixinObj,
  178. shareData,
  179. appId,
  180. setRouter,
  181. shareLoad
  182. }