栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot+weixin-java-miniapp+uniapp开发流程,获取openid

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot+weixin-java-miniapp+uniapp开发流程,获取openid

一. springboot
    微信小程序,工具包:https://gitee.com/binary/weixin-java-tools/tree/develop
 
   com.github.binarywang
   weixin-java-miniapp
   4.2.0
 
    application.yml配置
# 微信小程序配置(注册小程序账号获取)
wx:
  miniapp:
    appId: xxxxxxx
    secret: xxxxxxxx
    Java代码

配置类

@Data
@Configuration
@ConditionalOnClass(WxMaService.class)
@ConfigurationProperties(prefix ="wx.miniapp")
public class WxMaConfiguration {

    private String appId;
    private String secret;

    @Bean
    @ConditionalOnMissingBean(WxMaService.class)
    public WxMaService wxMaService() {
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(this.getAppId());
        config.setSecret(this.getSecret());
        WxMaService service = new WxMaServiceImpl();
        service.setWxMaConfig(config);
        return service;
    }
}

工具类

@Slf4j
@Component
public class WxUtil {
    @Autowired
    private AlarmService alarmService; //业务类,用于保存小程序用户的openid
    @Autowired
    WxMaService wxMaService;
    
    public void saveOpenId(Long userId, String jsCode) {
        try {
            WxMaJscode2SessionResult code2SessionInfo = wxMaService.jsCode2SessionInfo(jsCode);
            if (code2SessionInfo != null) {
                alarmService.addWechatLoginInfo(userId, code2SessionInfo.getOpenid(),
                        code2SessionInfo.getSessionKey(), LocalDateTime.now());
            }
        } catch (Exception e) {
            log.error("保存微信登录信息发生错误错误~ [userId=" + userId + ", jsCode=" + jsCode, e);
        }
    }
}

登录请求对象
小程序在发起登录请求时,要提前获取jsCode,然后传给后端

public class WxLoginBody
{
    
    private String username;

    
    private String password;

    
    private String jsCode;
	
	// set get方法略
}

后端登录接口
验证成功,返回token信息

    
    @ApiOperation("用户登录")
    @PostMapping("/wx/login")
    public AjaxResult wxLogin(@RequestBody WxLoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getJsCode());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }
二. uniapp
    login() {
      uni.login({
        provider: 'weixin',
          success: function (loginRes) {
            
            uni.request({
                url: 'http://xxxxx/wx/login', //服务端地址
                method:"POST",
                data: {
                    "username": "用户名",
                    "password": "密码",
                    "jsCode": loginRes.code
                },
                header: {
                    'custom-header': 'hello' //自定义请求头信息
                },
                success: (res) => {
                    console.log(res);
                }
            });
            
          }
      })
    }

后续持续更新。。。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/763569.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号