ming 7 months ago
commit 0f65a3cc3b

@ -0,0 +1,86 @@
package com.example.entity;
/**
*
*/
public class Account {
private Integer id;
/** 用户名 */
private String username;
/** 名称 */
private String name;
/** 密码 */
private String password;
/** 角色标识 */
private String role;
/** 新密码 */
private String newPassword;
/** 头像 */
private String avatar;
private String token;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getNewPassword() {
return newPassword;
}
public void setNewPassword(String newPassword) {
this.newPassword = newPassword;
}
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
}

@ -0,0 +1,59 @@
package com.example.entity;
import java.io.Serializable;
/**
*
*/
public class Address implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private Integer userId;
private String username;
private String useraddress;
private String phone;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUseraddress() {
return useraddress;
}
public void setUseraddress(String useraddress) {
this.useraddress = useraddress;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}

@ -0,0 +1,87 @@
package com.example.controller;
import com.example.common.Result;
import com.example.entity.Address;
import com.example.service.AddressService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
*
**/
@RestController
@RequestMapping("/address")
public class AddressController {
@Resource
private AddressService addressService;
/**
*
*/
@PostMapping("/add")
public Result add(@RequestBody Address address) {
addressService.add(address);
return Result.success();
}
/**
*
*/
@DeleteMapping("/delete/{id}")
public Result deleteById(@PathVariable Integer id) {
addressService.deleteById(id);
return Result.success();
}
/**
*
*/
@DeleteMapping("/delete/batch")
public Result deleteBatch(@RequestBody List<Integer> ids) {
addressService.deleteBatch(ids);
return Result.success();
}
/**
*
*/
@PutMapping("/update")
public Result updateById(@RequestBody Address address) {
addressService.updateById(address);
return Result.success();
}
/**
* ID
*/
@GetMapping("/selectById/{id}")
public Result selectById(@PathVariable Integer id) {
Address address = addressService.selectById(id);
return Result.success(address);
}
/**
*
*/
@GetMapping("/selectAll")
public Result selectAll(Address address ) {
List<Address> list = addressService.selectAll(address);
return Result.success(list);
}
/**
*
*/
@GetMapping("/selectPage")
public Result selectPage(Address address,
@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize) {
PageInfo<Address> page = addressService.selectPage(address, pageNum, pageSize);
return Result.success(page);
}
}

@ -0,0 +1,39 @@
package com.example.mapper;
import com.example.entity.Address;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* address
*/
public interface AddressMapper {
/**
*
*/
int insert(Address address);
/**
*
*/
int deleteById(Integer id);
/**
*
*/
int updateById(Address address);
/**
* ID
*/
Address selectById(Integer id);
/**
*
*/
List<Address> selectAll(Address address);
}

@ -0,0 +1,84 @@
package com.example.service;
import com.example.common.enums.RoleEnum;
import com.example.entity.Account;
import com.example.entity.Address;
import com.example.mapper.AddressMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
*
**/
@Service
public class AddressService {
@Resource
private AddressMapper addressMapper;
/**
*
*/
public void add(Address address) {
addressMapper.insert(address);
}
/**
*
*/
public void deleteById(Integer id) {
addressMapper.deleteById(id);
}
/**
*
*/
public void deleteBatch(List<Integer> ids) {
for (Integer id : ids) {
addressMapper.deleteById(id);
}
}
/**
*
*/
public void updateById(Address address) {
addressMapper.updateById(address);
}
/**
* ID
*/
public Address selectById(Integer id) {
return addressMapper.selectById(id);
}
/**
*
*/
public List<Address> selectAll(Address address) {
Account currentUser = TokenUtils.getCurrentUser();
if (RoleEnum.USER.name().equals(currentUser.getRole())) {
address.setUserId(currentUser.getId());
}
return addressMapper.selectAll(address);
}
/**
*
*/
public PageInfo<Address> selectPage(Address address, Integer pageNum, Integer pageSize) {
Account currentUser = TokenUtils.getCurrentUser();
if (RoleEnum.USER.name().equals(currentUser.getRole())) {
address.setUserId(currentUser.getId());
}
PageHelper.startPage(pageNum, pageSize);
List<Address> list = addressMapper.selectAll(address);
return PageInfo.of(list);
}
}

@ -0,0 +1,96 @@
package com.example.entity;
import java.io.Serializable;
/**
*
*/
public class Collect implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private Integer userId;
private Integer businessId;
private Integer goodsId;
private String businessName;
private String goodsName;
private String goodsImg;
private String goodUnit;
private Double goodsPrice;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public Integer getBusinessId() {
return businessId;
}
public void setBusinessId(Integer businessId) {
this.businessId = businessId;
}
public Integer getGoodsId() {
return goodsId;
}
public void setGoodsId(Integer goodsId) {
this.goodsId = goodsId;
}
public String getBusinessName() {
return businessName;
}
public void setBusinessName(String businessName) {
this.businessName = businessName;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public String getGoodsImg() {
return goodsImg;
}
public void setGoodsImg(String goodsImg) {
this.goodsImg = goodsImg;
}
public String getGoodUnit() {
return goodUnit;
}
public void setGoodUnit(String goodUnit) {
this.goodUnit = goodUnit;
}
public Double getGoodsPrice() {
return goodsPrice;
}
public void setGoodsPrice(Double goodsPrice) {
this.goodsPrice = goodsPrice;
}
}

@ -0,0 +1,87 @@
package com.example.controller;
import com.example.common.Result;
import com.example.entity.Collect;
import com.example.service.CollectService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
*
**/
@RestController
@RequestMapping("/collect")
public class CollectController {
@Resource
private CollectService collectService;
/**
*
*/
@PostMapping("/add")
public Result add(@RequestBody Collect collect) {
collectService.add(collect);
return Result.success();
}
/**
*
*/
@DeleteMapping("/delete/{id}")
public Result deleteById(@PathVariable Integer id) {
collectService.deleteById(id);
return Result.success();
}
/**
*
*/
@DeleteMapping("/delete/batch")
public Result deleteBatch(@RequestBody List<Integer> ids) {
collectService.deleteBatch(ids);
return Result.success();
}
/**
*
*/
@PutMapping("/update")
public Result updateById(@RequestBody Collect collect) {
collectService.updateById(collect);
return Result.success();
}
/**
* ID
*/
@GetMapping("/selectById/{id}")
public Result selectById(@PathVariable Integer id) {
Collect collect = collectService.selectById(id);
return Result.success(collect);
}
/**
*
*/
@GetMapping("/selectAll")
public Result selectAll(Collect collect ) {
List<Collect> list = collectService.selectAll(collect);
return Result.success(list);
}
/**
*
*/
@GetMapping("/selectPage")
public Result selectPage(Collect collect,
@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize) {
PageInfo<Collect> page = collectService.selectPage(collect, pageNum, pageSize);
return Result.success(page);
}
}

@ -0,0 +1,41 @@
package com.example.mapper;
import com.example.entity.Collect;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* collect
*/
public interface CollectMapper {
/**
*
*/
int insert(Collect collect);
/**
*
*/
int deleteById(Integer id);
/**
*
*/
int updateById(Collect collect);
/**
* ID
*/
Collect selectById(Integer id);
/**
*
*/
List<Collect> selectAll(Collect collect);
@Select("select * from collect where user_id = #{userId} and goods_id = #{goodsId}")
Collect selectByUserIdAndGoodsId(@Param("userId") Integer userId, @Param("goodsId") Integer goodsId);
}

@ -0,0 +1,88 @@
package com.example.service;
import cn.hutool.core.util.ObjectUtil;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.Account;
import com.example.entity.Collect;
import com.example.exception.CustomException;
import com.example.mapper.CollectMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
*
**/
@Service
public class CollectService {
@Resource
private CollectMapper collectMapper;
/**
*
*/
public void add(Collect collect) {
// 判断一下该用户有没有收藏过该商品,如果有,就要提示用户不能重复收藏
Collect dbCollect = collectMapper.selectByUserIdAndGoodsId(collect.getUserId(), collect.getGoodsId());
if (ObjectUtil.isNotEmpty(dbCollect)) {
throw new CustomException(ResultCodeEnum.COLLECT_ALREADY_ERROR);
}
collectMapper.insert(collect);
}
/**
*
*/
public void deleteById(Integer id) {
collectMapper.deleteById(id);
}
/**
*
*/
public void deleteBatch(List<Integer> ids) {
for (Integer id : ids) {
collectMapper.deleteById(id);
}
}
/**
*
*/
public void updateById(Collect collect) {
collectMapper.updateById(collect);
}
/**
* ID
*/
public Collect selectById(Integer id) {
return collectMapper.selectById(id);
}
/**
*
*/
public List<Collect> selectAll(Collect collect) {
return collectMapper.selectAll(collect);
}
/**
*
*/
public PageInfo<Collect> selectPage(Collect collect, Integer pageNum, Integer pageSize) {
Account currentUser = TokenUtils.getCurrentUser();
if (RoleEnum.USER.name().equals(currentUser.getRole())) {
collect.setUserId(currentUser.getId());
}
PageHelper.startPage(pageNum, pageSize);
List<Collect> list = collectMapper.selectAll(collect);
return PageInfo.of(list);
}
}

@ -0,0 +1,62 @@
package com.example.entity;
import java.io.Serializable;
/**
*
*/
public class Notice implements Serializable {
private static final long serialVersionUID = 1L;
/** ID */
private Integer id;
/** 标题 */
private String title;
/** 内容 */
private String content;
/** 创建时间 */
private String time;
/** 创建人 */
private String user;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
}

@ -0,0 +1,86 @@
package com.example.controller;
import com.example.common.Result;
import com.example.entity.Notice;
import com.example.service.NoticeService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
*
**/
@RestController
@RequestMapping("/notice")
public class NoticeController {
@Resource
private NoticeService noticeService;
/**
*
*/
@PostMapping("/add")
public Result add(@RequestBody Notice notice) {
noticeService.add(notice);
return Result.success();
}
/**
*
*/
@DeleteMapping("/delete/{id}")
public Result deleteById(@PathVariable Integer id) {
noticeService.deleteById(id);
return Result.success();
}
/**
*
*/
@DeleteMapping("/delete/batch")
public Result deleteBatch(@RequestBody List<Integer> ids) {
noticeService.deleteBatch(ids);
return Result.success();
}
/**
*
*/
@PutMapping("/update")
public Result updateById(@RequestBody Notice notice) {
noticeService.updateById(notice);
return Result.success();
}
/**
* ID
*/
@GetMapping("/selectById/{id}")
public Result selectById(@PathVariable Integer id) {
Notice notice = noticeService.selectById(id);
return Result.success(notice);
}
/**
*
*/
@GetMapping("/selectAll")
public Result selectAll(Notice notice ) {
List<Notice> list = noticeService.selectAll(notice);
return Result.success(list);
}
/**
*
*/
@GetMapping("/selectPage")
public Result selectPage(Notice notice,
@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize) {
PageInfo<Notice> page = noticeService.selectPage(notice, pageNum, pageSize);
return Result.success(page);
}
}

@ -0,0 +1,36 @@
package com.example.mapper;
import com.example.entity.Notice;
import java.util.List;
/**
* notice
*/
public interface NoticeMapper {
/**
*
*/
int insert(Notice notice);
/**
*
*/
int deleteById(Integer id);
/**
*
*/
int updateById(Notice notice);
/**
* ID
*/
Notice selectById(Integer id);
/**
*
*/
List<Notice> selectAll(Notice notice);
}

@ -0,0 +1,79 @@
package com.example.service;
import cn.hutool.core.date.DateUtil;
import com.example.entity.Account;
import com.example.entity.Notice;
import com.example.mapper.NoticeMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.List;
/**
*
**/
@Service
public class NoticeService {
@Resource
private NoticeMapper noticeMapper;
/**
*
*/
public void add(Notice notice) {
notice.setTime(DateUtil.today());
Account currentUser = TokenUtils.getCurrentUser();
notice.setUser(currentUser.getUsername());
noticeMapper.insert(notice);
}
/**
*
*/
public void deleteById(Integer id) {
noticeMapper.deleteById(id);
}
/**
*
*/
public void deleteBatch(List<Integer> ids) {
for (Integer id : ids) {
noticeMapper.deleteById(id);
}
}
/**
*
*/
public void updateById(Notice notice) {
noticeMapper.updateById(notice);
}
/**
* ID
*/
public Notice selectById(Integer id) {
return noticeMapper.selectById(id);
}
/**
*
*/
public List<Notice> selectAll(Notice notice) {
return noticeMapper.selectAll(notice);
}
/**
*
*/
public PageInfo<Notice> selectPage(Notice notice, Integer pageNum, Integer pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<Notice> list = noticeMapper.selectAll(notice);
return PageInfo.of(list);
}
}

@ -0,0 +1,37 @@
package com.example.mapper;
import com.example.entity.Type;
import java.util.List;
/**
* type
*/
public interface TypeMapper {
/**
*
*/
int insert(Type type);
/**
*
*/
int deleteById(Integer id);
/**
*
*/
int updateById(Type type);
/**
* ID
*/
Type selectById(Integer id);
/**
*
*/
List<Type> selectAll(Type type);
}

@ -0,0 +1,75 @@
package com.example.service;
import com.example.entity.Type;
import com.example.mapper.TypeMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
*
**/
@Service
public class TypeService {
@Resource
private TypeMapper typeMapper;
/**
*
*/
public void add(Type type) {
typeMapper.insert(type);
}
/**
*
*/
public void deleteById(Integer id) {
typeMapper.deleteById(id);
}
/**
*
*/
public void deleteBatch(List<Integer> ids) {
for (Integer id : ids) {
typeMapper.deleteById(id);
}
}
/**
*
*/
public void updateById(Type type) {
typeMapper.updateById(type);
}
/**
* ID
*/
public Type selectById(Integer id) {
return typeMapper.selectById(id);
}
/**
*
*/
public List<Type> selectAll(Type type) {
return typeMapper.selectAll(type);
}
/**
*
*/
public PageInfo<Type> selectPage(Type type, Integer pageNum, Integer pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<Type> list = typeMapper.selectAll(type);
return PageInfo.of(list);
}
}

@ -0,0 +1,104 @@
package com.example.entity;
import java.io.Serializable;
/**
*
*/
public class User extends Account implements Serializable {
private static final long serialVersionUID = 1L;
/** ID */
private Integer id;
/** 用户名 */
private String username;
/** 密码 */
private String password;
/** 昵称 */
private String name;
/** 电话 */
private String phone;
/** 邮箱 */
private String email;
/** 头像 */
private String avatar;
/** 角色标识 */
private String role;
@Override
public Integer getId() {
return id;
}
@Override
public void setId(Integer id) {
this.id = id;
}
@Override
public String getUsername() {
return username;
}
@Override
public void setUsername(String username) {
this.username = username;
}
@Override
public String getPassword() {
return password;
}
@Override
public void setPassword(String password) {
this.password = password;
}
@Override
public String getName() {
return name;
}
@Override
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String getAvatar() {
return avatar;
}
@Override
public void setAvatar(String avatar) {
this.avatar = avatar;
}
@Override
public String getRole() {
return role;
}
@Override
public void setRole(String role) {
this.role = role;
}
}

@ -0,0 +1,89 @@
package com.example.controller;
import com.example.common.Result;
import com.example.entity.Business;
import com.example.entity.User;
import com.example.service.BusinessService;
import com.example.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
*
**/
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
/**
*
*/
@PostMapping("/add")
public Result add(@RequestBody User user) {
userService.add(user);
return Result.success();
}
/**
*
*/
@DeleteMapping("/delete/{id}")
public Result deleteById(@PathVariable Integer id) {
userService.deleteById(id);
return Result.success();
}
/**
*
*/
@DeleteMapping("/delete/batch")
public Result deleteBatch(@RequestBody List<Integer> ids) {
userService.deleteBatch(ids);
return Result.success();
}
/**
*
*/
@PutMapping("/update")
public Result updateById(@RequestBody User user) {
userService.updateById(user);
return Result.success();
}
/**
* ID
*/
@GetMapping("/selectById/{id}")
public Result selectById(@PathVariable Integer id) {
User user = userService.selectById(id);
return Result.success(user);
}
/**
*
*/
@GetMapping("/selectAll")
public Result selectAll(User user ) {
List<User> list = userService.selectAll(user);
return Result.success(list);
}
/**
*
*/
@GetMapping("/selectPage")
public Result selectPage(User user,
@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize) {
PageInfo<User> page = userService.selectPage(user, pageNum, pageSize);
return Result.success(page);
}
}

@ -0,0 +1,40 @@
package com.example.mapper;
import com.example.entity.User;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* user
*/
public interface UserMapper {
/**
*
*/
int insert(User user);
/**
*
*/
int deleteById(Integer id);
/**
*
*/
int updateById(User user);
/**
* ID
*/
User selectById(Integer id);
/**
*
*/
List<User> selectAll(User user);
@Select("select * from user where username = #{username}")
User selectByUsername(String username);
}

@ -0,0 +1,136 @@
package com.example.service;
import cn.hutool.core.util.ObjectUtil;
import com.example.common.Constants;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.common.enums.StatusEnum;
import com.example.entity.Account;
import com.example.entity.User;
import com.example.exception.CustomException;
import com.example.mapper.UserMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
*
**/
@Service
public class UserService {
@Resource
private UserMapper userMapper;
/**
*
*/
public void add(User user) {
User dbUser = userMapper.selectByUsername(user.getUsername());
if (ObjectUtil.isNotNull(dbUser)) {
throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR);
}
if (ObjectUtil.isEmpty(user.getPassword())) {
user.setPassword(Constants.USER_DEFAULT_PASSWORD);
}
if (ObjectUtil.isEmpty(user.getName())) {
user.setName(user.getUsername());
}
user.setRole(RoleEnum.USER.name());
userMapper.insert(user);
}
/**
*
*/
public void deleteById(Integer id) {
userMapper.deleteById(id);
}
/**
*
*/
public void deleteBatch(List<Integer> ids) {
for (Integer id : ids) {
userMapper.deleteById(id);
}
}
/**
*
*/
public void updateById(User user) {
userMapper.updateById(user);
}
/**
* ID
*/
public User selectById(Integer id) {
return userMapper.selectById(id);
}
/**
*
*/
public List<User> selectAll(User user) {
return userMapper.selectAll(user);
}
/**
*
*/
public PageInfo<User> selectPage(User user, Integer pageNum, Integer pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<User> list = userMapper.selectAll(user);
return PageInfo.of(list);
}
/**
*
*/
public Account login(Account account) {
Account dbUser = userMapper.selectByUsername(account.getUsername());
if (ObjectUtil.isNull(dbUser)) {
throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
}
if (!account.getPassword().equals(dbUser.getPassword())) {
throw new CustomException(ResultCodeEnum.USER_ACCOUNT_ERROR);
}
// 生成token
String tokenData = dbUser.getId() + "-" + RoleEnum.USER.name();
String token = TokenUtils.createToken(tokenData, dbUser.getPassword());
dbUser.setToken(token);
return dbUser;
}
/**
*
*/
public void register(Account account) {
User user = new User();
BeanUtils.copyProperties(account, user);
add(user);
}
/**
*
*/
public void updatePassword(Account account) {
User dbUser = userMapper.selectByUsername(account.getUsername());
if (ObjectUtil.isNull(dbUser)) {
throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
}
if (!account.getPassword().equals(dbUser.getPassword())) {
throw new CustomException(ResultCodeEnum.PARAM_PASSWORD_ERROR);
}
dbUser.setPassword(account.getNewPassword());
userMapper.updateById(dbUser);
}
}

@ -0,0 +1,97 @@
package com.example.controller;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.example.common.Result;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.Account;
import com.example.service.AdminService;
import com.example.service.BusinessService;
import com.example.service.UserService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
*
*/
@RestController
public class WebController {
@Resource
private AdminService adminService;
@Resource
private BusinessService businessService;
@Resource
private UserService userService;
@GetMapping("/")
public Result hello() {
return Result.success("访问成功");
}
/**
*
*/
@PostMapping("/login")
public Result login(@RequestBody Account account) {
if (ObjectUtil.isEmpty(account.getUsername()) || ObjectUtil.isEmpty(account.getPassword())
|| ObjectUtil.isEmpty(account.getRole())) {
return Result.error(ResultCodeEnum.PARAM_LOST_ERROR);
}
if (RoleEnum.ADMIN.name().equals(account.getRole())) {
account = adminService.login(account);
}
if (RoleEnum.BUSINESS.name().equals(account.getRole())){
account = businessService.login(account);
}
if(RoleEnum.USER.name().equals(account.getRole())){
account = userService.login(account);
}
return Result.success(account);
}
/**
*
*/
@PostMapping("/register")
public Result register(@RequestBody Account account) {
if (StrUtil.isBlank(account.getUsername()) || StrUtil.isBlank(account.getPassword())
|| ObjectUtil.isEmpty(account.getRole())) {
return Result.error(ResultCodeEnum.PARAM_LOST_ERROR);
}
if (RoleEnum.ADMIN.name().equals(account.getRole())) {
adminService.register(account);
}
if (RoleEnum.BUSINESS.name().equals(account.getRole())) {
businessService.register(account);
}
if (RoleEnum.USER.name().equals(account.getRole())) {
userService.register(account);
}
return Result.success();
}
/**
*
*/
@PutMapping("/updatePassword")
public Result updatePassword(@RequestBody Account account) {
if (StrUtil.isBlank(account.getUsername()) || StrUtil.isBlank(account.getPassword())
|| ObjectUtil.isEmpty(account.getNewPassword())) {
return Result.error(ResultCodeEnum.PARAM_LOST_ERROR);
}
if (RoleEnum.ADMIN.name().equals(account.getRole())) {
adminService.updatePassword(account);
}
if (RoleEnum.BUSINESS.name().equals(account.getRole())) {
businessService.updatePassword(account);
}
if (RoleEnum.USER.name().equals(account.getRole())) {
userService.updatePassword(account);
}
return Result.success();
}
}
Loading…
Cancel
Save