mitao
2025-02-21 31573d6180d15ef65ed0df9c2732495f40b12663
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package com.panzhihua.auth.service.impl;
 
import java.util.Date;
import java.util.concurrent.TimeUnit;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
 
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.model.vos.community.ComActVO;
import com.panzhihua.common.model.vos.user.SysOperLogVO;
import com.panzhihua.common.service.user.UserService;
import com.panzhihua.common.utlis.IPUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
 
import com.panzhihua.auth.service.LoginService;
import com.panzhihua.common.constants.TokenConstant;
import com.panzhihua.common.constants.UserConstants;
import com.panzhihua.common.model.vos.LoginReturnVO;
import com.panzhihua.common.model.vos.LoginReturnsVO;
import com.panzhihua.common.model.vos.LoginUserInfoVO;
import com.panzhihua.common.utlis.JWTTokenUtil;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.bind.annotation.RequestParam;
 
import static java.util.Objects.nonNull;
 
/**
 * @program: springcloud_k8s_panzhihuazhihuishequ
 * @description: token
 * @author: huang.hongfa weixin hhf9596 qq 959656820
 * @create: 2020-11-19 17:07
 **/
@Service
public class LoginServiceImpl implements LoginService {
    @Resource
    private AuthenticationManager authenticationManager;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Resource
    private UserService userService;
 
    /**
     * 微信小程序登录
     *
     * @param openId
     *            微信标识
     * @return jwt
     */
    @Override
    public LoginReturnVO loginApplets(String openId,String appId) {
        Authentication authentication = null;
        authentication = authenticationManager
            .authenticate(new UsernamePasswordAuthenticationToken(openId + "_1"+"_"+appId, UserConstants.PASSWORD));
        LoginUserInfoVO loginUser = (LoginUserInfoVO)authentication.getPrincipal();
        String token = JWTTokenUtil.generateToken(loginUser);
        String refeshToken = JWTTokenUtil.generateRefeshToken(loginUser);
        LoginReturnVO loginReturnVO = new LoginReturnVO();
        loginReturnVO.setToken(token);
        loginReturnVO.setRefreshToken(refeshToken);
        return loginReturnVO;
    }
 
    /**
     * 小程序用户登出
     *
     * @param token
     *            登录用户token
     */
    @Override
    public void logoutApplets(String token) {
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        token = token.replaceAll(TokenConstant.TOKEN_PRE, "");
        valueOperations.set(UserConstants.LOGOUT_TOKEN + token, token, TokenConstant.EXPIRETIME, TimeUnit.MINUTES);
    }
 
    /**
     * 运营后台登录
     *
     * @param account
     *            账户
     * @param password
     *            密码
     * @return 登录结果
     */
    @Override
    public LoginReturnVO loginAppletsBackStage(String account, String password,String appid) {
        Authentication authentication = null;
        authentication =
            authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(account + "_2"+"_"+appid, password));
        LoginUserInfoVO loginUser = (LoginUserInfoVO)authentication.getPrincipal();
        String token = JWTTokenUtil.generateToken(loginUser);
        String refeshToken = JWTTokenUtil.generateRefeshToken(loginUser);
        LoginReturnVO loginReturnVO = new LoginReturnVO();
        loginReturnVO.setToken(token);
        loginReturnVO.setRefreshToken(refeshToken);
        return loginReturnVO;
    }
 
    /**
     * 社区后台登录
     *
     * @param account
     *            账户
     * @param password
     *            密码
     * @return 登录结果
     */
    @Override
    public LoginReturnVO loginCommunityBackage(String account, String password,String appId) {
        Authentication authentication = null;
        authentication =
            authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(account + "_3"+"_"+appId, password));
        LoginUserInfoVO loginUser = (LoginUserInfoVO)authentication.getPrincipal();
        String token = JWTTokenUtil.generateToken(loginUser);
        String refeshToken = JWTTokenUtil.generateRefeshToken(loginUser);
        ComActVO comActVO = loginUser.getComActVO();
        LoginReturnVO loginReturnVO = new LoginReturnVO();
        loginReturnVO.setToken(token);
        loginReturnVO.setRefreshToken(refeshToken);
        loginReturnVO.setCommunityId(loginUser.getCommunityId());
        if (nonNull(comActVO)) {
            loginReturnVO.setLat(comActVO.getLat());
            loginReturnVO.setLng(comActVO.getLng());
        }
        //保存登录日志
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        SysOperLogVO operlog = new SysOperLogVO();
        operlog.setTitle("登录操作"); // 操作模块
        operlog.setBusinessType(1); // 操作类型
        operlog.setMethod("com.panzhihua.community_backstage.api.LoginApi.login"); // 请求方法
        operlog.setAccount(loginUser.getAccount());
        operlog.setOperName(loginUser.getName()); // 请求用户名称
        operlog.setOperIp(IPUtil.getIpAddress(request)); // 请求IP
        operlog.setOperUrl(request.getRequestURI()); // 请求URI
        operlog.setRequestMethod(request.getMethod());
        operlog.setOperLocation(IPUtil.getIpBelongAddress(request));
        operlog.setOperTime(new Date()); // 创建时间
        operlog.setCommunityId(loginUser.getCommunityId());
        userService.addOperLog(operlog);
        return loginReturnVO;
    }
 
    /**
     * 商家后台登录
     *
     * @param account
     *            账户
     * @param password
     *            密码
     * @return 登录结果
     */
    @Override
    public LoginReturnVO loginShopBackStage(String account, String password,String appId) {
        Authentication authentication = null;
        authentication =
                authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(account + "_5"+"_"+appId, password));
        LoginUserInfoVO loginUser = (LoginUserInfoVO)authentication.getPrincipal();
        String token = JWTTokenUtil.generateToken(loginUser);
        String refeshToken = JWTTokenUtil.generateRefeshToken(loginUser);
        ComActVO comActVO = loginUser.getComActVO();
        LoginReturnVO loginReturnVO = new LoginReturnVO();
        loginReturnVO.setToken(token);
        loginReturnVO.setRefreshToken(refeshToken);
        loginReturnVO.setCommunityId(loginUser.getCommunityId());
        if (nonNull(comActVO)) {
            loginReturnVO.setLat(comActVO.getLat());
            loginReturnVO.setLng(comActVO.getLng());
        }
        //保存登录日志
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        SysOperLogVO operlog = new SysOperLogVO();
        operlog.setTitle("登录操作"); // 操作模块
        operlog.setBusinessType(1); // 操作类型
        operlog.setMethod("com.panzhihua.community_backstage.api.LoginApi.loginShopBackStage"); // 请求方法
        operlog.setAccount(loginUser.getAccount());
        operlog.setOperName(loginUser.getName()); // 请求用户名称
        operlog.setOperIp(IPUtil.getIpAddress(request)); // 请求IP
        operlog.setOperUrl(request.getRequestURI()); // 请求URI
        operlog.setRequestMethod(request.getMethod());
        operlog.setOperLocation(IPUtil.getIpBelongAddress(request));
        operlog.setOperTime(new Date()); // 创建时间
        operlog.setCommunityId(loginUser.getCommunityId());
        userService.addOperLog(operlog);
        return loginReturnVO;
    }
 
    /**
     * 大数据统计平台(区、街道、社区三层登陆接口)
     * 
     * @param account
     *            账户
     * @param password
     *            密码
     * @author manailin
     * @return 登录结果
     * @date 2021-5-13 10:56
     */
    @Override
    public LoginReturnVO loginBigDataBackStage(String account, String password,String appid) {
        Authentication authentication = null;
        authentication =
            authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(account + "_8"+"_"+appid, password));
        LoginUserInfoVO loginUser = (LoginUserInfoVO)authentication.getPrincipal();
        String token = JWTTokenUtil.generateToken(loginUser);
        String refeshToken = JWTTokenUtil.generateRefeshToken(loginUser);
        LoginReturnVO loginReturnVO = new LoginReturnVO();
        loginReturnVO.setToken(token);
        loginReturnVO.setRefreshToken(refeshToken);
        return loginReturnVO;
    }
 
    /**
     * 商家后台登录
     *
     * @param account
     *            账户
     * @param password
     *            密码
     * @return 登录结果
     */
    @Override
    public LoginReturnsVO loginGridApp(String account, String password,String appid) {
        Authentication authentication = null;
        authentication =
            authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(account + "_6"+"_"+appid, password));
        LoginUserInfoVO loginUser = (LoginUserInfoVO)authentication.getPrincipal();
        String token = JWTTokenUtil.generateToken(loginUser);
        String refeshToken = JWTTokenUtil.generateRefeshToken(loginUser);
        LoginReturnsVO loginReturnVO = new LoginReturnsVO();
        loginReturnVO.setToken(token);
        loginReturnVO.setRefreshToken(refeshToken);
        loginReturnVO.setUserId(loginUser.getUserId());
        return loginReturnVO;
    }
 
    /**
     * 商家后台登录
     *
     * @param account
     *            账户
     * @param password
     *            密码
     * @return 登录结果
     */
    @Override
    public LoginReturnVO loginGridBackstage(String account, String password,String appid) {
        Authentication authentication = null;
        authentication =
            authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(account + "_7"+"_"+appid, password));
        LoginUserInfoVO loginUser = (LoginUserInfoVO)authentication.getPrincipal();
        String token = JWTTokenUtil.generateToken(loginUser);
        String refeshToken = JWTTokenUtil.generateRefeshToken(loginUser);
        LoginReturnVO loginReturnVO = new LoginReturnVO();
        loginReturnVO.setToken(token);
        loginReturnVO.setRefreshToken(refeshToken);
        return loginReturnVO;
    }
 
    @Override
    public LoginReturnVO loginCgBackage(String account, String password,String appid){
        Authentication authentication = null;
        authentication =
                authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(account + "_9"+"_"+appid, password));
        LoginUserInfoVO loginUser = (LoginUserInfoVO)authentication.getPrincipal();
        String token = JWTTokenUtil.generateToken(loginUser);
        String refeshToken = JWTTokenUtil.generateRefeshToken(loginUser);
        LoginReturnVO loginReturnVO = new LoginReturnVO();
        loginReturnVO.setToken(token);
        loginReturnVO.setRefreshToken(refeshToken);
        return loginReturnVO;
    }
 
    @Override
    public LoginReturnsVO loginAlarmApp(String account, String password,String appid) {
        Authentication authentication = null;
        authentication =
                authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(account + "_10"+"_"+appid, password));
        LoginUserInfoVO loginUser = (LoginUserInfoVO)authentication.getPrincipal();
        String token = JWTTokenUtil.generateToken(loginUser);
        String refeshToken = JWTTokenUtil.generateRefeshToken(loginUser);
        LoginReturnsVO loginReturnVO = new LoginReturnsVO();
        loginReturnVO.setToken(token);
        loginReturnVO.setRefreshToken(refeshToken);
        loginReturnVO.setUserId(loginUser.getUserId());
        return loginReturnVO;
    }
 
    /**
     * 便民服务商家后台登录
     * @param account 账户
     * @param password 密码
     * @return 登录结果
     */
    @Override
    public LoginReturnVO loginMerchantBackStage(String account, String password,String appid) {
        Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(account + "_10"+"_"+appid, password));
        LoginUserInfoVO loginUser = (LoginUserInfoVO)authentication.getPrincipal();
        String token = JWTTokenUtil.generateToken(loginUser);
        String refeshToken = JWTTokenUtil.generateRefeshToken(loginUser);
        LoginReturnVO loginReturnVO = new LoginReturnVO();
        loginReturnVO.setToken(token);
        loginReturnVO.setRefreshToken(refeshToken);
        return loginReturnVO;
    }
 
    /**
     * 西区大屏登录
     * @param account   账号
     * @param password  密码
     * @return  登录结果
     */
    @Override
    public LoginReturnVO loginXQDP(String account, String password,String appid){
        Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(account + "_15"+"_"+appid, password));
        LoginUserInfoVO loginUser = (LoginUserInfoVO)authentication.getPrincipal();
        String token = JWTTokenUtil.generateToken(loginUser);
        String refeshToken = JWTTokenUtil.generateRefeshToken(loginUser);
        LoginReturnVO loginReturnVO = new LoginReturnVO();
        loginReturnVO.setToken(token);
        loginReturnVO.setRefreshToken(refeshToken);
        return loginReturnVO;
    }
 
    /**
     * 微商业街用户登录
     * @param account 账户
     * @param password 密码
     * @return 登录结果
     */
    @Override
    public LoginReturnVO loginMcsUser(String account, String password,String appid) {
        Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(account + "_11"+"_"+appid, password));
        LoginUserInfoVO loginUser = (LoginUserInfoVO)authentication.getPrincipal();
        String token = JWTTokenUtil.generateToken(loginUser);
        String refeshToken = JWTTokenUtil.generateRefeshToken(loginUser);
        LoginReturnVO loginReturnVO = new LoginReturnVO();
        loginReturnVO.setToken(token);
        loginReturnVO.setRefreshToken(refeshToken);
        return loginReturnVO;
    }
 
    @Override
    public LoginReturnVO tfLogin(String account, String password, String appid) {
        Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(account + "_16"+"_"+appid, password));
        LoginUserInfoVO loginUser = (LoginUserInfoVO)authentication.getPrincipal();
        String token = JWTTokenUtil.generateToken(loginUser);
        String refeshToken = JWTTokenUtil.generateRefeshToken(loginUser);
        LoginReturnVO loginReturnVO = new LoginReturnVO();
        loginReturnVO.setToken(token);
        loginReturnVO.setRefreshToken(refeshToken);
        return loginReturnVO;
    }
 
    /**
     * 电动车商城后台用户登录
     *
     * @param account
     *            账户
     * @param password
     *            密码
     * @return 登录结果
     */
    @Override
    public LoginReturnVO loginBatteryUser(String account, String password,String appid) {
        Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(account + "_20"+"_"+appid, password));
        LoginUserInfoVO loginUser = (LoginUserInfoVO)authentication.getPrincipal();
        String token = JWTTokenUtil.generateToken(loginUser);
        String refeshToken = JWTTokenUtil.generateRefeshToken(loginUser);
        LoginReturnVO loginReturnVO = new LoginReturnVO();
        loginReturnVO.setToken(token);
        loginReturnVO.setRefreshToken(refeshToken);
        return loginReturnVO;
    }
}