common.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. try {
  24. uni.setStorageSync(key, val);
  25. } catch (e) {
  26. // error
  27. }
  28. }
  29. export const getStorageSync = (key) => {
  30. try {
  31. const value = uni.getStorageSync(key);
  32. return value || ''
  33. } catch (e) {
  34. // error
  35. }
  36. }
  37. export const removeStorageSync = (key) => {
  38. try {
  39. uni.removeStorageSync(key);
  40. } catch (e) {
  41. // error
  42. }
  43. }
  44. export const refreshAccount = () => {
  45. store.commit('app/SET_TOKEN', '')
  46. uni.reLaunch({
  47. url: '/pages/index/index'
  48. });
  49. }
  50. // 判断是否登录
  51. export const ifLogin_ = () => {
  52. return new Promise((resolve, reject) => {
  53. if (getToken()) {
  54. resolve()
  55. } else {
  56. uni.navigateTo({
  57. url: '/pages/login/index'
  58. });
  59. reject()
  60. };
  61. })
  62. };
  63. export const reverseBack = (path = undefined) => {
  64. const pages = getCurrentPages();
  65. if (pages.length <= 1) {
  66. uni.reLaunch({
  67. url: '/pages/index/index'
  68. });
  69. } else {
  70. let deltaNum = 1;
  71. if (path) {
  72. if (path.indexOf('/') === 0) {
  73. path = path.substring(1);
  74. };
  75. const pageLength = pages.length
  76. let num = pageLength;
  77. for (let i = 1; i <= pageLength; i++) {
  78. num -= 1;
  79. if (pages[num].route === path) {
  80. deltaNum = i;
  81. break;
  82. }
  83. }
  84. };
  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 , type = false) => {
  119. if(type){
  120. return nums >= 0 ? true : false
  121. }else{
  122. return nums >= 0 ? 'zhang' : 'die'
  123. }
  124. }
  125. const doubleDigit = (t) => {
  126. if (t <= 9) {
  127. return `0${t}`
  128. }
  129. return t
  130. }
  131. export const getData_ = (time = '', type = true) => {
  132. if (time || type) {
  133. let now = time ? new Date(time) : new Date();
  134. const year = now.getFullYear();
  135. const month = doubleDigit(now.getMonth() + 1);
  136. const day = doubleDigit(now.getDate());
  137. const hour = doubleDigit(now.getHours());
  138. const min = doubleDigit(now.getMinutes());
  139. const seon = doubleDigit(now.getSeconds());
  140. return `${year}-${month}-${day} ${hour}:${min}:${seon}`
  141. }
  142. return ''
  143. }
  144. export const scanCode = () => {
  145. // 允许从相机和相册扫码
  146. return new Promise((resolve, reject) => {
  147. uni.scanCode({
  148. success: res => {
  149. console.log('条码类型:' + res.scanType);
  150. console.log('条码内容:' + res.result);
  151. resolve(res.result)
  152. },
  153. fail: err => {
  154. reject()
  155. },
  156. });
  157. })
  158. }
  159. // 阅读文章详情
  160. export const readArticleInfo = (id) => {
  161. uni.navigateTo({
  162. url: `/pages/content/article-details?id=${id}`
  163. })
  164. }
  165. // 数字截取
  166. export const numIntercepting = (num = 0, decimals = 0) => {
  167. if (num) {
  168. try {
  169. num = parseInt(num).toFixed(decimals)
  170. } catch {}
  171. }
  172. return num
  173. }
  174. export const getChange = (item, type) => {
  175. // 计算
  176. let num = ''
  177. switch (type) {
  178. case 1:
  179. // 浮动盈亏 = ((开仓价格 - 标记价格) × 数量 - 手续费)*100/ 标记价格
  180. num = ((item.update_price - item.origin_price) * item.number - item.trade_fee) * 100 / item
  181. .origin_price
  182. if (num) {
  183. num = num.toFixed(2)
  184. };
  185. break;
  186. case 2:
  187. // 保证金*100/可用余额
  188. num = item.caution_money * 100 / item.user.lever
  189. break;
  190. case 3:
  191. // 强平价 = 标记价格*100
  192. num = item.origin_price * 100
  193. break;
  194. }
  195. return (num || num === 0) ? num : '--'
  196. }