123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- import {
- wechatConfig,
- share
- } from '@/api/wx';
- import * as log from './log.js'
- import {
- isAndroid
- } from './platform.js'
- import store from '../store';
- import weixinObj from "@/plugin/jweixin-module/index.js";
- let shareData = '';
- let appId = '';
- let router = '';
- export async function loginWinxin() {
- console.log('1');
- let pages, page, path;
- if (!router) {
- router = await setRouter();
- }
- try {
- pages = getCurrentPages();
-
- page = pages[pages.length - 1];
-
- path = page.route;
- } catch (e) {
- console.log(e);
-
- }
-
- if (path != 'pages/redirect/redirect') {
- log.addLog('开始注册微信')
- weixindata().then(() => {
-
- try {
- weixinSq();
- } catch (e) {
- console.log(e);
-
- }
- });
- }
- };
- function weixinSq() {
-
- try {
-
- const type = router.mode === "history"?'':'/#'
- let ul = encodeURIComponent(store.state.baseURL + store.state.urlFile +type+ '/pages/redirect/redirect');
-
- 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';
- window.location.href = url;
- } catch (e) {
- console.log(e);
-
- }
- };
- export async function weixindata(config) {
- let url;
- store.commit('setWeiChatObj', weixinObj)
- try {
-
- if (!router) {
- router = await setRouter();
- }
- if (router.mode === "history") {
-
- 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];
- }
- log.addLog('注册开始', url)
- console.log('开始注册', url);
- } catch (e) {
- console.log('错误', e);
- }
- return new Promise((ok, error) => {
- try {
-
- wechatConfig({
- url
- })
- .then(({
- data
- }) => {
- try {
-
- appId = data.appId
-
- weixinObj.config({
- debug: false,
- appId: data.appId,
- timestamp: data.timestamp,
- nonceStr: data.nonceStr,
- signature: data.signature,
- jsApiList: data.jsApiList
- });
- weixinObj.ready((e) => {
- log.addLog("注册完毕", data)
- console.log('注册完毕');
- ok(data)
- })
- } catch (e) {
- console.log(e)
- }
- })
- .catch(e => {
- error(e);
- console.log(e);
- });
- } catch (e) {
- console.log(e);
-
- }
- })
- }
- export function shareLoad(config) {
- console.log('开始调用分享')
- try {
- weixindata().then((e) => {
-
- if (config) {
- shareFun(config)
- } else {
-
- if (shareData) {
- shareFun()
- } else {
-
- share({}).then(({
- data
- }) => {
- shareData = data.data
- shareFun()
- });
- }
- }
- })
- } catch (e) {
- console.log('报错', e)
-
- }
- }
- function shareFun(config) {
- try {
- console.log('再付分享内容', config);
- let mess;
- if (config) {
- mess = {
- link: config.link,
- imgUrl: config.imgUrl,
- desc: config.desc,
- title: config.title,
- success: config.success || function(e) {
- console.log(e);
- },
- fail: config.fail || function(e) {
- console.log(e);
- }
- }
- } else {
- const userInfo = uni.getStorageSync('userInfo')
- console.log(userInfo);
- const url = window.location.href + '?spread=' + userInfo.uid;
- url = url.replace(/[\?,&]{0,1}from=singlemessage/g, '');
- mess = {
- title: shareData.title,
- link: url,
- imgUrl: shareData.img,
- desc: shareData.synopsis,
- success: function() {
-
-
-
-
-
-
-
-
-
-
- }
- }
- }
-
-
- weixinObj.updateAppMessageShareData(mess);
-
- weixinObj.updateTimelineShareData(mess)
- } catch (e) {
- console.log(e);
-
- }
- }
- 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
- }
|