huanghongfa
2020-12-03 00036b47f300ac0221075aab1e2d366003195289
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
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.panzhihua.applets.config.WxMaConfiguration;
import com.panzhihua.applets.model.vos.LoginRequest;
import com.panzhihua.common.model.vos.LoginReturnVO;
import com.panzhihua.common.service.auth.TokenService;
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;
 
    @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");
            sessionInfo.setUnionid("44444444");
        }
        String openid = sessionInfo.getOpenid();
        String sessionKey = sessionInfo.getSessionKey();
        String unionid = sessionInfo.getUnionid();
        userService.addOrUpdate(openid,sessionKey,unionid);
        R r = tokenService.loginApplets(openid);
        return r;
    }
 
    @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();
        Integer 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) {
            throw new WeiXinException("微信解析基本信息失败");
        }
        String avatarUrl = wxUserInfo.getAvatarUrl();
        String gender = wxUserInfo.getGender();
        String nickName = wxUserInfo.getNickName();
        R r1=userService.updateUserWeiXinInfo(userId,nickName,Integer.parseInt(gender),avatarUrl);
        return r1;
    }
 
    @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();
        Integer userId = this.getUserId();
        boolean empty2 = ObjectUtils.isEmpty(userId);
        if (empty2) {
            throw new UnAuthenticationException();
        }
        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())) {
            throw new WeiXinException("微信解析手机号失败");
        }
        String purePhoneNumber = wxMaPhoneNumberInfo.getPurePhoneNumber();
        R r1=userService.updateUserWeiXinPhone(userId,purePhoneNumber);
        return r1;
    }
 
    @ApiOperation(value = "用户登出")
    @PostMapping("logout")
    public R updateUserWeiXinPhone(){
        String token = this.getToken();
        boolean empty2 = ObjectUtils.isEmpty(token);
        if (empty2) {
            return R.ok();
        }
        R r=tokenService.logout(token);
        return r;
    }
 
    @ApiOperation(value = "刷新token",response = LoginReturnVO.class)
    @GetMapping("refreshToken")
    @ApiImplicitParam(name ="refreshToken",value = "登录返回的刷新token")
    public R refreshToken(@RequestParam("refreshToken")String refreshToken){
        R r=tokenService.refreshToken(refreshToken);
        return r;
    }
 
}