重新溫習(xí)下用戶的注冊(cè)的方式,開發(fā)一個(gè)用戶登錄的spring boot接口。源碼:https://github.com/limingios/wxProgram.git 中的wx-springboot 和 No.15
專注于為中小企業(yè)提供成都做網(wǎng)站、成都網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)邱縣免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了近千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
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); ????/** ?????*?查詢用戶對(duì)象 ?????*?@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="用戶注冊(cè)登錄的接口",tags={"注冊(cè)和登錄的controller"})
public class RegistLoginController {
? ?@Autowired
? ?private UserService userService;
? ?@ApiOperation(value="用戶注冊(cè)",notes="用戶注冊(cè)的接口")
? ?@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("用戶名或已經(jīng)存在,請(qǐng)更換在試試!");
? ? ? ?}
? ? ? ?//防止密碼返回被獲取到
? ? ? ?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);
? ?}
}
???? ???????? ???????? ???????? ????
const?app?=?getApp() Page({ ??data:?{ ??}, ??doLogin:?function?(e)?{ ????var?formObject?=?e.detail.value; ????var?username?=?formObject.username; ????var?password?=?formObject.password; ????//?簡(jiǎn)單驗(yàn)證 ????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'?//?默認(rèn)值 ????????}, ????????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("跳轉(zhuǎn)到注冊(cè)"); ??} })
PS:測(cè)試成功了,其實(shí)登錄和注冊(cè)頁面基本一樣的,就是改了個(gè)標(biāo)題,請(qǐng)求的request地址發(fā)生了改變,唯一區(qū)別比較大的是,https://baike.baidu.com/item/criteria/5354117 這是spring boot常用的。