转自:https://idig8.com/2018/09/01/xiaochengxujavashizhanxiaochengxudengluyuhouduanliandiao36/

重新温习下用户的注册的方式,开发一个用户登录的spring boot接口。源码:https://github.com/limingios/wxProgram.git 中的wx-springboot 和 No.15

service 类的开发

  • 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;
} }

controller 类的开发

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);
}
}

wx前端部分的开发

<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常用的。

「小程序JAVA实战」小程序登录与后端联调(36)的更多相关文章

  1. 「小程序JAVA实战」小程序的flex布局(22)

    转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-22/ 之前已经把小程序的框架说完了,接下来说说小程序的组件,在说组件之前,先说说布局吧.源码:ht ...

  2. 「小程序JAVA实战」小程序的留言和评价功能(70)

    转自:https://idig8.com/2018/10/28/xiaochengxujavashizhanxiaochengxudeliuyanhepingjiagongneng69/ 目前小程序这 ...

  3. 「小程序JAVA实战」小程序的举报功能开发(68)

    转自:https://idig8.com/2018/09/25/xiaochengxujavashizhanxiaochengxudeweixinapicaidancaozuo66-2/ 通过点击举报 ...

  4. 「小程序JAVA实战」小程序的个人信息作品,收藏,关注(66)

    转自:https://idig8.com/2018/09/24/xiaochengxujavashizhanxiaochengxudegerenxinxizuopinshoucangguanzhu65 ...

  5. 「小程序JAVA实战」小程序的关注功能(65)

    转自:https://idig8.com/2018/09/24/xiaochengxujavashizhanxiaochengxudeguanzhugongneng64/ 在个人页面,根据发布者个人和 ...

  6. 「小程序JAVA实战」小程序的视频点赞功能开发(62)

    转自:https://idig8.com/2018/09/24/xiaochengxujavashizhanxiaochengxudeshipindianzangongnengkaifa61/ 视频点 ...

  7. 「小程序JAVA实战」小程序的springboot后台拦截器(61)

    转自:https://idig8.com/2018/09/24/xiaochengxujavashizhanxiaochengxudespringboothoutailanjieqi60/ 之前咱们把 ...

  8. 「小程序JAVA实战」小程序首页视频(49)

    转自:https://idig8.com/2018/09/21/xiaochengxujavashizhanxiaochengxushouyeshipin48/ 视频显示的内容是视频的截图,用户的头像 ...

  9. 「小程序JAVA实战」小程序视频封面处理(48)

    转自:https://idig8.com/2018/09/16/xiaochengxujavashizhanxiaochengxushipinfengmianchuli47/ 截图这块,在微信小程序工 ...

  10. 「小程序JAVA实战」小程序上传短视频(46)

    转自:https://idig8.com/2018/09/14/xiaochengxujavashizhanxiaochengxushangchuanduanshipin45/ 个人信息:用户上传短视 ...

随机推荐

  1. Eclipse远程调试Java程序

    1. 在服务器上运行jar包时加入参数 -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address= 2. 在Eclipse中操作 ...

  2. 用IdHTTPServer搞个简单的WEB服务器下载文件

    放在公司共享盘中的文件,不时就被其他人剪切走了,本想用Apache搭个服务端,提供文件下载的功能,写php脚本时碰到点问题,没折腾出来,一狠心,用Indy的IdHttpServer写.不过中间也碰到了 ...

  3. C# 后台模拟前台post发送json数据

    public static string PostMoths(string url, string param) { string strURL = url; System.Net.HttpWebRe ...

  4. Codeforces 559C Gerald and Giant Chess【组合数学】【DP】

    LINK 题目大意 有一个wxh的网格,上面有n个黑点,问你从(1,1)走到(w,h)不经过任何黑点的方案数 思路 考虑容斥 先把所有黑点按照x值进行排序方便计算 \(dp_{i}\)表示从起点走到第 ...

  5. 使用python处理selenium中的css_selector定位元素的模糊匹配问题

    # 匹配id,先指定一个html标签,然后加上“#”符号,再加上id的属性值 self.driver.find_element_by_css_selector('div#ID').click() # ...

  6. Python Qt5 Creator 使用创建项目教程

    1.下载Creator 4.2.1 2.点击文件-新建项目-QT-QT designer Form 然后 choose 3. 4.窗口组件选择 5.下一步,然后就可以自己设计了,,, 最后说一下,保存 ...

  7. ZBar的简单使用

    NSRunLoop类声明的编程接口用于管理输入源对象.一个NSRunLoop对象处理像来自窗体系统中的鼠标和键盘事件,NSPORT对象和NSConnection连接对象这类的输入源.一个NSRunLo ...

  8. ecmall公告挂件分析(转)--此挂件写法已有更新的写法。

    ecmall的首页,基本上都是由挂件的形式实现的.ecmall所有的挂件程序,都在external\widgets文件下面.ecmall首页公告的插件,就是notice目录里面. 分析里面文件,con ...

  9. licode从客户端到连上信令服务器流程

    var config = {audio: true, video: true, data: true, screen: screen, videoSize: [640, 480, 640, 480], ...

  10. OPCDAAuto.dll 的一个坑

    最近项目需要对SCADA系统的下位机采集实时数据,常见做法是两种,一种采用ModBus RTU/TCP协议直接通过支持ModBus的下位机通信,一种是通过OPC规范,使用厂商提供的OPC Server ...