<template>
  <view>
    <view class="logo_box">
      <image src="../../static/logo2.jpg" class="logo_image"></image>
    </view>
    <!-- #ifdef MP-WEIXIN -->
    <button open-type="getPhoneNumber" @getphonenumber="getPhonenumber" class="get_phone">授权登录</button>
    <!-- #endif -->
    <!-- #ifdef H5 -->
    <button @click="toPhoneLogin" class="get_phone">授权登录</button>
    <!-- #endif -->
  </view>
</template>

<script>
export default {
  data() {
    return {};
  },
  methods: {
    getPhonenumber(re) {
      // // 如果授权失败的话
      if (re.detail.errMsg != "getPhoneNumber:ok") {
        uni.showToast({
          icon: "error",
          title: "授权失败",
        });
        return;
      }
      // 授权成功以后，调用登录
      this.$http.request("api/wechat/phone_number", { code: re.detail.code }, "post").then((re) => {
        // 成功的话
        if (re.code == "success") {
          // 存储登录标识
          uni.setStorageSync("userLogin", re.data);
          // 跳转到页面
          uni.switchTab({ url: "/pages/user/index" });
        } else {
          uni.showToast({
            title: re.msg,
            icon: "none",
          });
        }
      });
    },
    toPhoneLogin(re) {
      // 登录效果
      this.$http.request("api/wechat/phone_number", this.requestParam).then((re) => {
        // 成功的话
        if (re.code == "success") {
          // 存储登录标识
          uni.setStorageSync("userLogin", re.data);
          // 跳转到页面
          uni.switchTab({ url: "/pages/user/index" });
        } else {
          uni.showToast({
            title: re.msg,
            icon: "none",
          });
        }
      });
    },
  },
};
</script>

<style lang="less">
.logo_box {
  display: block;
  margin: 0rpx auto;
  margin-top: 80rpx;
  .logo_image {
    width: 320rpx;
    height: 320rpx;
    display: block;
    margin: 0rpx auto;
  }
}
.get_phone {
  display: block;
  width: 180rpx;
  height: 60rpx;
  color: #ffffff;
  font-size: 28rpx;
  background: green;
  margin: 0rpx auto;
  line-height: 60rpx;
  margin-top: 160rpx;
  border-radius: 30rpx;
  border: 0rpx solid #dddddd;
}
.get_phone::after {
  border: 0rpx solid #dddddd;
}
</style>
