header.vue 5.0 KB

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