import utils from '@/utils/encode' export default { //调起上传图片 chooseImage(clickfn) { const JSESSIONID = utils.uuid() let chooseImageUrl = '' uni.chooseImage({ success: (chooseImageRes) => { uni.showLoading({ title:'加载中' }) const tempFilePaths = chooseImageRes.tempFilePaths; uni.uploadFile({ url: 'https://www.xingoil.com/adminapi/oil-oss/obejct/uploadFile', filePath: tempFilePaths[0], name: 'file', formData: { ossKey: 'xingyou', pathKey: 'publicxingyou', encrypt: 'PUBLIC', }, header: { "Content-Type": "multipart/form-data", "Authorization": uni.getStorageSync('Authorization'), 'dataSources': 'MP', "imei": uni.getStorageSync('unionid'), "openId": uni.getStorageSync('openid'), 'JSESSIONID': JSESSIONID, 'token': utils.md5Salt(JSESSIONID) }, success: (uploadFileRes) => { uni.hideLoading() chooseImageUrl = JSON.parse(uploadFileRes.data).data.publicUrl clickfn(chooseImageUrl) }, fail:()=>{ uni.hideLoading() } }); }, fail:()=>{ uni.hideLoading() } }); }, //数据转为两位小数 numberSetting(obj, key, nm=2) { if (arguments.length < 2) throw new Error('请检查函数配置参数,最少传递两个必填参数'); if (!Array.isArray(key)) throw new Error('参数2必须为Array'); // if (obj.constructor.toString().indexOf('Object') == -1) throw new Error('参数1必须为Object'); if (Array.isArray(obj)) { obj.forEach((item, index) => { if (Array.isArray(item)) { this.numberSetting(item, key, nm) } let keyArray = Object.keys(item) keyArray.forEach(i => { if (key.includes(i)) { item[i] = isNaN(Number(item[i]))? 0.00: Number(item[i]).toFixed(nm) } }) }) } else { Object.keys(obj).forEach(keys => { if (key.includes(keys)) { // if (isNaN(Number(obj[keys]))) throw new Error(keys + '值不是数字') // obj[keys] = Number(obj[keys]).toFixed(nm); obj[keys] = isNaN(Number(obj[keys]))? 0.00: Number(obj[keys]).toFixed(nm) } }) } }, /* 校验数据规范 data:校验源数据, options:校验配置, correct:是否修正 */ checkData(data, options, correct = false) { // 校验对象 let checkData = { typeFn(data, type) { return typeof data == type }, correctFn(item) { console.log('校验失败',item) //查看是否修正 if (correct) { //修正 switch (item.type) { case 'number': data[item.field] = data[item.field]==null?item.defaultValue:data[item.field]==='' || isNaN(Number(data[item.field])) ? item.defaultValue : Number(data[item.field]) break; case 'string': data[item.field] = data[item.field]==null?item.defaultValue:String(data[item.field]) break; case 'null': data[item.field] = null break; } } else { //无需直接退出 wx.exitMiniProgram({ success: function () { console.log('拜拜') } }) // throw new Error('错误!!!'); } }, } //参数校验 if (arguments.length < 2) throw new Error('请检查函数配置参数,最少传递两个必填参数'); if (!Array.isArray(options)) throw new Error('参数2必须为Array'); //获取对象的key let dataKeys = Object.keys(data); //开始通过 options 校验 options.forEach(item => { //判断是否有该字段 if (typeof data[item.field] !== 'undefined') { //校验数据类型 if (!checkData.typeFn(data[item.field],item.type)) { //类型校验失败 checkData.correctFn(item); } } else { console.log(`%c ${item.field} ← 错误值,请检查源数据中该字段`, 'font-size:50px;color:red'); checkData.correctFn(item); } }) }, //表单校验 checkFn(obj, whiteList, configure = {}) { //校验状态 con let con = { result: true, field: '', WrongText: '', errCheckField: '' } //校验参数 if (arguments.length < 1) throw new Error('请检查函数配置参数,最少传递1个必填参数'); if (obj.constructor.toString().indexOf('Object') == -1) throw new Error('参数1必须为Object'); if (whiteList && !Array.isArray(whiteList)) throw new Error('参数2必须为Array'); let configurationTable = Object.keys(configure) // console.log(configurationTable, obj, '需要校验的字段数组') // 循环需要校验的对象 Object.keys(obj).forEach(keys => { // console.log(keys, '当前循环的校验对象') //如果循环的当前字段在白名单内跳过 // console.log(whiteList, '白名单') if (whiteList.includes(keys)) return //校验状态改变跳过 // console.log(con.result, '校验状态') if (!con.result) return if (configurationTable.includes(keys)) { //当前需要匹配的字段 obj[keys] configure[keys] //循环配置参数 拿到校验配置 // console.log('key:', keys, '源对象:', obj[keys], '校验配置', configure[keys], '匹配到的需要校验的数据') Object.keys(configure[keys]).forEach(pages => { if (!con.result) return // console.log('字段:', keys, '开始校验', pages, ) // configure[keys][pages] switch (pages) { case 'type': con.result = typeof obj[keys] == configure[keys][pages] if (!con.result) { con.field = keys con.WrongText = configure[keys].WrongText ? configure[keys].WrongText : '字段:' + con.field + '校验出错' con.errCheckField = pages } break; case 'length': con.result = obj[keys].toString().length == configure[keys][pages] if (!con.result) { con.field = keys con.WrongText = configure[keys].WrongText ? configure[keys].WrongText : '字段:' + con.field + '校验出错' con.errCheckField = pages } break; case 'minLength': con.result = obj[keys].toString().length >= configure[keys][pages] if (!con.result) { con.field = keys con.WrongText = configure[keys].WrongText ? configure[keys].WrongText : '字段:' + con.field + '校验出错' con.errCheckField = pages } break; case 'maxLength': con.result = obj[keys].toString().length <= configure[keys][pages] if (!con.result) { con.field = keys con.WrongText = configure[keys].WrongText ? configure[keys].WrongText : '字段:' + con.field + '校验出错' con.errCheckField = pages } break; case 'custom': con.result = configure[keys][pages].test(obj[keys]) if (!con.result) { con.field = keys con.WrongText = configure[keys].WrongText ? configure[keys].WrongText : '字段:' + con.field + '校验出错' con.errCheckField = pages } // console.log('自定义类型校验', configure[keys][pages], '值', obj[keys]) break; case 'tacitly': con.result = Boolean(obj[keys]) if (!con.result) { con.field = keys con.WrongText = configure[keys].WrongText ? configure[keys].WrongText : '字段:' + con.field + ',' + pages + '校验出错' con.errCheckField = pages } // console.log('自定义类型校验', configure[keys][pages], '值', obj[keys]) break; default: // console.log(pages, '其他') break; } }) } }); // console.log(con, '校验结果') return con } }