您好,登錄后才能下訂單哦!
從用戶注冊接口開始。源碼:https://github.com/limingios/wxProgram.git 中的wx-springboot
返回統一工具類JSONResult 放入common工程中
package?com.idig8.utils; /** ?*?@Description:?自定義響應數據結構 ?*??????????????這個類是提供給門戶,ios,安卓,微信商城用的 ?*??????????????門戶接受此類數據后需要使用本類的方法轉換成對于的數據類型格式(類,或者list) ?*??????????????其他自行處理 ?*??????????????200:表示成功 ?*??????????????500:表示錯誤,錯誤信息在msg字段中 ?*??????????????501:bean驗證錯誤,不管多少個錯誤都以map形式返回 ?*??????????????502:攔截器攔截到用戶token出錯 ?*??????????????555:異常拋出信息 ?*/ public?class?JSONResult?{ ????//?響應業務狀態 ????private?Integer?status; ????//?響應消息 ????private?String?msg; ????//?響應中的數據 ????private?Object?data; ????private?String?ok;??//?不使用 ????public?static?JSONResult?build(Integer?status,?String?msg,?Object?data)?{ ????????return?new?JSONResult(status,?msg,?data); ????} ????public?static?JSONResult?ok(Object?data)?{ ????????return?new?JSONResult(data); ????} ????public?static?JSONResult?ok()?{ ????????return?new?JSONResult(null); ????} ????public?static?JSONResult?errorMsg(String?msg)?{ ????????return?new?JSONResult(500,?msg,?null); ????} ????public?static?JSONResult?errorMap(Object?data)?{ ????????return?new?JSONResult(501,?"error",?data); ????} ????public?static?JSONResult?errorTokenMsg(String?msg)?{ ????????return?new?JSONResult(502,?msg,?null); ????} ????public?static?JSONResult?errorException(String?msg)?{ ????????return?new?JSONResult(555,?msg,?null); ????} ????public?JSONResult()?{ ????} ????public?JSONResult(Integer?status,?String?msg,?Object?data)?{ ????????this.status?=?status; ????????this.msg?=?msg; ????????this.data?=?data; ????} ????public?JSONResult(Object?data)?{ ????????this.status?=?200; ????????this.msg?=?"OK"; ????????this.data?=?data; ????} ????public?Boolean?isOK()?{ ????????return?this.status?==?200; ????} ????public?Integer?getStatus()?{ ????????return?status; ????} ????public?void?setStatus(Integer?status)?{ ????????this.status?=?status; ????} ????public?String?getMsg()?{ ????????return?msg; ????} ????public?void?setMsg(String?msg)?{ ????????this.msg?=?msg; ????} ????public?Object?getData()?{ ????????return?data; ????} ????public?void?setData(Object?data)?{ ????????this.data?=?data; ????} ????public?String?getOk()?{ ????????return?ok; ????} ????public?void?setOk(String?ok)?{ ????????this.ok?=?ok; ????} } ```` *?加密類MD5Utils放入common工程中 ```?java package?com.idig8.utils; import?java.security.MessageDigest; import?org.apache.commons.codec.binary.Base64; public?class?MD5Utils?{ ????/** ?????*?@Description:?對字符串進行md5加密 ?????*/ ????public?static?String?getMD5Str(String?strValue)?throws?Exception?{ ????????MessageDigest?md5?=?MessageDigest.getInstance("MD5"); ????????String?newstr?=?Base64.encodeBase64String(md5.digest(strValue.getBytes())); ????????return?newstr; ????} ????public?static?void?main(String[]?args)?{ ????????try?{ ????????????String?md5?=?getMD5Str("idig8"); ????????????System.out.println(md5); ????????}?catch?(Exception?e)?{ ????????????e.printStackTrace(); ????????} ????} }
org包下面的放入common工程中,主要里面有個Sid的自動生成Id的工具
UserService.java
package?com.idig8.service; import?com.idig8.pojo.Users; public?interface?UserService?{ ????/** ?????*?判斷用戶名是否存在 ?????*?@param?username ?????*?@return ?????*/ ????public?boolean?queryUsernameIsExist(String?username); ????/** ?????*?保存用戶 ?????*?@param?user ?????*?@return ?????*/ ????public?void?saveUser(Users?user); }
UserServiceImpl.java
package?com.idig8.service; import?org.n3r.idworker.Sid; import?org.springframework.beans.factory.annotation.Autowired; import?org.springframework.transaction.annotation.Propagation; import?org.springframework.transaction.annotation.Transactional; import?com.idig8.mapper.UsersMapper; import?com.idig8.pojo.Users; public?class?UserServiceImpl?implements?UserService?{ ????@Autowired ????private?UsersMapper?usersMapper; ????@Autowired ????private?Sid?sid; ????@Transactional(propagation?=Propagation.SUPPORTS) ????@Override ????public?boolean?queryUsernameIsExist(String?username)?{ ????????Users?user?=?new?Users(); ????????user.setUsername(username); ????????Users?result?=?usersMapper.selectOne(user); ????????return?result==null??false:true; ????} ????@Transactional(propagation?=Propagation.REQUIRED) ????@Override ????public?void?saveUser(Users?user)?{ ????????String?userId?=sid.nextShort(); ????????user.setId(userId); ????????usersMapper.insert(user); ????} }
RegistLoginController
package?com.idig8.controller; import?org.apache.commons.lang3.StringUtils; import?org.springframework.beans.factory.annotation.Autowired; import?org.springframework.web.bind.annotation.PostMapping; import?org.springframework.web.bind.annotation.RequestBody; import?org.springframework.web.bind.annotation.RestController; import?com.github.pagehelper.util.StringUtil; import?com.idig8.pojo.Users; import?com.idig8.service.UserService; import?com.idig8.utils.JSONResult; import?com.idig8.utils.MD5Utils; @RestController public?class?RegistLoginController?{ ????@Autowired ????private?UserService?userService; ????@PostMapping("/regist") ????public?JSONResult?regist(@RequestBody?Users?user)?{ ????????//1.判斷用戶名和密碼不能為空 ????????if(StringUtils.isBlank(user.getUsername())||StringUtils.isBlank(user.getPassword()))?{ ????????????return?JSONResult.errorMsg("用戶名或密碼不能為空"); ????????} ????????//2.判斷用戶名是否存在 ????????boolean?usernameIsExist?=?userService.queryUsernameIsExist(user.getUsername()); ????????if(!usernameIsExist)?{ ????????????user.setNickname(user.getUsername()); ????????????try?{ ????????????????user.setPassword(MD5Utils.getMD5Str(user.getPassword())); ????????????}?catch?(Exception?e)?{ ????????????????return?JSONResult.errorMsg(e.getMessage()); ????????????} ????????????user.setFollowCounts(0); ????????????user.setReceiveLikeCounts(0); ????????????user.setFansCounts(0); ????????????userService.saveUser(user); ????????}else?{ ????????????return?JSONResult.errorMsg("用戶名或已經存在,請更換在試試!"); ????????} ????????return?JSONResult.ok(); ????} }
PS:基本的流程就是先確定業務邏輯,開發service類,開發controller類,common里面都是一些工具。下次說說swagger的使用。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。