header.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <template>
  2. <div class="header">
  3. <img class="logo" src="../static/img/logo.svg" alt="DeNet" />
  4. <div class="operation">
  5. <div class="login" @click="login">
  6. <img class="add" src="../static/img/header-add.svg" alt="" />
  7. <span>Create NFTs</span>
  8. </div>
  9. <div class="down" @click="install">
  10. <div class="text">Install DeNet</div>
  11. </div>
  12. </div>
  13. </div>
  14. <div class="header-place"></div>
  15. </template>
  16. <script lang="ts" setup>
  17. import Api from '../static/http/api'
  18. import { postRequest } from '../static/http'
  19. import { getMid, appVersionCode, getOauthUrl, createWindow, callBackUrl } from '../static/utils'
  20. import { getStorage, removeStorage, setStorage, storageKey } from '../static/utils/storage'
  21. import { ref } from 'vue'
  22. import { ElMessage } from 'element-plus'
  23. const timer = ref(0)
  24. const install = () => {
  25. window.open(`https://chrome.google.com/webstore/detail/denet/inlfbeejfdgkknpiodhemfcokbdgofja`);
  26. }
  27. const checkInstall = () => {
  28. return new Promise((resolve, reject) => {
  29. // chrome-extension://inlfbeejfdgkknpiodhemfcokbdgofja/img/icon-denet-logo.svg
  30. let dom = document.querySelector('#denet_message');
  31. if (dom) {
  32. resolve(true)
  33. } else {
  34. reject(false)
  35. }
  36. })
  37. }
  38. const login = () => {
  39. checkInstall().then(() => {
  40. let userInfo = getStorage(storageKey.userInfo);
  41. if (userInfo) {
  42. location.href = `/nft/list`
  43. } else {
  44. twitterAuth()
  45. }
  46. }).catch(() => {
  47. install()
  48. })
  49. }
  50. const twitterAuth = () => {
  51. postRequest(Api.twitterRequestToken, {
  52. baseInfo: {
  53. mid: getMid(),
  54. appVersionCode,
  55. },
  56. params: {
  57. oauthCallback: callBackUrl
  58. }
  59. }).then(res => {
  60. let { code, data, msg } = res;
  61. if ( code === 0 ) {
  62. let url = getOauthUrl(data.authToken);
  63. let win = createWindow(url);
  64. timer.value = setInterval(() => {
  65. if (win && win.closed) {
  66. clearInterval(timer.value);
  67. twitterLogin(data);
  68. }
  69. }, 500)
  70. } else {
  71. ElMessage({
  72. offset: 100,
  73. type: 'error',
  74. message: msg,
  75. })
  76. }
  77. })
  78. }
  79. const twitterLogin = (data: { authToken: string, consumerKey: string }) => {
  80. let verifier = getStorage(storageKey.verifier)
  81. if (verifier) {
  82. postRequest(Api.twitterLogin, {
  83. baseInfo: {
  84. mid: getMid(),
  85. appVersionCode,
  86. },
  87. params: {
  88. consumerKey: data.consumerKey,
  89. oauthToken: data.authToken,
  90. oauthVerifier: verifier
  91. }
  92. }).then(res => {
  93. let { code, data, msg } = res;
  94. if ( code === 0 ) {
  95. setStorage(storageKey.userInfo, data);
  96. removeStorage(storageKey.verifier);
  97. location.href = `/nft/list`
  98. } else {
  99. ElMessage({
  100. offset: 100,
  101. type: 'error',
  102. message: msg,
  103. })
  104. }
  105. })
  106. }
  107. }
  108. </script>
  109. <style lang="less" scoped>
  110. .header {
  111. position: fixed;
  112. z-index: 3;
  113. display: flex;
  114. justify-content: space-between;
  115. align-items: center;
  116. top: 0;
  117. left: 0;
  118. width: 100%;
  119. height: 60px;
  120. background-color: #ffffff;
  121. box-shadow: 0px 2px 3px rgba(0, 0, 0, 0.1);
  122. .logo {
  123. height: 38px;
  124. margin-left: 26px;
  125. }
  126. .operation {
  127. display: flex;
  128. flex-direction: row;
  129. }
  130. .login {
  131. display: flex;
  132. align-items: center;
  133. justify-content: center;
  134. box-sizing: border-box;
  135. height: 38px;
  136. cursor: pointer;
  137. padding: 0 18px;
  138. font-size: 15px;
  139. font-weight: 600;
  140. color: #1D9BF0;
  141. margin-right: 19px;
  142. border-radius: 20px;
  143. border: solid 1px #1D9BF0;
  144. .add {
  145. width: 20px;
  146. margin-right: 4px;
  147. }
  148. }
  149. .down {
  150. display: flex;
  151. align-items: center;
  152. justify-content: center;
  153. height: 38px;
  154. cursor: pointer;
  155. padding: 0 24px;
  156. margin-right: 26px;
  157. border-radius: 20px;
  158. background: #1D9BF0;
  159. .text {
  160. color: #fff;
  161. font-size: 15px;
  162. font-weight: 600;
  163. margin-top: -2px;
  164. }
  165. }
  166. &-place {
  167. height: 60px;
  168. }
  169. }
  170. </style>