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


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

    1. <!-- 微信授权登录全程代码实现 -->
    2. <template>
    3. <view>
    4. <view>
    5. <!-- isloding是用来记录当前用户是否是第一次授权使用的 -->
    6. <view>
    7. <view class="header">
    8. <image src="/static/logo.png"></image>
    9. </view>
    10. <view class="content">
    11. <view>申请获取以下权限</view>
    12. <text>获得你的公开信息(昵称,头像、地区等)</text>
    13. </view>
    14. <button
    15. @click="login"
    16. @getphonenumber="getPhone"
    17. open-type="getPhoneNumber"
    18. >
    19. 手机号授权登录
    20. </button>
    21. </view>
    22. </view>
    23. </view>
    24. </template>
    25. <script>
    26. export default {
    27. data() {
    28. return {
    29. code: "",
    30. openid: "",
    31. };
    32. },
    33. methods: {
    34. // 获取手机号
    35. getPhone(e) {
    36. console.log(e);
    37. uniCloud
    38. .callFunction({
    39. name: "login",
    40. data: {
    41. code: e.detail.code,
    42. },
    43. })
    44. .then((res) => {
    45. console.log(res, "ressssssss");
    46. // 在这里就可以获取到手机号还有token了
    47. });
    48. },
    49. // 获取code
    50. login() {
    51. let that = this;
    52. wx.login({
    53. success(res) {
    54. console.log(res, "code---");
    55. // that.code = res.code;
    56. let appid = "你的appid";
    57. let secret = "你的secret ";
    58. let url =
    59. "https://api.weixin.qq.com/sns/jscode2session?appid=" +
    60. appid +
    61. "&secret=" +
    62. secret +
    63. "&js_code=" +
    64. res.code;
    65. uni.request({
    66. url: url, // 请求路径
    67. success: (result) => {
    68. console.log(result, "openid00000000000");
    69. that.openid = result.data.openid;
    70. },
    71. });
    72. },
    73. });
    74. },
    75. },
    76. };
    77. </script>
    78. <style>
    79. .header {
    80. margin: 90rpx 0 90rpx 50rpx;
    81. border-bottom: 1px solid #ccc;
    82. text-align: center;
    83. width: 650rpx;
    84. height: 300rpx;
    85. line-height: 450rpx;
    86. }
    87. .header image {
    88. width: 200rpx;
    89. height: 200rpx;
    90. }
    91. .content {
    92. margin-left: 50rpx;
    93. margin-bottom: 90rpx;
    94. }
    95. .content text {
    96. display: block;
    97. color: #9d9d9d;
    98. margin-top: 40rpx;
    99. }
    100. .bottom {
    101. border-radius: 80rpx;
    102. margin: 70rpx 50rpx;
    103. font-size: 35rpx;
    104. }
    105. </style>

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

    1. // 云函数
    2. exports.main = async function (event,context) {// 获取token
    3. const appid = '你的appid'
    4. const secret = '你的secret '
    5. const res = await uniCloud.httpclient.request('https://api.weixin.qq.com/cgi-bin/token', {
    6. method: 'GET',
    7. data: {
    8. grant_type: 'client_credential',
    9. appid: appid,
    10. secret: secret,
    11. },
    12. dataType: 'json',
    13. header: {
    14. 'content-type': 'application/json'
    15. }
    16. })
    17. // 获取用户手机号
    18. let ress = {}
    19. if (res && res.data.access_token) {
    20. ress = await uniCloud.httpclient.request(
    21. 'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=' + res.data
    22. .access_token, {
    23. method: 'post',
    24. data: {
    25. code: event.code,
    26. },
    27. dataType: 'json',
    28. contentType: 'json', // 指定以application/json发送data内的数据
    29. })
    30. }
    31. // 执行入库等操作,正常情况下不要把完整手机号返回给前端
    32. return {
    33. code: event.code,
    34. message: res.data.access_token,
    35. data: ress.data,
    36. }
    37. }

  • 相关阅读:
    人力物力和时间资源有限?守住1个原则,精准覆盖所有兼容性测试!
    网络层解析——IP协议、地址管理、路由选择
    Android NDK 中有导出 sp智能指针吗?如果没有,可以用什么方法代替 android::sp 智能指针
    SpringBoot静态资源配置
    洛谷题解 | AT_abc321_c Primes on Interval
    软件测试培训之十个无脚本测试方案
    Qt中https的使用,报错TLS initialization failed和不能打开ssl.lib问题解决
    医疗器械安全最佳实践
    阿里云-Centos7配置Nginx实现HTTPS(保姆级安装教程-无脑执行)
    基于elementui二次封装tooltip组件
  • 原文地址:https://blog.csdn.net/weixin_54368936/article/details/133241627