Search in sources :

Example 1 with UserInfo

use of org.linlinjava.litemall.wx.dto.UserInfo in project litemall by linlinjava.

the class WxAuthController method register.

/**
 * 账号注册
 *
 * @param body    请求内容
 *                {
 *                username: xxx,
 *                password: xxx,
 *                mobile: xxx
 *                code: xxx
 *                }
 *                其中code是手机验证码,目前还不支持手机短信验证码
 * @param request 请求对象
 * @return 登录结果
 * 成功则
 * {
 * errno: 0,
 * errmsg: '成功',
 * data:
 * {
 * token: xxx,
 * tokenExpire: xxx,
 * userInfo: xxx
 * }
 * }
 * 失败则 { errno: XXX, errmsg: XXX }
 */
@PostMapping("register")
public Object register(@RequestBody String body, HttpServletRequest request) {
    String username = JacksonUtil.parseString(body, "username");
    String password = JacksonUtil.parseString(body, "password");
    String mobile = JacksonUtil.parseString(body, "mobile");
    String code = JacksonUtil.parseString(body, "code");
    // 如果是小程序注册,则必须非空
    // 其他情况,可以为空
    String wxCode = JacksonUtil.parseString(body, "wxCode");
    if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password) || StringUtils.isEmpty(mobile) || StringUtils.isEmpty(code)) {
        return ResponseUtil.badArgument();
    }
    List<LitemallUser> userList = userService.queryByUsername(username);
    if (userList.size() > 0) {
        return ResponseUtil.fail(AUTH_NAME_REGISTERED, "用户名已注册");
    }
    userList = userService.queryByMobile(mobile);
    if (userList.size() > 0) {
        return ResponseUtil.fail(AUTH_MOBILE_REGISTERED, "手机号已注册");
    }
    if (!RegexUtil.isMobileSimple(mobile)) {
        return ResponseUtil.fail(AUTH_INVALID_MOBILE, "手机号格式不正确");
    }
    // 判断验证码是否正确
    String cacheCode = CaptchaCodeManager.getCachedCaptcha(mobile);
    if (cacheCode == null || cacheCode.isEmpty() || !cacheCode.equals(code)) {
        return ResponseUtil.fail(AUTH_CAPTCHA_UNMATCH, "验证码错误");
    }
    String openId = "";
    // 继续验证openid
    if (!StringUtils.isEmpty(wxCode)) {
        try {
            WxMaJscode2SessionResult result = this.wxService.getUserService().getSessionInfo(wxCode);
            openId = result.getOpenid();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail(AUTH_OPENID_UNACCESS, "openid 获取失败");
        }
        userList = userService.queryByOpenid(openId);
        if (userList.size() > 1) {
            return ResponseUtil.serious();
        }
        if (userList.size() == 1) {
            LitemallUser checkUser = userList.get(0);
            String checkUsername = checkUser.getUsername();
            String checkPassword = checkUser.getPassword();
            if (!checkUsername.equals(openId) || !checkPassword.equals(openId)) {
                return ResponseUtil.fail(AUTH_OPENID_BINDED, "openid已绑定账号");
            }
        }
    }
    LitemallUser user = null;
    BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
    String encodedPassword = encoder.encode(password);
    user = new LitemallUser();
    user.setUsername(username);
    user.setPassword(encodedPassword);
    user.setMobile(mobile);
    user.setWeixinOpenid(openId);
    user.setAvatar("https://yanxuan.nosdn.127.net/80841d741d7fa3073e0ae27bf487339f.jpg?imageView&quality=90&thumbnail=64x64");
    user.setNickname(username);
    user.setGender((byte) 0);
    user.setUserLevel((byte) 0);
    user.setStatus((byte) 0);
    user.setLastLoginTime(LocalDateTime.now());
    user.setLastLoginIp(IpUtil.getIpAddr(request));
    userService.add(user);
    // 给新用户发送注册优惠券
    couponAssignService.assignForRegister(user.getId());
    // userInfo
    UserInfo userInfo = new UserInfo();
    userInfo.setNickName(username);
    userInfo.setAvatarUrl(user.getAvatar());
    // token
    String token = UserTokenManager.generateToken(user.getId());
    Map<Object, Object> result = new HashMap<Object, Object>();
    result.put("token", token);
    result.put("userInfo", userInfo);
    return ResponseUtil.ok(result);
}
Also used : HashMap(java.util.HashMap) WxMaJscode2SessionResult(cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult) UserInfo(org.linlinjava.litemall.wx.dto.UserInfo) LitemallUser(org.linlinjava.litemall.db.domain.LitemallUser) BCryptPasswordEncoder(org.linlinjava.litemall.core.util.bcrypt.BCryptPasswordEncoder)

Example 2 with UserInfo

use of org.linlinjava.litemall.wx.dto.UserInfo in project litemall by linlinjava.

the class WxAuthController method loginByWeixin.

/**
 * 微信登录
 *
 * @param wxLoginInfo 请求内容,{ code: xxx, userInfo: xxx }
 * @param request     请求对象
 * @return 登录结果
 */
@PostMapping("login_by_weixin")
public Object loginByWeixin(@RequestBody WxLoginInfo wxLoginInfo, HttpServletRequest request) {
    String code = wxLoginInfo.getCode();
    UserInfo userInfo = wxLoginInfo.getUserInfo();
    if (code == null || userInfo == null) {
        return ResponseUtil.badArgument();
    }
    String sessionKey = null;
    String openId = null;
    try {
        WxMaJscode2SessionResult result = this.wxService.getUserService().getSessionInfo(code);
        sessionKey = result.getSessionKey();
        openId = result.getOpenid();
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (sessionKey == null || openId == null) {
        return ResponseUtil.fail();
    }
    LitemallUser user = userService.queryByOid(openId);
    if (user == null) {
        user = new LitemallUser();
        user.setUsername(openId);
        user.setPassword(openId);
        user.setWeixinOpenid(openId);
        user.setAvatar(userInfo.getAvatarUrl());
        user.setNickname(userInfo.getNickName());
        user.setGender(userInfo.getGender());
        user.setUserLevel((byte) 0);
        user.setStatus((byte) 0);
        user.setLastLoginTime(LocalDateTime.now());
        user.setLastLoginIp(IpUtil.getIpAddr(request));
        user.setSessionKey(sessionKey);
        userService.add(user);
        // 新用户发送注册优惠券
        couponAssignService.assignForRegister(user.getId());
    } else {
        user.setLastLoginTime(LocalDateTime.now());
        user.setLastLoginIp(IpUtil.getIpAddr(request));
        user.setSessionKey(sessionKey);
        if (userService.updateById(user) == 0) {
            return ResponseUtil.updatedDataFailed();
        }
    }
    // token
    String token = UserTokenManager.generateToken(user.getId());
    Map<Object, Object> result = new HashMap<Object, Object>();
    result.put("token", token);
    result.put("userInfo", userInfo);
    return ResponseUtil.ok(result);
}
Also used : HashMap(java.util.HashMap) WxMaJscode2SessionResult(cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult) UserInfo(org.linlinjava.litemall.wx.dto.UserInfo) LitemallUser(org.linlinjava.litemall.db.domain.LitemallUser)

Example 3 with UserInfo

use of org.linlinjava.litemall.wx.dto.UserInfo in project litemall by linlinjava.

the class WxAuthController method login.

/**
 * 账号登录
 *
 * @param body    请求内容,{ username: xxx, password: xxx }
 * @param request 请求对象
 * @return 登录结果
 */
@PostMapping("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.serious();
    } else if (userList.size() == 0) {
        return ResponseUtil.fail(AUTH_INVALID_ACCOUNT, "账号不存在");
    } else {
        user = userList.get(0);
    }
    BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
    if (!encoder.matches(password, user.getPassword())) {
        return ResponseUtil.fail(AUTH_INVALID_ACCOUNT, "账号密码不对");
    }
    // 更新登录情况
    user.setLastLoginTime(LocalDateTime.now());
    user.setLastLoginIp(IpUtil.getIpAddr(request));
    if (userService.updateById(user) == 0) {
        return ResponseUtil.updatedDataFailed();
    }
    // userInfo
    UserInfo userInfo = new UserInfo();
    userInfo.setNickName(username);
    userInfo.setAvatarUrl(user.getAvatar());
    // token
    String token = UserTokenManager.generateToken(user.getId());
    Map<Object, Object> result = new HashMap<Object, Object>();
    result.put("token", token);
    result.put("userInfo", userInfo);
    return ResponseUtil.ok(result);
}
Also used : HashMap(java.util.HashMap) UserInfo(org.linlinjava.litemall.wx.dto.UserInfo) LitemallUser(org.linlinjava.litemall.db.domain.LitemallUser) BCryptPasswordEncoder(org.linlinjava.litemall.core.util.bcrypt.BCryptPasswordEncoder)

Example 4 with UserInfo

use of org.linlinjava.litemall.wx.dto.UserInfo in project litemall by linlinjava.

the class UserInfoService method getInfo.

public UserInfo getInfo(Integer userId) {
    LitemallUser user = userService.findById(userId);
    Assert.state(user != null, "用户不存在");
    UserInfo userInfo = new UserInfo();
    userInfo.setNickName(user.getNickname());
    userInfo.setAvatarUrl(user.getAvatar());
    return userInfo;
}
Also used : UserInfo(org.linlinjava.litemall.wx.dto.UserInfo) LitemallUser(org.linlinjava.litemall.db.domain.LitemallUser)

Example 5 with UserInfo

use of org.linlinjava.litemall.wx.dto.UserInfo in project litemall by linlinjava.

the class WxCommentController method list.

/**
 * 评论列表
 *
 * @param type     类型ID。 如果是0,则查询商品评论;如果是1,则查询专题评论。
 * @param valueId  商品或专题ID。如果type是0,则是商品ID;如果type是1,则是专题ID。
 * @param showType 显示类型。如果是0,则查询全部;如果是1,则查询有图片的评论。
 * @param page     分页页数
 * @param limit     分页大小
 * @return 评论列表
 */
@GetMapping("list")
public Object list(@NotNull Byte type, @NotNull Integer valueId, @NotNull Integer showType, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit) {
    List<LitemallComment> commentList = commentService.query(type, valueId, showType, page, limit);
    List<Map<String, Object>> commentVoList = new ArrayList<>(commentList.size());
    for (LitemallComment comment : commentList) {
        Map<String, Object> commentVo = new HashMap<>();
        commentVo.put("addTime", comment.getAddTime());
        commentVo.put("content", comment.getContent());
        commentVo.put("adminContent", comment.getAdminContent());
        commentVo.put("picList", comment.getPicUrls());
        commentVo.put("star", comment.getStar());
        UserInfo userInfo = userInfoService.getInfo(comment.getUserId());
        commentVo.put("userInfo", userInfo);
        commentVoList.add(commentVo);
    }
    return ResponseUtil.okList(commentVoList, commentList);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) UserInfo(org.linlinjava.litemall.wx.dto.UserInfo) LitemallComment(org.linlinjava.litemall.db.domain.LitemallComment) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

UserInfo (org.linlinjava.litemall.wx.dto.UserInfo)5 HashMap (java.util.HashMap)4 LitemallUser (org.linlinjava.litemall.db.domain.LitemallUser)4 WxMaJscode2SessionResult (cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult)2 BCryptPasswordEncoder (org.linlinjava.litemall.core.util.bcrypt.BCryptPasswordEncoder)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 LitemallComment (org.linlinjava.litemall.db.domain.LitemallComment)1