common.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. store.commit('app/SET_TOKEN', '')
  47. uni.reLaunch({
  48. url: '/pages/index/index'
  49. });
  50. }
  51. // 判断是否登录
  52. export const ifLogin_ = () => {
  53. return new Promise((resolve, reject) => {
  54. if (getToken()) {
  55. resolve()
  56. } else {
  57. uni.navigateTo({
  58. url: '/pages/login/index'
  59. });
  60. reject()
  61. };
  62. })
  63. };
  64. export const reverseBack = (path = undefined) => {
  65. const pages = getCurrentPages();
  66. if (pages.length <= 1) {
  67. uni.reLaunch({
  68. url: '/pages/index/index'
  69. });
  70. } else {
  71. let deltaNum = 1;
  72. if (path) {
  73. if (path.indexOf('/') === 0) {
  74. path = path.substring(1);
  75. };
  76. const pageLength = pages.length
  77. let num = pageLength;
  78. for (let i = 1; i <= pageLength; i++) {
  79. num -= 1;
  80. if (pages[num].route === path) {
  81. deltaNum = i;
  82. break;
  83. }
  84. }
  85. };
  86. uni.navigateBack({
  87. delta: deltaNum,
  88. fail: err => {}
  89. })
  90. }
  91. }
  92. export const decimalNum = {
  93. // 加法
  94. add: (a, b) => {
  95. return new Decimal(a).add(new Decimal(b))
  96. },
  97. // 减法
  98. sub: (a, b) => {
  99. return new Decimal(a).sub(new Decimal(b))
  100. },
  101. // 乘法
  102. mul: (a, b) => {
  103. return new Decimal(a).mul(new Decimal(b))
  104. },
  105. // 除法
  106. div: (a, b) => {
  107. return new Decimal(a).div(new Decimal(b))
  108. },
  109. // // 加法
  110. // let c = new Decimal(a).add(new Decimal(b))
  111. // // 减法
  112. // let d = new Decimal(a).sub(new Decimal(b))
  113. // // 乘法
  114. // let e = new Decimal(a).mul(new Decimal(b))
  115. // // 除法
  116. // let f = new Decimal(a).div(new Decimal(b))
  117. }
  118. // 涨跌颜色
  119. export const setColor = (nums) => {
  120. return nums >= 0 ? 'zhang' : 'die'
  121. }
  122. const doubleDigit = (t) => {
  123. if (t <= 9) {
  124. return `0${t}`
  125. }
  126. return t
  127. }
  128. export const getData_ = (time = '', type = true) => {
  129. if (time || type) {
  130. let now = time ? new Date(time) : new Date()
  131. console.log('now = ', now, time)
  132. const year = now.getFullYear();
  133. const month = doubleDigit(now.getMonth() + 1);
  134. const day = doubleDigit(now.getDate());
  135. const hour = doubleDigit(now.getHours());
  136. const min = doubleDigit(now.getMinutes());
  137. const seon = doubleDigit(now.getSeconds());
  138. return `${year}-${month}-${day} ${hour}:${min}:${seon}`
  139. }
  140. return ''
  141. }
  142. export const scanCode = () => {
  143. // 允许从相机和相册扫码
  144. return new Promise((resolve, reject) => {
  145. uni.scanCode({
  146. success: res => {
  147. console.log('条码类型:' + res.scanType);
  148. console.log('条码内容:' + res.result);
  149. resolve(res.result)
  150. },
  151. fail: err => {
  152. reject()
  153. },
  154. });
  155. })
  156. }
  157. // 阅读文章详情
  158. export const readArticleInfo = (id) => {
  159. uni.navigateTo({
  160. url: `/pages/content/article-details?id=${id}`
  161. })
  162. }
  163. // 数字截取
  164. export const numIntercepting = (num = 0, decimals = 0) => {
  165. if (num) {
  166. try {
  167. num = parseInt(num).toFixed(decimals)
  168. } catch {}
  169. }
  170. return num
  171. }