utils.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. export function isDefine(value) {
  210. if (value == null || value == "" || value == "undefined" || value == undefined || value == "null" || value == "(null)" || value == 'NULL' || typeof (value) == 'undefined') {
  211. return false;
  212. }
  213. else {
  214. value = value + "";
  215. value = value.replace(/\s/g, "");
  216. if (value == "") {
  217. return false;
  218. }
  219. return true;
  220. }
  221. };
  222. export function isMobile(value){
  223. var reg = /^1[0-9]{10,10}$/;
  224. if (!reg.test(value)) {
  225. return false;
  226. }else{
  227. return true;
  228. }
  229. }
  230. export function isMoney(price){
  231. price = parseFloat(price);
  232. if (!isDefine(price) || isNaN(price) || price<0){
  233. return false;
  234. }
  235. return true;
  236. }
  237. /**
  238. * 不四舍五入保留n位小数
  239. * params: val 需要处理的数字
  240. * params: len 需要保留的小数点位数
  241. */
  242. export function formatNub(num, n = 2) {
  243. if (typeof num != "number" && !Number(num)) {
  244. return "0.00";
  245. }
  246. num = Number(num).toString();
  247. let result = "";
  248. let zeroResult = function(n) {
  249. let zero = "";
  250. for (let i = 0; i < n; i++) {
  251. zero += "0";
  252. }
  253. return zero;
  254. };
  255. if (num % 1 == 0) {
  256. //整数
  257. result = num + "." + zeroResult(n);
  258. } else {
  259. //小数
  260. let num1 = num.split(".");
  261. if (num1[1].length < n) {
  262. result = num1[0] + "." + num1[1] + zeroResult(n - num1[1].length);
  263. } else {
  264. result = num1[0] + "." + num1[1].substring(0, n);
  265. }
  266. }
  267. return result;
  268. };
  269. //省市区截取
  270. export function getArea(str) {
  271. let area = {}
  272. let index11 = 0
  273. let index1 = str.indexOf("省")
  274. if (index1 == -1) {
  275. index11 = str.indexOf("自治区")
  276. if (index11 != -1) {
  277. area.provinceName = str.substring(0, index11 + 3)
  278. } else {
  279. area.provinceName = str.substring(0, 0)
  280. }
  281. } else {
  282. area.provinceName = str.substring(0, index1 + 1)
  283. }
  284. let index2 = str.indexOf("市")
  285. if (index11 == -1) {
  286. area.cityName = str.substring(index11 + 1, index2 + 1)
  287. } else {
  288. if (index11 == 0) {
  289. area.cityName = str.substring(index1 + 1, index2 + 1)
  290. } else {
  291. area.cityName = str.substring(index11 + 3, index2 + 1)
  292. }
  293. }
  294. let index3 = str.lastIndexOf("区")
  295. if (index3 == -1) {
  296. index3 = str.indexOf("县")
  297. if (index3 == -1) {
  298. index3 = str.lastIndexOf("市")
  299. area.districtName = str.substring(index2 + 1, index3 + 1)
  300. } else {
  301. area.districtName = str.substring(index2 + 1, index3 + 1)
  302. }
  303. } else {
  304. area.districtName = str.substring(index2 + 1, index3 + 1)
  305. }
  306. area.address = str.substring(index3 + 1)
  307. return area;
  308. }
  309. /**
  310. * 将手机号以3-4-4格式展示
  311. * */
  312. export const mobileFormat = (mobile) => {
  313. return mobile.replace(/(\d{3})(\d{4})(\d{4})/, '$1 $2 $3');
  314. }
  315. /**
  316. * 将手机号空格还原
  317. * */
  318. export const mobileRe = (mobile) => {
  319. return mobile.replace(/\s/g, '')
  320. }