| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- package com.izouma.nineth.service.netease;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import com.github.kevinsawicki.http.HttpRequest;
- import com.izouma.nineth.domain.netease.NeteaseUser;
- import com.izouma.nineth.domain.User;
- import com.izouma.nineth.dto.PageQuery;
- import com.izouma.nineth.exception.BusinessException;
- import com.izouma.nineth.repo.NeteaseUserRepo;
- import com.izouma.nineth.repo.UserRepo;
- import com.izouma.nineth.utils.JpaUtils;
- import com.izouma.nineth.utils.netease.CheckSumBuilder;
- import lombok.AllArgsConstructor;
- import org.apache.commons.lang.RandomStringUtils;
- import org.springframework.data.domain.Page;
- import org.springframework.stereotype.Service;
- import java.sql.Timestamp;
- import java.time.LocalDateTime;
- import java.time.ZoneOffset;
- import java.util.HashMap;
- import java.util.Map;
- @Service
- @AllArgsConstructor
- public class NeteaseUserService {
- private NeteaseUserRepo neteaseUserRepo;
- private UserRepo userRepo;
- public Page<NeteaseUser> all(PageQuery pageQuery) {
- return neteaseUserRepo
- .findAll(JpaUtils.toSpecification(pageQuery, NeteaseUser.class), JpaUtils.toPageRequest(pageQuery));
- }
- public String httpPost(String url, String contentType, Map<String, Object> params) {
- Map<String, String> headers = new HashMap<>();
- String appKey = "872dd9d0a0f8eda25b579654745db459";
- String nonce = RandomStringUtils.randomAlphabetic(32);
- Timestamp timestamp = new Timestamp(LocalDateTime.now(ZoneOffset.UTC).toInstant(ZoneOffset.UTC)
- .getEpochSecond());
- String curTime = String.valueOf(timestamp.getTime());
- String appSecret = "24e63777e4bb";
- headers.put("AppKey", appKey);
- headers.put("Nonce", nonce);
- headers.put("CurTime", curTime);
- headers.put("CheckSum", CheckSumBuilder.getCheckSum(appSecret, nonce, curTime));
- return HttpRequest.post("https://api.netease.im/nimserver/" + url)
- .contentType(contentType)
- .headers(headers)
- .form(params)
- .body();
- }
- public NeteaseUser create(Long userId) {
- User user = userRepo.findById(userId).orElseThrow(new BusinessException("暂无用户信息"));
- String url = "user/create.action";
- String contentType = "application/x-www-form-urlencoded;charset=utf-8";
- Map<String, Object> params = new HashMap<>();
- params.put("accid", String.valueOf(user.getId()));
- params.put("name", user.getNickname());
- params.put("mobile", "+86-" + user.getPhone());
- params.put("icon", user.getAvatar());
- String result = httpPost(url, contentType, params);
- JSONObject jsonObject = JSON.parseObject(result);
- Integer code = jsonObject.getInteger("code");
- if (code != 200) {
- throw new BusinessException("注册出错,请核查后重新注册");
- }
- JSONObject info = jsonObject.getJSONObject("info");
- String accId = info.getString("accid");
- String name = info.getString("name");
- String token = info.getString("token");
- String icon = info.getString("icon");
- NeteaseUser neteaseUser = NeteaseUser.builder().accId(accId).userId(userId).name(name).token(token).icon(icon)
- .build();
- return neteaseUserRepo.save(neteaseUser);
- }
- public NeteaseUser updateToken(Long userId) {
- NeteaseUser record = neteaseUserRepo.findById(userId).orElseThrow(new BusinessException("该用户暂无注册信息"));
- String url = "user/update.refreshToken";
- String contentType = "application/x-www-form-urlencoded;charset=utf-8";
- Map<String, Object> params = new HashMap<>();
- params.put("accid", String.valueOf(userId));
- String result = httpPost(url, contentType, params);
- JSONObject jsonObject = JSON.parseObject(result);
- Integer code = jsonObject.getInteger("code");
- if (code != 200) {
- throw new BusinessException("注册出错,请核查后重新注册");
- }
- JSONObject info = jsonObject.getJSONObject("info");
- String token = info.getString("token");
- record.setToken(token);
- return neteaseUserRepo.save(record);
- }
- }
|