支付宝账号登陆小程序
<h3>修改application.yml文件</h3>
<p><img src="https://www.showdoc.cc/server/api/common/visitfile/sign/50aa1a4f10deb85bc2428556f1f91c4f?showdoc=.jpg" alt="" /></p>
<h3>修改ZfbController中的值注入</h3>
<pre><code class="language-java">@Value("${application.alipay.microApp.app-id}")
private String microApp_appId;
@Value("${application.alipay.microApp.public-key}")
private String microApp_publicKey;
@Value("${application.alipay.microApp.private-key}")
private String microApp_privateKey;
@Value("${application.alipay.app.app-id}")
private String app_appId;
@Value("${application.alipay.app.public-key}")
private String app_publicKey;
@Value("${application.alipay.app.private-key}")
private String app_privateKey;
@Value("${application.alipay.gateway}")
private String gateway;</code></pre>
<h3>编写ZfbLoginForm.java</h3>
<pre><code class="language-java">@Data
@ApiModel(value = "支付宝登录表单")
public class ZfbLoginForm {
@ApiModelProperty(value = "临时登陆凭证")
@NotBlank(message="临时登陆凭证不能为空")
private String authCode;
@ApiModelProperty(value = "昵称")
@NotBlank(message="昵称不能为空")
private String nickname;
@ApiModelProperty(value = "头像URL")
@NotBlank(message="头像URL不能为空")
private String photo;
}</code></pre>
<h3>编写ZfbController.java中的登陆方法</h3>
<pre><code class="language-java">@PostMapping("login")
@ApiOperation("登录")
public R login(@RequestBody ZfbLoginForm form) {
//表单校验
ValidatorUtils.validateEntity(form);
AlipayClient client = new DefaultAlipayClient(gateway, microApp_appId,
microApp_privateKey, "json", "UTF-8", microApp_publicKey,
"RSA2");
AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
request.setGrantType("authorization_code");
request.setCode(form.getAuthCode());
try {
AlipaySystemOauthTokenResponse response = client.execute(request);
if (response.isSuccess()) {
String userId = response.getUserId();
UserEntity user = new UserEntity();
user.setOpenId(userId);
QueryWrapper wrapper = new QueryWrapper(user);
int count = userService.count(wrapper);
if (count == 0) {
user.setNickname(form.getNickname());
user.setPhoto(form.getPhoto());
user.setType(2);
user.setCreateTime(new Date());
userService.save(user);
}
user = new UserEntity();
user.setOpenId(userId);
wrapper = new QueryWrapper(user);
user = userService.getOne(wrapper);
long id = user.getUserId();
String token = jwtUtils.generateToken(id);
Map<String, Object> result = new HashMap<>();
result.put("token", token);
result.put("expire", jwtUtils.getExpire());
return R.ok(result);
} else {
return R.error("登陆授权失败");
}
} catch (Exception e) {
e.printStackTrace();
return R.error("登陆授权失败");
}
}</code></pre>
<h3>编写小程序登陆代码</h3>
<pre><code class="language-js">my.getAuthCode({
scopes: "auth_user",
success: function(resp) {
let authCode = resp.authCode
my.getAuthUserInfo({
success: function(resp) {
let nickName = resp.nickName
let avatar = resp.avatar
uni.request({
url: that.url.zfb.login,
method: "POST",
data: {
"authCode": authCode,
"nickname": nickName,
"photo": avatar
},
success: function(resp) {
console.log(resp)
let token = resp.data.token
let expire = resp.data.expire
console.log(token)
uni.setStorageSync("token", token)
uni.setStorageSync("expire", expire)
uni.switchTab({
url: "../index/index"
})
}
})
}
})
}
})</code></pre>