UserAuthService.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * *************************************************
  3. * Copyright (c) 2018 QMTH. All Rights Reserved.
  4. * Created by Deason on 2018-07-16 17:48:37.
  5. * *************************************************
  6. */
  7. package cn.com.qmth.examcloud.app.service;
  8. import cn.com.qmth.examcloud.app.model.Constants;
  9. import cn.com.qmth.examcloud.app.model.ResBody;
  10. import cn.com.qmth.examcloud.app.model.Result;
  11. import cn.com.qmth.examcloud.app.model.UserInfo;
  12. import cn.com.qmth.examcloud.app.utils.HttpBuilder;
  13. import cn.com.qmth.examcloud.app.utils.HttpUtils;
  14. import cn.com.qmth.examcloud.app.utils.JsonMapper;
  15. import okhttp3.*;
  16. import org.slf4j.Logger;
  17. import org.slf4j.LoggerFactory;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.stereotype.Service;
  20. import org.springframework.util.Assert;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23. import static cn.com.qmth.examcloud.app.model.Constants.PARAM_KEY;
  24. import static cn.com.qmth.examcloud.app.model.Constants.PARAM_TOKEN;
  25. /**
  26. * 认证中心业务服务接口
  27. */
  28. @Service
  29. public class UserAuthService {
  30. private static Logger log = LoggerFactory.getLogger(UserAuthService.class);
  31. @Autowired
  32. private PropertyService propertyService;
  33. public Result<UserInfo> login(String account, String password, String accountType, Long rootOrgId, String domain) throws Exception {
  34. //封装请求参数
  35. final String requestUrl = String.format("%s/api/ecs_core/auth/login", propertyService.getUserAuthUrl());
  36. Map<String, String> params = new HashMap<>();
  37. params.put("accountValue", account);
  38. params.put("password", password);
  39. params.put("accountType", accountType);
  40. params.put("rootOrgId", rootOrgId != null ? rootOrgId.toString() : "");
  41. params.put("domain", domain);
  42. String json = new JsonMapper().toJson(params);
  43. RequestBody formBody = FormBody.create(MediaType.parse(Constants.CHARSET_JSON_UTF8), json);
  44. Request request = new Request.Builder()
  45. .url(requestUrl)
  46. .post(formBody)
  47. .build();
  48. //执行请求
  49. Response response = HttpBuilder.client.getInstance().newCall(request).execute();
  50. String bodyStr = response.body().string();
  51. if (response.isSuccessful()) {
  52. JsonMapper mapper = new JsonMapper();
  53. UserInfo userInfo = mapper.fromJson(bodyStr, UserInfo.class);
  54. return new Result().success(userInfo);
  55. } else {
  56. log.warn("Http response is " + bodyStr);
  57. ResBody body = new JsonMapper().fromJson(bodyStr, ResBody.class);
  58. if (body != null && body.getCode() != null) {
  59. return new Result().error(body.getDesc());
  60. }
  61. return new Result().error(bodyStr);
  62. }
  63. }
  64. public Result logout(String key, String token) throws Exception {
  65. //封装请求参数
  66. final String requestUrl = String.format("%s/api/ecs_core/auth/logout", propertyService.getUserAuthUrl());
  67. RequestBody formBody = new FormBody.Builder()
  68. .add(PARAM_KEY, key)
  69. .add(PARAM_TOKEN, token)
  70. .build();
  71. return HttpUtils.doPost(requestUrl, formBody, key, token);
  72. }
  73. public Result getUserInfo(String key, String token) throws Exception {
  74. //封装请求参数
  75. final String requestUrl = String.format("%s/api/ecs_core/auth/getLoginUser", propertyService.getUserAuthUrl());
  76. RequestBody formBody = new FormBody.Builder()
  77. .add(PARAM_KEY, key)
  78. .add(PARAM_TOKEN, token)
  79. .build();
  80. //todo 待补充'头像、手机号、年级'字段
  81. return HttpUtils.doPost(requestUrl, formBody, key, token);
  82. }
  83. public Result updatePassword(String key, String token, Long userId, String password) throws Exception {
  84. Assert.notNull(userId, "UserId must be not null.");
  85. //封装请求参数
  86. final String requestUrl = String.format("%s/api/ecs_core/user/password", propertyService.getUserAuthUrl());
  87. RequestBody formBody = new FormBody.Builder()
  88. .add("userId", userId.toString())
  89. .add("password", password)
  90. .build();
  91. return HttpUtils.doPut(requestUrl, formBody, key, token);
  92. }
  93. public Result userBindingPhone(String key, String token, Long userId, String phone, String code) {
  94. //todo
  95. return new Result().error();
  96. }
  97. public Result loginByPhone(String phone, String password) {
  98. //todo
  99. return new Result().error();
  100. }
  101. }