|
@@ -0,0 +1,145 @@
|
|
|
+package com.zzys.lightting.user.rest;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.zzys.lightting.base.rest.BaseController;
|
|
|
+import com.zzys.lightting.user.dao.model.UserInfo;
|
|
|
+import com.zzys.lightting.user.dao.model.UserPhotoInfo;
|
|
|
+import com.zzys.lightting.user.service.UserPhotoService;
|
|
|
+import com.zzys.lightting.user.service.UserService;
|
|
|
+import com.zzys.lightting.utils.Constant;
|
|
|
+import com.zzys.lightting.utils.JWTUtil;
|
|
|
+import com.zzys.lightting.utils.Result;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import javax.crypto.BadPaddingException;
|
|
|
+import javax.crypto.Cipher;
|
|
|
+import javax.crypto.IllegalBlockSizeException;
|
|
|
+import javax.crypto.NoSuchPaddingException;
|
|
|
+import javax.crypto.spec.IvParameterSpec;
|
|
|
+import javax.crypto.spec.SecretKeySpec;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.security.*;
|
|
|
+import java.security.spec.InvalidParameterSpecException;
|
|
|
+import java.util.Arrays;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Author:zzf
|
|
|
+ * @Date:2024/5/22:11:32
|
|
|
+ * @Description:
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@RestController
|
|
|
+@RequestMapping("/user")
|
|
|
+@Api(tags = "用户管理")
|
|
|
+public class UserController extends BaseController {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserService userService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserPhotoService userPhotoService;
|
|
|
+
|
|
|
+ @Value("${weixin.appid}")
|
|
|
+ private String appid;
|
|
|
+
|
|
|
+ @Value("${weixin.secret}")
|
|
|
+ private String secret;
|
|
|
+
|
|
|
+ @ApiOperation("快速登录")
|
|
|
+ @PostMapping(value = "/login")
|
|
|
+ public Result login(@RequestBody String json) throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, InvalidParameterSpecException, BadPaddingException, NoSuchProviderException, InvalidKeyException {
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(json);
|
|
|
+ String encryptedData = jsonObject.getString("encryptedData");
|
|
|
+ String iv = jsonObject.getString("iv");
|
|
|
+ String code = jsonObject.getString("code");
|
|
|
+ String photoPath = jsonObject.getString("photoPath");
|
|
|
+ String userName = jsonObject.getString("userName");
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
+ UserPhotoInfo userPhotoEntity ;
|
|
|
+ String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret + "&js_code=" + code + "&grant_type=authorization_code";
|
|
|
+ ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
|
|
|
+ jsonObject = JSONObject.parseObject(responseEntity.getBody());
|
|
|
+ int errcode = jsonObject.getIntValue("errcode");
|
|
|
+ if(errcode == 40029){
|
|
|
+ return fail(null,"临时code无效!");
|
|
|
+ }else if(errcode == 45011){
|
|
|
+ return fail(null,"调用太频繁,请稍后再试!");
|
|
|
+ } else if (errcode == 40226) {
|
|
|
+ return fail(null,"高风险等级用户,小程序登录拦截!");
|
|
|
+ } else if (errcode == -1) {
|
|
|
+ return fail(null,"系统繁忙");
|
|
|
+ }
|
|
|
+ UserInfo userInfo = userService.findByOpenId(jsonObject.getString("openid"));
|
|
|
+ //用户存在
|
|
|
+ if(userInfo != null){
|
|
|
+ userPhotoEntity = userPhotoService.findByUserId(userInfo.getId());
|
|
|
+ if(userPhotoEntity!=null){
|
|
|
+ userPhotoEntity.setPath(photoPath);
|
|
|
+ userInfo.setPhotoPath(photoPath);
|
|
|
+ }
|
|
|
+ }else {//新用户
|
|
|
+ userInfo = new UserInfo();
|
|
|
+ userInfo.setPhone(getPhoneNumber(encryptedData,jsonObject,iv));
|
|
|
+ userInfo.setOpenId(jsonObject.getString("openid"));
|
|
|
+ userInfo.setType(Constant.USER_TYPE_C);
|
|
|
+ userPhotoEntity = new UserPhotoInfo();
|
|
|
+ userPhotoEntity.setPath(photoPath);
|
|
|
+ }
|
|
|
+ userInfo.setName(userName);
|
|
|
+ userInfo = this.userService.save(userInfo);
|
|
|
+ userPhotoEntity.setUserId(userInfo.getId());
|
|
|
+ this.userPhotoService.save(userPhotoEntity);
|
|
|
+ JSONObject resultJson = new JSONObject();
|
|
|
+ resultJson.put("userInfo",userInfo);
|
|
|
+ resultJson.put("userPhoto",userPhotoEntity);
|
|
|
+ resultJson.put("token", JWTUtil.generateToken(userInfo));
|
|
|
+ return success(resultJson,"登录成功");
|
|
|
+ }
|
|
|
+ @ApiOperation(value = "管理端登录",notes = "userName:用户名;password:密码")
|
|
|
+ @PostMapping("/blogin")
|
|
|
+ public Result blogin(@RequestBody String json){
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(json);
|
|
|
+ UserInfo userInfo = this.userService.login(jsonObject.getString("userName"),jsonObject.getString("password"));
|
|
|
+ if(userInfo == null){
|
|
|
+ return fail("用户名或密码错误");
|
|
|
+ }else {
|
|
|
+ return success(JWTUtil.generateToken(userInfo));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ String getPhoneNumber(String encryptedData,JSONObject jsonObject,String iv) throws NoSuchPaddingException, NoSuchAlgorithmException, NoSuchProviderException, InvalidParameterSpecException, InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
|
|
|
+ encryptedData = encryptedData.replaceAll(" ","+");
|
|
|
+ byte[] dataByte = org.bouncycastle.util.encoders.Base64.decode(encryptedData);
|
|
|
+ byte[] keyByte = org.bouncycastle.util.encoders.Base64.decode(jsonObject.getString("session_key"));
|
|
|
+ byte[] ivByte = org.bouncycastle.util.encoders.Base64.decode(iv);
|
|
|
+ int base = 16;
|
|
|
+ if(keyByte.length%base!=0){
|
|
|
+ int groups = keyByte.length/base+1;
|
|
|
+ byte[] temp = new byte[groups*base];
|
|
|
+ Arrays.fill(temp, (byte) 0);
|
|
|
+ System.arraycopy(keyByte,0,temp,0,keyByte.length);
|
|
|
+ keyByte = temp;
|
|
|
+ }
|
|
|
+ // 初始化
|
|
|
+ Security.addProvider(new BouncyCastleProvider());
|
|
|
+ Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
|
|
|
+ SecretKeySpec spec = new SecretKeySpec(keyByte, "AES");
|
|
|
+ AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES");
|
|
|
+ parameters.init(new IvParameterSpec(ivByte));
|
|
|
+ cipher.init(Cipher.DECRYPT_MODE, spec, parameters);// 初始化
|
|
|
+ byte[] resultByte = cipher.doFinal(dataByte);
|
|
|
+ if (null != resultByte && resultByte.length > 0) {
|
|
|
+ String result = new String(resultByte, StandardCharsets.UTF_8);
|
|
|
+ return JSONObject.parseObject(result).getString("phoneNumber");
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|