tl-uniapp
Version:
自主开发的UniApp组件——Жидзин(triangulum-ui)系列组件库。用于组件
90 lines (84 loc) • 2.68 kB
JavaScript
const takeType = data => Object.prototype.toString.call(data).slice(8, -1); // Get the current data type
const isFunc = data => takeType(data) === 'Function';
const isUndef = data => ['', null, undefined].includes(data);
function getUrlParam(name) {
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
const url = window.location.href.split('#')[0];
const search = url.split('?')[1];
if (search) {
var r = search.substr(0).match(reg);
if (r !== null) return unescape(r[2]);
return null;
}
return null;
}
const defaultConfig = {
debug: false,
appId: '',
timestamp: '',
nonceStr: '',
signatureL: '',
openTagList: [],
jsApiList: ['updateAppMessageShareData', 'updateTimelineShareData', 'hideOptionMenu', 'showOptionMenu', 'chooseWXPay'],
};
const XWxSdk = {};
/**
*
*/
XWxSdk.config = {};
/**
* @function 设置对象
* @param {object} config
*/
XWxSdk.setConfig = function (config) {
XWxSdk.config = Object.assign({}, defaultConfig, config);
jWeixin.config(config);
};
/**
*
* @param {*} config
* @param {*} isJump
* @param {*} callback
* @returns
*/
XWxSdk.wxAuthorize = function (config, callback) {
if (!this.isWechat) return false;
const code = getUrlParam('code');
const { appId, redirect_url } = config;
if (isUndef(code)) {
const handleRedirectUrl = encodeURIComponent(redirect_url);
const authURL = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${handleRedirectUrl}&response_type=code&scope=snsapi_userinfo&state=release#wechat_redirect`;
console.log(authURL);
window.location.href = authURL;
} else {
callback && callback(code);
}
};
XWxSdk.ready = function (config, success) {
jWeixin.config(this.config);
let promise;
if (!isFunc(success) && window.Promise) {
promise = new Promise((resolve, reject) => {
success = function ({ err, error }) {
err === 0 ? resolve({ err: 0 }) : reject({ err: 4000, error });
};
});
}
jWeixin.ready(() => success && success({ err: 0, jWeixin }));
jWeixin.error(error => {
console.log(error);
success && success({ err: 4000, error });
});
if (promise) return promise;
};
// #ifdef H5
/**
* 是不是在localhost中
*/
XWxSdk.isDevLocation = (() => /localhost/i.test(window.location.host))();
/**
* 是不是微信浏览器
*/
XWxSdk.isWechat = (() => /MicroMessenger/i.test(window.navigator.userAgent))();
// #endif
export default XWxSdk;