|
8 | 8 | import me.chanjar.weixin.common.error.WxErrorException;
|
9 | 9 | import org.apache.dubbo.config.annotation.DubboReference;
|
10 | 10 | import org.apache.http.util.TextUtils;
|
| 11 | +import org.dromara.common.core.constant.GlobalConstants; |
11 | 12 | import org.dromara.common.core.constant.HttpStatus;
|
12 | 13 | import org.dromara.common.core.domain.R;
|
13 | 14 | import org.dromara.common.core.exception.base.BaseException;
|
14 | 15 | import org.dromara.common.core.utils.ServletUtils;
|
| 16 | +import org.dromara.common.core.utils.TokenUtils; |
| 17 | +import org.dromara.common.json.utils.JsonUtils; |
15 | 18 | import org.dromara.common.redis.utils.RedisUtils;
|
16 | 19 | import org.dromara.omind.api.common.utils.ip.IpUtils;
|
17 | 20 | import org.dromara.omind.mp.constant.XcxRedisKey;
|
18 | 21 | import org.dromara.omind.mp.domain.request.MpInfoRequest;
|
19 | 22 | import org.dromara.omind.mp.domain.request.MpLoginRequest;
|
| 23 | +import org.dromara.omind.mp.domain.request.MpMobileLoginRequest; |
20 | 24 | import org.dromara.omind.mp.domain.request.MpPhoneRequest;
|
21 | 25 | import org.dromara.omind.mp.domain.vo.UserVo;
|
22 | 26 | import org.dromara.omind.mp.service.MpService;
|
@@ -184,4 +188,48 @@ public R userInfo(Long uid) {
|
184 | 188 | userVo.setUserInfo(userInfo);
|
185 | 189 | return R.ok(userVo);
|
186 | 190 | }
|
| 191 | + |
| 192 | + @Override |
| 193 | + public R<UserVo> mobileLogin(MpMobileLoginRequest mpMobileLoginRequest) { |
| 194 | + String key = GlobalConstants.CAPTCHA_CODE_KEY + mpMobileLoginRequest.getMobile(); |
| 195 | + if(!RedisUtils.hasKey(key)){ |
| 196 | + return R.fail("无效的验证码"); |
| 197 | + } |
| 198 | + String verCode = RedisUtils.getCacheObject(key).toString(); |
| 199 | + if(TextUtils.isBlank(verCode)){ |
| 200 | + return R.fail("无效的验证码"); |
| 201 | + } |
| 202 | + else if(!verCode.equals(mpMobileLoginRequest.getVerCode())){ |
| 203 | + return R.fail("无效的验证码"); |
| 204 | + } |
| 205 | + final String ip = IpUtils.getIpAddr(ServletUtils.getRequest()); |
| 206 | + //正确 开始登录/注册 |
| 207 | + OmindUserEntity omindUserEntity = userService.getUserByMobile(mpMobileLoginRequest.getMobile()); |
| 208 | + if(omindUserEntity == null){ |
| 209 | + //注册用户 |
| 210 | + omindUserEntity = new OmindUserEntity(); |
| 211 | + omindUserEntity.setMobile(mpMobileLoginRequest.getMobile()); |
| 212 | + omindUserEntity.setLastVisitIp(ip); |
| 213 | + omindUserEntity.setLastVisitTime(new Date()); |
| 214 | + if(!userService.addUser(omindUserEntity)){ |
| 215 | + return R.fail("用户注册失败"); |
| 216 | + } |
| 217 | + //补充默认值 |
| 218 | + omindUserEntity = userService.getUserByMobile(mpMobileLoginRequest.getMobile()); |
| 219 | + } |
| 220 | + else{ |
| 221 | + omindUserEntity.setLastVisitIp(ip); |
| 222 | + omindUserEntity.setLastVisitTime(new Date()); |
| 223 | + userService.updateById(omindUserEntity); |
| 224 | + } |
| 225 | + if(omindUserEntity.getDisableFlag() == 1){ |
| 226 | + throw new BaseException("账号被禁用,请联系管理员"); |
| 227 | + } |
| 228 | + String token = TokenUtils.generateToken(JsonUtils.toJsonString(omindUserEntity)); |
| 229 | + RedisUtils.setCacheObject(XcxRedisKey.USER_TOKEN + token, omindUserEntity.getUid(), Duration.ofDays(1)); |
| 230 | + UserVo userVo = new UserVo(); |
| 231 | + userVo.setToken(token); |
| 232 | + userVo.setUserInfo(omindUserEntity); |
| 233 | + return R.ok(userVo); |
| 234 | + } |
187 | 235 | }
|
0 commit comments