header.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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, getCookie, removeCookie } 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. })
  63. // Report
  64. Report({
  65. baseInfo: {
  66. pageSource: pageSource.homePage,
  67. },
  68. params: {
  69. eventData: {
  70. businessType: businessType.buttonClick,
  71. objectType: objectType.createNftsButton,
  72. }
  73. }
  74. })
  75. }
  76. const twitterAuth = () => {
  77. postRequest(Api.twitterRequestToken, {
  78. params: {
  79. oauthCallback: callBackUrl
  80. }
  81. }).then(res => {
  82. let { code, data, msg } = res;
  83. if ( code === 0 ) {
  84. let url = getOauthUrl(data.authToken);
  85. let win = createWindow(url);
  86. // timer
  87. clearInterval(timer.value);
  88. timer.value = setInterval(() => {
  89. if (win && win.closed) {
  90. clearInterval(timer.value);
  91. twitterLogin(data);
  92. }
  93. }, 500)
  94. } else {
  95. ElMessage({
  96. type: 'error',
  97. message: msg,
  98. })
  99. }
  100. })
  101. }
  102. const twitterLogin = (data: { authToken: string, consumerKey: string }) => {
  103. let verifier = getCookie(storageKey.verifier)
  104. if (verifier) {
  105. postRequest(Api.twitterLogin, {
  106. params: {
  107. consumerKey: data.consumerKey,
  108. oauthToken: data.authToken,
  109. oauthVerifier: verifier
  110. }
  111. }).then(res => {
  112. let { code, data, msg } = res;
  113. if ( code === 0 ) {
  114. setStorage(storageKey.userInfo, data);
  115. removeCookie(storageKey.verifier);
  116. location.href = `/nft/list`
  117. } else {
  118. ElMessage({
  119. type: 'error',
  120. message: msg,
  121. })
  122. }
  123. })
  124. }
  125. }
  126. </script>
  127. <style lang="less" scoped>
  128. .header {
  129. position: fixed;
  130. z-index: 3;
  131. display: flex;
  132. justify-content: space-between;
  133. align-items: center;
  134. top: 0;
  135. left: 0;
  136. width: 100%;
  137. height: 60px;
  138. background-color: #ffffff;
  139. box-shadow: 0px 2px 3px rgba(0, 0, 0, 0.1);
  140. .logo {
  141. height: 38px;
  142. margin-left: 26px;
  143. }
  144. .operation {
  145. display: flex;
  146. flex-direction: row;
  147. }
  148. .login {
  149. display: flex;
  150. align-items: center;
  151. justify-content: center;
  152. box-sizing: border-box;
  153. height: 38px;
  154. cursor: pointer;
  155. padding: 0 18px;
  156. font-size: 15px;
  157. font-weight: 600;
  158. color: #1D9BF0;
  159. margin-right: 19px;
  160. border-radius: 20px;
  161. border: solid 1px #1D9BF0;
  162. .add {
  163. width: 20px;
  164. margin-right: 4px;
  165. }
  166. }
  167. .down {
  168. display: flex;
  169. align-items: center;
  170. justify-content: center;
  171. height: 38px;
  172. cursor: pointer;
  173. padding: 0 24px;
  174. margin-right: 26px;
  175. border-radius: 20px;
  176. background: #1D9BF0;
  177. .text {
  178. color: #fff;
  179. font-size: 15px;
  180. font-weight: 600;
  181. margin-top: -2px;
  182. }
  183. }
  184. &-place {
  185. height: 60px;
  186. }
  187. }
  188. </style>