wxAuthorized.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import {
  2. wechatConfig,
  3. share
  4. } from '@/api/wx';
  5. // 加载日志
  6. import * as log from '@/libs/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 function loginWinxin() {
  24. console.log('1');
  25. let pages, page, path;
  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. log.addLog('开始注册微信')
  39. weixindata().then(() => {
  40. console.log('2');
  41. // 调用
  42. try {
  43. weixinSq();
  44. } catch (e) {
  45. console.log(e);
  46. //TODO handle the exception
  47. }
  48. });
  49. }
  50. };
  51. // 微信授权登录
  52. function weixinSq() {
  53. // 微信授权后跳转页面
  54. try {
  55. let ul = encodeURIComponent(store.state.baseURL + store.state.urlFile + '/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 function weixindata(config) {
  73. let url;
  74. if (router.mode === "history") {
  75. if (window.entryUrl === '') {
  76. window.entryUrl = location.href.split('#')[0]
  77. }
  78. url = isAndroid() ? location.href.split('#')[0] : window.entryUrl;
  79. }
  80. if (router.mode === "hash") {
  81. url = location.href.split('#')[0];
  82. }
  83. log.addLog('注册开始',url)
  84. return new Promise((ok, error) => {
  85. //注册微信信息
  86. wechatConfig({
  87. url
  88. })
  89. .then(({
  90. data
  91. }) => {
  92. // 保存appId
  93. appId = data.appId
  94. // 微信信息配置
  95. weixinObj.config({
  96. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  97. appId: data.appId, // 必填,企业号的唯一标识,此处填写企业号corpid
  98. timestamp: data.timestamp, // 必填,生成签名的时间戳
  99. nonceStr: data.nonceStr, // 必填,生成签名的随机串
  100. signature: data.signature, // 必填,签名,见附录1
  101. jsApiList: data.jsApiList // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
  102. });
  103. weixinObj.ready((e) => {
  104. ok(data)
  105. })
  106. })
  107. .catch(e => {
  108. error(e);
  109. console.log(e);
  110. });
  111. })
  112. }
  113. // 判断分享调用方法
  114. function shareLoad(config) {
  115. weixindata().then((e) => {
  116. // 判断有无自定义数据
  117. if (config) {
  118. shareFun(config)
  119. } else {
  120. // 判断是否已经缓存了默认数据
  121. if (shareData) {
  122. shareFun()
  123. } else {
  124. // 请求获取默认数据
  125. share({}).then(({
  126. data
  127. }) => {
  128. shareData = data.data
  129. shareFun()
  130. });
  131. }
  132. }
  133. })
  134. }
  135. // 配置分享数据
  136. function shareFun(config) {
  137. let mess;
  138. if (config) {
  139. mess = {
  140. link: config.link, // 分享链接
  141. imgUrl: config.imgUrl,
  142. desc: config.desc,
  143. title: config.title,
  144. success: config.success,
  145. fail: config.fail
  146. }
  147. } else {
  148. const userInfo = uni.getStorageSync('userInfo')
  149. console.log(userInfo);
  150. const url = window.location.href + '?spread=' + userInfo.uid;
  151. url = url.replace(/[\?,&]{0,1}from=singlemessage/g, '');
  152. mess = {
  153. title: shareData.title,
  154. link: url, // 分享链接
  155. imgUrl: shareData.img, // 分享图标
  156. desc: shareData.synopsis,
  157. success: function() {
  158. // uni.showModal({
  159. // title: '分享',
  160. // content: '分享成功',
  161. // showCancel: false,
  162. // success: res => {
  163. // console.log('分享成功回调接口');
  164. // },
  165. // fail: () => {},
  166. // complete: () => {}
  167. // });
  168. }
  169. }
  170. }
  171. // 获取仓库数据
  172. // 分享好友
  173. weixinObj.updateAppMessageShareData(mess);
  174. // 分享朋友圈
  175. weixinObj.updateTimelineShareData(mess)
  176. }
  177. // 保存路由对象
  178. export function setRouter(route) {
  179. router = route
  180. }
  181. export default {
  182. weixinObj,
  183. shareData,
  184. appId,
  185. setRouter
  186. }