|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706 |
- let WebIM = uni.WebIM = require("./WebIM.js")["default"];
- import * as CryptoJS from '../components/crypto-js'
- function formatLongTime(mss) {
- var days = parseInt(mss / (1000 * 60 * 60 * 24));
- var hours = parseInt(mss % (1000 * 60 * 60 * 24) / (1000 * 60 * 60));
- var minutes = parseInt(mss % (1000 * 60 * 60) / (1000 * 60));
- var seconds = mss % (1000 * 60) / 1000;
- seconds = parseInt(seconds);
-
- if (minutes < 9) {
- minutes = "0" + minutes;
- }
-
- if (seconds < 9) {
- seconds = "0" + seconds;
- }
-
- return minutes + ":" + seconds + "";
- }
- /**
- * 格式化时间
- * @param {String} date 原始时间格式
- * 格式后的时间:yyyy/mm/dd hh:mm:ss
- **/
-
-
- /**
- * 格式化时间
- * @param {String} date 原始时间格式
- * 格式后的时间:yyyy/mm/dd hh:mm:ss
- **/
- const formatSecond = seconds=> {
- var h = Math.floor(seconds / 3600) < 10 ? '0'+Math.floor(seconds / 3600) : Math.floor(seconds / 3600);
- var m = Math.floor((seconds / 60 % 60)) < 10 ? '0' + Math.floor((seconds / 60 % 60)) : Math.floor((seconds / 60 % 60));
- var s = Math.floor((seconds % 60)) < 10 ? '0' + Math.floor((seconds % 60)) : Math.floor((seconds % 60));
- return h + ":" + m + ":" + s;
- }
-
- /**
- *加密处理
- */
- const encryption = (params) => {
- let {
- data,
- type,
- param,
- key
- } = params
- const result = JSON.parse(JSON.stringify(data))
- if (type === 'Base64') {
- param.forEach(ele => {
- result[ele] = btoa(result[ele])
- })
- } else {
- param.forEach(ele => {
- var data = result[ele]
- key = CryptoJS.enc.Latin1.parse(key)
- var iv = key
- // 加密
- var encrypted = CryptoJS.AES.encrypt(
- data,
- key, {
- iv: iv,
- mode: CryptoJS.mode.CBC,
- padding: CryptoJS.pad.ZeroPadding
- })
- result[ele] = encrypted.toString()
- })
- }
- return result
- }
-
-
- const formatTime = date => {
- const year = date.getFullYear();
- const month = date.getMonth() + 1;
- const day = date.getDate();
- const hour = date.getHours();
- const minute = date.getMinutes();
- const second = date.getSeconds();
- return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':');
- };
- /**
- * 格式化时间
- * @param {String} date 原始时间格式
- * 格式后的时间:hh:mm
- **/
-
-
- const formatDateTime = date => {
- const hour = date.getHours();
- const minute = date.getMinutes();
- return [hour, minute, second].map(formatNumber).join(':');
- };
- /**
- * 格式化时间
- * @param {String} date 原始时间格式
- * 格式后的时间:yyyy-mm-dd
- **/
-
-
- const formatDate = date => {
- const year = date.getFullYear();
- const month = date.getMonth() + 1;
- const day = date.getDate();
- return [year, month, day].map(formatNumber).join('-');
- };
-
- const formatNumber = n => {
- n = n.toString();
- return n[1] ? n : '0' + n;
- };
- /**
- * 获取指定日期前后N天的日期、前N个月日期、后N个月日期
- * @param {String} sdate 当前日期
- * @param {Number} interval 间隔天数
- * @param {String} caret 间隔符号
- * @example 获取当天日期 getNowFormatDate("",0,"-"); 结果为"2018-09-18";
- 获取前一天日期 getNowFormatDate("2018-03-01",-1,"-"); 结果为"2018-02-18";
- 获取后一天日期 getNowFormatDate("2018-02-28",1,"-"); 结果为"2018-03-01";
- **/
-
-
- function getNowFormatDate(sdate, interval, caret) {
- var patt1 = /^\d{4}-([0-1]?[0-9])-([0-3]?[0-9])$/; //判断输入的日期是否符合格式正则表达式
-
- if (!(sdate && typeof sdate == "string" && patt1.test(sdate))) {
- sdate = new Date(); //不满足日期的则使用当前年月日
- }
-
- interval = isNaN(parseInt(interval)) ? 0 : parseInt(interval); //若没有输入间隔,则使用当前日
-
- caret = caret && typeof caret == "string" ? caret : "";
- var gdate = new Date(sdate).getTime(); //获取指定年月日
-
- gdate = gdate + 1000 * 60 * 60 * 24 * interval; //加减相差毫秒数
-
- var speDate = new Date(gdate); //获取指定好毫秒数时间
-
- var preYear = speDate.getFullYear();
- var preMonth = speDate.getMonth() + 1;
- var preDay = speDate.getDate();
- preMonth = preMonth < 10 ? "0" + preMonth : preMonth;
- preDay = preDay < 10 ? "0" + preDay : preDay;
- var preDate = preYear + caret + preMonth + caret + preDay;
- return preDate;
- } // 获取某年某月的有多少周
-
-
- String.prototype.weekInMonthCount = function () {
- var date = new Date(new Date(this.replace(/-/g, "/")) || new Date());
- var firstWeekDate = 1; // 默认第一周是本月1号 为了模拟本月1号是否为本月第1周的判断
-
- if (date.getDay() === 1) {
- // 判断1号是周一
- firstWeekDate = 1;
- } else if (date.getDay() === 0) {
- // 判断1号是周日
- firstWeekDate = 8 - 7 + 1;
- } else {
- // 判断1号是周二至周六之间
- firstWeekDate = 8 - date.getDay() + 1;
- }
-
- date.setMonth(date.getMonth() + 1);
- date.setDate(0);
- var monthHasDays = date.getDate(); // 本月天数
-
- monthHasDays = date.getDate() - firstWeekDate + 1;
- var hasWeek = Math.ceil(monthHasDays / 7); // 计算本月有几周
-
- return hasWeek;
- }; // 获取今天是第几周 注:ios不支持2018-01-14 需转化为2018/01/14
-
-
- String.prototype.weekIndexInMonth = function () {
- var date_trim = (this.trim() != "" ? this : new Date()).replace(/-/g, "/");
- var date = new Date(date_trim);
- var dateStart_trim = new Date((this.trim() != "" ? this : new Date()).replace(/-/g, "/")).setDate(1);
- var dateStart = new Date(dateStart_trim); // 本月初
-
- var firstWeek = 1;
-
- if (dateStart.getDay() === 1) {
- firstWeek = 1;
- } else if (dateStart.getDay() === 0) {
- firstWeek = 8 - 7 + 1;
- } else {
- firstWeek = 8 - dateStart.getDay() + 1;
- }
-
- var weekIndex = 1;
- var c = date.getDate();
-
- if (date.getDay() === 1 && date.getDate() < 7) {
- weekIndex = 1;
- } else if (c < firstWeek) {
- weekIndex = -1;
- } else {
- if (c < 7) {
- weekIndex = Math.ceil(c / 7);
- } else {
- c = c - firstWeek + 1;
-
- if (c % 7 === 0) {
- if (dateStart.getDay() !== 6) {
- weekIndex = c / 7;
- } else {
- weekIndex = c / 7 + 1;
- }
- } else {
- weekIndex = Math.ceil(c / 7);
- }
- }
- }
-
- return weekIndex;
- };
- /**
- * 验证车牌号是否正确
- * @param number carNumber
- **/
-
-
- function isLicensePlate(carNumber) {
- if (carNumber == '') {
- return false;
- }
-
- if (/^[A-Za-z]+$/.test(carNumber.slice(1))) {
- //全为字母
- return false;
- }
-
- return /^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$/.test(carNumber);
- }
- /**
- * 验证手机号
- */
-
-
- function checkPhone(phone) {
- let that = this;
-
- if (phone == '') {
- return false;
- }
-
- if (!/^1[3456789]\d{9}$/.test(phone)) {
- return false;
- }
-
- return true;
- }
- /**
- * 隐藏字符串
- * @param string str 字符串
- * @param int frontLen 前面需要保留几位
- * @param int endLen 后面需要保留几位
- */
-
-
- function hiddenString(str, frontLen, endLen) {
- var len = str.length - frontLen - endLen;
- var xing = '';
-
- for (var i = 0; i < len; i++) {
- xing += '*';
- }
-
- return str.substring(0, frontLen) + xing + str.substring(str.length - endLen);
- }
- /**
- * 从一个数组中随机取出若干个元素组成数组
- * @param {Array} arr 原数组
- * @param {Number} count 需要随机取得个数
- **/
-
-
- const getRandomArray = (arr, count) => {
- var shuffled = arr.slice(0),
- i = arr.length,
- min = i - count,
- temp,
- index;
-
- while (i-- > min) {
- index = Math.floor((i + 1) * Math.random());
- temp = shuffled[index];
- shuffled[index] = shuffled[i];
- shuffled[i] = temp;
- }
-
- return shuffled.slice(min);
- };
- /**
- * 从一个数组中随机取出一个元素
- * @param {Array} arr 原数组
- **/
-
-
- const getRandomArrayElement = arr => {
- return arr[Math.floor(Math.random() * arr.length)];
- };
- /**
- * 去除数组中重复的值
- * @param {Array} arr 原数组
- **/
-
-
- const getUnique = array => {
- var n = {},
- r = [],
- len = array.length,
- val,
- type;
-
- for (var i = 0; i < array.length; i++) {
- val = array[i];
- type = typeof val;
-
- if (!n[val]) {
- n[val] = [type];
- r.push(val);
- } else if (n[val].indexOf(type) < 0) {
- n[val].push(type);
- r.push(val);
- }
- }
-
- return r;
- };
- /**
- * 读取xml字符串
- * @param {String} xmlString 数据
- **/
-
-
- function loadXMLStr(xmlString) {
- var danmulist = [];
-
- if (xmlString.indexOf("<d p")) {
- var str = xmlString.substring(xmlString.indexOf("<d p"), xmlString.length);
- var reg = /<d p/g;
- var arr = str.match(reg);
-
- if (arr) {
- console.log(str);
- console.log(arr.length);
-
- for (var i = 0; i < arr.length; ++i) {
- var getstr = str.substring(0, str.indexOf("</d>") + "</d>".length);
- str = str.substring(str.indexOf("</d>") + "</d>".length, str.length);
- var danmu = {
- text: getstr.substring(getstr.indexOf(">") + ">".length, getstr.indexOf("</d>")),
- color: '#ff00ff',
- time: Math.floor(getstr.substring(getstr.indexOf("<d p=\"") + "<d p=\"".length, getstr.indexOf(",")))
- };
- danmulist.push(danmu);
- }
- }
- }
-
- return danmulist;
- } // 显示警告提示(7个汉字长度)
-
-
- var showWarn = text => uni.showToast({
- title: text,
- image: 'https://qufang.oss-cn-beijing.aliyuncs.com/upload/icon/xcx/jjycrm/warn.png'
- }); // 显示错误提示(7个汉字长度)
-
-
- var showError = text => uni.showToast({
- title: text,
- image: 'https://qufang.oss-cn-beijing.aliyuncs.com/upload/icon/xcx/jjycrm/error.png'
- }); // 显示繁忙提示(7个汉字长度)
-
-
- var showBusy = text => uni.showToast({
- title: text,
- icon: 'loading',
- duration: 1500
- }); // 显示成功提示(7个汉字长度)
-
-
- var showSuccess = text => uni.showToast({
- title: text,
- icon: 'success'
- }); // 显示无图标提示(两行)
-
-
- var showNone = text => uni.showToast({
- title: text,
- icon: 'none',
- duration: 1500
- }); // 显示失败提示框
-
-
- var showModel = (title, content) => {
- uni.hideToast();
- uni.showModal({
- title,
- content: JSON.stringify(content),
- showCancel: false
- });
- }; //获取列表
-
-
- function getList(url, userinfo, page, pageSize, callback) {
- this.showBusy('加载中...');
- var token = uni.getStorageSync('weapp_session_login_data');
- uni.request({
- url: url,
- data: {
- skey: userinfo.skey,
- tel: userinfo.userinfo.mobile_phone,
- page: page,
- pageSize: pageSize
- },
- method: 'POST',
- header: {
- 'content-type': 'application/x-www-form-urlencoded',
- 'Authorization': "Bearer "+token.token
- },
- success: function (result) {
- if (result.data.code == '200') {
- uni.hideToast();
- var result = result.data;
- callback(result);
- }
- },
-
- fail(error) {
- this.showModel('请求失败', error);
- return false;
- }
-
- });
- } //请求数据
-
-
- function getRequest(url, params, callback) {
- this.showBusy('加载中...');
- var token = uni.getStorageSync('weapp_session_login_data');
- uni.request({
- url: url,
- data: params,
- method: 'POST',
- header: {
- 'content-type': 'application/json',
- 'Authorization': "Bearer "+token.token
- },
- success: function (result) {
- if (result.data.code == '10003' || result.data.code == '20006') {
- //未登录
- uni.hideToast();
- uni.showModal({
- title: '提示',
- content: '您的登录已失效,请重新登录',
- showCancel: false,
-
- success(res) {
- if (res.confirm) {
- try {
- uni.clearStorageSync();
- uni.reLaunch({
- url: '/pages/main/login/index' //绝对路径
-
- });
- } catch (e) {
- return false;
- }
- }
- }
-
- }); // WebIM.conn.close();
-
- return false;
- }
- if (result.data.code == '10000') {
- uni.hideToast();
- var result = result.data;
- callback(result.data);
- } else {
- uni.hideToast();
- uni.showModal({
- title: '提示',
- content: '加载失败,请重新刷新',
- showCancel: false
- });
- return false;
- }
- },
-
- fail(error) {
- uni.hideToast();
- uni.showModal({
- title: '提示',
- content: '网络异常,请重新刷新',
- showCancel: false
- });
- return false;
- }
-
- });
- } //请求数据(异步)
-
-
- function getRequestPromise(url, params, isNone,option) {
- var isNone = isNone ? true : false;
-
- if (!isNone) {
- this.showBusy('加载中...');
- }
-
- var token = uni.getStorageSync('weapp_session_login_data');
- return new Promise(function (resolve, reject) {
- uni.request({
- url: url,
- data: params,
- method: option?option:'POST',
- header: {
- 'content-type': 'application/json',
- 'Authorization': 'Bearer '+token.token
- },
- success: function (result) {
- if (result.data.code != 1) {
- uni.hideToast();
- var results = result.data;
- resolve(results.data);
- } else if (result.data.code == 1 || result.data.code == '20006') {
- //未登录
- uni.hideToast();
- uni.showModal({
- title: '提示',
- content: '您的登录已失效,请重新登录',
- showCancel: false,
-
- success(res) {
- if (res.confirm) {
- try {
- uni.clearStorageSync();
- uni.reLaunch({
- url: '/pages/main/login/index' //绝对路径
-
- });
- } catch (e) {
- return false;
- }
- }
- }
-
- }); // WebIM.conn.close();
-
- return false;
- } else if (result.data.code == '60001') {
- //已录入客户
- uni.hideToast();
- uni.showToast({
- title: '您已经录入了该客户!',
- icon: 'none',
- duration: 1500
- });
- return false;
- } else if (result.data.code == '20005') {
- //已录入客户
- uni.hideToast();
- uni.showModal({
- title: '提示',
- content: '原始密码错误',
- showCancel: false
- });
- return false;
- } else {
- uni.hideToast();
- uni.showModal({
- title: '提示',
- content: result.data.message ? result.data.message : '请求数据失败,请重新尝试',
- showCancel: false
- });
- return false;
- }
- },
-
- fail(error) {
- console.log(error);
- uni.hideToast();
- uni.showModal({
- title: '提示',
- content: '网络异常,请重新尝试',
- showCancel: false
- });
- return false;
- }
-
- });
- });
- } //请求数据自定义
-
-
- function getRequestCustom(url, params, isShow) {
- if (isShow != 1) {
- this.showBusy('加载中...');
- }
- var token = uni.getStorageSync('weapp_session_login_data');
- return new Promise(function (resolve, reject) {
- uni.request({
- url: url,
- data: params,
- method: 'POST',
- header: {
- 'content-type': 'application/x-www-form-urlencoded',
- 'Authorization': "Bearer "+token.token
- },
- success: function (result) {
- if (result.data.data == undefined) {
- resolve(result.data);
- } else {
- if (result.data.success) {
- resolve(result.data.data);
- } else {
- uni.hideToast();
- uni.showModal({
- title: '提示',
- content: result.data.message ? result.data.message : '请求数据失败,请重新尝试',
- showCancel: false
- });
- return false;
- }
- }
- },
-
- fail(error) {
- uni.showModal({
- title: '提示',
- content: '网络异常,请重新尝试',
- showCancel: false
- });
- return false;
- }
-
- });
- });
- } //请求数据(异步)-自定义
-
-
- function getRequestPromiseCustom(url, params,option) {
- this.showBusy('请求中...');
- var token = uni.getStorageSync('weapp_session_login_data');
- return new Promise(function (resolve, reject) {
- uni.request({
- url: url,
- data: params,
- method: option?option:'POST',
- header: {
- 'content-type': 'application/json',
- 'Authorization': "Bearer "+token.token
- },
- success: function (result) {
- uni.hideToast();
- var results = result.data;
- resolve(results);
- },
-
- fail(error) {
- uni.hideToast();
- uni.showModal({
- title: '提示',
- content: '网络异常,请重新尝试',
- showCancel: false
- });
- return false;
- }
-
- });
- });
- }
-
- module.exports = {
- formatSecond,
- formatTime,
- formatDate,
- encryption,
- formatDateTime,
- getRandomArray: getRandomArray,
- getRandomArrayElement: getRandomArrayElement,
- showWarn,
- showError,
- showBusy,
- showSuccess,
- showNone,
- showModel,
- getList: getList,
- getRequest: getRequest,
- getRequestPromise: getRequestPromise,
- getRequestCustom: getRequestCustom,
- getRequestPromiseCustom: getRequestPromiseCustom,
- getNowFormatDate: getNowFormatDate,
- isLicensePlate: isLicensePlate,
- checkPhone: checkPhone,
- hiddenString: hiddenString,
- getUnique: getUnique,
- loadXMLStr: loadXMLStr,
- formatLongTime: formatLongTime
- };
|