ソースを参照

update[litemall-wx,litemall-wx-api,litemall-db]:实现用户注册,但是因为手机短信发送不支持,因此手机号码验证码是无意义的。“

Junling Bu 7 年 前
コミット
38f8d93e52

+ 6 - 0
litemall-db/src/main/java/org/linlinjava/litemall/db/service/LitemallUserService.java

@@ -69,4 +69,10 @@ public class LitemallUserService {
         example.or().andUsernameEqualTo(username);
         return userMapper.selectByExample(example);
     }
+
+    public List<LitemallUser> queryByMobile(String mobile) {
+        LitemallUserExample example = new LitemallUserExample();
+        example.or().andMobileEqualTo(mobile);
+        return userMapper.selectByExample(example);
+    }
 }

+ 56 - 1
litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxAuthController.java

@@ -15,6 +15,7 @@ import org.linlinjava.litemall.wx.dao.UserToken;
 import org.linlinjava.litemall.wx.service.UserTokenManager;
 import org.linlinjava.litemall.wx.util.IpUtil;
 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.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
@@ -37,7 +38,7 @@ public class WxAuthController {
     private WxMaService wxService;
 
     /**
-     * 微信登录
+     * 账号登录
      */
     @RequestMapping("login")
     public Object login(@RequestBody String body, HttpServletRequest request) {
@@ -143,4 +144,58 @@ public class WxAuthController {
         result.put("userInfo", userInfo);
         return ResponseUtil.ok(result);
     }
+
+    /**
+     * 账号注册
+     */
+    @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");
+
+        if(username == null || password == null || mobile == null || code == null){
+            return ResponseUtil.badArgument();
+        }
+
+        List<LitemallUser> userList = userService.queryByUsername(username);
+        if(userList.size() > 0){
+            return ResponseUtil.fail(403, "用户名已注册");
+        }
+
+        userList = userService.queryByMobile(mobile);
+        if(userList.size() > 0){
+            return ResponseUtil.fail(403, "手机号已注册");
+        }
+
+        LitemallUser user = new LitemallUser();
+        user = new LitemallUser();
+        user.setUsername(username);
+        user.setPassword(password);
+        user.setWeixinOpenid("");
+        user.setAvatar("https://yanxuan.nosdn.127.net/80841d741d7fa3073e0ae27bf487339f.jpg?imageView&quality=90&thumbnail=64x64");
+        user.setNickname(username);
+        user.setGender("未知");
+        user.setUserLevel("普通用户");
+        user.setStatus("可用");
+        user.setLastLoginTime(LocalDate.now());
+        user.setLastLoginIp(IpUtil.client(request));
+        userService.add(user);
+
+
+        // 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);
+    }
 }

+ 5 - 4
litemall-wx/config/api.js

@@ -22,6 +22,7 @@ module.exports = {
 
     AuthLoginByWeixin: WxApiRoot + 'auth/login_by_weixin', //微信登录
     AuthLoginByAccount: WxApiRoot + 'auth/login', //账号登录
+    AuthRegister: WxApiRoot + 'auth/register', //账号注册
 
     GoodsCount: WxApiRoot + 'goods/count',  //统计商品总数
     GoodsList: WxApiRoot + 'goods/list',  //获得商品列表
@@ -43,7 +44,6 @@ module.exports = {
     CartGoodsCount: WxApiRoot + 'cart/goodscount', // 获取购物车商品件数
     CartCheckout: WxApiRoot + 'cart/checkout', // 下单前信息确认
 
-    OrderSubmit: WxApiRoot + 'order/submit', // 提交订单
     PayPrepayId: WxApiRoot + 'pay/prepay', //获取微信统一下单prepay_id
 
     CollectList: WxApiRoot + 'collect/list',  //收藏列表
@@ -57,10 +57,10 @@ module.exports = {
     TopicDetail: WxApiRoot + 'topic/detail',  //专题详情
     TopicRelated: WxApiRoot + 'topic/related',  //相关专题
 
-    SearchIndex: WxApiRoot + 'search/index',  //搜索页面数据
-    SearchResult: WxApiRoot + 'search/result',  //搜索数据
+    SearchIndex: WxApiRoot + 'search/index',  //搜索关键字
+    SearchResult: WxApiRoot + 'search/result',  //搜索结果
     SearchHelper: WxApiRoot + 'search/helper',  //搜索帮助
-    SearchClearHistory: WxApiRoot + 'search/clearhistory',  //搜索帮助
+    SearchClearHistory: WxApiRoot + 'search/clearhistory',  //搜索历史清楚
 
     AddressList: WxApiRoot + 'address/list',  //收货地址列表
     AddressDetail: WxApiRoot + 'address/detail',  //收货地址详情
@@ -69,6 +69,7 @@ module.exports = {
 
     RegionList: WxApiRoot + 'region/list',  //获取区域列表
 
+    OrderSubmit: WxApiRoot + 'order/submit', // 提交订单
     OrderList: WxApiRoot + 'order/list',  //订单列表
     OrderDetail: WxApiRoot + 'order/detail',  //订单详情
     OrderCancel: WxApiRoot + 'order/cancel',  //取消订单

+ 38 - 11
litemall-wx/pages/auth/register/register.js

@@ -5,8 +5,8 @@ Page({
     username: '',
     password: '',
     confirmPassword: '',
-    code: '',
-    loginErrorCount: 0
+    mobile: '',
+    code: ''
   },
   onLoad: function (options) {
     // 页面初始化 options为页面跳转所带来的参数
@@ -28,10 +28,17 @@ Page({
     // 页面关闭
 
   },
+  sendCode: function () {
+    wx.showModal({
+      title: '注意',
+      content: '由于目前不支持手机短信发送,因此验证码任意值都可以',
+      showCancel: false
+    });
+  },
   startRegister: function () {
     var that = this;
 
-    if (that.data.password.length < 3 || that.data.username.length < 3) {
+    if (this.data.password.length < 3 || this.data.username.length < 3) {
       wx.showModal({
         title: '错误信息',
         content: '用户名和密码不得少于3位',
@@ -40,7 +47,7 @@ Page({
       return false;
     }
 
-    if (that.data.password != that.data.confirmPassword) {
+    if (this.data.password != this.data.confirmPassword) {
       wx.showModal({
         title: '错误信息',
         content: '确认密码不一致',
@@ -49,21 +56,31 @@ Page({
       return false;
     }
 
+    if (this.data.mobile.length == 0 || this.data.code.length == 0) {
+      wx.showModal({
+        title: '错误信息',
+        content: '手机号和验证码不能为空',
+        showCancel: false
+      });
+      return false;
+    }
+
     wx.request({
-      url: api.ApiRootUrl + 'auth/register',
+      url: api.AuthRegister,
       data: {
         username: that.data.username,
-        password: that.data.password
+        password: that.data.password,
+        mobile: that.data.mobile,
+        code: that.data.code
       },
       method: 'POST',
       header: {
         'content-type': 'application/json'
       },
       success: function (res) {
-        if (res.data.code == 200) {
-          that.setData({
-            'loginErrorCount': 0
-          });
+        if (res.data.errno == 0) {
+          app.globalData.hasLogin = true;
+          wx.setStorageSync('userInfo', res.data.data.userInfo);
           wx.setStorage({
             key: "token",
             data: res.data.data.token,
@@ -75,7 +92,6 @@ Page({
           });
 
         }
-        console.log(res.data.data.token)
       }
     });
   },
@@ -97,6 +113,12 @@ Page({
       confirmPassword: e.detail.value
     });
   },
+  bindMobileInput: function (e) {
+
+    this.setData({
+      mobile: e.detail.value
+    });
+  },
   bindCodeInput: function (e) {
 
     this.setData({
@@ -120,6 +142,11 @@ Page({
           confirmPassword: ''
         });
         break;
+      case 'clear-mobile':
+        this.setData({
+          mobile: ''
+        });
+        break;        
       case 'clear-code':
         this.setData({
           code: ''

+ 8 - 3
litemall-wx/pages/auth/register/register.wxml

@@ -11,17 +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">
+      <view class="form-item">
     		<input class="password" value="{{confirmPassword}}" password bindinput="bindConfirmPasswordInput" placeholder="确认密码"/>
     		<image class="clear" id="clear-confirm-password" wx:if="{{ confirmPassword.length > 0 }}" src="/static/images/clear_input.png" catchtap="clearInput"></image>
     	</view>
 
-		<view class="form-item-code" >
+    <view class="form-item">
+      <input class="mobile" value="{{mobile}}" bindinput="bindMobileInput" placeholder="手机号" />
+    	<image wx:if="{{ mobile.length > 0 }}" id="clear-mobile" class="clear" src="/static/images/clear_input.png" catchtap="clearInput"></image>
+    </view>
+		
+    <view class="form-item-code" >
 			<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 class="code-btn" bindtap="sendCode">获取验证码</view>
 		</view>
 
     	<button type="default" class="login-btn" bindtap="startRegister">注册</button>

+ 5 - 5
litemall-wx/pages/auth/register/register.wxss

@@ -14,7 +14,7 @@
     border-bottom: 1px solid #d9d9d9;
 }
 
-.form-item .username, .form-item .password, .form-item .code{
+.form-item .username, .form-item .password, .form-item .mobile, .form-item .code{
     position: absolute;
     top: 26rpx;
     left: 0;
@@ -38,11 +38,11 @@
     width: 350rpx;
 }
 
-.form-item-code .code-img{
+.form-item-code .code-btn{
     float: right;
-    margin-top: 4rpx;
-    height: 88rpx;
-    width: 236rpx;
+    padding: 20rpx 40rpx;
+    border: 1px solid #d9d9d9;
+    border-radius: 10rpx;
 }
 
 .form-item .clear{