package com.yeshi.buwan.controller.api; import com.google.gson.*; import com.yeshi.buwan.controller.parser.UserParser; import com.yeshi.buwan.domain.user.LoginUser; import com.yeshi.buwan.domain.user.LoginUserExtra; import com.yeshi.buwan.domain.vip.UserVIPInfo; import com.yeshi.buwan.service.inter.LoginUserService; import com.yeshi.buwan.service.inter.vip.VIPService; import com.yeshi.buwan.util.JsonUtil; import com.yeshi.buwan.util.StringUtil; import com.yeshi.buwan.util.ThreadUtil; import com.yeshi.buwan.util.factory.vo.UserInfoVOFactory; import com.yeshi.buwan.vo.AcceptData; import com.yeshi.buwan.vo.client.user.UserInfoVO; import org.json.JSONObject; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import javax.servlet.http.HttpSession; import java.lang.reflect.Type; import java.text.DateFormat; import java.util.Date; @Controller @RequestMapping("api/v2/user") public class UserController { @Resource private LoginUserService loginUserService; @Resource private VIPService vipService; @RequestMapping("getUserInfo") @ResponseBody public String getUserInfo(AcceptData acceptData, String loginUid) { LoginUser loginUser = loginUserService.getLoginUser(loginUid); if (loginUser == null) { return JsonUtil.loadFalseJson("用户不存在"); } LoginUserExtra extra = loginUserService.getExtra(loginUid); //初始化用户信息 if (extra == null) extra = loginUserService.initExtra(loginUid); //是否有pptvuid if (extra != null && StringUtil.isNullOrEmpty(extra.getPptvUid())) { extra = loginUserService.initPPTVUid(extra.getId()); } //需要初始化VIP信息 vipService.initUserVipInfo(loginUid); UserVIPInfo vipInfo = vipService.getVIPInfo(loginUid); UserInfoVO vo = UserInfoVOFactory.create(loginUser, extra, vipInfo); //线程执行设备信息更新 ThreadUtil.run(new Runnable() { @Override public void run() { loginUserService.setUtdId(loginUid, acceptData.getUtdId()); } }); if ("ios".equalsIgnoreCase(acceptData.getPlatform())) { if(StringUtil.isNullOrEmpty(vo.getBirthday())){ vo.setBirthday(loginUser.getBirthday()); } if(StringUtil.isNullOrEmpty(vo.getSex())){ vo.setSex(loginUser.getSex()); } if(StringUtil.isNullOrEmpty(vo.getSign())){ vo.setSign(loginUser.getSign()); } Gson gson = new GsonBuilder().enableComplexMapKeySerialization() .setDateFormat(DateFormat.LONG).registerTypeAdapter(Date.class, new JsonSerializer() { @Override public JsonElement serialize(Date value, Type theType, JsonSerializationContext context) { if (value == null) { return new JsonPrimitive("0"); } else { return new JsonPrimitive(value.getTime()); } } }).setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)// 会把字段首字母大写 .setPrettyPrinting().setVersion(1.0).create(); net.sf.json.JSONObject root= net.sf.json.JSONObject.fromObject(gson.toJson(vo)); root.put("Nickname",vo.getNickName()); return JsonUtil.loadTrueJson(root.toString()); } else { return JsonUtil.loadTrueJson(new Gson().toJson(vo)); } } @RequestMapping("updateUserInfo") @ResponseBody public String updateUserInfo(AcceptData acceptData, String portrait, String loginUid, String nickName, String birthday, String sex, String personSign, HttpSession session) { LoginUser loginUser = new LoginUser(); LoginUserExtra extra = new LoginUserExtra(); loginUser.setId(loginUid); extra.setId(loginUid); if (!StringUtil.isNullOrEmpty(portrait)) { loginUser.setPortrait(UserParser.savePortrait(portrait, session)); } if (!StringUtil.isNullOrEmpty(nickName)) { loginUser.setName(nickName); } if (!StringUtil.isNullOrEmpty(birthday)) { loginUser.setBirthday(birthday); extra.setBirthday(birthday); } if (!StringUtil.isNullOrEmpty(sex)) { loginUser.setSex(sex); extra.setSex(sex); } if (!StringUtil.isNullOrEmpty(personSign)) { loginUser.setSign(personSign); extra.setSign(personSign); } loginUserService.updateSelectiveByPrimaryKey(loginUser); loginUserService.updateSelectiveByPrimaryKey(extra); return JsonUtil.loadTrueJson(""); } }