common.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // import config from "./config"
  2. import store from "@/store/index.js"
  3. import {
  4. Decimal
  5. } from 'decimal.js'
  6. import config from "./config.js"
  7. export const setToken = (tokenVal) => {
  8. try {
  9. uni.setStorageSync(config.tokenKey, tokenVal);
  10. } catch (e) {
  11. // error
  12. }
  13. }
  14. export const getToken = () => {
  15. try {
  16. const value = uni.getStorageSync(config.tokenKey);
  17. return value || ''
  18. } catch (e) {
  19. // error
  20. }
  21. }
  22. export const setStorageSync = (key, val) => {
  23. console.log('setStorageSync = ', key, val)
  24. try {
  25. uni.setStorageSync(key, val);
  26. } catch (e) {
  27. // error
  28. }
  29. }
  30. export const getStorageSync = (key) => {
  31. try {
  32. const value = uni.getStorageSync(key);
  33. return value || ''
  34. } catch (e) {
  35. // error
  36. }
  37. }
  38. export const removeStorageSync = (key) => {
  39. try {
  40. uni.removeStorageSync(key);
  41. } catch (e) {
  42. // error
  43. }
  44. }
  45. export const refreshAccount = () => {
  46. // removeStorageSync(config.tokenKey);
  47. store.commit('app/SET_TOKEN' , '')
  48. uni.reLaunch({
  49. url: '/pages/index/index'
  50. });
  51. }
  52. // 判断是否登录
  53. export const ifLogin_ = () => {
  54. return new Promise((resolve, reject) => {
  55. if (getToken()) {
  56. resolve()
  57. } else {
  58. uni.navigateTo({
  59. url: '/pages/login/index'
  60. });
  61. reject()
  62. };
  63. })
  64. };
  65. export const reverseBack = (path = undefined) => {
  66. const pages = getCurrentPages()
  67. if (pages.length <= 1) {
  68. uni.reLaunch({
  69. url: '/pages/index/index'
  70. });
  71. } else {
  72. let deltaNum = 1;
  73. if (path) {
  74. const pageLength = pages.length
  75. let num = pageLength;
  76. for (let i = 1; i < pageLength; i++) {
  77. num -= 1;
  78. if (pages[num].route === path) {
  79. deltaNum = i;
  80. }
  81. }
  82. };
  83. console.log('pages = ' , pages)
  84. console.log('deltaNum = ' , deltaNum)
  85. uni.navigateBack({
  86. delta: deltaNum,
  87. fail: err => {}
  88. })
  89. }
  90. }
  91. export const decimalNum = {
  92. // 加法
  93. add: (a, b) => {
  94. return new Decimal(a).add(new Decimal(b))
  95. },
  96. // 减法
  97. sub: (a, b) => {
  98. return new Decimal(a).sub(new Decimal(b))
  99. },
  100. // 乘法
  101. mul: (a, b) => {
  102. return new Decimal(a).mul(new Decimal(b))
  103. },
  104. // 除法
  105. div: (a, b) => {
  106. return new Decimal(a).div(new Decimal(b))
  107. },
  108. // // 加法
  109. // let c = new Decimal(a).add(new Decimal(b))
  110. // // 减法
  111. // let d = new Decimal(a).sub(new Decimal(b))
  112. // // 乘法
  113. // let e = new Decimal(a).mul(new Decimal(b))
  114. // // 除法
  115. // let f = new Decimal(a).div(new Decimal(b))
  116. }
  117. // 涨跌颜色
  118. export const setColor = (nums) => {
  119. return nums > 0 ? 'zhang' : 'die'
  120. }