/* * ************************************************* * Copyright (c) 2018 QMTH. All Rights Reserved. * Created by Deason on 2018-07-16 17:48:37. * ************************************************* */ package cn.com.qmth.examcloud.app.service; import cn.com.qmth.examcloud.app.model.Constants; import cn.com.qmth.examcloud.app.model.ResBody; import cn.com.qmth.examcloud.app.model.Result; import cn.com.qmth.examcloud.app.model.UserInfo; import cn.com.qmth.examcloud.app.utils.HttpBuilder; import cn.com.qmth.examcloud.app.utils.HttpUtils; import cn.com.qmth.examcloud.app.utils.JsonMapper; import okhttp3.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.Assert; import java.util.HashMap; import java.util.Map; import static cn.com.qmth.examcloud.app.model.Constants.PARAM_KEY; import static cn.com.qmth.examcloud.app.model.Constants.PARAM_TOKEN; /** * 认证中心业务服务接口 */ @Service public class UserAuthService { private static Logger log = LoggerFactory.getLogger(UserAuthService.class); @Autowired private PropertyService propertyService; public Result login(String account, String password, String accountType, Long rootOrgId, String domain) throws Exception { //封装请求参数 final String requestUrl = String.format("%s/api/ecs_core/auth/login", propertyService.getUserAuthUrl()); Map params = new HashMap<>(); params.put("accountValue", account); params.put("password", password); params.put("accountType", accountType); params.put("rootOrgId", rootOrgId != null ? rootOrgId.toString() : ""); params.put("domain", domain); String json = new JsonMapper().toJson(params); RequestBody formBody = FormBody.create(MediaType.parse(Constants.CHARSET_JSON_UTF8), json); Request request = new Request.Builder() .url(requestUrl) .post(formBody) .build(); //执行请求 Response response = HttpBuilder.client.getInstance().newCall(request).execute(); String bodyStr = response.body().string(); if (response.isSuccessful()) { JsonMapper mapper = new JsonMapper(); UserInfo userInfo = mapper.fromJson(bodyStr, UserInfo.class); return new Result().success(userInfo); } else { log.warn("Http response is " + bodyStr); ResBody body = new JsonMapper().fromJson(bodyStr, ResBody.class); if (body != null && body.getCode() != null) { return new Result().error(body.getDesc()); } return new Result().error(bodyStr); } } public Result logout(String key, String token) throws Exception { //封装请求参数 final String requestUrl = String.format("%s/api/ecs_core/auth/logout", propertyService.getUserAuthUrl()); RequestBody formBody = new FormBody.Builder() .add(PARAM_KEY, key) .add(PARAM_TOKEN, token) .build(); return HttpUtils.doPost(requestUrl, formBody, key, token); } public Result getUserInfo(String key, String token) throws Exception { //封装请求参数 final String requestUrl = String.format("%s/api/ecs_core/auth/getLoginUser", propertyService.getUserAuthUrl()); RequestBody formBody = new FormBody.Builder() .add(PARAM_KEY, key) .add(PARAM_TOKEN, token) .build(); //todo 待补充'头像、手机号、年级'字段 return HttpUtils.doPost(requestUrl, formBody, key, token); } public Result updatePassword(String key, String token, Long userId, String password) throws Exception { Assert.notNull(userId, "UserId must be not null."); //封装请求参数 final String requestUrl = String.format("%s/api/ecs_core/user/password", propertyService.getUserAuthUrl()); RequestBody formBody = new FormBody.Builder() .add("userId", userId.toString()) .add("password", password) .build(); return HttpUtils.doPut(requestUrl, formBody, key, token); } public Result userBindingPhone(String key, String token, Long userId, String phone, String code) { //todo return new Result().error(); } public Result loginByPhone(String phone, String password) { //todo return new Result().error(); } }