wxAuthorized.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import {
  2. wechatConfig,
  3. share
  4. } from '@/api/wx';
  5. import store from '../store';
  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. console.log('weixinObj.ready');
  32. // 调用
  33. try {
  34. console.log(shareData);
  35. console.log(path);
  36. weixinSq(shareData, path);
  37. } catch (e) {
  38. console.log(e);
  39. //TODO handle the exception
  40. }
  41. });
  42. weixinObj.error((e) => {
  43. console.log(e);
  44. })
  45. }, 10)
  46. }
  47. };
  48. // 微信授权登录
  49. function weixinSq(data, path) {
  50. // 微信授权后跳转页面
  51. try {
  52. let ul = encodeURIComponent(store.state.baseURL + store.state.urlFile + '/#/pages/redirect/redirect');
  53. console.log(ul, "ul")
  54. // 打开微信授权页面
  55. let url =
  56. 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
  57. appId +
  58. '&redirect_uri=' +
  59. ul +
  60. '&response_type=code&scope=snsapi_userinfo&state=' +
  61. new Date().getTime() +
  62. '#wechat_redirect';
  63. console.log(url, "url")
  64. window.location.href = url;
  65. } catch (e) {
  66. console.log(e);
  67. }
  68. };
  69. // 微信注册
  70. export function weixindata(config) {
  71. // console.log(config);
  72. //注册微信信息
  73. let weixinObj = require('jweixin-module');
  74. // 保存微信对象到全局仓库
  75. store.commit('setWeiChatObj', weixinObj)
  76. console.log(window.location.href);
  77. console.log('当前注册url地址',window.location.href);
  78. wechatConfig({
  79. // url: 'pages/applic/location',
  80. // hhred.igxys.com
  81. // url: 'http://hhred.igxys.com/index/',
  82. url: store.state.baseURL+ store.state.urlFile+'/',
  83. })
  84. .then(({
  85. data
  86. }) => {
  87. // uni.showModal({ url: 'http://yyred.igxys.com/index/',
  88. // title:'地址',
  89. // content: window.location.href
  90. // })
  91. // 保存appId
  92. store.commit('setWeiChatInfo', data)
  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. console.log('分享数据',shareData);
  105. if (shareData == '') {
  106. share({}).then(({
  107. data
  108. }) => {
  109. shareData = data.data
  110. shareLoad(config)
  111. });
  112. } else {
  113. shareLoad(config)
  114. }
  115. })
  116. })
  117. .catch(e => {
  118. console.log(e);
  119. });
  120. }
  121. // 加载分享数据
  122. function shareLoad(config) {
  123. let mess;
  124. if (config) {
  125. mess = {
  126. link: config.link, // 分享链接
  127. imgUrl: config.imgUrl,
  128. desc: config.desc,
  129. title: config.title,
  130. success: config.success,
  131. fail: config.fail||function (e) {
  132. console.log(e);
  133. }
  134. }
  135. } else {
  136. // console.log(window.location.href, 55)
  137. // let url = window.location.href + '?spread=' + store.state.userInfo.uid;
  138. let url = window.location.href;
  139. url = url.replace(/[\?,&]{0,1}from=singlemessage/g, '');
  140. mess = {
  141. title: shareData.title,
  142. link: url, // 分享链接
  143. imgUrl: shareData.img, // 分享图标
  144. desc: shareData.synopsis,
  145. success: function() {
  146. },
  147. fail:function (e) {
  148. console.log(e);
  149. }
  150. }
  151. }
  152. // 获取仓库数据
  153. // 分享好友
  154. // weixinObj.updateAppMessageShareData(mess);
  155. weixinObj.onMenuShareAppMessage(mess);// 即将废弃
  156. // 分享朋友圈
  157. // weixinObj.updateTimelineShareData(mess)
  158. weixinObj.onMenuShareTimeline(mess);// 即将废弃
  159. }
  160. // 隐藏功能按钮
  161. export function weixinhideMenu(config) {
  162. console.log(config)
  163. weixinObj.ready((e) => {
  164. weixinObj.hideMenuItems({
  165. menuList: config, // 要隐藏的菜单项,只能隐藏“传播类”和“保护类”按钮,所有menu项见附录3
  166. });
  167. })
  168. }
  169. export default {
  170. weixinObj,
  171. shareData,
  172. appId
  173. }