huanghongfa
2020-12-22 47de8ceefb38ee55fc577f94bfda09dd5adbd29d
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
package com.panzhihua.applets.api;
 
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import com.alibaba.fastjson.JSONObject;
import com.panzhihua.applets.config.WxMaConfiguration;
import com.panzhihua.applets.model.vos.LoginRequest;
import com.panzhihua.common.model.vos.LoginReturnVO;
import com.panzhihua.common.model.vos.community.ComActVO;
import com.panzhihua.common.service.auth.TokenService;
import com.panzhihua.common.service.community.CommunityService;
import com.panzhihua.common.service.user.UserService;
import com.panzhihua.common.controller.BaseController;
import com.panzhihua.common.exceptions.UnAuthenticationException;
import com.panzhihua.common.exceptions.WeiXinException;
import com.panzhihua.common.model.vos.LoginUserInfoVO;
import com.panzhihua.common.model.vos.R;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
 
/**
 * @program: springcloud_k8s_panzhihuazhihuishequ
 * @description: 登录
 * @author: huang.hongfa weixin hhf9596 qq 959656820
 * @create: 2020-11-19 16:35
 **/
@Slf4j
@Api(tags = {"登录模块"})
@RestController("/")
public class LoginApi extends BaseController {
    @Resource
    private UserService userService;
    @Resource
    private WxMaConfiguration wxMaConfiguration;
    @Resource
    private TokenService tokenService;
    @Resource
    private CommunityService communityService;
 
    @ApiOperation(value = "登录" ,response =LoginReturnVO.class )
    @PostMapping("login")
    public R login(@RequestBody LoginRequest loginRequest) {
        String code = loginRequest.getCode();
        if (ObjectUtils.isEmpty(code)) {
            return R.fail("缺少登录参数");
        }
        WxMaService maService = wxMaConfiguration.getMaService();
        WxMaJscode2SessionResult sessionInfo=null;
        try {
             sessionInfo = maService.getUserService().getSessionInfo(code);
        } catch (Exception e) {
            log.error("微信登录失败【{}】", e.getMessage());
            sessionInfo=new WxMaJscode2SessionResult();
            sessionInfo.setOpenid("88888888");
            sessionInfo.setSessionKey("9999999");
        }
        log.info("微信登录成功【{}】", JSONObject.toJSONString(sessionInfo));
        String openid = sessionInfo.getOpenid();
        String sessionKey = sessionInfo.getSessionKey();
        String unionid = sessionInfo.getUnionid();
        if (ObjectUtils.isEmpty(unionid)) {
            unionid="无";
        }
        userService.addOrUpdate(openid,sessionKey,unionid);
        return tokenService.loginApplets(openid);
    }
 
    @ApiOperation(value = "维护用户基本信息(昵称、性别、头像)")
    @PostMapping("updateUserWeiXinInfo")
    public R updateUserWeiXinInfo(@RequestBody LoginRequest loginRequest){
        String encryptedData = loginRequest.getEncryptedData();
        String iv = loginRequest.getIv();
        boolean empty = ObjectUtils.isEmpty(iv);
        boolean empty1 = ObjectUtils.isEmpty(encryptedData);
        if (empty||empty1) {
            return R.fail("微信用户参数不全");
        }
        WxMaService maService = wxMaConfiguration.getMaService();
        Long userId = this.getUserId();
        boolean empty2 = ObjectUtils.isEmpty(userId);
        if (empty2) {
            throw new UnAuthenticationException();
        }
        R<LoginUserInfoVO> r = userService.getUserInfoByUserId(userId + "");
        LoginUserInfoVO loginUserInfoVO =r.getData();
        WxMaUserInfo wxUserInfo = maService.getUserService().getUserInfo(loginUserInfoVO.getSessionKey(),
                encryptedData, iv);
        if (null == wxUserInfo) {
            return R.fail("微信解析基本信息失败");
        }
        String avatarUrl = wxUserInfo.getAvatarUrl();
        String gender = wxUserInfo.getGender();
        String nickName = wxUserInfo.getNickName();
        return userService.updateUserWeiXinInfo(userId,nickName,Integer.parseInt(gender),avatarUrl);
    }
 
    @ApiOperation(value = "维护微信用户手机号")
    @PostMapping("updateUserWeiXinPhone")
    public R updateUserWeiXinPhone(@RequestBody LoginRequest loginRequest){
        String encryptedData = loginRequest.getEncryptedData();
        String iv = loginRequest.getIv();
        boolean empty = ObjectUtils.isEmpty(iv);
        boolean empty1 = ObjectUtils.isEmpty(encryptedData);
        if (empty||empty1) {
            return R.fail("微信用户参数不全");
        }
        WxMaService maService = wxMaConfiguration.getMaService();
        Long userId = this.getUserId();
        R<LoginUserInfoVO> r = userService.getUserInfoByUserId(userId + "");
        LoginUserInfoVO loginUserInfoVO = r.getData();
        WxMaPhoneNumberInfo wxMaPhoneNumberInfo = maService.getUserService().getPhoneNoInfo(loginUserInfoVO.getSessionKey(),
                encryptedData, iv);
        if (ObjectUtils.isEmpty(wxMaPhoneNumberInfo) || ObjectUtils.isEmpty(wxMaPhoneNumberInfo.getPhoneNumber())) {
            return R.fail("微信解析手机号失败");
        }
        String purePhoneNumber = wxMaPhoneNumberInfo.getPurePhoneNumber();
        return userService.updateUserWeiXinPhone(userId,purePhoneNumber);
    }
 
    @ApiOperation(value = "用户登出")
    @PostMapping("logout")
    public R updateUserWeiXinPhone(){
        String token = this.getToken();
        boolean empty2 = ObjectUtils.isEmpty(token);
        if (empty2) {
            return R.ok();
        }
        return tokenService.logout(token);
    }
 
    @ApiOperation(value = "刷新token",response = LoginReturnVO.class)
    @GetMapping("refreshToken")
    @ApiImplicitParam(name ="refreshToken",value = "登录返回的刷新token")
    public R refreshToken(@RequestParam("refreshToken")String refreshToken){
        return tokenService.refreshToken(refreshToken);
    }
 
    @ApiOperation(value = "查询社区")
    @GetMapping("listcommunity")
    public R listCommunity(@RequestBody ComActVO comActVO){
        return communityService.listCommunity(comActVO);
    }
 
    @ApiOperation(value = "用户绑定社区、小区")
    @PutMapping("putusercommunityarea")
    public R putUserCommunityArea(@RequestBody LoginUserInfoVO loginUserInfoVO){
        Long userId = this.getUserId();
        Long communityId = loginUserInfoVO.getCommunityId();
        Long areaId = loginUserInfoVO.getAreaId();
        if (null==communityId||0==communityId) {
            return R.fail("社区未选择");
        }
        if (null==areaId||0==areaId) {
            return R.fail("小区未选择");
        }
        loginUserInfoVO.setUserId(userId);
        return userService.putUserCommunityArea(loginUserInfoVO);
    }
}