wxAuthorized.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. let router = '';
  16. //微信登录
  17. /**
  18. * @param {string} 当前页面地址信息
  19. */
  20. export async function loginWinxin() {
  21. console.log('1');
  22. let pages, page, path;
  23. if (!router) {
  24. router = await setRouter();
  25. }
  26. try {
  27. pages = getCurrentPages();
  28. // 获取跳转前页面
  29. page = pages[pages.length - 1];
  30. // 获取跳转前路由地址
  31. path = page.route;
  32. } catch (e) {
  33. console.log(e);
  34. //TODO handle the exception
  35. }
  36. // 判断是否在登录页
  37. if (path != 'pages/redirect/redirect') {
  38. weixindata().then(() => {
  39. // 调用
  40. try {
  41. weixinSq();
  42. } catch (e) {
  43. console.log(e);
  44. //TODO handle the exception
  45. }
  46. });
  47. }
  48. };
  49. // 微信授权登录
  50. function weixinSq() {
  51. // 微信授权后跳转页面
  52. try {
  53. // 判断是否真实路由模式
  54. const type = router.mode === "history" ? '' : '/#'
  55. let ul = encodeURIComponent(store.state.baseURL + store.state.urlFile + type + '/pages/redirect/redirect');
  56. // 打开微信授权页面
  57. let url =
  58. 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
  59. appId +
  60. '&redirect_uri=' +
  61. ul +
  62. '&response_type=code&scope=snsapi_userinfo&state=' +
  63. new Date().getTime() +
  64. '#wechat_redirect';
  65. window.location.href = url;
  66. } catch (e) {
  67. console.log(e);
  68. //TODO handle the exception
  69. }
  70. };
  71. // 微信注册
  72. export async function weixindata(config) {
  73. let url;
  74. try {
  75. // 判断是否存在router
  76. if (!router) {
  77. router = await setRouter();
  78. }
  79. if (router.mode === "history") {
  80. // 在ios中时候注册为微信刚进入时候的页面
  81. if (!window.entryUrl) {
  82. window.entryUrl = location.href.split('#')[0]
  83. }
  84. url = isAndroid() ? location.href.split('#')[0] : window.entryUrl;
  85. }
  86. if (router.mode === "hash") {
  87. url = location.href.split('#')[0];
  88. }
  89. console.log('开始注册', url);
  90. } catch (e) {
  91. console.log('错误', e);
  92. }
  93. return new Promise((ok, error) => {
  94. try {
  95. //注册微信信息
  96. wechatConfig({
  97. url
  98. })
  99. .then(({
  100. data
  101. }) => {
  102. try {
  103. // 保存appId
  104. appId = data.appId
  105. // 微信信息配置
  106. weixinObj.config({
  107. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  108. appId: data.appId, // 必填,企业号的唯一标识,此处填写企业号corpid
  109. timestamp: data.timestamp, // 必填,生成签名的时间戳
  110. nonceStr: data.nonceStr, // 必填,生成签名的随机串
  111. signature: data.signature, // 必填,签名,见附录1
  112. jsApiList: data.jsApiList // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
  113. });
  114. weixinObj.ready((e) => {
  115. console.log('注册完毕');
  116. store.commit('setWeiChatObj', weixinObj)
  117. ok(weixinObj)
  118. })
  119. } catch (e) {
  120. console.log(e)
  121. }
  122. })
  123. .catch(e => {
  124. error(e);
  125. console.log(e);
  126. });
  127. } catch (e) {
  128. console.log(e);
  129. //TODO handle the exception
  130. }
  131. })
  132. }
  133. // 加载分享数据
  134. function shareLoad(config) {
  135. let mess;
  136. if (config) {
  137. mess = {
  138. link: config.link, // 分享链接
  139. imgUrl: config.imgUrl,
  140. desc: config.desc,
  141. title: config.title,
  142. success: config.success,
  143. fail: config.fail
  144. }
  145. } else {
  146. console.log(window.location.href, 55)
  147. let url = window.location.href + '?spread=' + store.state.userInfo.uid;
  148. url = url.replace(/[\?,&]{0,1}from=singlemessage/g, '');
  149. mess = {
  150. title: shareData.title,
  151. link: url, // 分享链接
  152. imgUrl: shareData.img, // 分享图标
  153. desc: shareData.synopsis,
  154. success: function() {
  155. // uni.showModal({
  156. // title: '分享',
  157. // content: '分享成功',
  158. // showCancel: false,
  159. // success: res => {
  160. // console.log('分享成功回调接口');
  161. // },
  162. // fail: () => {},
  163. // complete: () => {}
  164. // });
  165. }
  166. }
  167. }
  168. // 获取仓库数据
  169. // 分享好友
  170. weixinObj.updateAppMessageShareData(mess);
  171. // 分享朋友圈
  172. weixinObj.updateTimelineShareData(mess)
  173. }
  174. // 保存路由对象
  175. export function setRouter(route) {
  176. return new Promise((ok, err) => {
  177. router = getApp().$router;
  178. console.log(router, '开始数据');
  179. if (!router) {
  180. const set = setInterval(() => {
  181. router = getApp().$router;
  182. console.log(router, '返回数据');
  183. if (router) {
  184. console.log(router, '结束');
  185. clearInterval(set)
  186. ok(router)
  187. }
  188. }, 100);
  189. } else {
  190. console.log(router, '成功');
  191. ok(router)
  192. }
  193. })
  194. }
  195. export default {
  196. weixinObj,
  197. shareData,
  198. appId,
  199. setRouter,
  200. }