wxAuthorized.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. console.log('分享数据',shareData);
  115. if (shareData == '') {
  116. share({}).then(({
  117. data
  118. }) => {
  119. console.log(config,'if 分享数据')
  120. shareData = data.data
  121. shareLoad(config)
  122. });
  123. } else {
  124. console.log(config,'else 分享数据')
  125. shareLoad(config)
  126. }
  127. store.commit('setWeiChatObj', weixinObj)
  128. ok(weixinObj)
  129. })
  130. } catch (e) {
  131. console.log(e)
  132. }
  133. })
  134. .catch(e => {
  135. error(e);
  136. console.log(e);
  137. });
  138. } catch (e) {
  139. console.log(e);
  140. //TODO handle the exception
  141. }
  142. })
  143. }
  144. // 加载分享数据
  145. export function shareLoad(config) {
  146. let mess;
  147. if (config) {
  148. mess = {
  149. link: config.link, // 分享链接
  150. imgUrl: config.imgUrl,
  151. desc: config.desc,
  152. title: config.title,
  153. success: config.success,
  154. fail: config.fail||function (e) {
  155. console.log(e);
  156. }
  157. }
  158. weixinObj.showAllNonBaseMenuItem();
  159. } else {
  160. console.log('123456789')
  161. // console.log(window.location.href, 55)
  162. // let url = window.location.href + '?spread=' + store.state.userInfo.uid;
  163. let url = window.location.href;
  164. url = url.replace(/[\?,&]{0,1}from=singlemessage/g, '');
  165. mess = {
  166. // title: shareData.title,
  167. title: '星拼乐欢迎您的加入',
  168. link: url, // 分享链接
  169. imgUrl: shareData.img, // 分享图标
  170. desc: shareData.synopsis,
  171. success: function() {
  172. },
  173. fail:function (e) {
  174. console.log(e);
  175. }
  176. }
  177. }
  178. // 获取仓库数据
  179. // 分享好友
  180. weixinObj.updateAppMessageShareData(mess);
  181. // weixinObj.onMenuShareAppMessage(mess);// 即将废弃
  182. // 分享朋友圈
  183. weixinObj.updateTimelineShareData(mess)
  184. // weixinObj.onMenuShareTimeline(mess);// 即将废弃
  185. }
  186. // 隐藏功能按钮
  187. export function weixinhideMenu(config) {
  188. console.log(config)
  189. weixinObj.ready((e) => {
  190. weixinObj.hideMenuItems({
  191. menuList: config, // 要隐藏的菜单项,只能隐藏“传播类”和“保护类”按钮,所有menu项见附录3
  192. });
  193. })
  194. }
  195. // 保存路由对象
  196. export function setRouter(route) {
  197. return new Promise((ok, err) => {
  198. router = getApp().$router;
  199. console.log(router,'开始数据');
  200. if (!router) {
  201. const set = setInterval(() => {
  202. router = getApp().$router;
  203. console.log(router,'返回数据');
  204. if (router) {
  205. console.log(router,'结束');
  206. clearInterval(set)
  207. ok(router)
  208. }
  209. }, 100);
  210. }else{
  211. console.log(router,'成功');
  212. ok(router)
  213. }
  214. })
  215. }
  216. export default {
  217. weixinObj,
  218. shareData,
  219. appId,
  220. setRouter,
  221. shareLoad
  222. }