12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- import {getUrlParams} from "@/utils/urlUtils";
- import {getOAuthUrl, getWxOpenId} from "@/api/wechat";
- import {useUserStore} from "@/store/modules/user";
- import {alertController, loadingController} from '@ionic/vue';
- export function isWechat() {
- return String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger";
- /*return true;*/
- }
- export async function wxAuth() {
- const userStore = useUserStore();
- if (isWechat() && !userStore.getOpenId) {
- const urlParams = getUrlParams() as any;
- const code = urlParams["code"];
- if (!code) {
- const loading = await loadingController.create({
- cssClass: 'my-custom-class',
- message: '微信授权中,请稍后...',
- duration: 2000,
- });
- await loading.present();
- getOAuthUrl().then((res: any) => {
- if (res) {
- window.location.href = res;
- } else {
- presentAlert("微信授权失败!");
- }
- });
- } else {
- getWxOpenId(code).then((res: any) => {
- userStore.setOpenId(res);
- });
- }
- }
- }
- const presentAlert = async (msg: any) => {
- const alert = await alertController.create({
- header: '错误!',
- message: msg,
- buttons: [
- '确定'
- ],
- });
- await alert.present();
- }
|