123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- /**
- * 获取 code
- * @returns
- */
- export function getCode () {
- const matched = /[?&]*code=([^&]+)/.exec(location.search)
- if (matched) {
- return decodeURIComponent(matched[1])
- }
- }
-
- /**
- * 跳转授权页面
- */
- export function getRedirectURL (force, code) {
- if (import.meta.env.DEV) return;
-
- const originCode = localStorage.getItem('wxcode');
- const wxfirst = localStorage.getItem('wxfirst') === null;
-
- localStorage.setItem('wxcode', code)
- localStorage.setItem('wxfirst', 'not');
-
- if (force || wxfirst || !code || code === originCode) {
- const origin = location.origin + location.pathname
- const searchStr = location.search.replace(/code\=[^&]+/, '').replace(/state\=[^&]+/, '').replace(/^&+/g, '').replace(/\?+$/, '')
- const local = origin + searchStr + location.hash
- console.log('--------------------->')
- console.log(JSON.stringify(location))
- console.log(local)
- console.log('--------------------->')
- return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxd3bab568bc42d1de&redirect_uri=${encodeURIComponent(local)}&response_type=code&scope=snsapi_base&state=123#wechat_redirect`
- }
- }
- /**
- * 初始化微信 SDK
- * @param {*} params
- * @returns
- */
- export function initWxSDK(params) {
- return new Promise((resolve, reject) => {
- wx.config({
- beta: true, // 获取微信抬头必须这个
- debug: import.meta.env.DEV, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
- appId: params.appId, // 必填,公众号的唯一标识
- timestamp: params.timestamp, // 必填,生成签名的时间戳
- nonceStr: params.nonceStr, // 必填,生成签名的随机串
- signature: params.signature,// 必填,签名
- jsApiList: ['chooseInvoiceTitle'] // 必填,需要使用的JS接口列表
- });
-
- wx.ready(() => {
- console.log('---微信SDK初始化成功---')
- resolve();
- window.__initwx = true;
- });
- wx.error(function(res){
- console.log('---微信SDK初始化失败---')
- console.error(res);
- window.__initwx = false;
- reject();
- });
- });
- }
-
- /**
- * 获取微信抬头
- * @param {*} scene
- * @returns
- */
- export function chooseInvoiceTitle(scene) {
- return new Promise((resolve, reject) => {
- if (!window.__initwx) {
- reject('微信SDK未成功初始化');
- return;
- }
-
- wx.invoke('chooseInvoiceTitle', { scene }, function (res) {
- console.log('----------chooseInvoiceTitle------', res);
- if (res.err_msg == 'chooseInvoiceTitle:ok') {
- // choose_invoice_title_info 结构如下:
- // {
- // "type":"0",
- // "title":"腾讯科技(深圳)有限公司",
- // "taxNumber":"123466789987646131",
- // "companyAddress":"深圳市南山区某某路腾讯大厦",
- // "telephone":"123456789",
- // "bankName":"某某银行",
- // "bankAccount":"621111111111290"
- // }
- resolve(typeof res.choose_invoice_title_info == 'string' ? JSON.parse(res.choose_invoice_title_info) : res.choose_invoice_title_info);
- } else {
- console.error(res.err_msg);
- console.debug(res);
- reject(res.err_msg);
- }
- });
- });
- }
-
- // 本系统的特殊情况,使用外部服务初始化微信SDK
- export function initWxSDK2() {
- const link = window.location.origin + window.location.pathname + window.location.search;
- const servAPI = '//api.h5.njyunzhi.com/mp/jssdk?url=' + encodeURIComponent(link);
-
- fetch(servAPI).then(res => res.json()).then(res => {
- console.log('----------', res.data);
- initWxSDK(res.data);
- });
-
- }
|