base.js 4.4 KB

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