Files
XiaoLingHou_web/utils/index.js
2023-12-29 00:08:10 +08:00

149 lines
3.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Vue from 'vue'
import {
thirdLogin
} from '../request/api.js'
import {
baseUrl
} from '../request/baseUrl.js'
export const isWechat = async () => {
// #ifdef H5
var ua = window.navigator.userAgent.toLowerCase();
//alert(ua);
// if (ua.match(/windowswechat/i) == "windowswechat") {
// //alert('这是电脑版微信浏览器');
// return false;
// } else {
// console.log('这不是微信浏览器');
// }
if (ua.match(/MicroMessenger/i) == 'micromessenger') {
return true;
}
return false;
// #endif
// #ifdef MP-WEIXIN
return true
// #endif
}
export const login = async () => {
// #ifdef H5
let appid = 'wx877c9b57a770f86e'
let url = "";
url = window.location.href;
var a =
`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(url)}&response_type=code&scope=snsapi_userinfo&state=isLogin#wechat_redirect`
window.location.href = a
// #endif
// #ifdef MP-WEIXIN
let res = await wx.login()
if (res.errMsg === 'login:ok') {
wechatloginAfter(res.code)
}
console.log('小程序登录结果:', res);
// wechatloginAfter(res.code)
// #endif
}
export const getUrlParams = (key) => {
// let pages = getCurrentPages()
// let currentRoute = pages[pages.length - 1]
// return currentRoute.options[key]
// 获取当前页面的 URL 参数
// var pages = getCurrentPages(); // 获取当前页面栈
// var currentPage = pages[pages.length - 1]; // 获取当前页面
// var url = currentPage.route; // 获取当前页面路径
// var options;
// // 判断是否在微信小程序环境下
// if (uni.getSystemInfoSync().platform === 'mp-weixin') {
// // 在微信小程序中,通过 options 属性获取 URL 参数
// options = currentPage.options;
// } else {
var searchParams = new URLSearchParams(window.location.search);
// 获取code参数的值
var code = searchParams.get(key);
return code
// }
}
export const wechatloginAfter = async (code) => {
// 向后端发起请求
let {
data
} = await thirdLogin({
platform: 'wechat',
code
})
console.log(data);
if (!data.data || !data.data.userinfo || !data.data.userinfo?.token) {
uni.$u.toast('微信自动登录失败');
return
}
uni.setStorageSync('userInfo', data.data.userinfo)
uni.setStorageSync('token', data.data.userinfo.token)
uni.setStorageSync('thirdinfo', data.data.thirdinfo)
uni.navigateTo({
url: '/pages/index/index?code=',
})
// 本地存储token
// console.log('获取到的用户数据', data.data);
// localStorage.setItem('USER_INFO', JSON.stringify(data.data.userinfo))
// localStorage.setItem('TOKEN', data.data.userinfo.token)
// localStorage.setItem('USER_THIRD', JSON.stringify(data.data.thirdinfo))
// _this.userInfo = data.data.userinfo
// console.log(_this.userInfo);
}
function qsFormatter(href) {
if (href.includes("?")) {
var a = href.split("?")[1]
var b = a.split("&")
var r = {};
b.forEach(item => {
var kv = item.split("=")
r[kv[0]] = kv[1]
})
return r
} else {
return {}
}
}
function isPhoneNumber(phone) {
// 使用正则表达式匹配手机号
var pattern = /^1[3456789]\d{9}$/;
return pattern.test(phone);
}
// 格式化日期
Date.prototype.$formatDate = function() {
console.log(this);
const year = this.getFullYear(); // 年份
const month = this.getMonth() + 1; // 月份(注意月份范围是 0-11
const day = this.getDate(); // 日期
// 拼接成字符串形式的日期
const formattedDate = `${year}${(month < 10 ? '0' : '') + month}${(day < 10 ? '0' : '') + day}`;
return formattedDate;
}
function showImg(urls, index) {
uni.previewImage({
urls,
index
})
}
Vue.prototype.$getUrlParams = getUrlParams
Vue.prototype.$showImg = showImg
Vue.prototype.$isWechat = isWechat
Vue.prototype.$wechatloginAfter = wechatloginAfter
Vue.prototype.$login = login
Vue.prototype.$isPhoneNumber = isPhoneNumber