utils.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /**
  2. * 将小数点后面的字体变小
  3. * */
  4. export const splitPrice = (price = 0, px = 10) => {
  5. let arr = (price || 0).toString().split(".");
  6. return `${arr[0]}<span style="font-size:${px}px;">.${arr[1]||"00"}</span>`;
  7. }
  8. // 判断市场常见的几种刘海屏机型
  9. export const modelmes = () => {
  10. // 判断市场常见的几种刘海屏机型
  11. const systemInfo = uni.getSystemInfoSync();
  12. let modelmes = systemInfo.model;
  13. if (modelmes.indexOf('iPhone X') >= 0 || modelmes.indexOf('iPhone XR') >= 0 || modelmes.indexOf('iPhone XS') >=
  14. 0 ||
  15. modelmes.indexOf('iPhone 12') >= 0 || modelmes.indexOf('iPhone 11') >= 0 || modelmes.indexOf('iPhone11') >=
  16. 0 ||
  17. modelmes.indexOf('iPhone12') >= 0 || modelmes.indexOf('iPhoneXR') >= 0 || modelmes.indexOf('iPhoneX') >= 0
  18. ) {
  19. return true
  20. } else {
  21. return false
  22. }
  23. }
  24. /**
  25. * 将rgba转为十六进制
  26. * */
  27. export const hexify = (color) => {
  28. if (!color) return ''
  29. var values = color
  30. .replace(/rgba?\(/, '')
  31. .replace(/\)/, '')
  32. .replace(/[\s+]/g, '')
  33. .split(',');
  34. var a = parseFloat(values[3] || 1),
  35. r = Math.floor(a * parseInt(values[0]) + (1 - a) * 255),
  36. g = Math.floor(a * parseInt(values[1]) + (1 - a) * 255),
  37. b = Math.floor(a * parseInt(values[2]) + (1 - a) * 255);
  38. return "#" +
  39. ("0" + r.toString(16)).slice(-2) +
  40. ("0" + g.toString(16)).slice(-2) +
  41. ("0" + b.toString(16)).slice(-2);
  42. }
  43. /**
  44. * 将十六进制颜色转为rgba
  45. *使用时只需传入十六进制字符串,“n”表示透明度
  46. * */
  47. export const colorRgba = (str, n) => {
  48. console.log(n)
  49. // 十六进制颜色值的正则表达式
  50. let reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/
  51. let sColor = str.toLowerCase()
  52. if (str === 'rgb(95,171,42)') {
  53. sColor = '#91E055'.toLowerCase()
  54. }
  55. if (str === 'rgb(145,224,85)') {
  56. sColor = '#5FAB2A'.toLowerCase()
  57. }
  58. // 十六进制颜色转换为RGB格式
  59. if (sColor && reg.test(sColor)) {
  60. if (sColor.length === 4) {
  61. let sColorNew = '#'
  62. for (let i = 1; i < 4; i += 1) { // 例如:#eee,#fff等
  63. sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1))
  64. }
  65. sColor = sColorNew
  66. }
  67. // 处理六位颜色值
  68. let sColorChange = []
  69. for (let i = 1; i < 7; i += 2) {
  70. sColorChange.push(parseInt('0x' + sColor.slice(i, i + 2)))
  71. }
  72. return 'rgba(' + sColorChange.join(',') + ',' + n + ')'
  73. } else {
  74. return sColor
  75. }
  76. }
  77. // 获取【加载更多】组件状态
  78. export function loadStatus(page, pageSize, total) {
  79. if (total / pageSize > page) {
  80. return 'loadmore';
  81. } else {
  82. return 'nomore';
  83. }
  84. }
  85. /*加法函数,用来得到精确的加法结果 */
  86. export const accAdd = (num1, num2) => {
  87. var p1 = 0;
  88. var p2 = 0;
  89. if (num1.toString().split('.').length > 1) {
  90. p1 = num1.toString().split('.')[1].length;
  91. }
  92. if (num2.toString().split('.').length > 1) {
  93. p2 = num2.toString().split(".")[1].length;
  94. }
  95. var p = p1 > p2 ? p1 : p2;
  96. var n1 = num1 * Math.pow(10, p);
  97. var n2 = num2 * Math.pow(10, p);
  98. var result = (n1 + n2) / Math.pow(10, p);
  99. return parseFloat(result.toPrecision(12));
  100. }
  101. /*减法函数,用来得到精确的减法结果 */
  102. export const Subtr = (arg1, arg2) => {
  103. var r1, r2, m, n;
  104. try {
  105. r1 = arg1.toString().split(".")[1].length
  106. } catch (e) {
  107. r1 = 0
  108. }
  109. try {
  110. r2 = arg2.toString().split(".")[1].length
  111. } catch (e) {
  112. r2 = 0
  113. }
  114. m = Math.pow(10, Math.max(r1, r2));
  115. //last modify by deeka
  116. //动态控制精度长度
  117. n = (r1 >= r2) ? r1 : r2;
  118. var result = ((arg1 * m - arg2 * m) / m).toFixed(n);
  119. return result
  120. }
  121. // 本月第一天
  122. export function showMonthFirstDay() {
  123. var Nowdate = new Date();
  124. var MonthFirstDay = new Date(Nowdate.getFullYear(), Nowdate.getMonth(), 1);
  125. const M = Number(MonthFirstDay.getMonth()) + 1;
  126. return (
  127. MonthFirstDay.getFullYear() +
  128. "-" +
  129. M +
  130. "-" +
  131. MonthFirstDay.getDate() +
  132. " 00:00:00"
  133. );
  134. }
  135. /**
  136. * @description 时间格式化 时间戳转换为时间
  137. * @param date
  138. * @param fmt
  139. * @return {*}
  140. */
  141. export function formatDate(date, fmt) {
  142. if (!date) {
  143. return ''
  144. }
  145. // if (typeof date !== 'string') {
  146. // return ''
  147. // }
  148. date *= (date.toString().length === 10 ? 1000 : 1)
  149. let _date = new Date(date)
  150. let _fmt = fmt || 'yyyy-MM-dd hh:mm:ss'
  151. let o = {
  152. 'M+': _date.getMonth() + 1,
  153. 'd+': _date.getDate(),
  154. 'h+': _date.getHours(),
  155. 'm+': _date.getMinutes(),
  156. 's+': _date.getSeconds()
  157. }
  158. if (/(y+)/.test(_fmt)) {
  159. _fmt = _fmt.replace(RegExp.$1, (_date.getFullYear() + '').substr(4 - RegExp.$1.length))
  160. }
  161. for (let k in o) {
  162. if (new RegExp('(' + k + ')').test(_fmt)) {
  163. _fmt = _fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k])
  164. .length)))
  165. }
  166. }
  167. return _fmt
  168. }
  169. /**
  170. * @description 时间格式化 时间字符串转换为时间戳
  171. * @param date
  172. * @param len 默认时间戳长度为10位
  173. * @return {*}
  174. */
  175. export function dateToStamp(str, len = 10) {
  176. // new Date(yyyy-mm-dd hh:mm:ss) 这里ios会返回null,把‘-’划分替换为‘/’划分
  177. let date = new Date(str.replace(/-/g, '/'))
  178. return parseInt(date.getTime() / (len === 13 ? 1 : 1000))
  179. }
  180. /**
  181. * 格式化数字,数字每隔三位加个逗号
  182. * params: num 需要处理的数字
  183. * params: len 需要保留的小数点位数
  184. */
  185. export function formattedNumber(num, len = 2) {
  186. let result = "",
  187. counter = 0;
  188. let formatNum;
  189. if (num < 0) {
  190. formatNum = formatNub(Math.abs(num) || 0, len);
  191. } else {
  192. formatNum = formatNub(num || 0, len);
  193. }
  194. let stringNum = formatNum.toString().split(".");
  195. for (let i = stringNum[0].length - 1; i >= 0; i--) {
  196. counter++;
  197. result = stringNum[0].charAt(i) + result;
  198. if (!(counter % 3) && i != 0) {
  199. result = "," + result;
  200. }
  201. }
  202. result = num < 0 ? "-" + result : result;
  203. if (stringNum[1]) {
  204. return "¥" + result + "." + stringNum[1];
  205. } else {
  206. return "¥" + result;
  207. }
  208. };
  209. /**
  210. * 不四舍五入保留n位小数
  211. * params: val 需要处理的数字
  212. * params: len 需要保留的小数点位数
  213. */
  214. export function formatNub(num, n = 2) {
  215. if (typeof num != "number" && !Number(num)) {
  216. return "0.00";
  217. }
  218. num = Number(num).toString();
  219. let result = "";
  220. let zeroResult = function(n) {
  221. let zero = "";
  222. for (let i = 0; i < n; i++) {
  223. zero += "0";
  224. }
  225. return zero;
  226. };
  227. if (num % 1 == 0) {
  228. //整数
  229. result = num + "." + zeroResult(n);
  230. } else {
  231. //小数
  232. let num1 = num.split(".");
  233. if (num1[1].length < n) {
  234. result = num1[0] + "." + num1[1] + zeroResult(n - num1[1].length);
  235. } else {
  236. result = num1[0] + "." + num1[1].substring(0, n);
  237. }
  238. }
  239. return result;
  240. };
  241. //省市区截取
  242. export function getArea(str) {
  243. let area = {}
  244. let index11 = 0
  245. let index1 = str.indexOf("省")
  246. if (index1 == -1) {
  247. index11 = str.indexOf("自治区")
  248. if (index11 != -1) {
  249. area.provinceName = str.substring(0, index11 + 3)
  250. } else {
  251. area.provinceName = str.substring(0, 0)
  252. }
  253. } else {
  254. area.provinceName = str.substring(0, index1 + 1)
  255. }
  256. let index2 = str.indexOf("市")
  257. if (index11 == -1) {
  258. area.cityName = str.substring(index11 + 1, index2 + 1)
  259. } else {
  260. if (index11 == 0) {
  261. area.cityName = str.substring(index1 + 1, index2 + 1)
  262. } else {
  263. area.cityName = str.substring(index11 + 3, index2 + 1)
  264. }
  265. }
  266. let index3 = str.lastIndexOf("区")
  267. if (index3 == -1) {
  268. index3 = str.indexOf("县")
  269. if (index3 == -1) {
  270. index3 = str.lastIndexOf("市")
  271. area.districtName = str.substring(index2 + 1, index3 + 1)
  272. } else {
  273. area.districtName = str.substring(index2 + 1, index3 + 1)
  274. }
  275. } else {
  276. area.districtName = str.substring(index2 + 1, index3 + 1)
  277. }
  278. area.address = str.substring(index3 + 1)
  279. return area;
  280. }
  281. /**
  282. * 将手机号以3-4-4格式展示
  283. * */
  284. export const mobileFormat = (mobile) => {
  285. return mobile.replace(/(\d{3})(\d{4})(\d{4})/, '$1 $2 $3');
  286. }
  287. /**
  288. * 将手机号空格还原
  289. * */
  290. export const mobileRe = (mobile) => {
  291. return mobile.replace(/\s/g, '')
  292. }