wxAuthorized.js 4.1 KB

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