wx.login(Object object)
调用接口获取登录凭证(code)。通过凭证进而换取用户登录态信息,包括用户的唯一标识(openid)及本次登录的会话密钥(session_key)等。用户数据的加解密通讯需要依赖会话密钥完成。更多使用方法详见 小程序登录。
参数
Object object
属性 | 类型 | 默认值 | 必填 | 说明 | 最低版本 |
---|---|---|---|---|---|
timeout | number | 否 | 超时时间,单位ms | 1.9.90 | |
success | function | 否 | 接口调用成功的回调函数 | ||
fail | function | 否 | 接口调用失败的回调函数 | ||
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
object.success 回调函数
参数
Object res
属性 | 类型 | 说明 |
---|---|---|
code | string | 用户登录凭证(有效期五分钟)。开发者需要在开发者服务器后台调用 auth.code2Session,使用 code 换取 openid 和 session_key 等信息 |
示例代码
wx.login({ success (res) { if (res.code) { //发起网络请求 wx.request({ url: 'https://test.com/onLogin', data: { code: res.code } }) } else { console.log('登录失败!' + res.errMsg) } } })
https://developers.weixin.qq.com/miniprogram/dev/api/open-api/login/wx.login.html
@GetMapping("/login") public String login(@PathVariable String appid, String code) { if (StringUtils.isBlank(code)) { return "empty jscode"; } final WxMaService wxService = WxMaConfiguration.getMaService(appid); try { WxMaJscode2SessionResult session = wxService.getUserService().getSessionInfo(code); this.logger.info(session.getSessionKey()); this.logger.info(session.getOpenid()); //TODO 可以增加自己的逻辑,关联业务相关数据 return JsonUtils.toJson(session); } catch (WxErrorException e) { this.logger.error(e.getMessage(), e); return e.toString(); } }
https://github.com/binarywang/weixin-java-miniapp-demo.git
https://github.com/NervJS/taro
import java.util.Date; import lombok.Data; @Data public class MpKfMsgPO { private Integer id; /** * 业务类型 */ private Byte bizType; /** * 业务ID */ private Integer bizId; /** * 消息使用的公众号Appid */ private String appid; /** * 是否是第一欢迎语. 0:不是;1:是 */ private Boolean first; /** * 消息延迟发送的分钟数 */ private Integer delayMinute; /** * 素材类型:0=文本类型,1=图片类型,2=语音类型,3=视频类型,5=图文类型 6=模板消息类型(非微信类型,兼容定时客服消息自定义的类型) */ private String msgType; /** * 图片、语音、视频标题 */ private String title; /** * 文本类型存储文字内容 */ private String content; /** * 存储在微信上素材的mediaId */ private String mediaId; /** * 图片、语音、视频素材url */ private String url; /** * 图文消息素材返回的json */ private String newsItem; /** * 是否使用小程序0:不使用,1:使用 */ private Boolean useMiniprogram; /** * 小程序AppId */ private String miniprogramPageAppid; /** * 小程序url */ private String miniprogramPageUrl; /** * 小程序title */ private String miniprogramPageTitle; /** * 删除标记 (0删除,1正常) */ private Boolean status; }