wxAuthorized.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import {
  2. wechatConfig,
  3. share
  4. } from '@/api/wx';
  5. import {
  6. isAndroid
  7. } from './platform.js'
  8. import store from '../store';
  9. // 保存wx对象
  10. import weixinObj from "@/plugin/jweixin-module/index.js";
  11. // 保存分享数据
  12. let shareData = '';
  13. // 保存注册返回appId数据
  14. let appId = '';
  15. //保存路由对象
  16. let router = '';
  17. //微信登录
  18. /**
  19. * @param {string} 当前页面地址信息
  20. */
  21. export async function loginWinxin() {
  22. let pages, page, path;
  23. try {
  24. if (!router) {
  25. router = await setRouter();
  26. }
  27. pages = getCurrentPages();
  28. // 获取跳转前页面
  29. page = pages[pages.length - 1];
  30. // 获取跳转前路由地址
  31. path = page.route;
  32. } catch (e) {
  33. console.log(e);
  34. }
  35. // 判断是否在登录页
  36. if (path != 'pages/redirect/redirect') {
  37. weixindata().then(() => {
  38. // 调用
  39. try {
  40. weixinSq();
  41. } catch (e) {
  42. console.log(e);
  43. //TODO handle the exception
  44. }
  45. });
  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 async function weixindata(config) {
  71. let url;
  72. try {
  73. // 判断是否存在router
  74. if (!router) {
  75. router = await setRouter();
  76. }
  77. if (router.mode === "history") {
  78. // 在ios中时候注册为微信刚进入时候的页面
  79. if (!window.entryUrl) {
  80. window.entryUrl = location.href.split('#')[0]
  81. }
  82. url = isAndroid() ? location.href.split('#')[0] : window.entryUrl;
  83. }
  84. if (router.mode === "hash") {
  85. url = location.href.split('#')[0];
  86. }
  87. console.log('开始注册', url);
  88. } catch (e) {
  89. console.log('错误', e);
  90. }
  91. return new Promise((ok, error) => {
  92. try {
  93. //注册微信信息
  94. wechatConfig({
  95. url
  96. })
  97. .then(({
  98. data
  99. }) => {
  100. try {
  101. // 保存appId
  102. appId = data.appId
  103. // 微信信息配置
  104. weixinObj.config({
  105. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  106. appId: data.appId, // 必填,企业号的唯一标识,此处填写企业号corpid
  107. timestamp: data.timestamp, // 必填,生成签名的时间戳
  108. nonceStr: data.nonceStr, // 必填,生成签名的随机串
  109. signature: data.signature, // 必填,签名,见附录1
  110. jsApiList: data.jsApiList // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
  111. });
  112. weixinObj.ready((e) => {
  113. console.log('注册完毕');
  114. store.commit('setWeiChatObj', weixinObj)
  115. ok(weixinObj)
  116. })
  117. } catch (e) {
  118. console.log(e)
  119. }
  120. })
  121. .catch(e => {
  122. error(e);
  123. console.log(e);
  124. });
  125. } catch (e) {
  126. console.log(e);
  127. //TODO handle the exception
  128. }
  129. })
  130. }
  131. // 加载分享数据
  132. export function shareLoad(config) {
  133. let mess;
  134. if (config) {
  135. mess = {
  136. link: config.link, // 分享链接
  137. imgUrl: config.imgUrl,
  138. desc: config.desc,
  139. title: config.title,
  140. success: config.success,
  141. fail: config.fail || function(e) {
  142. console.log(e);
  143. }
  144. }
  145. } else {
  146. // console.log(window.location.href, 55)
  147. // let url = window.location.href + '?spread=' + store.state.userInfo.uid;
  148. let url = window.location.href;
  149. url = url.replace(/[\?,&]{0,1}from=singlemessage/g, '');
  150. mess = {
  151. title: shareData.title,
  152. link: url, // 分享链接
  153. imgUrl: shareData.img, // 分享图标
  154. desc: shareData.synopsis,
  155. success: function() {},
  156. fail: function(e) {
  157. console.log(e);
  158. }
  159. }
  160. }
  161. // 获取仓库数据
  162. // 分享好友
  163. // weixinObj.updateAppMessageShareData(mess);
  164. weixinObj.onMenuShareAppMessage(mess); // 即将废弃
  165. // 分享朋友圈
  166. // weixinObj.updateTimelineShareData(mess)
  167. weixinObj.onMenuShareTimeline(mess); // 即将废弃
  168. }
  169. // 隐藏功能按钮
  170. export function weixinhideMenu(config) {
  171. console.log(config)
  172. weixinObj.ready((e) => {
  173. weixinObj.hideMenuItems({
  174. menuList: config, // 要隐藏的菜单项,只能隐藏“传播类”和“保护类”按钮,所有menu项见附录3
  175. });
  176. })
  177. }
  178. // 保存路由对象
  179. export function setRouter(route) {
  180. return new Promise((ok, err) => {
  181. router = getApp().$router;
  182. console.log(router, '开始数据');
  183. if (!router) {
  184. const set = setInterval(() => {
  185. router = getApp().$router;
  186. console.log(router, '返回数据');
  187. if (router) {
  188. console.log(router, '结束');
  189. clearInterval(set)
  190. ok(router)
  191. }
  192. }, 100);
  193. } else {
  194. console.log(router, '成功');
  195. ok(router)
  196. }
  197. })
  198. }
  199. export default {
  200. weixinObj,
  201. shareData,
  202. appId,
  203. setRouter,
  204. shareLoad
  205. }