newWork.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <template>
  2. <div class="game-nav-newwork" @mouseenter="testMainNet">
  3. <i class="wifi-icon" :class="wifiColor"></i>
  4. <div class="newwork-list">
  5. <p class="newwork-item" @click="changeMainnet(item.host, item.delay)" :class="getCurClass(item.host)" v-for="(item, key) in newWorkData" :key="key">
  6. <span class="name">{{item.mainnet}}</span>
  7. <span class="time" :class="stateMap[item.state]">{{item.delay}}</span>
  8. </p>
  9. </div>
  10. </div>
  11. </template>
  12. <script>
  13. import { mainNetMap, changeNewWork } from '../../util/contract.js'
  14. import Eos from 'eosjs'
  15. export default {
  16. name: 'gameNetWork',
  17. data () {
  18. return {
  19. newWorkData: [],
  20. stateMap: ['test', 'green', 'yellow', 'red'],
  21. currentDelay: 0
  22. }
  23. },
  24. computed: {
  25. mainnet () {
  26. return this.$store.state.mainnet
  27. },
  28. scatter () {
  29. return this.$store.state.scatter
  30. },
  31. wifiColor () {
  32. let deyTime = this.currentDelay
  33. let className = 'green'
  34. if (deyTime >= 1e3) {
  35. className = 'red'
  36. } else if (deyTime > 500 && deyTime < 1e3) {
  37. className = 'yellow'
  38. } else if (deyTime <= 500) {
  39. className = 'green'
  40. }
  41. return className
  42. }
  43. },
  44. methods: {
  45. testMainNet () {
  46. for (let i in mainNetMap) {
  47. let ind = Object.keys(mainNetMap).indexOf(i)
  48. let mainnet = mainNetMap[i]
  49. let curTime = new Date().getTime()
  50. fetch(`https://${mainnet}/v1/chain/get_info`)
  51. .then(res => {
  52. if (res.ok) {
  53. let deyTime = new Date().getTime() - curTime
  54. let state = 0
  55. if (deyTime >= 1e3) {
  56. state = 3
  57. } else if (deyTime > 500 && deyTime < 1e3) {
  58. state = 2
  59. } else if (deyTime <= 500) {
  60. state = 1
  61. }
  62. this.newWorkData[ind].state = state
  63. this.newWorkData[ind].delay = deyTime + 'ms'
  64. } else {
  65. return Promise.reject(new Error(`${mainnet} request fail`))
  66. }
  67. })
  68. .catch(error => {
  69. Promise.reject(error)
  70. })
  71. }
  72. },
  73. getCurClass (host) {
  74. if (this.mainnet === host) {
  75. return 'current'
  76. }
  77. },
  78. changeMainnet (host, delay) {
  79. if (window.location.port === '8080' || /^test-|\.webdev2\./.test(window.location.host)) {
  80. return
  81. }
  82. window.EOS = this.scatter.eos(changeNewWork(host), Eos, {})
  83. this.$store.commit('setMainnet', host)
  84. this.currentDelay = delay.replace(/ms/, '')
  85. }
  86. },
  87. created () {
  88. for (let i in mainNetMap) {
  89. this.newWorkData.push({
  90. mainnet: i,
  91. delay: 'connecting',
  92. state: 0,
  93. host: mainNetMap[i]
  94. })
  95. }
  96. }
  97. }
  98. </script>
  99. <style lang="scss">
  100. .game-nav-newwork{
  101. display: inline-block;
  102. height: px2rem(58);
  103. line-height: px2rem(58);
  104. width: px2rem(60);
  105. text-align: center;
  106. margin: 0 px2rem(10);
  107. position: relative;
  108. &:hover{
  109. .newwork-list{
  110. display: block;
  111. }
  112. }
  113. .wifi-icon{
  114. display: inline-block;
  115. width: px2rem(50);
  116. height: px2rem(50);
  117. cursor: pointer;
  118. margin-top: px2rem(6);
  119. &.green{
  120. background: url('../../assets/wifi-green.png') center center no-repeat;
  121. background-size: px2rem(30) px2rem(20);
  122. }
  123. &.yellow{
  124. background: url('../../assets/wifi-yellow.png') center center no-repeat;
  125. background-size: px2rem(30) px2rem(20);
  126. }
  127. &.red{
  128. background: url('../../assets/wifi-red.png') center center no-repeat;
  129. background-size: px2rem(30) px2rem(20);
  130. }
  131. }
  132. .newwork-list{
  133. position: absolute;
  134. display: none;
  135. top: px2rem(58);
  136. left: -px2rem(100);
  137. width: px2rem(245);
  138. border: 2px solid #9f87eb;
  139. background-color: #282475;
  140. border-radius: 6px;
  141. overflow: hidden;
  142. .close-icon{
  143. background: url('../../assets/newwork-close-icon.png');
  144. width: px2rem(16);
  145. height: px2rem(16);
  146. background-size: 100%;
  147. position: absolute;
  148. right: px2rem(10);
  149. top: px2rem(8);
  150. cursor: pointer;
  151. }
  152. }
  153. .newwork-item{
  154. height: px2rem(38);
  155. line-height: px2rem(38);
  156. text-align: left;
  157. padding: 0 px2rem(10);
  158. cursor: pointer;
  159. &.current{
  160. .name{
  161. color: #ffffff;
  162. position: relative;
  163. padding-left: px2rem(18);
  164. &::after{
  165. content: '';
  166. position: absolute;
  167. left: px2rem(4);
  168. top: px2rem(6);
  169. width: 0;
  170. height: 0;
  171. border-width:px2rem(6) 0 px2rem(6) px2rem(6);
  172. border-style:solid;
  173. border-color:transparent transparent transparent #ffffff;
  174. }
  175. }
  176. }
  177. &:nth-child(even) {
  178. background-color: #1b166c;
  179. }
  180. .name{
  181. font-size: px2rem(14);
  182. color: #b9b5ff;
  183. }
  184. .time{
  185. float: right;
  186. font-size: px2rem(14);
  187. &.green{
  188. color: #39cd6d;
  189. }
  190. &.yellow{
  191. color: #ffff00;
  192. }
  193. &.red{
  194. color: #ff3434;
  195. }
  196. &.test{
  197. color: #b9b5ff;
  198. }
  199. }
  200. }
  201. }
  202. // 手机兼容
  203. .moblie-nav{
  204. .game-nav-newwork{
  205. .newwork-list{
  206. top: 20px;
  207. left: -40px;
  208. width: 140px;
  209. padding-top: 20px;
  210. .close-icon{
  211. width: 16px;
  212. height: 16px;
  213. right: 4px;
  214. top: 2px;
  215. }
  216. }
  217. .newwork-item{
  218. height: 20px;
  219. line-height: 20px;
  220. padding: 0 4px;
  221. &.current{
  222. .name{
  223. padding-left: 10px;
  224. &::after{
  225. left: 2px;
  226. top: 5px;
  227. border-width:4px 0 4px 4px;
  228. }
  229. }
  230. }
  231. .name{
  232. font-size: 12px;
  233. }
  234. .time{
  235. font-size: 12px;
  236. }
  237. }
  238. }
  239. }
  240. </style>