wxAuthorized.js 3.7 KB

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