猎豆优选小程序

tools.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // 生成随机数 min ≤ r ≤ max
  2. export function getRandomNum(Min, Max){
  3. const Range = Max - Min;
  4. const Rand = Math.random();
  5. const num = Min + Math.round(Rand * Range);
  6. return num;
  7. }
  8. // 格式化数字 comma是否加逗号,默认1000加逗号 comma=true 不添加逗号
  9. export function NumberHandle ({value,numberDigit,comma}) {//数值小数点处理 ①5997;②8.1w;③2489kw;④4.2亿 且保留一位小数
  10. if(typeof parseFloat(value) === 'number' && !isNaN(value)){
  11. //判断是否是数值类型
  12. if((value >= 10000 && value < 100000000) || (value <= -10000 && value > -100000000)){
  13. if(hasDot(value/10000000,numberDigit,comma)==10){
  14. return '1亿'
  15. }else{
  16. return hasDot(value/10000,numberDigit,comma) + 'w'
  17. }
  18. }else if(value >= 100000000 || value <= -100000000){
  19. return hasDot(value/100000000,numberDigit,comma) + '亿'
  20. }else{
  21. if(value==undefined){
  22. return '-'
  23. }else{
  24. return hasDot(value,numberDigit,comma)
  25. }
  26. }
  27. }else{
  28. if(value==undefined){
  29. return '-'
  30. }else{
  31. return value
  32. }
  33. }
  34. }
  35. function hasDot(num,numberDigit,comma){
  36. //有小数点就保留一个小数,没有就直接返回 ,默认保留1位小数
  37. var digit = 1;
  38. if(numberDigit){
  39. digit = numberDigit
  40. }
  41. if (comma) { // 不加逗号
  42. return ((num + '').indexOf('.') != -1 ? parseFloat(Number(num).toFixed(digit)) : num );
  43. } else { // 默认加逗号
  44. return formatNum((num + '').indexOf('.') != -1 ? parseFloat(Number(num).toFixed(digit)) : num );
  45. }
  46. }
  47. export function formatNum (str){
  48. if (typeof parseFloat(str) === 'number' && !isNaN(str) && str != null) {
  49. //判断是否是数值类型
  50. var newStr = "";
  51. var count = 0;
  52. str = Number(str).toFixed(2)
  53. str = parseFloat(str)
  54. str = str.toString();
  55. if (str.indexOf(".") == -1) {
  56. for (var i = str.length - 1; i >= 0; i--) {
  57. if (count % 3 == 0 && count != 0 && str.charAt(i) != '-') {
  58. newStr = str.charAt(i) + "," + newStr;
  59. } else {
  60. newStr = str.charAt(i) + newStr;
  61. }
  62. count++;
  63. }
  64. str = newStr; //自动补小数点后两位
  65. } else {
  66. for (var i = str.indexOf(".") - 1; i >= 0; i--) {
  67. if (count % 3 == 0 && count != 0 && str.charAt(i) != '-') {
  68. newStr = str.charAt(i) + "," + newStr;
  69. } else {
  70. newStr = str.charAt(i) + newStr; //逐个字符相接起来
  71. }
  72. count++;
  73. }
  74. str = newStr + str.substring(str.indexOf("."), str.length);
  75. }
  76. }
  77. return str
  78. }
  79. export function getDay (day, haveHours) {
  80. //day为0表示今天的日期 -1为昨天 haveHours存在表示要时分秒 haveHours:true,false
  81. var today = new Date();
  82. var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
  83. today.setTime(targetday_milliseconds); //注意,这行是关键代码
  84. var tYear = today.getFullYear();
  85. var tMonth = today.getMonth();
  86. var tDate = today.getDate();
  87. var hours = today.getHours();
  88. var minutes = today.getMinutes(); //分
  89. var seconds = today.getSeconds(); //秒
  90. tMonth = doHandleMonth(tMonth + 1);
  91. tDate = doHandleMonth(tDate);
  92. if (haveHours) {
  93. if (day == 0) {
  94. return {
  95. 'now': tYear + "-" + tMonth + "-" + tDate + ' ' + (hours < 10 ? '0' + hours : hours) + ':' + (minutes < 10 ? '0' + minutes : minutes) + ':' + (seconds < 10 ? '0' + seconds : seconds),
  96. 'zero': tYear + "-" + tMonth + "-" + tDate + ' ' + '00:00:00'
  97. }
  98. } else {
  99. return tYear + "-" + tMonth + "-" + tDate + ' ' + hours + ':' + minutes + ':' + seconds
  100. }
  101. } else {
  102. return tYear + "-" + tMonth + "-" + tDate
  103. }
  104. function doHandleMonth (month) {
  105. var m = month;
  106. if (month.toString().length == 1) {
  107. m = "0" + month;
  108. }
  109. return m;
  110. }
  111. }
  112. //复制文案
  113. export function copyEvent({text, cb, toast_title}){
  114. // #ifdef H5
  115. let textarea = document.createElement("textarea")
  116. textarea.value = text
  117. textarea.readOnly = "readOnly"
  118. document.body.appendChild(textarea)
  119. textarea.select() // 选中文本内容
  120. textarea.setSelectionRange(0, text.length)
  121. uni.showToast({//提示
  122. title: toast_title ? toast_title : '复制成功' ,
  123. icon:'success',
  124. complete: () => {
  125. cb ? cb() : ''
  126. }
  127. })
  128. document.execCommand("copy")
  129. textarea.remove()
  130. // #endif
  131. // #ifndef H5
  132. uni.setClipboardData({
  133. data:text,//要被复制的内容
  134. showToast:false,
  135. success:()=>{//复制成功的回调函数
  136. uni.showToast({//提示
  137. title:toast_title ? toast_title : '复制成功',
  138. icon:'success',
  139. complete: () => {
  140. cb ? cb() : ''
  141. }
  142. })
  143. }
  144. },true);
  145. // #endif
  146. }
  147. export const loadmoreOptions = {
  148. LOAD_MORE: 'loadmore', // 加载更多
  149. LOADING: 'loading', // 加载中
  150. NO_MORE: 'nomore', // 没有更多了
  151. }
  152. /**
  153. * canvas文本超出范围时,自动换行
  154. * @param {*} ctx
  155. * @param {*} text
  156. * @param {*} x
  157. * @param {*} y
  158. * @param {*} maxWidth
  159. * @param {*} lineHeight
  160. */
  161. export function canvasTextAutoLine(ctx, text, x, y, maxWidth, lineHeight) {
  162. let lineWidth = 0
  163. let lastSubStrIndex = 0
  164. for (let i = 0; i < text.length; i++) {
  165. lineWidth += ctx.measureText(text[i]).width
  166. if (lineWidth > maxWidth) {
  167. ctx.fillText(text.substring(lastSubStrIndex, i), x, y)
  168. y += lineHeight
  169. lineWidth = 0
  170. lastSubStrIndex = i
  171. }
  172. if (i === text.length - 1) {
  173. ctx.fillText(text.substring(lastSubStrIndex, i + 1), x, y)
  174. }
  175. }
  176. }
  177. /*
  178. * 参数说明
  179. * ctx Canvas实例
  180. * img 图片地址
  181. * x x轴坐标
  182. * y y轴坐标
  183. * r 圆形半径
  184. */
  185. export function circleImgOne(ctx, img, x, y, r) {
  186. let d = r * 2;
  187. let cx = x + r;
  188. let cy = y + r;
  189. ctx.arc(cx, cy, r, 0, 2 * Math.PI);
  190. ctx.strokeStyle = '#FFFFFF'; // 设置绘制圆形边框的颜色
  191. ctx.stroke(); // 绘制出圆形,默认为黑色,可通过 ctx.strokeStyle = '#FFFFFF', 设置想要的颜色
  192. ctx.clip();
  193. ctx.drawImage(img, x, y, d, d);
  194. }