NeteaseUserService.java 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package com.izouma.nineth.service.netease;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.github.kevinsawicki.http.HttpRequest;
  5. import com.izouma.nineth.domain.netease.NeteaseUser;
  6. import com.izouma.nineth.domain.User;
  7. import com.izouma.nineth.dto.PageQuery;
  8. import com.izouma.nineth.exception.BusinessException;
  9. import com.izouma.nineth.repo.NeteaseUserRepo;
  10. import com.izouma.nineth.repo.UserRepo;
  11. import com.izouma.nineth.utils.JpaUtils;
  12. import com.izouma.nineth.utils.netease.CheckSumBuilder;
  13. import lombok.AllArgsConstructor;
  14. import org.apache.commons.lang.RandomStringUtils;
  15. import org.springframework.data.domain.Page;
  16. import org.springframework.stereotype.Service;
  17. import java.sql.Timestamp;
  18. import java.time.LocalDateTime;
  19. import java.time.ZoneOffset;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. @Service
  23. @AllArgsConstructor
  24. public class NeteaseUserService {
  25. private NeteaseUserRepo neteaseUserRepo;
  26. private UserRepo userRepo;
  27. public Page<NeteaseUser> all(PageQuery pageQuery) {
  28. return neteaseUserRepo
  29. .findAll(JpaUtils.toSpecification(pageQuery, NeteaseUser.class), JpaUtils.toPageRequest(pageQuery));
  30. }
  31. public String httpPost(String url, String contentType, Map<String, Object> params) {
  32. Map<String, String> headers = new HashMap<>();
  33. String appKey = "872dd9d0a0f8eda25b579654745db459";
  34. String nonce = RandomStringUtils.randomAlphabetic(32);
  35. Timestamp timestamp = new Timestamp(LocalDateTime.now(ZoneOffset.UTC).toInstant(ZoneOffset.UTC)
  36. .getEpochSecond());
  37. String curTime = String.valueOf(timestamp.getTime());
  38. String appSecret = "24e63777e4bb";
  39. headers.put("AppKey", appKey);
  40. headers.put("Nonce", nonce);
  41. headers.put("CurTime", curTime);
  42. headers.put("CheckSum", CheckSumBuilder.getCheckSum(appSecret, nonce, curTime));
  43. return HttpRequest.post("https://api.netease.im/nimserver/" + url)
  44. .contentType(contentType)
  45. .headers(headers)
  46. .form(params)
  47. .body();
  48. }
  49. public NeteaseUser create(Long userId) {
  50. User user = userRepo.findById(userId).orElseThrow(new BusinessException("暂无用户信息"));
  51. String url = "user/create.action";
  52. String contentType = "application/x-www-form-urlencoded;charset=utf-8";
  53. Map<String, Object> params = new HashMap<>();
  54. params.put("accid", String.valueOf(user.getId()));
  55. params.put("name", user.getNickname());
  56. params.put("mobile", "+86-" + user.getPhone());
  57. params.put("icon", user.getAvatar());
  58. String result = httpPost(url, contentType, params);
  59. JSONObject jsonObject = JSON.parseObject(result);
  60. Integer code = jsonObject.getInteger("code");
  61. if (code != 200) {
  62. throw new BusinessException("注册出错,请核查后重新注册");
  63. }
  64. JSONObject info = jsonObject.getJSONObject("info");
  65. String accId = info.getString("accid");
  66. String name = info.getString("name");
  67. String token = info.getString("token");
  68. String icon = info.getString("icon");
  69. NeteaseUser neteaseUser = NeteaseUser.builder().accId(accId).userId(userId).name(name).token(token).icon(icon)
  70. .build();
  71. return neteaseUserRepo.save(neteaseUser);
  72. }
  73. public NeteaseUser updateToken(Long userId) {
  74. NeteaseUser record = neteaseUserRepo.findById(userId).orElseThrow(new BusinessException("该用户暂无注册信息"));
  75. String url = "user/update.refreshToken";
  76. String contentType = "application/x-www-form-urlencoded;charset=utf-8";
  77. Map<String, Object> params = new HashMap<>();
  78. params.put("accid", String.valueOf(userId));
  79. String result = httpPost(url, contentType, params);
  80. JSONObject jsonObject = JSON.parseObject(result);
  81. Integer code = jsonObject.getInteger("code");
  82. if (code != 200) {
  83. throw new BusinessException("注册出错,请核查后重新注册");
  84. }
  85. JSONObject info = jsonObject.getJSONObject("info");
  86. String token = info.getString("token");
  87. record.setToken(token);
  88. return neteaseUserRepo.save(record);
  89. }
  90. }