提交 64c7a533 authored 作者: 李秋林's avatar 李秋林

微信小程序快速获取手机号组件防刷(每天最多可以获取5次)

上级 d2fe551d
package com.wangxiaolu.promotion.controller.user.tem;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.wangxiaolu.promotion.common.redis.RedisKeys;
import com.wangxiaolu.promotion.common.redis.service.RedisCache;
import com.wangxiaolu.promotion.exception.FlowException;
import com.wangxiaolu.promotion.result.basedata.R;
import com.wangxiaolu.promotion.result.basedata.RCode;
import com.wangxiaolu.promotion.utils.TencentUtils;
import org.springframework.beans.factory.annotation.Autowired;
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;
import java.util.Objects;
/**
* @author : liqiulin
......@@ -15,28 +21,53 @@ import java.util.Map;
* @describe :
*/
@RestController
@RequestMapping("/wechat/miniapp/")
@RequestMapping("/wechat/miniapp")
public class MiappInfoController {
@Autowired
private TencentUtils tencentUtils;
@Autowired
private RedisCache redisCache;
/**
* 获取小程序信息
* @return
* 获取小程序信息 access_token
*/
@GetMapping("/wxtoken")
public R getCX() {
return R.success(tencentUtils.getMiniappToken());
}
// @GetMapping("/wxtoken")
// public R getCX() {
// return R.success(tencentUtils.getMiniappToken());
// }
@GetMapping("/phone")
public R getPhone(String accessToken,String code){
return R.success(tencentUtils.getUserPhoneByAccessToken(accessToken,code));
public R getPhone(String openId, String code) {
// 检测当前openId获取phone的次数
checkPhone(openId);
JSONObject tokenJson = tencentUtils.getMiniappToken();
JSONObject phoneJson = tencentUtils.getUserPhoneByAccessToken(tokenJson.getString("access_token"), code);
return R.success(phoneJson);
}
@GetMapping("/openid")
public R getOpenid(String jsCode){
public R getOpenid(String jsCode) {
return R.success(tencentUtils.getOpenid(jsCode));
}
private synchronized void checkPhone(String openId) {
String key = RedisKeys.TemporaryKeys.TEMPORARY_LOGIN_OPENID_COUNT.getKey();
key = key + DateUtil.today() + "_" + openId;
String countStr = redisCache.get(key);
if (Objects.isNull(countStr)) {
countStr = String.valueOf(1);
redisCache.addToMinute(key, countStr, 1440);
return;
}
Integer count = Integer.valueOf(countStr);
count +=1;
if (count >= 6) {
throw new FlowException(RCode.LOGIN_PHONE_COUNT_MUCH);
}
countStr = String.valueOf(count);
redisCache.addToMinute(key, countStr, 1440);
}
}
......@@ -13,14 +13,10 @@ 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;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* @author : liqiulin
* @date : 2024-04-15 15
......@@ -60,9 +56,6 @@ public class TencentUtils {
@Value("${wx.cx_miniapp.app_secret}")
private String xltAppSecret;
public Map<String,String> getCX() {
return Maps.of("appId", xltAppId, "appSecret", xltAppSecret);
}
/**
* 给指定手机号发送验证码
*
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论