common.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. // 判断是否登录
  39. export const ifLogin_ = () => {
  40. return new Promise((resolve, reject) => {
  41. if (getToken()) {
  42. resolve()
  43. } else {
  44. uni.navigateTo({
  45. url: '/pages/login/index'
  46. });
  47. reject()
  48. };
  49. })
  50. };
  51. export const reverseBack = (path = undefined) => {
  52. const pages = getCurrentPages()
  53. if (pages.length <= 1) {
  54. uni.reLaunch({
  55. url: '/pages/index/index'
  56. });
  57. } else {
  58. let deltaNum = 1;
  59. if (path) {
  60. const pageLength = pages.length
  61. let num = pageLength;
  62. for (let i = 1; i < pageLength; i++) {
  63. num -= 1;
  64. if (pages[num].route === path) {
  65. deltaNum = i;
  66. }
  67. }
  68. };
  69. console.log('pages = ' , pages)
  70. console.log('deltaNum = ' , deltaNum)
  71. uni.navigateBack({
  72. delta: deltaNum,
  73. fail: err => {}
  74. })
  75. }
  76. }
  77. export const decimalNum = {
  78. // 加法
  79. add: (a, b) => {
  80. return new Decimal(a).add(new Decimal(b))
  81. },
  82. // 减法
  83. sub: (a, b) => {
  84. return new Decimal(a).sub(new Decimal(b))
  85. },
  86. // 乘法
  87. mul: (a, b) => {
  88. return new Decimal(a).mul(new Decimal(b))
  89. },
  90. // 除法
  91. div: (a, b) => {
  92. return new Decimal(a).div(new Decimal(b))
  93. },
  94. // // 加法
  95. // let c = new Decimal(a).add(new Decimal(b))
  96. // // 减法
  97. // let d = new Decimal(a).sub(new Decimal(b))
  98. // // 乘法
  99. // let e = new Decimal(a).mul(new Decimal(b))
  100. // // 除法
  101. // let f = new Decimal(a).div(new Decimal(b))
  102. }