123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- import {
- wechatConfig,
- share
- } from '@/api/wx';
- import {
- isAndroid
- } from './platform.js'
- import store from '../store';
- // 保存wx对象
- import weixinObj from "@/plugin/jweixin-module/index.js";
- // 保存分享数据
- let shareData = '';
- // 保存注册返回appId数据
- let appId = '';
- //保存路由对象
- let router = '';
- //微信登录
- /**
- * @param {string} 当前页面地址信息
- */
- export async function loginWinxin() {
- let pages, page, path;
- try {
- if (!router) {
- router = await setRouter();
- }
- pages = getCurrentPages();
- // 获取跳转前页面
- page = pages[pages.length - 1];
- // 获取跳转前路由地址
- path = page.route;
- } catch (e) {
- console.log(e);
- }
- // 判断是否在登录页
- if (path != 'pages/redirect/redirect') {
- weixindata().then(() => {
- // 调用
- try {
- weixinSq();
- } catch (e) {
- console.log(e);
- //TODO handle the exception
- }
- });
- }
- };
- // 微信授权登录
- function weixinSq(data, path) {
- // 微信授权后跳转页面
- try {
- let ul = encodeURIComponent(store.state.baseURL + store.state.urlFile + '/pages/redirect/redirect');
- console.log(ul, "ul")
- // 打开微信授权页面
- let url =
- 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
- appId +
- '&redirect_uri=' +
- ul +
- '&response_type=code&scope=snsapi_userinfo&state=' +
- new Date().getTime() +
- '#wechat_redirect';
- console.log(url, "url")
- window.location.href = url;
- } catch (e) {
- console.log(e);
- }
- };
- // 微信注册
- export async function weixindata(config) {
- let url;
- try {
- // 判断是否存在router
- if (!router) {
- router = await setRouter();
- }
- if (router.mode === "history") {
- // 在ios中时候注册为微信刚进入时候的页面
- if (!window.entryUrl) {
- window.entryUrl = location.href.split('#')[0]
- }
- url = isAndroid() ? location.href.split('#')[0] : window.entryUrl;
- }
- if (router.mode === "hash") {
- url = location.href.split('#')[0];
- }
- console.log('开始注册', url);
- } catch (e) {
- console.log('错误', e);
- }
- return new Promise((ok, error) => {
- try {
- //注册微信信息
- wechatConfig({
- url
- })
- .then(({
- data
- }) => {
- try {
- // 保存appId
- appId = data.appId
- // 微信信息配置
- weixinObj.config({
- debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
- appId: data.appId, // 必填,企业号的唯一标识,此处填写企业号corpid
- timestamp: data.timestamp, // 必填,生成签名的时间戳
- nonceStr: data.nonceStr, // 必填,生成签名的随机串
- signature: data.signature, // 必填,签名,见附录1
- jsApiList: data.jsApiList // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
- });
- weixinObj.ready((e) => {
- console.log('注册完毕');
- store.commit('setWeiChatObj', weixinObj)
- ok(weixinObj)
- })
- } catch (e) {
- console.log(e)
- }
- })
- .catch(e => {
- error(e);
- console.log(e);
- });
- } catch (e) {
- console.log(e);
- //TODO handle the exception
- }
- })
- }
- // 加载分享数据
- export function shareLoad(config) {
- let mess;
- if (config) {
- mess = {
- link: config.link, // 分享链接
- imgUrl: config.imgUrl,
- desc: config.desc,
- title: config.title,
- success: config.success,
- fail: config.fail || function(e) {
- console.log(e);
- }
- }
- } else {
- // console.log(window.location.href, 55)
- // let url = window.location.href + '?spread=' + store.state.userInfo.uid;
- let url = window.location.href;
- url = url.replace(/[\?,&]{0,1}from=singlemessage/g, '');
- mess = {
- title: shareData.title,
- link: url, // 分享链接
- imgUrl: shareData.img, // 分享图标
- desc: shareData.synopsis,
- success: function() {},
- fail: function(e) {
- console.log(e);
- }
- }
- }
- // 获取仓库数据
- // 分享好友
- // weixinObj.updateAppMessageShareData(mess);
- weixinObj.onMenuShareAppMessage(mess); // 即将废弃
- // 分享朋友圈
- // weixinObj.updateTimelineShareData(mess)
- weixinObj.onMenuShareTimeline(mess); // 即将废弃
- }
- // 隐藏功能按钮
- export function weixinhideMenu(config) {
- console.log(config)
- weixinObj.ready((e) => {
- weixinObj.hideMenuItems({
- menuList: config, // 要隐藏的菜单项,只能隐藏“传播类”和“保护类”按钮,所有menu项见附录3
- });
- })
- }
- // 保存路由对象
- export function setRouter(route) {
- return new Promise((ok, err) => {
- router = getApp().$router;
- console.log(router, '开始数据');
- if (!router) {
- const set = setInterval(() => {
- router = getApp().$router;
- console.log(router, '返回数据');
- if (router) {
- console.log(router, '结束');
- clearInterval(set)
- ok(router)
- }
- }, 100);
- } else {
- console.log(router, '成功');
- ok(router)
- }
- })
- }
- export default {
- weixinObj,
- shareData,
- appId,
- setRouter,
- shareLoad
- }
|