提交 585fdb6b authored 作者: 李秋林's avatar 李秋林

后台接口获取小程序openid\phone\accessToken

上级 b2442f74
......@@ -7,6 +7,8 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
/**
* @author : liqiulin
* @date : 2025-03-07 13
......@@ -20,11 +22,23 @@ public class MiappInfoController {
private TencentUtils tencentUtils;
/**
* 获取小程序 - 小卤通信息
* 获取小程序信息
* @return
*/
@GetMapping("/xlt")
public R getXLT() {
return R.success(tencentUtils.getXltMini());
@GetMapping("/wxtoken")
public R getCX() {
Map<String, String> cxInfo = tencentUtils.getCX();
return R.success(tencentUtils.getMiniappToken(cxInfo));
}
@GetMapping("/phone")
public R getPhone(String accessToken,String code){
return R.success(tencentUtils.getUserPhoneByAccessToken(accessToken,code));
}
@GetMapping("/openid")
public R getOpenid(String jsCode){
Map<String, String> cxInfo = tencentUtils.getCX();
return R.success(tencentUtils.getOpenid(jsCode,cxInfo));
}
}
package com.wangxiaolu.promotion.utils;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
......@@ -11,6 +13,7 @@ import com.tencentcloudapi.sms.v20210111.models.SendStatus;
import com.wangxiaolu.promotion.exception.ParamException;
import com.wangxiaolu.promotion.result.basedata.RCode;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import org.apache.commons.lang3.StringUtils;
import org.apache.groovy.util.Maps;
import org.springframework.beans.factory.annotation.Value;
......@@ -52,12 +55,12 @@ public class TencentUtils {
private long overdueLong;
// 小卤通信息
@Value("${wx.xlt_miniapp.app_id}")
@Value("${wx.cx_miniapp.app_id}")
private String xltAppId;
@Value("${wx.xlt_miniapp.app_secret}")
@Value("${wx.cx_miniapp.app_secret}")
private String xltAppSecret;
public Map<String,String> getXltMini() {
public Map<String,String> getCX() {
return Maps.of("appId", xltAppId, "appSecret", xltAppSecret);
}
/**
......@@ -139,4 +142,25 @@ public class TencentUtils {
}
}
public JSONObject getMiniappToken(Map<String, String> miniappInfo) {
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+miniappInfo.get("appId")+"&secret="+miniappInfo.get("appSecret");
String body = HttpUtil.createGet(url).execute().body();
JSONObject jsonObject = JSONObject.parseObject(body);
jsonObject.putAll(miniappInfo);
return jsonObject;
}
public JSONObject getUserPhoneByAccessToken(String accessToken,String code) {
JSONObject param = new JSONObject();
param.put("code",code);
String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token="+accessToken;
String body = HttpUtil.createPost(url).body(param.toString()).execute().body();
return JSONObject.parseObject(body);
}
public JSONObject getOpenid(String jsCode,Map<String, String> miniappInfo) {
String url = "https://api.weixin.qq.com/sns/jscode2session?appid="+miniappInfo.get("appId")+"&secret="+miniappInfo.get("appSecret")+"&js_code="+jsCode+"&grant_type=authorization_code";
String body = HttpUtil.createGet(url).execute().body();
return JSONObject.parseObject(body);
}
}
......@@ -44,7 +44,7 @@ wx:
token_secret: sXwHPj#U#xmim^ts
employee:
token_secret: xZq%kXASSPD$UtL
xlt_miniapp:
cx_miniapp:
app_id: wxac14dc7765484d7d
app_secret: b3315e66608a397f88e7e82b8f950bcd
......
......@@ -43,7 +43,7 @@ wx:
token_secret: sXwHPj#U#xmim^ts
employee:
token_secret: xZq%kXASSPD$UtL
xlt_miniapp:
cx_miniapp:
app_id: wxac14dc7765484d7d
app_secret: b3315e66608a397f88e7e82b8f950bcd
tengxunyun:
......
......@@ -44,7 +44,7 @@ wx:
token_secret: sXwHPj#U#xmim^ts
employee:
token_secret: xZq%kXASSPD$UtL
xlt_miniapp:
cx_miniapp:
app_id: wxac14dc7765484d7d
app_secret: b3315e66608a397f88e7e82b8f950bcd
tengxunyun:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论