admin
2024-09-05 ab35ac8b769b2d9816dffb33a64f2c6f7bd5dd6e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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<Date>() {
                        @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("");
    }
 
 
}