Skip to content

Commit

Permalink
update[litemall-wx,litemall-wx-api,litemall-db]:实现账户登录,但多次失败验证码还不支持。"
Browse files Browse the repository at this point in the history
  • Loading branch information
linlinjava committed Apr 1, 2018
1 parent eb499e8 commit 8bd3e8d
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,10 @@ public int count() {
LitemallUserExample example = new LitemallUserExample();
return (int)userMapper.countByExample(example);
}

public List<LitemallUser> queryByUsername(String username) {
LitemallUserExample example = new LitemallUserExample();
example.or().andUsernameEqualTo(username);
return userMapper.selectByExample(example);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
Expand All @@ -35,14 +36,59 @@ public class WxAuthController {
@Autowired
private WxMaService wxService;

/**
* 微信登录
*/
@RequestMapping("login")
public Object login(@RequestBody String body, HttpServletRequest request) {
String username = JacksonUtil.parseString(body, "username");
String password = JacksonUtil.parseString(body, "password");
if(username == null || password == null){
return ResponseUtil.badArgument();
}

List<LitemallUser> userList =userService.queryByUsername(username);
LitemallUser user = null;
if(userList.size() > 1){
return ResponseUtil.fail502();
}
else if(userList.size() == 0){
return ResponseUtil.badArgumentValue();
}
else {
user = userList.get(0);
}

if(!user.getPassword().equals(password)){
return ResponseUtil.badArgumentValue();
}

// userInfo
UserInfo userInfo = new UserInfo();
userInfo.setNickName(username);
userInfo.setAvatarUrl(user.getAvatar());

// token
UserToken userToken = UserTokenManager.generateToken(user.getId());

Map<Object, Object> result = new HashMap<Object, Object>();
result.put("token", userToken.getToken());
result.put("tokenExpire", userToken.getExpireTime().toString());
result.put("userInfo", userInfo);
return ResponseUtil.ok(result);
}

/**
* 微信登录
*/
@RequestMapping("login_by_weixin")
public Object loginByWeixin(@RequestBody String body, HttpServletRequest request) {

String code = JacksonUtil.parseString(body, "code");
FullUserInfo fullUserInfo = JacksonUtil.parseObject(body, "userInfo", FullUserInfo.class);
if(code == null || fullUserInfo == null){
return ResponseUtil.badArgument();
}

UserInfo userInfo = fullUserInfo.getUserInfo();

String sessionKey = null;
Expand Down
1 change: 1 addition & 0 deletions litemall-wx/config/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module.exports = {
CatalogCurrent: WxApiRoot + 'catalog/current', //分类目录当前分类数据接口

AuthLoginByWeixin: WxApiRoot + 'auth/login_by_weixin', //微信登录
AuthLoginByAccount: WxApiRoot + 'auth/login', //账号登录

GoodsCount: WxApiRoot + 'goods/count', //统计商品总数
GoodsList: WxApiRoot + 'goods/list', //获得商品列表
Expand Down
12 changes: 8 additions & 4 deletions litemall-wx/pages/auth/login/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Page({
accountLogin: function () {
var that = this;

if (that.data.password.length < 1 || that.data.username.length < 1) {
if (this.data.password.length < 1 || this.data.username.length < 1) {
wx.showModal({
title: '错误信息',
content: '请输入用户名和密码',
Expand All @@ -58,7 +58,7 @@ Page({
}

wx.request({
url: api.ApiRootUrl + 'auth/login',
url: api.AuthLoginByAccount,
data: {
username: that.data.username,
password: that.data.password
Expand All @@ -68,11 +68,12 @@ Page({
'content-type': 'application/json'
},
success: function (res) {
if(res.data.code == 200){
if (res.data.errno == 0){
that.setData({
'loginErrorCount': 0
loginErrorCount: 0
});
app.globalData.hasLogin = true;
wx.setStorageSync('userInfo', res.data.data.userInfo);
wx.setStorage({
key:"token",
data: res.data.data.token,
Expand All @@ -84,6 +85,9 @@ Page({
});
}
else{
that.setData({
loginErrorCount: that.data.loginErrorCount + 1
});
app.globalData.hasLogin = false;
util.showErrorToast('账户登录失败');
}
Expand Down
14 changes: 7 additions & 7 deletions litemall-wx/pages/auth/login/login.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<view class="form-box">

<view class="form-item">
<input class="username" value="{{username}}" bindinput="bindUsernameInput" placeholder="账号" auto-focus/>
<input class="username" value="{{username}}" bindinput="bindUsernameInput" placeholder="账号"/>
<image wx:if="{{ username.length > 0 }}" id="clear-username" class="clear" src="/static/images/clear_input.png" catchtap="clearInput"></image>
</view>

Expand All @@ -11,22 +11,22 @@
<image class="clear" id="clear-password" wx:if="{{ password.length > 0 }}" src="/static/images/clear_input.png" catchtap="clearInput"></image>
</view>

<view class="form-item-code" wx-if="{{loginErrorCount >= 3}}">
<!-- <view class="form-item-code" wx-if="{{loginErrorCount >= 3}}">
<view class="form-item code-item">
<input class="code" value="{{code}}" bindinput="bindCodeInput" placeholder="验证码"/>
<image class="clear" id="clear-code" wx:if="{{ code.length > 0 }}" src="/static/images/clear_input.png" catchtap="clearInput"></image>
</view>
<image class="code-img" src="https://dl.reg.163.com/cp?pd=yanxuan_web&pkid=SkeBZeG&random=1489903563234"></image>
</view>
</view> -->

<button type="default" class="login-btn" bindtap="accountLogin">账号登录</button>

<view class="form-item-text">
<navigator url="/pages/auth/register/register" class="register">注册账号</navigator>
<view class="form-item-text">
<navigator url="/pages/auth/register/register" class="register">注册账号</navigator>
<navigator url="/pages/auth/reset/reset" class="reset">忘记密码</navigator>
</view>
</view>

<button type="primary" class="login-btn" bindtap="wxLogin">微信直接登录</button>
<button type="primary" class="login-btn" bindtap="wxLogin">微信直接登录</button>

</view>
</view>

0 comments on commit 8bd3e8d

Please sign in to comment.