獲取appId java
添加獲取會員信息功能列表
步驟 :開發管理 -> 功能列表 -> 添加功能 -> 勾選會員信息 ->確認,效果如圖: spring
設置密鑰小程序
@Data
@Component
@ConfigurationProperties(prefix = "alipaytest.ali.xcx")
public class AliXcxProperty {
private String serverUrl;
private String appId;
private String privateKey;
private String publicKey;
}
複製代碼
@Controller
public class AliAuthController {
@Autowired
private AliAuthService aliAuthService;
@GetMapping("/auth")
@ResponseBody
public AlipayUserInfoShareResponse auth(@RequestParam(value="authCode") String authCode){
System.out.println(authCode);
AlipayUserInfoShareResponse auth = aliAuthService.auth(authCode);
return auth;
}
}
複製代碼
@Slf4j
@Service
public class AliAuthService {
@Resource
private AliXcxProperty aliXcxProperty;
public AlipayUserInfoShareResponse auth(String authcode) {
AlipayClient alipayClient = new DefaultAlipayClient(aliXcxProperty.getServerUrl(),
aliXcxProperty.getAppId(),
aliXcxProperty.getPrivateKey(),
"JSON", "utf-8", aliXcxProperty.getPublicKey(), "RSA2");
//獲取uid&token
AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
request.setGrantType("authorization_code");//值爲authorization_code時,表明用code換取;值爲refresh_token時,表明用refresh_token換取
request.setCode(authcode);
try {
AlipaySystemOauthTokenResponse response = alipayClient.execute(request);
if (response.isSuccess()) {
log.info("auth success [{}]", JSONObject.toJSONString(response));
AlipayUserInfoShareRequest alipayUserInfoShareRequest = new AlipayUserInfoShareRequest();
AlipayUserInfoShareResponse alipayUserInfoShareResponse = alipayClient.execute(alipayUserInfoShareRequest, response.getAccessToken());
if (alipayUserInfoShareResponse.isSuccess()) {
log.info("AlipayUserInfoShareResponse success [{}]", JSONObject.toJSONString(alipayUserInfoShareResponse));
} else {
log.info("AlipayUserInfoShareResponse fail [{}]", JSONObject.toJSONString(alipayUserInfoShareResponse));
}
return alipayUserInfoShareResponse;
} else {
log.info("auth fail [{}]", JSONObject.toJSONString(response));
}
} catch (AlipayApiException e) {
log.error("auth exception", e);
}
return null;
}
}
複製代碼
const app = getApp();
Page({
data: {
userInfo:{}, //存放用戶信息
userLogin:false, //判斷當前是否登陸
userNotLogin:true //判斷當前是否登陸
},
onLoad() {
var me = this;
var userInfo = app.getGlobalUserInfo(); //在全局緩存中獲取用戶信息
if(userInfo!=null&&userInfo!=undefined){ //有則不發起受權登陸
me.setData({
userInfo:userInfo,
userLogin:true,
userNotLogin:false
})
return;
}
//無緩存則發起受權登陸,第一步先經過小程序api獲取受權碼authCode
my.getAuthCode({
scopes:"auth_user",
success: (res) => {
if(res!=null&&res!=undefined){
console.log(res.authCode);
//第一步成功則調用後端接口,並傳入authCode
my.request({
url:app.myServerUrl+"/auth?authCode="+res.authCode,
success(res){
console.log(res.data);
//成功則賦值到data數據域
me.setData({
userInfo : res.data,
userLogin:true,
userNotLogin:false,
})
//存入到緩存中
app.setGlobalUserInfo(res.data);
},
})
}
},
});
},
login(){
this.onLoad();
},
});
複製代碼
App({
myServerUrl:"https://app2134991587test.mapp-test.xyz", //線上雲服務端的地址
localhostServerUrl:"http://127.0.0.1:8080", //本地springboot開啓內嵌的tomcat的地址,用於本地測試。
//從本地緩存中獲取全局的用戶對象
getGlobalUserInfo(){
var golbalUserInfo = my.getStorageSync({
key: 'globalUserInfo', // 緩存數據的key
}).data;
return golbalUserInfo;
},
setGlobalUserInfo(golbalUserInfo){
my.setStorageSync({
key: 'globalUserInfo', // 緩存數據的key
data: golbalUserInfo, // 要緩存的數據
});
},
});
複製代碼
測試結果以下 : 後端
下次再次進入不會彈框受權,若有須要,將用戶受權信息刪除掉便可,以下: api