123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- // 生成随机数 min ≤ r ≤ max
- export function getRandomNum(Min, Max){
- const Range = Max - Min;
- const Rand = Math.random();
- const num = Min + Math.round(Rand * Range);
- return num;
- }
- // 格式化数字 comma是否加逗号,默认1000加逗号 comma=true 不添加逗号
- export function NumberHandle ({value,numberDigit,comma}) {//数值小数点处理 ①5997;②8.1w;③2489kw;④4.2亿 且保留一位小数
- if(typeof parseFloat(value) === 'number' && !isNaN(value)){
- //判断是否是数值类型
- if((value >= 10000 && value < 100000000) || (value <= -10000 && value > -100000000)){
- if(hasDot(value/10000000,numberDigit,comma)==10){
- return '1亿'
- }else{
- return hasDot(value/10000,numberDigit,comma) + 'w'
- }
- }else if(value >= 100000000 || value <= -100000000){
- return hasDot(value/100000000,numberDigit,comma) + '亿'
- }else{
- if(value==undefined){
- return '-'
- }else{
- return hasDot(value,numberDigit,comma)
- }
- }
- }else{
- if(value==undefined){
- return '-'
- }else{
- return value
- }
- }
- }
- function hasDot(num,numberDigit,comma){
- //有小数点就保留一个小数,没有就直接返回 ,默认保留1位小数
- var digit = 1;
- if(numberDigit){
- digit = numberDigit
- }
- if (comma) { // 不加逗号
- return ((num + '').indexOf('.') != -1 ? parseFloat(Number(num).toFixed(digit)) : num );
- } else { // 默认加逗号
- return formatNum((num + '').indexOf('.') != -1 ? parseFloat(Number(num).toFixed(digit)) : num );
- }
- }
- export function formatNum (str){
- if (typeof parseFloat(str) === 'number' && !isNaN(str) && str != null) {
- //判断是否是数值类型
- var newStr = "";
- var count = 0;
- str = Number(str).toFixed(2)
- str = parseFloat(str)
- str = str.toString();
- if (str.indexOf(".") == -1) {
- for (var i = str.length - 1; i >= 0; i--) {
- if (count % 3 == 0 && count != 0 && str.charAt(i) != '-') {
- newStr = str.charAt(i) + "," + newStr;
- } else {
- newStr = str.charAt(i) + newStr;
- }
- count++;
- }
- str = newStr; //自动补小数点后两位
- } else {
- for (var i = str.indexOf(".") - 1; i >= 0; i--) {
- if (count % 3 == 0 && count != 0 && str.charAt(i) != '-') {
- newStr = str.charAt(i) + "," + newStr;
- } else {
- newStr = str.charAt(i) + newStr; //逐个字符相接起来
- }
- count++;
- }
- str = newStr + str.substring(str.indexOf("."), str.length);
- }
- }
- return str
- }
- export function getDay (day, haveHours) {
- //day为0表示今天的日期 -1为昨天 haveHours存在表示要时分秒 haveHours:true,false
- var today = new Date();
- var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
- today.setTime(targetday_milliseconds); //注意,这行是关键代码
- var tYear = today.getFullYear();
- var tMonth = today.getMonth();
- var tDate = today.getDate();
- var hours = today.getHours();
- var minutes = today.getMinutes(); //分
- var seconds = today.getSeconds(); //秒
- tMonth = doHandleMonth(tMonth + 1);
- tDate = doHandleMonth(tDate);
- if (haveHours) {
- if (day == 0) {
- return {
- 'now': tYear + "-" + tMonth + "-" + tDate + ' ' + (hours < 10 ? '0' + hours : hours) + ':' + (minutes < 10 ? '0' + minutes : minutes) + ':' + (seconds < 10 ? '0' + seconds : seconds),
- 'zero': tYear + "-" + tMonth + "-" + tDate + ' ' + '00:00:00'
- }
- } else {
- return tYear + "-" + tMonth + "-" + tDate + ' ' + hours + ':' + minutes + ':' + seconds
- }
- } else {
- return tYear + "-" + tMonth + "-" + tDate
- }
- function doHandleMonth (month) {
- var m = month;
- if (month.toString().length == 1) {
- m = "0" + month;
- }
- return m;
- }
- }
- //复制文案
- export function copyEvent({text, cb, toast_title}){
- // #ifdef H5
- let textarea = document.createElement("textarea")
- textarea.value = text
- textarea.readOnly = "readOnly"
- document.body.appendChild(textarea)
- textarea.select() // 选中文本内容
- textarea.setSelectionRange(0, text.length)
- uni.showToast({//提示
- title: toast_title ? toast_title : '复制成功' ,
- icon:'success',
- complete: () => {
- cb ? cb() : ''
- }
- })
- document.execCommand("copy")
- textarea.remove()
- // #endif
- // #ifndef H5
- uni.setClipboardData({
- data:text,//要被复制的内容
- showToast:false,
- success:()=>{//复制成功的回调函数
- uni.showToast({//提示
- title:toast_title ? toast_title : '复制成功',
- icon:'success',
- complete: () => {
- cb ? cb() : ''
- }
- })
- }
- },true);
- // #endif
- }
- export const loadmoreOptions = {
- LOAD_MORE: 'loadmore', // 加载更多
- LOADING: 'loading', // 加载中
- NO_MORE: 'nomore', // 没有更多了
- }
- /**
- * canvas文本超出范围时,自动换行
- * @param {*} ctx
- * @param {*} text
- * @param {*} x
- * @param {*} y
- * @param {*} maxWidth
- * @param {*} lineHeight
- */
- export function canvasTextAutoLine(ctx, text, x, y, maxWidth, lineHeight) {
- let lineWidth = 0
- let lastSubStrIndex = 0
- for (let i = 0; i < text.length; i++) {
- lineWidth += ctx.measureText(text[i]).width
- if (lineWidth > maxWidth) {
- ctx.fillText(text.substring(lastSubStrIndex, i), x, y)
- y += lineHeight
- lineWidth = 0
- lastSubStrIndex = i
- }
- if (i === text.length - 1) {
- ctx.fillText(text.substring(lastSubStrIndex, i + 1), x, y)
- }
- }
- }
- /*
- * 参数说明
- * ctx Canvas实例
- * img 图片地址
- * x x轴坐标
- * y y轴坐标
- * r 圆形半径
- */
- export function circleImgOne(ctx, img, x, y, r) {
- let d = r * 2;
- let cx = x + r;
- let cy = y + r;
- ctx.arc(cx, cy, r, 0, 2 * Math.PI);
- ctx.strokeStyle = '#FFFFFF'; // 设置绘制圆形边框的颜色
- ctx.stroke(); // 绘制出圆形,默认为黑色,可通过 ctx.strokeStyle = '#FFFFFF', 设置想要的颜色
- ctx.clip();
- ctx.drawImage(img, x, y, d, d);
- }
|