添加微信获取code的方法
This commit is contained in:
parent
2956c4fa90
commit
bb8e368a5e
@ -1,3 +1,5 @@
|
||||
import { tansParams } from "./ruoyi";
|
||||
|
||||
/**
|
||||
* 通配符比较
|
||||
* @param {*} str 待比较的字符串
|
||||
@ -6,15 +8,15 @@
|
||||
*/
|
||||
export function wildcardCompare(str: string, pattern: string): boolean {
|
||||
const regex = pattern.replace(/[*?]/g, (match) => {
|
||||
if (match === '*') {
|
||||
return '.*';
|
||||
} else if (match === '?') {
|
||||
return '.';
|
||||
if (match === "*") {
|
||||
return ".*";
|
||||
} else if (match === "?") {
|
||||
return ".";
|
||||
} else {
|
||||
return match
|
||||
return match;
|
||||
}
|
||||
});
|
||||
const regexPattern = new RegExp('^' + regex + '$');
|
||||
const regexPattern = new RegExp("^" + regex + "$");
|
||||
return regexPattern.test(str);
|
||||
}
|
||||
|
||||
@ -24,7 +26,7 @@ export function wildcardCompare(str: string, pattern: string): boolean {
|
||||
* @returns 复制的对象
|
||||
*/
|
||||
export function deepClone(obj: any) {
|
||||
if (obj == null || typeof obj !== 'object') {
|
||||
if (obj == null || typeof obj !== "object") {
|
||||
return obj;
|
||||
}
|
||||
let result;
|
||||
@ -47,7 +49,7 @@ export function deepClone(obj: any) {
|
||||
* @returns 复制的对象
|
||||
*/
|
||||
export function deepCloneTo<T>(obj: T, result: T) {
|
||||
if (obj == null || typeof obj !== 'object') {
|
||||
if (obj == null || typeof obj !== "object") {
|
||||
return obj;
|
||||
}
|
||||
for (let [key, value] of Object.entries(obj)) {
|
||||
@ -62,15 +64,66 @@ export function deepCloneTo<T>(obj: T, result: T) {
|
||||
* @returns 生成的uuid字符串
|
||||
*/
|
||||
export function generateUUID(): string {
|
||||
let uuid = '';
|
||||
const chars = '0123456789abcdef';
|
||||
let uuid = "";
|
||||
const chars = "0123456789abcdef";
|
||||
|
||||
for (let i = 0; i < 32; i++) {
|
||||
if (i === 8 || i === 12 || i === 16 || i === 20) {
|
||||
uuid += '-';
|
||||
uuid += "-";
|
||||
}
|
||||
uuid += chars[Math.floor(Math.random() * chars.length)];
|
||||
}
|
||||
|
||||
return uuid;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取code
|
||||
* @returns 生成的code字符串
|
||||
*/
|
||||
export async function getCode() {
|
||||
// #ifdef H5
|
||||
let appid = "wx98501e665b0f0596";
|
||||
let url = "http://localhost:3002/index.html";
|
||||
let code = "";
|
||||
|
||||
// 截取url中的code方法
|
||||
function getUrlCode() {
|
||||
let url = location.search;
|
||||
console.log(url);
|
||||
let theRequest: any = new Object();
|
||||
if (url.indexOf("?") != -1) {
|
||||
let str = url.substr(1);
|
||||
let strs = str.split("&");
|
||||
for (let i = 0; i < strs.length; i++) {
|
||||
theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
|
||||
}
|
||||
}
|
||||
return theRequest as { code: string };
|
||||
}
|
||||
|
||||
code = getUrlCode().code; // 截取code
|
||||
if (code == undefined || code == "" || code == null) {
|
||||
// 如果没有code,则去请求
|
||||
console.log("h5");
|
||||
window.location.href =
|
||||
"https://open.weixin.qq.com/connect/oauth2/authorize?" +
|
||||
tansParams({
|
||||
appid: appid,
|
||||
redirect_uri: url,
|
||||
response_type: "code",
|
||||
scope: "snsapi_userinfo",
|
||||
state: "STATE",
|
||||
}) +
|
||||
"#wechat_redirect";
|
||||
} else {
|
||||
return code;
|
||||
}
|
||||
// #endif
|
||||
|
||||
// #ifdef MP-WEIXIN
|
||||
// @ts-ignore
|
||||
const res = await wx.login();
|
||||
return res.code;
|
||||
// #endif
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user