123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- const ACCESS_TOKEN = 'Access-Token'
- const VUE_STORE_BASE = 'pro__'
- /**
- * 元换算为万元
- * @param num :
- * @returns {number} :
- */
- function divTenThousand (num) {
- if (num == null) {
- return 0
- }
- return div(num, 10000, 3)
- // return num
- }
- /**
- * 元换算为万元
- * @param num :
- * @returns {RegExpMatchArray} :
- */
- function divScale (num) {
- if (num == null) {
- return 0
- }
- const str = Number(Math.round(num * 100) / 100).toString()
- if (str.indexOf('.') > 0) {
- return (str + '00').match(/^\d+(?:\.\d{0,2})?/)
- }
- return str + '.00'
- // return num
- }
- /**
- * 元换算为万元,并格式化
- * @param num :
- * @returns {string} :
- */
- // eslint-disable-next-line no-unused-vars
- function divTenThousandFormatter (num) {
- return formatter(divTenThousand(num))
- }
- function formatter (value) {
- if (value == null) {
- return value
- }
- return value.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
- }
- // eslint-disable-next-line no-unused-vars
- function parser (value) {
- return value.replace(/\$\s?|(,*)/g, '')
- }
- /**
- * 万元换算为元
- * @param num
- * @returns {*}
- */
- // eslint-disable-next-line no-unused-vars
- function mulTenThousand (num) {
- if (num == null || num === '') {
- return 0
- }
- return mul(num, 10000, 6)
- // return num
- }
- // eslint-disable-next-line no-unused-vars
- function add (arg1, arg2) {
- if (arg1 == null || arg1 === '') {
- arg1 = 0
- }
- if (arg2 == null || arg2 === '') {
- arg2 = 0
- }
- let r1, r2
- try {
- r1 = arg1.toString().split('.')[1].length
- } catch (e) {
- r1 = 0
- }
- try {
- r2 = arg2.toString().split('.')[1].length
- } catch (e) {
- r2 = 0
- }
- const m = Math.pow(10, Math.max(r1, r2))
- const n = (r1 >= r2) ? r1 : r2
- return ((arg1 * m + arg2 * m) / m).toFixed(n)
- }
- // eslint-disable-next-line no-unused-vars
- function sub (arg1, arg2) {
- if (arg1 == null || arg1 === '') {
- arg1 = 0
- }
- if (arg2 == null || arg2 === '') {
- arg2 = 0
- }
- let re1, re2
- try {
- re1 = arg1.toString().split('.')[1].length
- } catch (e) {
- re1 = 0
- }
- try {
- re2 = arg2.toString().split('.')[1].length
- } catch (e) {
- re2 = 0
- }
- const m = Math.pow(10, Math.max(re1, re2))
- const n = (re1 >= re2) ? re1 : re2
- return ((arg1 * m - arg2 * m) / m).toFixed(n)
- }
- /**
- * 1
- * @param arg1
- * @param arg2
- * @returns {number}
- */
- // eslint-disable-next-line no-unused-vars
- function mul (arg1, arg2) {
- if (arg1 == null || arg1 === '') {
- arg1 = 0
- }
- if (arg2 == null || arg2 === '') {
- arg2 = 0
- }
- let m = 0
- const s1 = arg1.toString()
- const s2 = arg2.toString()
- try {
- m += s1.split('.')[1].length
- } catch (e) {}
- try {
- m += s2.split('.')[1].length
- } catch (e) {}
- return Number(s1.replace('.', '')) * Number(s2.replace('.', '')) / Math.pow(10, m)
- }
- /**
- * 除法 注意如果整除了 小数点将无效
- * @param arg1 : 被除数
- * @param arg2 : 除数
- * @param digit :精确位数
- * @returns {number}
- */
- function div (arg1, arg2, digit) {
- if (arg1 == null || arg1 === '') {
- arg1 = 0
- }
- if (arg2 == null || arg2 === '') {
- arg2 = 1
- }
- let t1 = 0
- let t2 = 0
- try { t1 = arg1.toString().split('.')[1].length } catch (e) {}
- try { t2 = arg2.toString().split('.')[1].length } catch (e) {}
- const r1 = Number(arg1.toString().replace('.', ''))
- const r2 = Number(arg2.toString().replace('.', ''))
- // 获取小数点后的计算值
- const result = ((r1 / r2) * Math.pow(10, t2 - t1)).toString()
- let result2 = result.split('.')[1]
- if (result2 !== undefined) {
- result2 = result2.substring(0, digit > result2.length ? result2.length : digit)
- } else {
- result2 = ''
- }
- while (result2.length < digit) {
- result2 = result2 + '0'
- }
- if (digit === 0) {
- return Number(result.split('.')[0])
- }
- return Number(result.split('.')[0] + '.' + result2)
- }
- /**
- * 获取vue token
- * @returns {string}
- */
- function getToken () {
- const storeTokenJson = JSON.parse(localStorage.getItem(VUE_STORE_BASE + ACCESS_TOKEN))
- return 'Bearer ' + storeTokenJson.value
- }
- function getHeader () {
- return {
- Authorization: getToken(),
- accept: 'application/json; charset=utf-8',
- 'Access-Control-Allow-Origin': 'http://localhost:5000',
- 'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept',
- 'Content-Type': 'application/json;charset=UTF-8'
- }
- }
- function ajaxError (xhr) {
- if (xhr.status === 401) {
- // 跳转登录页面
- window.parent.location.href = '/user/login'
- } else {
- console.log('请求错误')
- }
- }
|