luodangjia
2024-07-29 dc9239d73b15b9a51c46a9e8d25c0d4400e613ce
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
package com.ruoyi.user.controller;
 
 
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.ruoyi.admin.api.entity.*;
import com.ruoyi.admin.api.feignClient.AdminClient;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.constant.RedisConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.vo.AddressDto;
import com.ruoyi.common.core.vo.UserDto;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.system.api.model.LoginUserInfo;
import com.ruoyi.user.entity.RecoveryClassify;
import com.ruoyi.user.entity.RecoveryServe;
import com.ruoyi.user.entity.User;
import com.ruoyi.user.entity.UserRecipient;
import com.ruoyi.user.request.LoginPhoneRequest;
import com.ruoyi.user.service.RecoveryClassifyService;
import com.ruoyi.user.service.RecoveryServeService;
import com.ruoyi.user.service.UserRecipientService;
import com.ruoyi.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * <p>
 * 用户列表 前端控制器
 * </p>
 *
 * @author hjl
 * @since 2024-06-03
 */
@RestController
@RequestMapping("/user")
@Api(tags = {"用户端-登录"})
public class UserController {
 
    @Resource
    private UserService userService;
    @Resource
    private RedisService redisService;
    @Resource
    private TokenService tokenService;
    @Resource
    private AdminClient adminClient;
    @Resource
    private RecoveryClassifyService recoveryClassifyService;
    @Resource
    private RecoveryServeService recoveryServeService;
    @Resource
    private     UserRecipientService userRecipientService;
 
    /**
     * 用户端默认头像
     */
    @Value("${default.profilePicture}")
    private String profilePicture;
 
    /**
     * 解密用户敏感数据
     *
     * @param encryptedData 明文,加密数据
     * @param iv            加密算法的初始向量
     * @param code          用户允许登录后,回调内容会带上 code(有效期五分钟),开发者需要将 code 发送到开发者服务器后台,使用code 换取 session_key api,将 code 换成 openid 和 session_key
     * @return 登录信息
     */
    @ApiOperation("微信授权一键登录")
    @GetMapping("/wxAuthorize")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户允许登录后回调内容code", name = "code", dataType = "String", required = true),
            @ApiImplicitParam(value = "明文,加密数据", name = "encryptedData", dataType = "String", required = true),
            @ApiImplicitParam(value = "加密算法的初始向量", name = "iv", dataType = "String", required = true)
    })
    public R<Map<String, Object>> decodeUserInfo(@RequestParam("code") String code,
                                                 @RequestParam("encryptedData") String encryptedData,
                                                 @RequestParam("iv") String iv) {
        return R.ok(userService.decodeUserInfo(code, encryptedData, iv));
    }
 
    /**
     * 用户端-获取微信openId
     *
     * @param code 随机code
     */
    @ApiOperation(value = "获取微信openId", tags = {"用户端-登录"})
    @PostMapping(value = "/decodeOpenid")
    public R<String> decodeOpenid(HttpServletResponse response, String code) {
        return userService.decodeOpenid(response, code);
    }
 
    /**
     * 用户端-手机验证码登录
     *
     * @param loginPhoneRequest 手机号及密码信息
     */
    @ApiOperation(value = "手机验证码登录", tags = {"用户端-登录"})
    @PostMapping(value = "/phoneCodeLogin")
    public R<Object> phoneCodeLogin(@RequestBody @Validated LoginPhoneRequest loginPhoneRequest) {
        String phone = loginPhoneRequest.getPhone();
        String phoneCode = loginPhoneRequest.getPhoneCode();
        // 默认验证码 123456
        if (!"123456".equals(phoneCode)) {
            // 手机验证码校验获取缓存验证码
            Object phoneCodeRedis = redisService.getCacheObject(RedisConstants.USER_LOGIN_PHONE_CODE + phone);
            if (null == phoneCodeRedis) {
                return R.errorCode("验证码错误!");
            } else {
                // redis 验证码的value 为 code:时间戳
                String rCodeAndTime = String.valueOf(phoneCodeRedis);
                String rCode = rCodeAndTime.split(":")[0];
                if (!rCode.equalsIgnoreCase(phoneCode)) {
                    return R.errorCode("验证码错误!");
                }
            }
        }
        // 用户账号校验
        User user = userService.lambdaQuery().eq(User::getPhone, phone)
                .eq(User::getIsDelete, 0).one();
        if (null != user) {
            if (!Constants.ONE.equals(user.getState())) {
                return R.notEnabled("账号已被禁用,请联系平台管理员。");
            }
        } else {
            user = new User();
            // 随机编号
            user.setUserNo(String.format(Constants.USER_NO_PRE, RandomUtil.randomNumbers(Constants.EIGHT)));
            user.setState(Constants.ONE);
            user.setProfilePicture(profilePicture);
            user.setNickname(String.format(Constants.USER_NO_PRE, StringUtils.getCharAndNum(Constants.SIX)));
            user.setPhone(phone);
            user.setCity(loginPhoneRequest.getCity());
            user.setOpenId(loginPhoneRequest.getOpenId());
            // 生成用户信息
            userService.save(user);
        }
        // 校验通过,生成token及过期时间
        LoginUserInfo loginUserInfo = new LoginUserInfo();
        loginUserInfo.setName(user.getPhone());
        loginUserInfo.setUserid(user.getId());
        loginUserInfo.setPhone(user.getPhone());
        loginUserInfo.setLoginTime(System.currentTimeMillis());
        HashMap<String, Object> map = new HashMap<>(8);
        map.put("token", tokenService.createTokenByUser(loginUserInfo));
        return R.ok(map);
    }
 
    /**
     * 用户端-首页回收分类推荐
     */
    @ApiOperation(value = "用户注册协议/用户隐私协议", tags = {"用户端-登录"})
    @GetMapping(value = "/registerOrAgreement")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "0:用户注册协议、1:用户隐私协议", name = "type", dataType = "Integer", required = true)
    })
    public R<Agreement> registerOrAgreement(@RequestParam Integer type) {
        if (!Constants.ZERO.equals(type) && !Constants.ONE.equals(type)) {
            throw new GlobalException("获取类型异常!");
        }
        R<Agreement> r = adminClient.agreementPolicy(type);
        if (500 == r.getCode()) {
            return R.fail(r.getMsg());
        }
        return R.ok(r.getData());
    }
 
    /**
     * 用户端-轮播图列表
     */
    @ApiOperation(value = "轮播图列表", tags = {"用户端-首页"})
    @GetMapping(value = "/banner")
    public R<List<Rotate>> banner() {
        R<List<Rotate>> r = adminClient.bannerList();
        List<Rotate> data = r.getData();
        if (null == data) {
            return R.fail(r.getMsg());
        }
        for (Rotate datum : data) {
            RecoveryServe recoveryServe = recoveryServeService.lambdaQuery()
                    .eq(RecoveryServe::getId, datum.getRotateServeId()).one();
            if (null != recoveryServe) {
                Integer classifyId = recoveryServe.getClassifyId();
                RecoveryClassify classify = recoveryClassifyService.lambdaQuery()
                        .eq(RecoveryClassify::getId, classifyId).one();
                if (null != classify) {
                    String supClassify = classify.getSupClassify();
                    if (Constants.RECOVERY.equals(supClassify)) {
                        datum.setType(Constants.ONE);
                    } else {
                        datum.setType(Constants.ZERO);
                    }
                }
            }
        }
        return R.ok(data);
    }
 
    /**
     * 用户端-通知公告轮换条
     */
    @ApiOperation(value = "通知公告", tags = {"用户端-首页"})
    @GetMapping(value = "/notice")
    public R<List<Notices>> notice() {
        return R.ok(adminClient.noticesList().getData());
    }
 
    /**
     * 用户端-通知公告详情
     */
    @ApiOperation(value = "通知公告详情", tags = {"用户端-首页"})
    @GetMapping(value = "/noticeDetail")
    public R<Notices> notice(@RequestParam Integer id) {
        return R.ok(adminClient.noticesDetail(id).getData());
    }
 
    /**
     * 用户端-服务优势
     */
    @ApiOperation(value = "服务优势", tags = {"用户端-首页"})
    @GetMapping(value = "/advantage")
    public R<List<ServeAdvantage>> advantage() {
        return R.ok(adminClient.advantageList().getData());
    }
 
    /**
     * 用户端-常见问题
     */
    @ApiOperation(value = "常见问题", tags = {"用户端-首页"})
    @GetMapping(value = "/problem")
    public R<List<Problem>> problem() {
        return R.ok(adminClient.problemList().getData());
    }
 
    /**
     * 用户端首页回收分类推荐
     */
    @GetMapping(value = "/recommend")
    @ApiOperation(value = "首页回收分类推荐", tags = {"用户端-首页"})
    public R<List<RecoveryClassify>> recommend() {
        return R.ok(recoveryClassifyService.lambdaQuery().eq(RecoveryClassify::getIsRecommend, Constants.ONE)
                .eq(RecoveryClassify::getIsDelete, 0).orderByDesc(RecoveryClassify::getSort)
                .orderByDesc(RecoveryClassify::getCreateTime).list());
    }
 
    /**
     * 用户端-首页回收分类推荐
     */
    @ApiOperation(value = "回收服务搜索", tags = {"用户端-首页"})
    @GetMapping(value = "/recoverySearch")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "搜索关键字", name = "keyword", dataType = "String", required = true)
    })
    public R<List<RecoveryServe>> recoverySearch(@RequestParam String keyword) {
        LambdaQueryChainWrapper<RecoveryServe> wrapper = recoveryServeService.lambdaQuery()
                .eq(RecoveryServe::getIsDelete, 0)
                .orderByDesc(RecoveryServe::getSort);
        wrapper = null != keyword && !"".equals(keyword.trim()) ?
                wrapper.like(RecoveryServe::getServeName, keyword) : wrapper;
        List<RecoveryServe> serveList = wrapper.orderByDesc(RecoveryServe::getCreateTime).list();
        for (RecoveryServe recoveryServe : serveList) {
            RecoveryClassify classify = recoveryClassifyService.lambdaQuery()
                    .eq(RecoveryClassify::getId, recoveryServe.getClassifyId()).one();
            if (null != classify) {
                if (Constants.RECOVERY.equals(classify.getSupClassify())) {
                    recoveryServe.setType(Constants.ONE);
                } else {
                    recoveryServe.setType(Constants.ZERO);
                }
            }
        }
        return R.ok(serveList);
    }
 
 
    @ApiOperation(value = "订单列表-更改订单提现状态", tags = {"后台-订单管理"})
    @PostMapping(value = "/getUser")
    public R<UserDto> updateWithdrawalState(@RequestParam("userId") Integer userId) {
 
        User byId = userService.getById(userId);
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(byId,userDto);
        return R.ok(userDto);
 
    }
 
//    @ApiOperation(value = "订单列表-更改订单提现状态", tags = {"后台-订单管理"})
    @PostMapping(value = "/getCityCode")
    public R<AddressDto> getCityCode(@RequestParam("addressId") Integer addressId) {
        UserRecipient byId = userRecipientService.getById(addressId);
        AddressDto userDto = new AddressDto();
        userDto.setCityCode(byId.getCityCode());
        userDto.setCity(byId.getCity());
        return R.ok(userDto);
 
    }
 
 
    /**
     * 用户端-个人中心用户信息
     */
    @ApiOperation(value = "个人中心用户信息", tags = {"用户端-个人中心"})
    @GetMapping(value = "/userInfo")
    public R<User> userInfo() {
        LoginUserInfo loginUser = tokenService.getLoginUserByUser();
        if (null == loginUser) {
            return R.loginExpire("登录失效!");
        }
        return R.ok(userService.lambdaQuery().eq(User::getId, loginUser.getUserid())
                .eq(User::getIsDelete, 0).one());
    }
 
    /**
     * 用户端-修改头像&昵称
     */
    @ApiOperation(value = "修改头像&昵称", tags = {"用户端-个人中心"})
    @GetMapping(value = "/updateInfo")
    public R<Boolean> updateInfo(@RequestParam String picture, @RequestParam String nickname) {
        LoginUserInfo loginUser = tokenService.getLoginUserByUser();
        if (null == loginUser) {
            return R.loginExpire("登录失效!");
        }
        return R.ok(userService.lambdaUpdate().eq(User::getId, loginUser.getUserid())
                .set(User::getProfilePicture, picture)
                .set(User::getNickname, nickname).update());
    }
 
    /**
     * 用户端-修改用户定位城市
     */
    @ApiOperation(value = "修改用户定位城市", tags = {"用户端-个人中心"})
    @GetMapping(value = "/updateCity")
    public R<Boolean> updateCity(@RequestParam String city, @RequestParam String cityCode) {
        LoginUserInfo loginUser = tokenService.getLoginUserByUser();
        if (null == loginUser) {
            return R.loginExpire("登录失效!");
        }
        return R.ok(userService.lambdaUpdate().eq(User::getId, loginUser.getUserid())
                .set(User::getCity, city)
                .set(User::getCityCode, cityCode).update());
    }
 
}