zhaoxiaoqiang
2022-08-04 d43e16aad5d019b4677ab24571bd093a4d1ab5b7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 * @Author: 小明丶
 * @Date: 2019-09-02 09:12:30
 * @LastEditors: 小明丶
 * @LastEditTime: 2019-09-02 09:18:52
 * @Description:
 */
/**
 * Created by c.y
 * 2018/4/12
 * form--提交数据组合--例如房抵贷
 */
import Vue from 'vue';
import validate from './validator';
import IDValidator from './IDValidator';
 
 
// 把后台返回的null,转换为空字符串, infoObj需要检查
function changeNullToString (infoObj) {
  for (let i in infoObj) {
    infoObj[i] === null ? infoObj[i] = '' : infoObj[i] = infoObj[i];
  }
  return infoObj;
}
 
// 组合后台返回省,市,区---来满足vux的地址选择插件需要的格式
// province省 city市 country区
function getAddressFormat (province, city, country) {
  let addressList = [];
  if (!validate.checkValEmpty(province)) {
    addressList.push(province);
  }
  if (!validate.checkValEmpty(city)) {
    addressList.push(city);
  }
  if (!validate.checkValEmpty(country)) {
    addressList.push(country);
  }
  return addressList;
}
 
// 判断地址类型的值,是否必填,并进行提示
// addressList, 代表需要校验的地址数组
function checkAddressRequired (addressList) {
  return validate.checkValEmpty(addressList[0]);
}
 
// 得到selector的列表
function getOptionsList (optionList) {
  let tmpList = [];
  if (optionList instanceof Array) {
    optionList.forEach(function (item) {
      tmpList.push({
        key: item.code,
        value: item.name
      });
    });
  }
  return tmpList;
}
 
// 检查必填的字段以及错误提示, requiredList是检查规则, checkInfo要检查的对象
// context为this对象
function checkRequiredField (requiredList, checkInfo, context) {
  let allPassLabel = true;
  for (let i = 0; i < requiredList.length; i++) {
    // 如果配置了validator,那么就以validator进行验证,忽略其他验证(validator需要是函数)
    if (requiredList[i].validator && Object.prototype.toString.call(requiredList[i].validator) === '[object Function]') {
      if (!requiredList[i].validator(requiredList[i], checkInfo, context)) {
        allPassLabel = false;
        return false;
      }
    // 如果是地址的话
    } else if (requiredList[i].type === 'address') {
      // 对于地址检查,是否为空,为空的话,返回true
      if (checkAddressRequired(context[requiredList[i].checkKey])) {
        checkValidateTip(requiredList[i], context);
        allPassLabel = false;
        return false;
      }
      // 如果是非地址的话,那么第一步检查是否为空
    } else {
      // 第一步,先检查是否填写
      if (validate.checkValEmpty(checkInfo[requiredList[i].key])) {
        checkValidateTip(requiredList[i], context);
        allPassLabel = false;
        return false;
      }
      // 如果有patter的话,直接使用pattern,即正则判断(pattern需要正则)
      if (requiredList[i].pattern && Object.prototype.toString.call(requiredList[i].pattern) === '[object RegExp]') {
          if(!requiredList[i].pattern.test(checkInfo[requiredList[i].key])) {
            checkRegularExpreTip(requiredList[i], context);
            allPassLabel = false;
            return false;
          }
      }
      // 对于特定类型,进行判断是否添加正确的类型
      // 如果是名字类型
      if (requiredList[i].type === 'name') {
        if (!validate.checkName(checkInfo[requiredList[i].key])) {
          checkRegularExpreTip(requiredList[i], context);
          allPassLabel = false;
          return false;
        }
        // 如果是手机号
      } else if (requiredList[i].type === 'tel') {
        if (!validate.checkPhone(checkInfo[requiredList[i].key])) {
          checkRegularExpreTip(requiredList[i], context);
          allPassLabel = false;
          return false;
        }
        // 如果是身份证
      } else if (requiredList[i].type === 'idCard') {
        if (!IDValidator.isValid(checkInfo[requiredList[i].key])) {
          checkRegularExpreTip(requiredList[i], context);
          allPassLabel = false;
          return false;
        }
        // 如果是座机
      } else if (requiredList[i].type === 'lineTel') {
        if (!validate.checkLandline(checkInfo[requiredList[i].key])) {
          checkRegularExpreTip(requiredList[i], context);
          allPassLabel = false;
          return false;
        }
        // 如果是房屋楼层
      } else if (requiredList[i].type === 'floor') {
        if (!validate.checkFloor(checkInfo[requiredList[i].key])) {
          checkRegularExpreTip(requiredList[i], context);
          allPassLabel = false;
          return false;
        }
        // 如果是银行卡
      }else if (requiredList[i].type === 'bankCardNo') {
        if (!validate.bankCard(checkInfo[requiredList[i].key])) {
          checkRegularExpreTip(requiredList[i], context);
          allPassLabel = false;
          return false;
        }
      }else if (requiredList[i].type === 'isEmail') {
          if (!validate.checkEmail(checkInfo[requiredList[i].key])) {
              checkRegularExpreTip(requiredList[i], context);
              allPassLabel = false;
              return false;
          }
      }
    }
  }
  return allPassLabel;
}
 
// 错误提示, requiredItem为检查的对象,context为this对象
function checkValidateTip (requiredItem, context) {
  let _this = context;
  // tipType值为1为输入框,为2的话是下拉选择
  if (requiredItem.tipType === 1) {
    // 如果进行正则验证的话,那么提示内容是正确,而一般的提示没有正确的这个提示字
    // 如果没有type属性的话,那么就是默认的
    Vue.prototype.$notify('请输入' + requiredItem.message, 'middle');
    return false;
  } else if (requiredItem.tipType === 2) {
    Vue.prototype.$notify('请选择' + requiredItem.message, 'middle');
    return false;
  }
}
 
// 错题提示,requiredItem为检查的对象,context为this对象
// 这个是正则匹配的检测或者其他特殊检测的提示
function checkRegularExpreTip (requiredItem, context) {
  Vue.prototype.$notify('请输入正确的' + requiredItem.message, 'middle');
  return false;
}
 
// 封装提交数据 submitVal需要提交的val对象
function packageSubmitInfo (submitVal) {
  let submitInfo = {};
  if (submitVal instanceof Object && submitVal !== null) {
    for (let i in submitVal) {
        // 对于非必填的话,其值为空的话,那么不传递给后台
        if (!validate.checkValEmpty(submitVal[i])) {
          submitInfo[i] = submitVal[i];
        }
    }
  }
  return submitInfo;
}
 
export default {
  getAddressFormat,
  getOptionsList,
  checkRequiredField,
  packageSubmitInfo,
  changeNullToString
};