微信小程序手机号码授权登录

文章目录


一、微信小程序开发

目前个人的小程序无法使用手机号码授权登录,可以使用测试号进行开发

二、使用步骤

1.前端代码

代码如下(示例):

html 复制代码
<button open-type="getPhoneNumber" bindgetphonenumber="getUserPhoneNumber">立即登录</button>
javascript 复制代码
Page({

getUserPhoneNumber(event) {
    if(event.detail.errMsg != "getPhoneNumber:ok") {
      // 授权失败
      return false;
    }
    // 发起请求,进行授权登录
    wx.request({
      url: app.globalData.basePath + '/system/applet/login',
      data: {
        phoneCode: event.detail.code
      },
      method:'POST',
      success: res => {
        console.log(res.data);
        wx.setStorageSync('applet', res.data);
      }
    })
  }
}}

2.后台配置

java 复制代码
# 小程序配置
applet:
  appId: wx53fda****c6b0a3
  appSecret: ade99d6dca77****f52fecd5d35039b5
  grantType: authorization_code

3.后台代码

代码如下(示例):

java 复制代码
package com.ruoyi.system.domain.io;

public class AppletLoginInput {

    private String phoneCode;

    private String iv;

    private String encryptedData;

    public String getPhoneCode() {
        return phoneCode;
    }

    public void setPhoneCode(String phoneCode) {
        this.phoneCode = phoneCode;
    }

    public String getIv() {
        return iv;
    }

    public void setIv(String iv) {
        this.iv = iv;
    }

    public String getEncryptedData() {
        return encryptedData;
    }

    public void setEncryptedData(String encryptedData) {
        this.encryptedData = encryptedData;
    }
}
java 复制代码
package com.ruoyi.system.domain.io;

public class AppletLoginOutput {

    /**
     * token信息
     */
    private String token;

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }
}
java 复制代码
/**
 * 微信小程序手机号码授权登录
 * @param input
 */
@Anonymous
@PostMapping("/login")
public AppletLoginOutput login(@RequestBody AppletLoginInput input) {
    AppletLoginOutput appletLoginOutput = new AppletLoginOutput();
    // 首先获取最新的token信息
    String tokenParam = "appid=" + appletConfig.getAppId() + "&secret=" + appletConfig.getAppSecret() + "&grant_type=client_credential";
    String tokenResult = HttpUtils.sendGet("https://api.weixin.qq.com/cgi-bin/token", tokenParam);
    JSONObject tokenJSONObject = JSONObject.parseObject(tokenResult);

    // 通过登录信息获取用户的手机号码
    // 获取token 信息
    String body = "{\"code\": \"" + input.getPhoneCode() + "\"}";
    HttpRequest post = HttpUtil.createPost("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + tokenJSONObject.getString("access_token"));
    post.header("Content-Type", "application/json");
    post.body(body);
    HttpResponse execute = post.execute();
    AppletResultModel resultModel = JSONObject.parseObject(execute.body(), AppletResultModel.class);
    String jsonString = JSONObject.toJSONString(resultModel);
    System.out.println(jsonString);
    // {"errcode":0,"errmsg":"ok","phone_info":{"countryCode":"86","phoneNumber":"178****3721","purePhoneNumber":"178****3721","watermark":{"appid":"wx53fda****1c6b0a3","timestamp":1754865784}}}
    return appletLoginOutput;
}

总结

人生物语:别哭泣,别叹息。悲伤唤不回逝去的时光。做一个简单的人,看得清世间繁杂却不在心中留下痕迹,保持平常心,简单,快乐。请别把我当傻瓜,有些事不是我不知道,只是我看在眼里,埋在心里。一个人最好记性不要太好,因为回忆越多,幸福感越少。

相关推荐
Emma歌小白3 天前
如何首次运行小程序后端
微信小程序
赣州云智科技的技术铺子3 天前
【一步步开发AI运动APP】十二、自定义扩展新运动项目1
微信小程序·小程序·云开发·智能小程序
2501_915918413 天前
iOS 上架全流程指南 iOS 应用发布步骤、App Store 上架流程、uni-app 打包上传 ipa 与审核实战经验分享
android·ios·小程序·uni-app·cocoa·iphone·webview
00后程序员张3 天前
iOS App 混淆与加固对比 源码混淆与ipa文件混淆的区别、iOS代码保护与应用安全场景最佳实践
android·安全·ios·小程序·uni-app·iphone·webview
破无差3 天前
《赛事报名系统小程序》
小程序·html·uniapp
00后程序员张3 天前
详细解析苹果iOS应用上架到App Store的完整步骤与指南
android·ios·小程序·https·uni-app·iphone·webview
海绵宝宝不喜欢侬3 天前
uniapp-微信小程序分享功能-onShareAppMessage
微信小程序·小程序·uni-app
2501_915106323 天前
Xcode 上传 ipa 全流程详解 App Store 上架流程、uni-app 生成 ipa 文件上传与审核指南
android·macos·ios·小程序·uni-app·iphone·xcode
亮子AI3 天前
【小程序】微信小程序隐私协议
微信小程序·小程序
weixin_177297220693 天前
短剧小程序系统开发:打造个性化娱乐新平台
小程序·娱乐·短剧