mapper
package com. ysu. shop. mapper;
import com. ysu. shop. pojo. User;
import org. apache. ibatis. annotations. *;
import org. apache. ibatis. annotations. Insert;
import java. util. List;
import java. util. Map;
@SuppressWarnings ( "rawtypes" )
public interface UserMapper {
@Delete ( "DELETE FROM user WHERE id=#{id};" )
int delUser ( int id) ;
@Insert ( "insert into " + "user(username, password, phone,email,role,question,answer,create_time,update_time)"
+ "values (#{username}, #{password}, #{phone},#{email},"
+ "#{role}, #{question},#{answer},NOW(), NOW() );" )
int insertUser ( User user) ;
@Update ( "UPDATE user SET password=#{password_new},update_time=NOW() WHERE username=#{username} AND password=#{password_old};" )
int updatePasswordByOldPassword ( String username, String password_old, String password_new) ;
@Update ( "UPDATE user SET password=#{password_new},update_time=NOW() WHERE username=#{username};" )
int updatePasswordByUsername ( String username, String password_new) ;
@Update ( "UPDATE user SET email=#{email},phone=#{phone},question=#{question},answer=#{"
+ "answer},update_time=NOW() WHERE username=#{username};" )
int updateUserByUsername ( User user) ;
@Select ( "SELECT * FROM user WHERE username=#{username} AND password=#{password} ;" )
User login ( String username, String password) ;
@Select ( "SELECT * FROM user WHERE username=#{username};" )
User queryByUsername ( String username) ;
@Select ( "SELECT * FROM user WHERE email=#{email};" )
User queryByEmail ( String email) ;
@Select ( "SELECT * FROM user WHERE id=#{id};" )
User queryById ( int id) ;
@Select ( "SELECT * FROM user WHERE username=#{username} AND question=#{question} AND answer=#{answer};" )
User answer ( String username, String question, String answer) ;
@Select ( "SELECT * FROM user WHERE username=#{username};" )
List< User> queryAll ( ) ;
@Select ( " <script>" + " SELECT * FROM user " + " where role != 'A' " + "limit #{start},#{limit} "
+ " </script>" )
List< User> getBatch ( Map params) ;
@Select ( "select count(*) from user" )
int getTotal ( ) ;
@Select ( "SELECT * FROM user WHERE token = #{userToken}" )
User getUserByToken ( String userToken) ;
@Update ( "UPDATE user SET token=#{token},update_time=NOW() WHERE id=#{id};" )
int updateUserToken ( int id, String token) ;
@Update ( "UPDATE user SET password=#{password},update_time=NOW() WHERE username=#{username} AND token=#{token};" )
int forgetResetPassword ( String username, String password, String token) ;
@Select ( "SELECT * FROM user WHERE id like CONCAT('%',#{msg},'%') " +
" or username like CONCAT('%',#{msg},'%') limit #{start},#{limit};" )
List< User> queryAllByName ( Map params) ;
@Select ( "SELECT count(*) FROM user WHERE id like CONCAT('%',#{msg},'%') " +
" or username like CONCAT('%',#{msg},'%');" )
int queryAllByNameNum ( Map params) ;
}
package com. ysu. shop. mapper;
import com. ysu. shop. pojo. Address;
import org. apache. ibatis. annotations. Delete;
import org. apache. ibatis. annotations. Insert;
import org. apache. ibatis. annotations. Select;
import org. apache. ibatis. annotations. Update;
import java. util. List;
import java. util. Map;
@SuppressWarnings ( "rawtypes" )
public interface AddressMapper {
@Insert ( "insert into " +
"address( user_id, receiver_name, receiver_phone,receiver_province,receiver_city,receiver_district," +
"receiver_address,receiver_zip,create_time,update_time) " +
"values(#{user_id}, #{receiver_name}, #{receiver_phone}," +
"#{receiver_province},#{receiver_city},#{receiver_district},#{receiver_address},#{receiver_zip},NOW(),NOW());" )
int insertAddress ( Address address) ;
@Delete ( "DELETE address FROM address,orders WHERE address.user_id=orders.user_id AND orders.shipping_id=#{shipping_Id}" )
int delAddressByShipping_id ( int shippingId) ;
@Update ( "UPDATE address SET receiver_province=#{receiver_province},receiver_city=#{receiver_city}," +
"receiver_district=#{receiver_district}, receiver_address=#{receiver_address} ," +
"receiver_zip=#{receiver_zip},update_time=NOW() WHERE id=#{id};" )
int updateAddressById ( Address address) ;
@Select ( "SELECT * FROM order,address WHERE address.user_id = order.user_id AND order.shipping_id=#{shippingId};" )
Address selectByShipping_Id ( int shippingId) ;
@Select ( "SELECT * FROM address WHERE user_id=#{userId} ;" )
List< Address> queryAll ( int userId) ;
@Select ( " <script>" +
" SELECT * FROM address " +
"<if test=\"offset!=null and limit!=null\">" +
"limit #{offset},#{limit}" +
"</if>" +
" </script>" )
List< Address> getBatch ( Map params) ;
@Select ( "select count(*) from address" )
int getTotal ( ) ;
@Select ( "select count(*) from address where user_id=#{user_id};" )
int getUserAddressTotal ( int id) ;
@Delete ( "delete from address where id=#{id};" )
int delById ( int id) ;
@Select ( "select * from address where id=#{id};" )
Address getAddressById ( int id) ;
}
service
package com. ysu. shop. service. impl;
import com. ysu. shop. mapper. UserMapper;
import com. ysu. shop. pojo. User;
import com. ysu. shop. service. UserService;
import com. ysu. shop. util. *;
import org. springframework. stereotype. Service;
import javax. annotation. Resource;
import java. util. Date;
import java. util. List;
import java. util. Map;
@Service
@SuppressWarnings ( { "unchecked" , "rawtypes" } )
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
public String getNewToken ( String sessionId, Integer userId) {
String src = sessionId + userId + NumberUtil. genRandomNum ( 4 ) ;
return SystemUtil. genToken ( src) ;
}
@Override
public User getUserByToken ( String token) {
return userMapper. getUserByToken ( token) ;
}
@Override
public User login ( String username, String password) {
String shaPassword = ShaUtil. getSHA256 ( password) ;
User user = userMapper. login ( username, shaPassword) ;
if ( user != null) {
String token = getNewToken ( System. currentTimeMillis ( ) + "" , user. getId ( ) ) ;
if ( userMapper. updateUserToken ( user. getId ( ) , token) > 0 ) {
user. setToken ( token) ;
return user;
}
}
return null;
}
@Override
public int insert ( User user) {
user. setPassword ( ShaUtil. getSHA256 ( user. getPassword ( ) ) ) ;
return userMapper. insertUser ( user) ;
}
@Override
public User queryById ( int id) {
return userMapper. queryById ( id) ;
}
@Override
public User queryByUsername ( String username) {
return userMapper. queryByUsername ( username) ;
}
@Override
public int delById ( int id) {
return delById ( id) ;
}
@Override
public int update ( User user) {
return userMapper. updateUserByUsername ( user) ;
}
@Override
public int update ( String username, String password_old, String password_new) {
password_new = ShaUtil. getSHA256 ( password_new) ;
password_old = ShaUtil. getSHA256 ( password_old) ;
return userMapper. updatePasswordByOldPassword ( username, password_old, password_new) ;
}
@Override
public PageResult getUserPage ( PageUtil pageUtil) {
List< User> users = userMapper. getBatch ( pageUtil) ;
int total = userMapper. getTotal ( ) ;
PageResult pageResult = new PageResult ( users, total, pageUtil. getLimit ( ) , pageUtil. getPage ( ) ) ;
return pageResult;
}
@Override
public int checkValid ( String type, String value) {
System. out. println ( type) ;
if ( "username" . equals ( type) ) {
return userMapper. queryByUsername ( value) == null ? 0 : 1 ;
} else {
return userMapper. queryByEmail ( value) == null ? 0 : 1 ;
}
}
@Override
public User forgetCheckAnswer ( String username, String question, String answer) {
User user = userMapper. answer ( username, question, answer) ;
if ( user != null) {
String token = getNewToken ( System. currentTimeMillis ( ) + "" , user. getId ( ) ) ;
if ( userMapper. updateUserToken ( user. getId ( ) , token) > 0 ) {
user. setToken ( token) ;
return user;
}
}
return null;
}
@Override
public int forgetResetPassword ( String username, String password, String token) {
User user = getUserByToken ( token) ;
if ( user == null)
return 0 ;
if ( ( new Date ( ) . getTime ( ) - user. getUpdate_time ( ) . getTime ( ) ) / ( 1000 * 60 ) > 5 ) {
return 0 ;
}
password = ShaUtil. getSHA256 ( password) ;
return userMapper. forgetResetPassword ( username, password, token) ;
}
@Override
public PageResult queryByName ( Map params) {
PageUtil pageUtil = new PageUtil ( params) ;
List< User> goods = userMapper. queryAllByName ( pageUtil) ;
int total = userMapper. queryAllByNameNum ( pageUtil) ;
return new PageResult ( goods, total, pageUtil. getLimit ( ) , pageUtil. getPage ( ) ) ;
}
}
package com. ysu. shop. service. impl;
import com. ysu. shop. mapper. AddressMapper;
import com. ysu. shop. pojo. Address;
import com. ysu. shop. service. AddressService;
import com. ysu. shop. util. PageResult;
import com. ysu. shop. util. PageUtil;
import org. springframework. stereotype. Service;
import javax. annotation. Resource;
import java. util. List;
@Service
public class AddressServiceImpl implements AddressService {
@Resource
private AddressMapper addressMapper;
@Override
public int addAddress ( Address address) {
return addressMapper. insertAddress ( address) ;
}
@Override
public int delAddressByShoppingId ( int shippingId) {
return addressMapper. delAddressByShipping_id ( shippingId) ;
}
@Override
public int updateAddressByToken ( Address address) {
return addressMapper. updateAddressById ( address) ;
}
@Override
public Address getAddressByShoppingId ( int shippingId) {
return addressMapper. selectByShipping_Id ( shippingId) ;
}
@Override
public PageResult getAddressPage ( PageUtil pageUtil) {
List< Address> address = addressMapper. getBatch ( pageUtil) ;
int total = addressMapper. getTotal ( ) ;
PageResult pageResult = new PageResult ( address, total, pageUtil. getLimit ( ) , pageUtil. getPage ( ) ) ;
return pageResult;
}
@Override
public int getUserAddressNum ( int id) {
return addressMapper. getUserAddressTotal ( id) ;
}
@Override
public int delById ( int id) {
return addressMapper. delById ( id) ;
}
@Override
public List< Address> getAddressByUser ( int id) {
return addressMapper. queryAll ( id) ;
}
@Override
public Address getAddressById ( int id) {
return addressMapper. getAddressById ( id) ;
}
}
control
package com. ysu. shop. controller;
import com. alibaba. fastjson. JSONObject;
import com. ysu. shop. annotation. TokenToUser;
import com. ysu. shop. exception. MyException;
import com. ysu. shop. exception. ParamValidException;
import com. ysu. shop. pojo. User;
import com. ysu. shop. service. UserService;
import com. ysu. shop. util. CheckUtil;
import com. ysu. shop. util. Constants;
import com. ysu. shop. util. Result;
import com. ysu. shop. util. ResultGenerator;
import org. slf4j. Logger;
import org. slf4j. LoggerFactory;
import org. springframework. transaction. annotation. Transactional;
import org. springframework. util. StringUtils;
import org. springframework. web. bind. annotation. *;
import io. swagger. annotations. *;
import springfox. documentation. annotations. ApiIgnore;
import javax. annotation. Resource;
@RestController
@RequestMapping ( "/user" )
@SuppressWarnings ( "rawtypes" )
@Transactional ( noRollbackFor = { MyException. class } , rollbackFor = Exception. class )
@Api ( value = "/user" , tags = "前臺用戶接口" )
public class UserController {
private Logger log = LoggerFactory. getLogger ( UserController. class ) ;
@Resource
private UserService userService;
@ApiOperation ( value = "前臺用戶登錄" , notes = "前臺用戶查看商品信息不需要登錄" )
@ApiImplicitParams ( { @ApiImplicitParam ( name = "password" , value = "密碼" , required = true , paramType = "body" ) ,
@ApiImplicitParam ( name = "username" , value = "用戶名" , required = true , paramType = "body" ) } )
@ApiResponses ( { @ApiResponse ( code = 406 , message = "用戶名或密碼不能爲空" ) , @ApiResponse ( code = 1 , message = "用戶名或密碼不正確" ) ,
@ApiResponse ( code = 200 , message = "SUCCESS" ) } )
@PostMapping ( "/login" )
public Result login ( @ApiIgnore @RequestBody JSONObject jsonParam) {
String username = jsonParam. getString ( "username" ) ;
String password = jsonParam. getString ( "password" ) ;
if ( StringUtils. isEmpty ( username) || StringUtils. isEmpty ( password) ) {
return ResultGenerator. getErrorResult ( Constants. RESULT_CODE_PARAM_ERROR, "用戶名或密碼不能爲空" ) ;
}
User user = userService. login ( username, password) ;
if ( user == null) {
return ResultGenerator. getErrorResult ( "用戶名或密碼不正確" ) ;
}
return ResultGenerator. getSuccessResult ( user) ;
}
@ApiOperation ( value = "前臺用戶註冊" , notes = "默認爲普通用戶" )
@ApiImplicitParams ( { @ApiImplicitParam ( name = "password" , value = "密碼" , required = true , paramType = "body" ) ,
@ApiImplicitParam ( name = "username" , value = "用戶名" , required = true , paramType = "body" ) ,
@ApiImplicitParam ( name = "phone" , value = "聯繫電話" , required = true , paramType = "body" ) ,
@ApiImplicitParam ( name = "email" , value = "電子郵箱地址" , required = true , paramType = "body" ) ,
@ApiImplicitParam ( name = "question" , value = "密保問題" , required = true , paramType = "body" ) ,
@ApiImplicitParam ( name = "answer" , value = "密保答案" , required = true , paramType = "body" )
} )
@ApiResponses ( { @ApiResponse ( code = 500 , message = "用戶已存在" ) , @ApiResponse ( code = 1 , message = "用戶名已存在" ) ,
@ApiResponse ( code = 200 , message = "註冊成功" ) , @ApiResponse ( code = 406 , message = "參數異常!" ) } )
@PostMapping ( "/register" )
public Result register ( @ApiIgnore @RequestBody User user) {
if ( user == null) {
throw new ParamValidException ( "用戶信息不能爲空" ) ;
}
log. debug ( user. toString ( ) ) ;
int r = userService. checkValid ( "username" , user. getUsername ( ) ) ;
if ( r != 0 ) {
return ResultGenerator. getErrorResult ( "用戶名已存在" ) ;
}
user. setRole ( "U" ) ;
r = userService. insert ( user) ;
if ( r == 0 )
return ResultGenerator. getFailResult ( "用戶已存在 " ) ;
return ResultGenerator. getSuccessResult ( "註冊成功" ) ;
}
public Object updateField ( Object newValue, Object oldValue) {
if ( ! StringUtils. isEmpty ( newValue) )
return newValue;
return oldValue;
}
@ApiOperation ( value = "用戶更新個人信息" , notes = "需要先登錄" )
@ApiImplicitParams ( { @ApiImplicitParam ( name = "phone" , value = "聯繫電話" , required = true , paramType = "body" ) ,
@ApiImplicitParam ( name = "email" , value = "電子郵箱地址" , required = true , paramType = "body" ) ,
@ApiImplicitParam ( name = "question" , value = "密保問題" , required = true , paramType = "body" ) ,
@ApiImplicitParam ( name = "answer" , value = "密保答案" , required = true , paramType = "body" ) ,
@ApiImplicitParam ( name = "token" , value = "獲取用戶信息" , required = true , paramType = "header" ) } )
@ApiResponses ( { @ApiResponse ( code = 1 , message = "更新失敗" ) , @ApiResponse ( code = 200 , message = "更新個人信息成功" ) , } )
@PutMapping ( "/updateInformation" )
public Result updateInformation ( @ApiIgnore @RequestBody User user, @TokenToUser User oldUser) {
CheckUtil. checkUserLogin ( oldUser) ;
oldUser. setAnswer ( ( String) updateField ( user. getAnswer ( ) , oldUser. getAnswer ( ) ) ) ;
oldUser. setEmail ( ( String) updateField ( user. getEmail ( ) , oldUser. getEmail ( ) ) ) ;
oldUser. setPhone ( ( String) updateField ( user. getPhone ( ) , oldUser. getPhone ( ) ) ) ;
oldUser. setQuestion ( ( String) updateField ( user. getQuestion ( ) , oldUser. getQuestion ( ) ) ) ;
int r = userService. update ( oldUser) ;
if ( r == 0 ) {
return ResultGenerator. getErrorResult ( "更新失敗" ) ;
}
return ResultGenerator. getSuccessResult ( "更新個人信息成功" ) ;
}
@ApiOperation ( value = "重置密碼" , notes = "登錄用戶操作" )
@ApiImplicitParams ( { @ApiImplicitParam ( name = "password_new" , value = "新密碼" , required = true , paramType = "body" ) ,
@ApiImplicitParam ( name = "password_old" , value = "舊密碼" , required = true , paramType = "body" ) ,
@ApiImplicitParam ( name = "token" , value = "獲取用戶信息" , required = true , paramType = "header" ) } )
@ApiResponses ( { @ApiResponse ( code = 1 , message = "舊密碼輸入錯誤" ) , @ApiResponse ( code = 200 , message = "修改密碼成功" ) , } )
@PutMapping ( "/resetPassword" )
public Result resetPassword ( @ApiIgnore @RequestBody JSONObject jsonObject, @ApiIgnore @TokenToUser User oldUser) {
CheckUtil. checkUserLogin ( oldUser) ;
String password = jsonObject. getString ( "password_new" ) ;
String oldPassword = jsonObject. getString ( "password_old" ) ;
int r = userService. update ( oldUser. getUsername ( ) , oldPassword, password) ;
if ( r == 0 ) {
return ResultGenerator. getErrorResult ( "舊密碼輸入錯誤" ) ;
}
return ResultGenerator. getSuccessResult ( "修改密碼成功" ) ;
}
@ApiOperation ( value = "通過得到個人用戶信息" , notes = "棄用" )
@ApiImplicitParams ( { @ApiImplicitParam ( name = "id" , value = "個人id" , required = true , paramType = "path" ) , } )
@ApiResponses ( { @ApiResponse ( code = 1 , message = "該用戶不存在" ) ,
@ApiResponse ( code = 200 , message = "SUCCESS" , response = User. class ) , } )
@GetMapping ( "/get/{id}" )
@Deprecated
@Transactional ( readOnly = true )
public Result getUser ( @PathVariable int id) {
User user = userService. queryById ( id) ;
if ( user == null) {
return ResultGenerator. getErrorResult ( "該用戶不存在" ) ;
}
return ResultGenerator. getSuccessResult ( user) ;
}
@ApiOperation ( value = "得到個人用戶信息" , notes = "用戶需要登錄" )
@ApiImplicitParams ( { @ApiImplicitParam ( name = "token" , value = "獲取用戶信息" , required = true , paramType = "header" ) } )
@ApiResponses ( { @ApiResponse ( code = 402 , message = "用戶未登錄" ) ,
@ApiResponse ( code = 200 , message = "SUCCESS" , response = User. class ) , } )
@GetMapping ( "/getInformation" )
@Transactional ( readOnly = true )
public Result getUser ( @ApiIgnore @TokenToUser User oldUser) {
CheckUtil. checkUserLogin ( oldUser) ;
return ResultGenerator. getSuccessResult ( oldUser) ;
}
@ApiOperation ( value = "刪除個人用戶" , notes = "棄用" )
@ApiImplicitParams ( { @ApiImplicitParam ( name = "id" , value = "個人id" , required = true , paramType = "path" ) } )
@ApiResponses ( { @ApiResponse ( code = 1 , message = "刪除失敗" ) , @ApiResponse ( code = 200 , message = "刪除成功" ) } )
@DeleteMapping ( "/del/{id}" )
@Deprecated
public Result delUser ( @PathVariable int id) {
int r = userService. delById ( id) ;
if ( r == 0 ) {
return ResultGenerator. getErrorResult ( "刪除失敗" ) ;
}
return ResultGenerator. getSuccessResult ( "刪除成功" ) ;
}
@ApiOperation ( value = "校驗信息" , notes = "註冊時檢查用戶名和郵箱是否存在" )
@ApiImplicitParams ( {
@ApiImplicitParam ( name = "str" , value = "需要校驗的信息" , required = true , paramType = "body" ) ,
@ApiImplicitParam ( name = "type" , value = "需要校驗的信息的類型" , required = true , paramType = "body" ) ,
} )
@ApiResponses ( {
@ApiResponse ( code = 1 , message = "用戶名已存在" ) ,
@ApiResponse ( code = 200 , message = "校驗成功" ) ,
@ApiResponse ( code = 406 , message = "參數不能爲空" )
} )
@PostMapping ( "/checkValid" )
public Result checkValid ( @ApiIgnore @RequestBody JSONObject jsonParam) {
String value = jsonParam. getString ( "str" ) ;
String type = jsonParam. getString ( "type" ) ;
if ( StringUtils. isEmpty ( value) || StringUtils. isEmpty ( type) ) {
return ResultGenerator. getErrorByParams ( "參數不能爲空" ) ;
}
int r = userService. checkValid ( type, value) ;
if ( r != 0 ) {
if ( "username" . equals ( type) ) {
return ResultGenerator. getErrorResult ( "該用戶名已存在" ) ;
}
return ResultGenerator. getErrorResult ( "該郵箱已存在" ) ;
}
return ResultGenerator. getSuccessResult ( "校驗成功" ) ;
}
@ApiOperation ( value = "得到個人用戶信息" , notes = "用戶需要登錄" )
@ApiImplicitParams ( { @ApiImplicitParam ( name = "token" , value = "獲取用戶信息" , required = true , paramType = "header" ) } )
@ApiResponses ( { @ApiResponse ( code = 402 , message = "用戶未登錄" ) ,
@ApiResponse ( code = 200 , message = "SUCCESS" , response = User. class ) , } )
@GetMapping ( "/getUserInfo" )
@Transactional ( readOnly = true )
public Result getUserInfo ( @TokenToUser User oldUser) {
CheckUtil. checkUserLogin ( oldUser) ;
oldUser. setId ( null) ;
oldUser. setPassword ( null) ;
oldUser. setQuestion ( null) ;
oldUser. setAnswer ( null) ;
return ResultGenerator. getSuccessResult ( oldUser) ;
}
@ApiOperation ( value = "忘記密碼的時候需要得到密保問題" , notes = "根據用戶名得到" )
@ApiImplicitParams ( { @ApiImplicitParam ( name = "username" , value = "用戶名" , required = true , paramType = "query" ) } )
@ApiResponses ( {
@ApiResponse ( code = 406 , message = "參數不能爲空" ) ,
@ApiResponse ( code = 200 , message = "SUCCESS" , response = String. class ) ,
@ApiResponse ( code = 1 , message = "你不能設置找回密碼問題,聯繫客服解決" )
} )
@GetMapping ( "/forgetGetQuestion" )
@Transactional ( readOnly = true )
public Result forgetGetQuestion ( String username) {
if ( StringUtils. isEmpty ( username) ) {
return ResultGenerator. getErrorByParams ( "參數不能爲空" ) ;
}
User user = userService. queryByUsername ( username) ;
if ( user == null || StringUtils. isEmpty ( user. getQuestion ( ) ) ) {
return ResultGenerator. getErrorResult ( "你不能設置找回密碼問題,聯繫客服解決" ) ;
}
JSONObject jsonObject = new JSONObject ( ) ;
jsonObject. put ( "question" , user. getQuestion ( ) ) ;
return ResultGenerator. getSuccessResult ( jsonObject) ;
}
@ApiOperation ( value = "忘記密碼的時候需要檢查答案" , notes = "正確返回一個token值,重置密碼需要" )
@ApiImplicitParams ( {
@ApiImplicitParam ( name = "answer" , value = "密保答案" , required = true , paramType = "body" ) ,
@ApiImplicitParam ( name = "username" , value = "用戶名" , required = true , paramType = "body" ) ,
@ApiImplicitParam ( name = "question" , value = "密保問題" , required = true , paramType = "body" ) ,
} )
@ApiResponses ( {
@ApiResponse ( code = 406 , message = "參數不能爲空" ) ,
@ApiResponse ( code = 200 , message = "SUCCESS" , response = String. class ) ,
@ApiResponse ( code = 1 , message = "問題答案錯誤" )
} )
@PostMapping ( "/forgetCheckAnswer" )
public Result forgetCheckAnswer ( @ApiIgnore @RequestBody JSONObject jsonParam) {
String answer = jsonParam. getString ( "answer" ) ;
String username = jsonParam. getString ( "username" ) ;
String question = jsonParam. getString ( "question" ) ;
if ( StringUtils. isEmpty ( answer) || StringUtils. isEmpty ( username) || StringUtils. isEmpty ( question) ) {
return ResultGenerator. getErrorByParams ( "參數不能爲空" ) ;
}
User user = userService. forgetCheckAnswer ( username, question, answer) ;
if ( user == null) {
return ResultGenerator. getErrorResult ( "問題答案錯誤" ) ;
}
return ResultGenerator. getSuccessResult ( ( Object) user. getToken ( ) ) ;
}
@ApiOperation ( value = "忘記密碼的時候需要檢查答案" , notes = "正確返回一個token值,重置密碼需要" )
@ApiImplicitParams ( {
@ApiImplicitParam ( name = "password_new" , value = "新密碼" , required = true , paramType = "body" ) ,
@ApiImplicitParam ( name = "username" , value = "用戶名" , required = true , paramType = "body" ) ,
@ApiImplicitParam ( name = "forget_token" , value = "修改密碼的憑證" , required = true , paramType = "body" ) ,
} )
@ApiResponses ( {
@ApiResponse ( code = 406 , message = "參數不能爲空" ) ,
@ApiResponse ( code = 200 , message = "修改密碼成功" ) ,
@ApiResponse ( code = 1 , message = "token已經失效" )
} )
@PostMapping ( "/forgetResetPassword" )
public Result forgetResetPassword ( @ApiIgnore @RequestBody JSONObject jsonParam) {
String username = jsonParam. getString ( "username" ) ;
String password_new = jsonParam. getString ( "password_new" ) ;
String forget_token = jsonParam. getString ( "forget_token" ) ;
if ( StringUtils. isEmpty ( username) || StringUtils. isEmpty ( password_new) || StringUtils. isEmpty ( forget_token) ) {
return ResultGenerator. getErrorByParams ( "參數不能爲空" ) ;
}
int r = userService. forgetResetPassword ( username, password_new, forget_token) ;
if ( r == 0 ) {
return ResultGenerator. getErrorResult ( "token已經失效" ) ;
}
return ResultGenerator. getSuccessResult ( "修改密碼成功" ) ;
}
}
package com. ysu. shop. controller;
import com. ysu. shop. annotation. TokenToUser;
import com. ysu. shop. exception. AddressLimitException;
import com. ysu. shop. exception. MyException;
import com. ysu. shop. pojo. Address;
import com. ysu. shop. pojo. User;
import com. ysu. shop. service. AddressService;
import com. ysu. shop. util. CheckUtil;
import com. ysu. shop. util. Constants;
import com. ysu. shop. util. Result;
import com. ysu. shop. util. ResultGenerator;
import io. swagger. annotations. *;
import org. springframework. transaction. annotation. Transactional;
import org. springframework. web. bind. annotation. *;
import org. slf4j. Logger;
import org. slf4j. LoggerFactory;
import springfox. documentation. annotations. ApiIgnore;
import javax. annotation. Resource;
@Api ( value = "/user/address" , tags = "前臺地址接口" )
@RestController
@RequestMapping ( "/user/address" )
@SuppressWarnings ( "rawtypes" )
@Transactional ( noRollbackFor= { MyException. class } , rollbackFor = Exception. class )
public class AddressController {
private Logger log = LoggerFactory. getLogger ( AddressController. class ) ;
@Resource
private AddressService addressService;
private int addressNumLimit ( int id) {
int r = addressService. getUserAddressNum ( id) ;
if ( r >= Constants. USER_ADDRESS_NUM_LIMIT) {
throw new AddressLimitException ( "現有地址數已超限" ) ;
}
return r;
}
@ApiOperation ( value= "前臺用戶獲取自己的所有地址的數目" , notes= "用戶根據登錄狀態獲取所有的地址" )
@ApiImplicitParams ( {
@ApiImplicitParam ( name = "token" , value = "獲取用戶信息" , required = true , paramType = "header" )
} )
@ApiResponses ( {
@ApiResponse ( code = 402 , message = "用戶未登錄" ) ,
@ApiResponse ( code = 200 , message = "SUCCESS" , response = Integer. class )
} )
@GetMapping ( "/total" )
@Transactional ( readOnly = true )
public Result getAddressTotal ( @ApiIgnore @TokenToUser User oldUser) {
CheckUtil. checkUserLogin ( oldUser) ;
return ResultGenerator. getSuccessResult ( addressNumLimit ( oldUser. getId ( ) ) ) ;
}
@ApiOperation ( value= "前臺用戶刪除地址" , notes= "前臺用戶根據id刪除地址" )
@ApiImplicitParams ( {
@ApiImplicitParam ( name= "id" , value= "地址id" , required= true , paramType= "path" , dataType= "Integer" ) ,
@ApiImplicitParam ( name = "token" , value = "獲取用戶信息" , required = true , paramType = "header" )
} )
@ApiResponses ( {
@ApiResponse ( code = 402 , message = "用戶未登錄" ) ,
@ApiResponse ( code = 200 , message = "SUCCESS" ) ,
@ApiResponse ( code = 1 , message = "刪除失敗" )
} )
@DeleteMapping ( "/del/{id}" )
public Result del ( @PathVariable int id, @ApiIgnore @TokenToUser User oldUser) {
CheckUtil. checkUserLogin ( oldUser) ;
int r = addressService. delById ( id) ;
if ( r == 0 ) {
return ResultGenerator. getErrorResult ( "刪除失敗" ) ;
}
return ResultGenerator. getSuccessResult ( "刪除成功" ) ;
}
@ApiOperation ( value= "用戶添加地址" , notes= "前臺用戶添加一個自己的地址" )
@ApiImplicitParams ( {
@ApiImplicitParam ( name = "token" , value = "獲取用戶信息" , required = true , paramType = "header" ) ,
@ApiImplicitParam ( name = "receiver_province" , value = "收件人省份" , required = true , paramType = "body" ) ,
@ApiImplicitParam ( name = "receiver_city" , value = "收件人市" , required = true , paramType = "body" ) ,
@ApiImplicitParam ( name = "receiver_district" , value = "收件人區縣" , required = true , paramType = "body" ) ,
@ApiImplicitParam ( name = "receiver_address" , value = "詳細地址" , required = true , paramType = "body" ) ,
@ApiImplicitParam ( name = "receiver_zip" , value = "郵編" , required = true , paramType = "body" )
} )
@ApiResponses ( {
@ApiResponse ( code = 402 , message = "用戶未登錄" ) ,
@ApiResponse ( code = 1 , message = "添加失敗" ) ,
@ApiResponse ( code = 200 , message = "SUCCESS" )
} )
@PostMapping ( "/add" )
public Result add ( @ApiIgnore @RequestBody Address address , @ApiIgnore @TokenToUser User oldUser) {
CheckUtil. checkUserLogin ( oldUser) ;
address. setUser_id ( oldUser. getId ( ) ) ;
address. setReceiver_name ( oldUser. getUsername ( ) ) ;
address. setReceiver_phone ( oldUser. getPhone ( ) ) ;
int r = addressService. addAddress ( address) ;
if ( r == 0 ) {
return ResultGenerator. getErrorResult ( "添加失敗" ) ;
}
return ResultGenerator. getSuccessResult ( "添加成功" ) ;
}
@ApiOperation ( value= "更新地址" , notes= "前臺用戶根據地址id更新地址" )
@ApiImplicitParams ( {
@ApiImplicitParam ( name = "token" , value = "獲取用戶信息" , required = true , paramType = "header" ) ,
@ApiImplicitParam ( name = "id" , value = "地址id" , required = true , paramType = "body" ) ,
@ApiImplicitParam ( name = "receiver_province" , value = "收件人省份" , required = true , paramType = "body" ) ,
@ApiImplicitParam ( name = "receiver_city" , value = "收件人市" , required = true , paramType = "body" ) ,
@ApiImplicitParam ( name = "receiver_district" , value = "收件人區縣" , required = true , paramType = "body" ) ,
@ApiImplicitParam ( name = "receiver_address" , value = "詳細地址" , required = true , paramType = "body" ) ,
@ApiImplicitParam ( name = "receiver_zip" , value = "郵編" , required = true , paramType = "body" )
} )
@ApiResponses ( {
@ApiResponse ( code = 402 , message = "用戶未登錄" ) ,
@ApiResponse ( code = 1 , message = "修改失敗" ) ,
@ApiResponse ( code = 200 , message = "SUCCESS" )
} )
@PutMapping ( "/update" )
public Result update ( @RequestBody Address address , @ApiIgnore @TokenToUser User oldUser) {
CheckUtil. checkUserLogin ( oldUser) ;
System. out. println ( address) ;
int r = addressService. updateAddressByToken ( address) ;
if ( r == 0 ) {
return ResultGenerator. getErrorResult ( "修改失敗" ) ;
}
return ResultGenerator. getSuccessResult ( "修改成功" ) ;
}
@ApiOperation ( value= "獲取所有地址" , notes= "根據登錄狀態獲取所有的地址" )
@ApiImplicitParams ( {
@ApiImplicitParam ( name = "token" , value = "獲取用戶信息" , required = true , paramType = "header" )
} )
@ApiResponses ( {
@ApiResponse ( code = 402 , message = "用戶未登錄" ) ,
@ApiResponse ( code = 200 , message = "SUCCESS" , response = Address. class )
} )
@GetMapping ( "/getAll" )
@Transactional ( readOnly = true )
public Result add ( @ApiIgnore @TokenToUser User oldUser) {
CheckUtil. checkUserLogin ( oldUser) ;
return ResultGenerator. getSuccessResult ( addressService. getAddressByUser ( oldUser. getId ( ) ) ) ;
}
@ApiOperation ( value= "根據id獲取地址信息" , notes= "前臺用戶根據id獲取地址信息" )
@ApiImplicitParams ( {
@ApiImplicitParam ( name= "id" , value= "地址id" , required= true , paramType= "path" , dataType= "Integer" ) ,
@ApiImplicitParam ( name = "token" , value = "獲取用戶信息" , required = true , paramType = "header" )
} )
@ApiResponses ( {
@ApiResponse ( code = 402 , message = "用戶未登錄" ) ,
@ApiResponse ( code = 1 , message = "未查找到該記錄" ) ,
@ApiResponse ( code = 200 , message = "SUCCESS" , response = Address. class )
} )
@GetMapping ( "/get/{id}" )
@Transactional ( readOnly = true )
public Result getAddress ( @PathVariable int id, @ApiIgnore @TokenToUser User oldUser) {
CheckUtil. checkUserLogin ( oldUser) ;
log. debug ( "get---id" + id) ;
Address address = addressService. getAddressById ( id) ;
if ( address == null) {
return ResultGenerator. getErrorResult ( "未查找到該記錄" ) ;
}
return ResultGenerator. getSuccessResult ( address) ;
}
}