uniapp微信小程序手机号一键登录获取手机号,获取openid

网上翻了很多都是app版本的,不适用于小程序

复制代码
<!-- 微信授权登录全程代码实现 -->
<template>
  <view>
    <view>
      <!-- isloding是用来记录当前用户是否是第一次授权使用的 -->
      <view>
        <view class="header">
          <image src="/static/logo.png"></image>
        </view>
        <view class="content">
          <view>申请获取以下权限</view>
          <text>获得你的公开信息(昵称,头像、地区等)</text>
        </view>
        <button
          @click="login"
          @getphonenumber="getPhone"
          open-type="getPhoneNumber"
        >
          手机号授权登录
        </button>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      code: "",
      openid: "",
    };
  },
  methods: {
    // 获取手机号
    getPhone(e) {
      console.log(e);
      uniCloud
        .callFunction({
          name: "login",
          data: {
            code: e.detail.code,
          },
        })
        .then((res) => {
          console.log(res, "ressssssss");
          // 在这里就可以获取到手机号还有token了
        });
    },
    // 获取code
    login() {
      let that = this;
      wx.login({
        success(res) {
          console.log(res, "code---");
          // that.code = res.code;
          let appid = "你的appid";
          let secret = "你的secret ";
          let url =
            "https://api.weixin.qq.com/sns/jscode2session?appid=" +
            appid +
            "&secret=" +
            secret +
            "&js_code=" +
            res.code;
          uni.request({
            url: url, // 请求路径
            success: (result) => {
              console.log(result, "openid00000000000");
              that.openid = result.data.openid;
            },
          });
        },
      });
    },
  },
};
</script>

<style>
.header {
  margin: 90rpx 0 90rpx 50rpx;
  border-bottom: 1px solid #ccc;
  text-align: center;
  width: 650rpx;
  height: 300rpx;
  line-height: 450rpx;
}

.header image {
  width: 200rpx;
  height: 200rpx;
}

.content {
  margin-left: 50rpx;
  margin-bottom: 90rpx;
}

.content text {
  display: block;
  color: #9d9d9d;
  margin-top: 40rpx;
}

.bottom {
  border-radius: 80rpx;
  margin: 70rpx 50rpx;
  font-size: 35rpx;
}
</style>

因为我用的是云函数 所以如下 云函数js是这样的(记得上传部署)

复制代码
// 云函数
exports.main = async function (event,context) {// 获取token
    const appid = '你的appid'
	const secret = '你的secret '
	const res = await uniCloud.httpclient.request('https://api.weixin.qq.com/cgi-bin/token', {
		method: 'GET',
		data: {
			grant_type: 'client_credential',
			appid: appid,
			secret: secret,
		},
		dataType: 'json',
		header: {
			'content-type': 'application/json'
		}
	})
	// 获取用户手机号
	let ress = {}
	if (res && res.data.access_token) {
		ress = await uniCloud.httpclient.request(
			'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=' + res.data
				.access_token, {
			method: 'post',
			data: {
				code: event.code,
			},
			dataType: 'json',
			contentType: 'json', // 指定以application/json发送data内的数据

		})
	}
	
	// 执行入库等操作,正常情况下不要把完整手机号返回给前端
	return {
		code: event.code,
		message: res.data.access_token,
		data: ress.data,
		
	}
}
相关推荐
三声三视4 小时前
uni-app 鸿蒙端传参变成 [object Object]?顺着源码追到 ArkTS router 底层才搞明白
人工智能·ai·uni-app·aigc·ai编程·harmonyos
admin and root10 小时前
「移动安全」安卓APP 反编译&frida脱壳技巧分享
android·开发语言·python·web安全·微信小程序·移动安全·攻防演练
小徐_233312 小时前
Open Wot 1.0.5 发布:让 AI 接入 wot-ui,只需要两条命令
前端·uni-app·ai编程
AI多Agent协作实战派15 小时前
AI多Agent协作系统实战(二十二):从6列到12列——任务监控报告的进化之路
java·人工智能·uni-app·bug
小杨小杨、努力变强!17 小时前
VS Code运行HBuilder X中的uni-app项目
vscode·uni-app·uni-app run
码兄科技1 天前
实战:基于Spring Boot + UniApp的地理信息小程序开发
spring boot·后端·uni-app
m0_462803882 天前
【无标题】
微信小程序
2501_916007472 天前
iOS和macOS应用程序性能分析和优化工具使用综合指南
android·macos·ios·小程序·uni-app·iphone·webview
2501_916007473 天前
深入理解HTTPS对称与非对称加密机制及Charles抓包实践
网络协议·http·ios·小程序·https·uni-app·iphone
小徐_23333 天前
AI 写 wot-ui 总在猜 API?我们把 Skills、MCP 和 CLI 都配好了
前端·uni-app·ai编程