您好,登錄后才能下訂單哦!
重新溫習下用戶的注冊的方式,開發一個用戶登錄的spring boot接口。源碼:https://github.com/limingios/wxProgram.git 中的wx-springboot 和 No.15
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); ????/** ?????*?查詢用戶對象 ?????*?@param?username ?????*?@return ?????*/ ????public?Users?queryUserIsExist(Users?user); } ··· *?UserServiceImpl.java >?Sid?是注入的id的生成工具,Example?queryExample?=?new?Example(Users.class); 和?Criteria?criteria?=?queryExample.createCriteria();?都是一種套路,就是查詢套路,大家可以看看 https://baike.baidu.com/item/criteria/5354117 ···?java package?com.idig8.service; import?org.n3r.idworker.Sid; import?org.springframework.beans.factory.annotation.Autowired; import?org.springframework.boot.autoconfigure.security.SecurityProperties.User; import?org.springframework.stereotype.Service; import?org.springframework.transaction.annotation.Propagation; import?org.springframework.transaction.annotation.Transactional; import?com.idig8.mapper.UsersMapper; import?com.idig8.pojo.Users; import?com.idig8.utils.MD5Utils; import?tk.mybatis.mapper.entity.Example; import?tk.mybatis.mapper.entity.Example.Criteria; @Service 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); ????} ????@Transactional(propagation?=Propagation.SUPPORTS) ????@Override ????public?Users?queryUserIsExist(Users?user)?{ ????????Example?queryExample?=?new?Example(Users.class); ????????Criteria?criteria?=?queryExample.createCriteria(); ????????criteria.andEqualTo("username",user.getUsername()); ????????try?{ ????????????criteria.andEqualTo("password",MD5Utils.getMD5Str(user.getPassword())); ????????}?catch?(Exception?e)?{ ????????????//?TODO?Auto-generated?catch?block ????????????e.printStackTrace(); ????????} ????????Users?userOne?=??usersMapper.selectOneByExample(queryExample); ????????return?userOne; ????} }
RegistLoginController.java
增加了登錄方法。
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.idig8.pojo.Users;
import com.idig8.service.UserService;
import com.idig8.utils.JSONResult;
import com.idig8.utils.MD5Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@RestController
@Api(value="用戶注冊登錄的接口",tags={"注冊和登錄的controller"})
public class RegistLoginController {
? ?@Autowired
? ?private UserService userService;
? ?@ApiOperation(value="用戶注冊",notes="用戶注冊的接口")
? ?@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("用戶名或已經存在,請更換在試試!");
? ? ? ?}
? ? ? ?//防止密碼返回被獲取到
? ? ? ?user.setPassword("");
? ? ? ?return JSONResult.ok(user);
? ?}
? ?@ApiOperation(value="用戶登錄",notes="用戶登錄的接口")
? ?@PostMapping("/login")
? ?public JSONResult login(@RequestBody Users user) {
? ? ? ?//1.判斷用戶名和密碼不能為空
? ? ? ?if(StringUtils.isBlank(user.getUsername())||StringUtils.isBlank(user.getPassword())) {
? ? ? ? ? ?return JSONResult.errorMsg("用戶名或密碼不能為空");
? ? ? ?}
? ? ? ?//2.判斷用戶名是否存在
? ? ? ?Users userObject = userService.queryUserIsExist(user);
? ? ? ?if(userObject==null){
? ? ? ? ? ?return JSONResult.errorMsg("用戶名或密碼不存在!");
? ? ? ?}
? ? ? ?//防止密碼返回被獲取到
? ? ? ?user.setPassword("");
? ? ? ?return JSONResult.ok(userObject);
? ?}
}
<view> ????<view?class="login-icon"> ????????<image?class="login-img"?src="../../resource/images/dsp.jpg"></image> ????</view> ????<view?class="login-from"> ????????<form?bindsubmit='doLogin'> ????????????<!--賬號--> ????????????<view?class="inputView"> ????????????????<image?class="nameImage"?src="../../resource/images/username.png"></image> ????????????????<label?class="loginLabel">賬號</label> ????????????????<input?name="username"?type="text"??maxlength="10"?class="inputText"?placeholder="請輸入賬號"/> ????????????</view> ????????????<view?class="line"></view> ????????????<!--密碼--> ????????????<view?class="inputView"> ????????????????<image?class="keyImage"?src="../../resource/images/password.png"></image> ????????????????<label?class="loginLabel">密碼</label> ????????????????<input?name="password"?maxlength="10"?type="text"?class="inputText"?password="{{true}}"?placeholder="請輸入密碼"/> ????????????</view> ????????????<!--按鈕--> ????????????<view> ????????????????<button?class="loginBtn"?type="primary"?form-type='submit'>登陸</button> ????????????</view> ????????????<view> ????????????????<button?class="goLoginBtn"?type="warn"?bindtap="goRegisterPage">返回注冊</button> ????????????</view> ????????</form> ????</view> </view>
const?app?=?getApp() Page({ ??data:?{ ??}, ??doLogin:?function?(e)?{ ????var?formObject?=?e.detail.value; ????var?username?=?formObject.username; ????var?password?=?formObject.password; ????//?簡單驗證 ????if?(username.length?==?0?||?password.length?==?0)?{ ??????wx.showToast({ ????????title:?'用戶名或密碼不能為空', ????????icon:?'none', ????????duration:?3000 ??????}) ????}?else?{ ??????wx.request({ ????????url:?app.serverUrl?+?"/login", ????????method:?"POST", ????????data:?{ ??????????username:?username, ??????????password:?password ????????}, ????????header:?{ ??????????'content-type':?'application/json'?//?默認值 ????????}, ????????success:?function?(res)?{ ??????????console.log(res.data); ??????????var?status?=?res.data.status; ??????????if?(status?==?200)?{ ????????????wx.showToast({ ??????????????title:?"用戶登陸成功~!", ??????????????icon:?'none', ??????????????duration:?3000 ????????????}) ????????????app.userinfo?=?res.data.data; ??????????}?else?if?(status?==?500)?{ ????????????wx.showToast({ ??????????????title:?res.data.msg, ??????????????icon:?'none', ??????????????duration:?3000 ????????????}) ??????????} ????????} ??????}) ????} ??}, ??goLoginPage:?function?(e)?{ ????console.log("跳轉到注冊"); ??} })
PS:測試成功了,其實登錄和注冊頁面基本一樣的,就是改了個標題,請求的request地址發生了改變,唯一區別比較大的是,https://baike.baidu.com/item/criteria/5354117 這是spring boot常用的。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。