Browse Source

小程序前后端校验修改

hyq 6 years ago
parent
commit
317651ddfb

+ 7 - 0
kmall-api/src/main/java/com/kmall/api/api/ApiAddressController.java

@@ -10,6 +10,7 @@ import com.kmall.api.service.ApiAddressService;
 import com.kmall.api.service.ApiStoreService;
 import com.kmall.api.util.ApiBaseAction;
 import com.kmall.api.util.CommonUtil;
+import com.kmall.common.utils.PhoneFormatCheckUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
@@ -63,6 +64,9 @@ public class ApiAddressController extends ApiBaseAction {
         JSONObject addressJson = this.getJsonRequest();
         AddressVo entity = new AddressVo();
         if (null != addressJson) {
+            if (!PhoneFormatCheckUtils.isPhoneLegal(addressJson.getString("telNumber"))) {
+                return toResponsFail("手机号格式有误!");
+            }
             entity.setId(addressJson.getLong("id"));
             entity.setUserId(loginUser.getId());
             entity.setUserName(addressJson.getString("userName"));
@@ -116,6 +120,9 @@ public class ApiAddressController extends ApiBaseAction {
         JSONObject addressJson = this.getJsonRequest();
         AddressVo entity = new AddressVo();
         if (null != addressJson) {
+            if (!PhoneFormatCheckUtils.isPhoneLegal(addressJson.getString("telNumber"))) {
+                return toResponsFail("手机号格式有误!");
+            }
             entity.setUserId(loginUser.getId());
             entity.setUserName(addressJson.getString("userName"));
             entity.setPostalCode(addressJson.getString("postalCode"));

+ 1 - 1
kmall-api/src/main/java/com/kmall/api/api/ApiGoodsController.java

@@ -543,7 +543,7 @@ public class ApiGoodsController extends ApiBaseAction {
     }
 
     /**
-     *   在售的商品总数
+     * 在售的商品总数
      */
     @IgnoreAuth
     @GetMapping("count")

+ 42 - 0
kmall-common/src/main/java/com/kmall/common/utils/PhoneFormatCheckUtils.java

@@ -0,0 +1,42 @@
+package com.kmall.common.utils;
+ 
+import java.util.regex.Matcher;  
+import java.util.regex.Pattern;  
+import java.util.regex.PatternSyntaxException;  
+  
+public class PhoneFormatCheckUtils {  
+  
+    /** 
+     * 大陆号码或香港号码均可 
+     */  
+    public static boolean isPhoneLegal(String str)throws PatternSyntaxException {  
+        return isChinaPhoneLegal(str) || isHKPhoneLegal(str);  
+    }  
+  
+    /** 
+     * 大陆手机号码11位数,匹配格式:前三位固定格式+后8位任意数 
+     * 此方法中前三位格式有: 
+     * 13+任意数 
+     * 15+除4的任意数 
+     * 18+除1和4的任意数 
+     * 17+除9的任意数 
+     * 147 
+     */  
+    public static boolean isChinaPhoneLegal(String str) throws PatternSyntaxException {  
+        String regExp = "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))\\d{8}$";  
+        Pattern p = Pattern.compile(regExp);  
+        Matcher m = p.matcher(str);  
+        return m.matches();  
+    }  
+  
+    /** 
+     * 香港手机号码8位数,5|6|8|9开头+7位任意数 
+     */  
+    public static boolean isHKPhoneLegal(String str)throws PatternSyntaxException {  
+        String regExp = "^(5|6|8|9)\\d{7}$";  
+        Pattern p = Pattern.compile(regExp);  
+        Matcher m = p.matcher(str);  
+        return m.matches();  
+    }  
+      
+}  

+ 7 - 1
wx-mall/pages/shopping/address/address.js

@@ -104,7 +104,11 @@ Page({
     })
   },
   syncAddress(res) {
-    let that= this;
+    let that = this;
+    if (!(/^1[34578]\d{9}$/.test(res.telNumber)) || res.telNumber.length != 11) {
+      util.showErrorToast('手机号有误');
+      return false;
+    }
     util.request(api.AddressSync, {
       userName: res.userName,
       telNumber: res.telNumber,
@@ -119,6 +123,8 @@ Page({
     }, 'POST').then(function (res) {
       if (res.errno === 0) {
         that.getAddressList();
+      } else {
+        util.showErrorToast(res.errmsg);
       }
     });
   },

+ 7 - 1
wx-mall/pages/ucenter/address/address.js

@@ -84,7 +84,11 @@ Page({
     })
   },
   syncAddress(res) {
-    let that= this;
+    let that = this;
+    if (!(/^1[34578]\d{9}$/.test(res.telNumber)) || res.telNumber.length != 11) {
+      util.showErrorToast('手机号有误');
+      return false;
+    }
     util.request(api.AddressSync, {
       userName: res.userName,
       telNumber: res.telNumber,
@@ -99,6 +103,8 @@ Page({
     }, 'POST').then(function (res) {
       if (res.errno === 0) {
         that.getAddressList();
+      } else {
+        util.showErrorToast(res.errmsg);
       }
     });
   },

+ 6 - 2
wx-mall/pages/ucenter/addressAdd/addressAdd.js

@@ -258,7 +258,6 @@ Page({
       if (res.errno === 0) {
         that.setData({
           regionList: res.data.map(item => {
-
             //标记已选择的
             if (regionType == item.type && that.data.selectRegionList[regionType - 1].id == item.id) {
               item.selected = true;
@@ -300,7 +299,10 @@ Page({
       util.showErrorToast('请输入详细地址');
       return false;
     }
-
+    if (!(/^1[34578]\d{9}$/.test(address.telNumber)) || address.telNumber.length != 11) {
+      util.showErrorToast('手机号有误');
+      return false;
+    }
 
     let that = this;
     util.request(api.AddressSave, {
@@ -319,6 +321,8 @@ Page({
         wx.navigateTo({
           url: '/pages/ucenter/address/address',
         })
+      }else{
+        util.showErrorToast(res.errmsg);
       }
     });
 

+ 2 - 2
wx-mall/pages/ucenter/index/index.js

@@ -68,9 +68,9 @@ Page({
   goLogin(){
     user.loginByWeixin().then(res => {
       this.setData({
-        userInfo: res.data.userInfo
+        userInfo: res.data.data.userInfo
       });
-      app.globalData.userInfo = res.data.userInfo;
+      app.globalData.userInfo = res.data.data.userInfo;
       app.globalData.token = res.data.token;
     }).catch((err) => {
       console.log(err)