微信网页授权登录

<template>
  <div class="box">
    <div class="wrap">
      <span>姓名</span>
      <input type="text" v-model="name" class="ipt1" />
    </div>
    <div class="wrap" style="margin-top: 0">
      <span>手机号</span>
      <input type="number" v-model="phone" class="ipt1" />
    </div>
    <button class="btn" @click="login">添加</button>
    <div>
    </div>
  </div>

  <!-- <span>{{ url }}</span> -->
</template>
<script>
import axios from "axios";
import { showToast } from "vant";

export default {
  data() {
    return {
      name: "",
      phone: "",
      admin_id: "",
      code: "",
      url: "",
      isValidPhoneNumber:false
    };
  },
  methods: {
    getUrlParam() {
      const currentUrl = window.location.href;
      // 根据问号 "?" 进行分割,获取查询参数部分
      const query = currentUrl.split("?")[1];
      if (query == undefined) {
        return {};
      }
      // 根据 "&" 进行分割,获取每个键值对
      const params = query.split("&");
      // 创建一个对象来存储键值对
      const values = {};
      // 遍历键值对,分割键和值,并存储到对象中
      for (let i = 0; i < params.length; i++) {
        const keyValue = params[i].split("=");
        const key = keyValue[0];
        const value = keyValue[1];
        values[key] = value;
      }
      return values;
    },
    async login() {
      const regex = /^1[3456789]\d{9}$/;
      this.isValidPhoneNumber = regex.test(this.phone);
      if (this.name == "") {
        showToast({
          message: "名字为空",
          position: "top",
        });
      } else if(this.isValidPhoneNumber==false){
        showToast({
          message: "手机号格式不对",
          position: "top",
        });
      }else{
       window.location.href=`https://zsff01.hiboxes.cn/api/accredit/public_oauth?admin_id=${this.admin_id}&name=${this.name}&mobile=${this.phone}` 
        // let { res } = await axios.post("/api/accredit/public_oauth", {
        //   name: this.name,
        //   mobile: this.phone,
        //   admin_id:this.admin_id,
        //   // code:this.code
        // });
        // console.log(res)
      }
    },
  },
  mounted() {
    let urlParam = this.getUrlParam();
    console.log(urlParam);
    if ("admin_id" in urlParam) {
      this.admin_id = urlParam.admin_id;
      localStorage.setItem("admin_id", this.admin_id);
    } else {
      this.admin_id = localStorage.getItem("admin_id");
    }
    // if ("code" in urlParam) {
    //   localStorage.setItem("code", this.code);
    //   this.code = urlParam.code;
    // } else {
    //   window.location.href =
    //     "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc09afc2cd6286241&redirect_uri=https%3A%2F%2Fzsff01.hiboxes.cn%2Faccredit&response_type=code&scope=snsapi_base&state=STATE&connect_redirect=1#wechat_redirect";
    // }
  },
};
</script>
<style scoped lang="scss">
.box {
  width: 375px;

  // height: 812px;
  // background:red;
  .wrap {
    width: 375px;
    height: 80px;
    margin-top: 100px;
    display: flex;
    justify-content: center;
    align-items: center;

    .ipt1 {
      width: 200px;
      height: 30px;
    }

    span {
      margin-right: 30px;
    }
  }

  .btn {
    margin-left: 35px;
    width: 305px;
    height: 50px;
    background: black;
    color: white;
    border: none;
    border-radius: 20px;
    margin-top: 70rpx;
  }
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
微信网页授权登录demo是一个示例程序,用于展示如何在网站或应用中使用微信登录功能。 首先,我们需要在微信公众平台上申请一个开发者账号,并创建一个应用。在应用设置中,我们需要配置网页授权的回调地址。 接下来,我们可以开始编写示例程序。首先,我们需要引入微信提供的SDK,这个SDK包含了一些必要的函数和方法。 在程序中,我们首先需要处理用户点击登录按钮的事件。当用户点击登录按钮时,我们需要调用微信的JSAPI,发起授权请求。我们可以通过调用微信提供的接口,获取到一个临时的code。 接着,我们需要通过这个code,向微信服务器发送请求,获取用户的access_token和openid。这些信息将用于后续的用户信息获取。 一般情况下,我们会将这些信息存储在服务器端或数据库中,以供后续使用。在示例程序中,我们可以简单地将这些信息暂存到本地的session中。 最后,我们可以根据用户的access_token和openid,通过微信提供的接口,获取到用户的基本信息,如昵称、头像等。 至此,我们就完成了一个简单的微信网页授权登录demo。当用户在我们的网站或应用中点击登录按钮后,会跳转到微信授权页面,用户同意授权后,我们可以获取到用户的基本信息,实现登录功能。 需要注意的是,我们在开发过程中还需要处理一些异常情况,如用户未同意授权授权过期等。另外,由于微信的接口是需要网络访问的,因此我们需要确保程序可以正常访问网络。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值