package com.jsh.erp.controller; import com.jsh.erp.datasource.entities.User; import com.jsh.erp.datasource.entities.UserEx; import com.jsh.erp.service.sms.SmsService; import com.jsh.erp.service.user.UserService; import com.jsh.erp.utils.BaseResponseInfo; import lombok.RequiredArgsConstructor; import org.springframework.util.ObjectUtils; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController @RequestMapping("/sms") @RequiredArgsConstructor public class SmsController { private final SmsService smsService; @Resource private UserService userService; @PostMapping("/send-code") public BaseResponseInfo sendCode(@RequestParam String phone) { BaseResponseInfo baseResponseInfo = new BaseResponseInfo(); try { boolean success = smsService.sendCode(phone); baseResponseInfo.code = 200; baseResponseInfo.data = success; } catch (Exception e) { baseResponseInfo.code = 500; baseResponseInfo.data = e.getMessage(); } return baseResponseInfo; } @PostMapping("/login") public BaseResponseInfo loginByCode(@RequestParam String phone, @RequestParam String code) { BaseResponseInfo baseResponseInfo = new BaseResponseInfo(); //验证验证码 if (smsService.verifyCode(phone, code)) { try { //登录处理逻辑 //查询用户表中有没有对应手机号的信息,没有就注册 User user = userService.getByPhone(phone); if (ObjectUtils.isEmpty(user)) { UserEx userEx = new UserEx(); userEx.setUsername(phone); userEx.setLoginName(phone); userEx.setPhonenum(phone); userService.addUser(userEx); } baseResponseInfo.code = 200; baseResponseInfo.data = true; } catch (Exception e) { baseResponseInfo.code = 500; baseResponseInfo.data = e.getMessage(); } return baseResponseInfo; } else { baseResponseInfo.code = 500; baseResponseInfo.data = false; return baseResponseInfo; } } }