base.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * 元换算为万元
  3. * @param num :
  4. * @returns {number} :
  5. */
  6. function divTenThousand (num) {
  7. if (num == null) {
  8. return 0
  9. }
  10. return div(num, 10000, 3)
  11. // return num
  12. }
  13. /**
  14. * 元换算为万元
  15. * @param num :
  16. * @returns {RegExpMatchArray} :
  17. */
  18. function divScale (num) {
  19. if (num == null) {
  20. return 0
  21. }
  22. const str = Number(Math.round(num * 100) / 100).toString()
  23. if (str.indexOf('.') > 0) {
  24. return (str + '00').match(/^\d+(?:\.\d{0,2})?/)
  25. }
  26. return str + '.00'
  27. // return num
  28. }
  29. /**
  30. * 元换算为万元,并格式化
  31. * @param num :
  32. * @returns {string} :
  33. */
  34. // eslint-disable-next-line no-unused-vars
  35. function divTenThousandFormatter (num) {
  36. return formatter(divTenThousand(num))
  37. }
  38. function formatter (value) {
  39. if (value == null) {
  40. return value
  41. }
  42. return value.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
  43. }
  44. // eslint-disable-next-line no-unused-vars
  45. function parser (value) {
  46. return value.replace(/\$\s?|(,*)/g, '')
  47. }
  48. /**
  49. * 万元换算为元
  50. * @param num
  51. * @returns {*}
  52. */
  53. // eslint-disable-next-line no-unused-vars
  54. function mulTenThousand (num) {
  55. if (num == null || num === '') {
  56. return 0
  57. }
  58. return mul(num, 10000, 6)
  59. // return num
  60. }
  61. // eslint-disable-next-line no-unused-vars
  62. function add (arg1, arg2) {
  63. if (arg1 == null || arg1 === '') {
  64. arg1 = 0
  65. }
  66. if (arg2 == null || arg2 === '') {
  67. arg2 = 0
  68. }
  69. let r1, r2
  70. try {
  71. r1 = arg1.toString().split('.')[1].length
  72. } catch (e) {
  73. r1 = 0
  74. }
  75. try {
  76. r2 = arg2.toString().split('.')[1].length
  77. } catch (e) {
  78. r2 = 0
  79. }
  80. const m = Math.pow(10, Math.max(r1, r2))
  81. const n = (r1 >= r2) ? r1 : r2
  82. return ((arg1 * m + arg2 * m) / m).toFixed(n)
  83. }
  84. // eslint-disable-next-line no-unused-vars
  85. function sub (arg1, arg2) {
  86. if (arg1 == null || arg1 === '') {
  87. arg1 = 0
  88. }
  89. if (arg2 == null || arg2 === '') {
  90. arg2 = 0
  91. }
  92. let re1, re2
  93. try {
  94. re1 = arg1.toString().split('.')[1].length
  95. } catch (e) {
  96. re1 = 0
  97. }
  98. try {
  99. re2 = arg2.toString().split('.')[1].length
  100. } catch (e) {
  101. re2 = 0
  102. }
  103. const m = Math.pow(10, Math.max(re1, re2))
  104. const n = (re1 >= re2) ? re1 : re2
  105. return ((arg1 * m - arg2 * m) / m).toFixed(n)
  106. }
  107. /**
  108. * 1
  109. * @param arg1
  110. * @param arg2
  111. * @returns {number}
  112. */
  113. // eslint-disable-next-line no-unused-vars
  114. function mul (arg1, arg2) {
  115. if (arg1 == null || arg1 === '') {
  116. arg1 = 0
  117. }
  118. if (arg2 == null || arg2 === '') {
  119. arg2 = 0
  120. }
  121. let m = 0
  122. const s1 = arg1.toString()
  123. const s2 = arg2.toString()
  124. try {
  125. m += s1.split('.')[1].length
  126. } catch (e) {}
  127. try {
  128. m += s2.split('.')[1].length
  129. } catch (e) {}
  130. return Number(s1.replace('.', '')) * Number(s2.replace('.', '')) / Math.pow(10, m)
  131. }
  132. /**
  133. * 除法 注意如果整除了 小数点将无效
  134. * @param arg1 : 被除数
  135. * @param arg2 : 除数
  136. * @param digit :精确位数
  137. * @returns {number}
  138. */
  139. function div (arg1, arg2, digit) {
  140. if (arg1 == null || arg1 === '') {
  141. arg1 = 0
  142. }
  143. if (arg2 == null || arg2 === '') {
  144. arg2 = 1
  145. }
  146. let t1 = 0
  147. let t2 = 0
  148. try { t1 = arg1.toString().split('.')[1].length } catch (e) {}
  149. try { t2 = arg2.toString().split('.')[1].length } catch (e) {}
  150. const r1 = Number(arg1.toString().replace('.', ''))
  151. const r2 = Number(arg2.toString().replace('.', ''))
  152. // 获取小数点后的计算值
  153. const result = ((r1 / r2) * Math.pow(10, t2 - t1)).toString()
  154. let result2 = result.split('.')[1]
  155. if (result2 !== undefined) {
  156. result2 = result2.substring(0, digit > result2.length ? result2.length : digit)
  157. } else {
  158. result2 = ''
  159. }
  160. while (result2.length < digit) {
  161. result2 = result2 + '0'
  162. }
  163. if (digit === 0) {
  164. return Number(result.split('.')[0])
  165. }
  166. return Number(result.split('.')[0] + '.' + result2)
  167. }
  168. /**
  169. * 获取vue token
  170. * @returns {string}
  171. */
  172. function getToken () {
  173. const storeTokenJson = JSON.parse(localStorage.getItem(VUE_STORE_BASE + ACCESS_TOKEN))
  174. return 'Bearer ' + storeTokenJson.value
  175. }
  176. function getHeader () {
  177. return {
  178. Authorization: getToken()
  179. // accept: "application/json; charset=utf-8",
  180. // "Access-Control-Allow-Origin": "http://localhost:5000",
  181. // "Access-Control-Allow-Headers":"Origin, X-Requested-With, Content-Type, Accept"
  182. }
  183. }
  184. function ajaxError (xhr) {
  185. if (xhr.status === 401) {
  186. // 跳转登录页面
  187. window.parent.location.href = '/user/login'
  188. } else {
  189. // 调用外部的error
  190. error && error(xhr, textStatus, errorThrown)
  191. }
  192. }