admin
2022-05-12 fa705507ba574c857b1667553737d23b1b7ff495
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package com.ks.app.controller.client.api;
 
import com.ks.app.dto.user.LoginInfoDTO;
import com.ks.app.entity.APPPlatform;
import com.ks.app.entity.user.UserInfo;
import com.ks.app.entity.user.UserLoginRecord;
import com.ks.app.entity.vip.UserVIPInfo;
import com.ks.app.exception.user.LoginException;
import com.ks.app.exception.user.UserAccountException;
import com.ks.app.service.inter.user.UserAccountService;
import com.ks.app.service.inter.user.UserExtraInfoService;
import com.ks.app.service.inter.user.UserInfoService;
import com.ks.app.service.inter.vip.VIPService;
import com.ks.app.service.manager.PushManager;
import com.ks.app.service.manager.VerifyCodeManager;
import com.ks.app.utils.ApiCodeConstant;
import com.ks.app.utils.ImageUtil;
import com.ks.app.utils.annotation.UserLogin;
import com.ks.app.vo.AcceptData;
import com.ks.app.vo.user.UserInfoVO;
import com.ks.lib.common.exception.ParamsException;
import com.ks.push.exception.BPushDeviceTokenException;
import com.ks.push.pojo.DO.BPushDeviceToken;
import com.ks.push.pojo.DO.PushPlatform;
import com.ks.push.service.BDeviceTokenService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.yeshi.utils.IPUtil;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.StringUtil;
import org.yeshi.utils.ThreadUtil;
import org.yeshi.utils.entity.FileUploadResult;
import org.yeshi.utils.tencentcloud.COSManager;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
 
/**
 * @author hxh
 * @title: UserController
 * @description: 用户接口
 * @date 2021/11/16 17:37
 */
@Controller
@RequestMapping("api/v1/user")
public class UserController {
 
    @Resource
    private UserAccountService userAccountService;
 
    @Resource
    private UserInfoService userInfoService;
 
    @Resource
    private VIPService vipService;
 
    @Resource
    private VerifyCodeManager verifyCodeManager;
 
    @Resource
    private PushManager pushManager;
 
    @Resource
    private UserExtraInfoService userExtraInfoService;
 
    @ResponseBody
    @RequestMapping("loginPhone")
    public String loginPhone(AcceptData acceptData, String phone, String vcode, String token, HttpServletRequest request) {
        LoginInfoDTO loginInfo = new LoginInfoDTO();
        loginInfo.setIpInfo(IPUtil.getRemotIP(request) + ":" + request.getRemotePort());
        loginInfo.setLoginType(UserLoginRecord.TYPE_LOGIN_PHONE);
        if (!StringUtil.isNullOrEmpty(phone)) {
            if (StringUtil.isNullOrEmpty(vcode)) {
                return JsonUtil.loadFalseResult("请上传验证码");
            }
 
            loginInfo.setPhone(phone);
            loginInfo.setVcode(vcode);
 
        } else if (!StringUtil.isNullOrEmpty(token)) {
            loginInfo.setPhoneAuthInfo(token);
        } else {
            return JsonUtil.loadFalseResult("信息不完整");
        }
        loginInfo.setSystem(acceptData.getSystem());
        return login(loginInfo, acceptData);
    }
 
 
    @ResponseBody
    @RequestMapping("loginWX")
    public String loginWX(AcceptData acceptData, String code, HttpServletRequest request) {
        LoginInfoDTO loginInfo = new LoginInfoDTO();
        loginInfo.setIpInfo(IPUtil.getRemotIP(request) + ":" + request.getRemotePort());
        loginInfo.setLoginType(UserLoginRecord.TYPE_LOGIN_WX);
        if (StringUtil.isNullOrEmpty(code)) {
            return JsonUtil.loadFalseResult("信息不完整");
        }
        loginInfo.setWxCode(code);
        loginInfo.setSystem(acceptData.getSystem());
        return login(loginInfo, acceptData);
    }
 
    private String login(LoginInfoDTO loginInfo, AcceptData acceptData) {
        try {
            UserInfo userInfo = userAccountService.login(loginInfo);
            ThreadUtil.run(new Runnable() {
                @Override
                public void run() {
                    try {
                        //登录成功
                        pushManager.bindUid(acceptData.getSystem(), userInfo.getId(), acceptData.getPlatform() == APPPlatform.ios ? acceptData.getIdfa() : acceptData.getUtdId());
                    } catch (Exception e) {
 
                    }
                }
            });
 
            return outUserInfoForLogin(userInfo);
        } catch (LoginException e) {
            return JsonUtil.loadFalseResult(e.getMessage());
        }
    }
 
    private String outUserInfoForLogin(UserInfo userInfo) {
        UserInfoVO vo = UserInfoVO.create(userInfo, userExtraInfoService.get(userInfo.getId()));
        //是否需要填写邀请码
        vo.setHasBoss(false);
        return JsonUtil.loadTrueResult(JsonUtil.getSimpleGson().toJson(vo));
    }
 
 
    @UserLogin(uid = "#uid")
    @ResponseBody
    @RequestMapping("bindWX")
    public String bindWX(AcceptData acceptData, Long uid, String code) {
 
        try {
            userAccountService.bindWX(uid, code);
        } catch (UserAccountException e) {
            e.printStackTrace();
            return JsonUtil.loadFalseResult(e.getMsg());
        }
 
        return JsonUtil.loadTrueResult("");
    }
 
    @UserLogin(uid = "#uid")
    @ResponseBody
    @RequestMapping("bindPhone")
    public String bindPhone(AcceptData acceptData, Long uid, String phone, String vcode, String token) {
 
        try {
            userAccountService.bindPhone(uid, phone, vcode, token);
        } catch (UserAccountException e) {
            e.printStackTrace();
            return JsonUtil.loadFalseResult(e.getMsg());
        }
 
        return JsonUtil.loadTrueResult("");
    }
 
    @UserLogin(uid = "#uid")
    @ResponseBody
    @RequestMapping("logout")
    public String logout(AcceptData acceptData, Long uid) {
        try {
            //解绑UID
            pushManager.unBind(acceptData.getSystem(), acceptData.getPlatform() == APPPlatform.ios ? acceptData.getIdfa() : acceptData.getUtdId());
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return JsonUtil.loadTrueResult("");
    }
 
 
    @UserLogin(uid = "#uid")
    @ResponseBody
    @RequestMapping("unRegister")
    public String unRegister(AcceptData acceptData, String vcode, String phone) {
 
        if (StringUtil.isNullOrEmpty(vcode)) {
            return JsonUtil.loadFalseResult("验证码不能为空");
        }
 
        if (StringUtil.isNullOrEmpty(phone)) {
            return JsonUtil.loadFalseResult("手机号不能为空");
        }
 
        if (!verifyCodeManager.isPhoneCodeRight(acceptData.getSystem(), phone, vcode)) {
            return JsonUtil.loadFalseResult("验证码错误");
        }
 
        UserInfo user = userInfoService.selectValidByPhone(acceptData.getSystem(), phone);
        if (user == null) {
            return JsonUtil.loadFalseResult("不存在绑定该手机号的用户");
        }
        //注销
        userAccountService.unRegister(user.getId());
        return JsonUtil.loadTrueResult("");
    }
 
    @UserLogin(uid = "#uid")
    @ResponseBody
    @RequestMapping("getUserInfo")
    public String getUserInfo(AcceptData acceptData, Long uid) {
 
        UserInfo user = userInfoService.get(uid);
 
        if (user == null) {
            return JsonUtil.loadFalseResult("用户不存在");
        }
        if (user.getStatus() == UserInfo.STATUS_FORBIDDEN) {
            return JsonUtil.loadFalseResult(ApiCodeConstant.CODE_FAIL_USER_FORBIDDEN, "账号被封禁");
        }
 
        if (user.getStatus() == UserInfo.STATUS_OWN_DELETE) {
            return JsonUtil.loadFalseResult(ApiCodeConstant.CODE_FAIL_USER_DELETE, "用户已被删除");
        }
 
        UserInfoVO vo = new UserInfoVO();
        vo.setId(user.getId() + "");
        vo.setNickName(user.getNickName());
        vo.setPortrait(user.getPortrait());
 
        UserVIPInfo userVIPInfo = vipService.getVIPInfo(user.getId());
        if (userVIPInfo != null) {
            vo.setVipExpireTime(userVIPInfo.getExpireDate().getTime());
        }
 
 
        return JsonUtil.loadTrueResult(vo);
    }
 
    @UserLogin(uid = "#uid")
    @ResponseBody
    @RequestMapping("updateUserInfo")
    public String updateUserInfo(AcceptData acceptData, Long uid, String nickName, @Nullable @RequestParam("portrait") MultipartFile portrait, String portraitMD5) {
 
        UserInfo user = userInfoService.get(uid);
 
        if (user == null) {
            return JsonUtil.loadFalseResult("用户不存在");
        }
 
        UserInfo update = new UserInfo();
        update.setId(uid);
 
        if (!StringUtil.isNullOrEmpty(nickName)) {
            update.setNickName(nickName);
        }
 
        if (portrait != null) {
 
            if (StringUtil.isNullOrEmpty(portraitMD5)) {
                return JsonUtil.loadFalseResult("头像文件加密值为空");
            }
 
            try {
                String md5 = DigestUtils.md5Hex(portrait.getBytes());
                if (!portraitMD5.equalsIgnoreCase(md5)) {
                    return JsonUtil.loadFalseResult("头像文件加密值错误");
                }
 
                InputStream inputStream = portrait.getInputStream();
                String contentType = portrait.getContentType();
                String key = String.format("/imgs/portrait/%s_%s.png", uid, md5);
                FileUploadResult result = COSManager.getInstance().uploadFile(inputStream, key);
                if (result == null) {
                    return JsonUtil.loadFalseResult("上传出错");
                }
                update.setPortrait(ImageUtil.getCOSImageUrl(key));
            } catch (Exception e) {
                return JsonUtil.loadFalseResult("头像修改出错");
            }
        }
 
        userInfoService.update(update);
        if (portrait != null) {
            //删除原来的头像
            if (!user.getPortrait().contains("default")) {
                try {
                    COSManager.getInstance().deleteFileByKey(ImageUtil.getUrlKey(user.getPortrait()));
                } catch (Exception e) {
                }
            }
        }
 
        return JsonUtil.loadTrueResult("");
    }
 
 
    @ResponseBody
    @RequestMapping("uploadPushRegId")
    public String uploadPushRegId(AcceptData acceptData, Long uid, String regId) {
 
        try {
            pushManager.saveToken(acceptData, uid, regId);
            return JsonUtil.loadTrueResult("");
        } catch (BPushDeviceTokenException e) {
            return JsonUtil.loadFalseResult(e.getCode(), "业务出错");
        } catch (ParamsException e) {
            return JsonUtil.loadFalseResult(e.getCode(), "参数错误");
        }
    }
 
 
}