auth-login.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import { defineComponent } from 'vue'
  2. import { Button, Image, Toast } from 'vant'
  3. import { removeAuth, setAuth } from './utils'
  4. import styles from './index.module.less'
  5. import {
  6. listenerMessage,
  7. postMessage,
  8. promisefiyPostMessage,
  9. removeListenerMessage
  10. } from '@/helpers/native-message'
  11. import iconStudent from '@common/images/icon-student-default.png'
  12. import logo from '@common/images/logo.png'
  13. import ColPopup from '@/components/col-popup'
  14. import InviteCode from './invite-code'
  15. import { setLogin, state } from '@/state'
  16. import request from '@/student/home-layout-orchestra/request-home'
  17. export default defineComponent({
  18. name: 'login-music',
  19. data() {
  20. return {
  21. username: '',
  22. imgCodeStatus: false
  23. }
  24. },
  25. mounted() {
  26. this.username = state.orchestraInfo.phone
  27. removeAuth()
  28. listenerMessage('webViewOnResume', () => {
  29. promisefiyPostMessage({ api: 'getUserAccount' }).then(
  30. async (res: any) => {
  31. const content = res.content
  32. state.orchestraInfo.token = content.token.split(' ')[1]
  33. state.orchestraInfo.phone = content.phone
  34. state.orchestraInfo.nickname = content.nickname
  35. state.orchestraInfo.avatar = content.avatar
  36. state.orchestraInfo.unionId = content.unionId || 0
  37. this.username = state.orchestraInfo.phone
  38. if (content.unionId && this.$route.path === '/home-auth') {
  39. this.getUserInfo()
  40. }
  41. }
  42. )
  43. })
  44. },
  45. unmounted() {
  46. removeListenerMessage('webViewOnResume', () => {
  47. //
  48. })
  49. },
  50. methods: {
  51. onLoginSuccess() {
  52. // 调用原生api去关联账号
  53. postMessage({ api: 'bindUserAccount', content: { phone: this.username } })
  54. // 登录成功
  55. setTimeout(() => {
  56. Toast('授权成功')
  57. }, 100)
  58. setTimeout(() => {
  59. this.getUserInfo()
  60. // this.$router.push('/home')
  61. // window.location.href = location.origin + location.pathname + '#/home'
  62. // setTimeout(() => {
  63. // window.location.reload()
  64. // }, 100)
  65. }, 1000)
  66. },
  67. async getUserInfo() {
  68. try {
  69. const res = await request.post('/api-auth/smsLogin', {
  70. requestType: 'form',
  71. data: {
  72. clientId: 'student',
  73. clientSecret: 'student',
  74. phone: state.orchestraInfo.phone,
  75. token: state.orchestraInfo.token,
  76. isSurportRegister: true
  77. }
  78. })
  79. const { authentication } = res.data
  80. setAuth(authentication.token_type + ' ' + authentication.access_token)
  81. promisefiyPostMessage({
  82. api: 'setCache',
  83. content: {
  84. key: 'h5-colexiu-token',
  85. value: authentication.token_type + ' ' + authentication.access_token
  86. }
  87. })
  88. const userCash = await request.get(
  89. '/api-student/student/queryUserInfo',
  90. {
  91. initRequest: true // 初始化接口
  92. }
  93. )
  94. setLogin(userCash.data)
  95. this.$router.push('/home')
  96. } catch {
  97. //
  98. }
  99. },
  100. async onLogin() {
  101. try {
  102. const res = await request.post('/api-auth/smsLogin', {
  103. requestType: 'form',
  104. data: {
  105. clientId: 'student',
  106. clientSecret: 'student',
  107. phone: this.username,
  108. token: state.orchestraInfo.token,
  109. isSurportRegister: true
  110. }
  111. })
  112. const { authentication } = res.data
  113. setAuth(authentication.token_type + ' ' + authentication.access_token)
  114. promisefiyPostMessage({
  115. api: 'setCache',
  116. content: {
  117. key: 'h5-colexiu-token',
  118. value: authentication.token_type + ' ' + authentication.access_token
  119. }
  120. })
  121. this.onLoginSuccess()
  122. } catch {
  123. //
  124. }
  125. }
  126. },
  127. render() {
  128. return (
  129. <div class={styles.login}>
  130. <div class={styles.container}>
  131. <div class={[styles.userInfo, styles.loginCenter]}>
  132. <Image
  133. src={state.orchestraInfo.avatar || iconStudent}
  134. class={styles.img}
  135. fit="cover"
  136. />
  137. <p class={styles.name}>{state.orchestraInfo.name}</p>
  138. </div>
  139. <div class={[styles.tips, styles.loginCenter]}>
  140. <Image src={logo} class={styles.logo} fit="contain" />
  141. <p class={styles.text}>同意酷乐秀获取您的管乐团信息并登录</p>
  142. <p class={styles.phone}>{this.username}</p>
  143. </div>
  144. </div>
  145. <div class={styles.btnGroup}>
  146. <Button
  147. round
  148. color="#FF8057"
  149. class={styles.btn}
  150. onClick={this.onLogin}
  151. >
  152. 授权登录
  153. </Button>
  154. <span
  155. class={styles.moreBtn}
  156. onClick={() => {
  157. this.$router.push('/home-login')
  158. }}
  159. >
  160. 其他手机号登录
  161. </span>
  162. </div>
  163. {/* <ColPopup v-model={this.imgCodeStatus}>
  164. {this.imgCodeStatus && (
  165. <InviteCode
  166. phone={this.username}
  167. onLoginSuccess={this.onLoginSuccess}
  168. />
  169. )}
  170. </ColPopup> */}
  171. </div>
  172. )
  173. }
  174. })