index.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { defineComponent, onMounted, reactive } from 'vue'
  2. import { Button, closeToast, Image, Popup } from 'vant'
  3. import styles from './index.module.less'
  4. import iconLogo from './images/icon-logo.png'
  5. import iconLargeLogo from './images/icon-large-logo.png'
  6. import { useRoute } from 'vue-router'
  7. import qs from 'query-string'
  8. import { browser, getUrlCode } from '@/helpers/utils'
  9. import request from '@/helpers/request'
  10. import dayjs from 'dayjs'
  11. import { state } from '@/state'
  12. export default defineComponent({
  13. name: 'bind-wechat',
  14. setup() {
  15. const route = useRoute()
  16. console.log(route.query)
  17. const forms = reactive({
  18. showPopup: false,
  19. code: '',
  20. phone: '' as any,
  21. time: '' as any,
  22. // platform: route.query.platform,
  23. popupMessage: '请使用微信打开'
  24. })
  25. // 浏览器自带加密方式
  26. // window.btoa() 加密
  27. // window.atob() 解密
  28. // console.log(qs.parse(location.hash))
  29. const getAppIdAndCode = async (url: string) => {
  30. try {
  31. const { data } = await request.get(state.platformApi + '/open/paramConfig/wechatAppId')
  32. // 判断是否有微信appId
  33. if (data) {
  34. closeToast()
  35. const replaceUrl =
  36. `https://online.lexiaoya.cn/getWxCode?appid=${data}&state=STATE&redirect_uri=` +
  37. encodeURIComponent(url)
  38. console.log(data, 'aaaa')
  39. console.log(replaceUrl)
  40. window.location.replace(replaceUrl)
  41. // goAuth(data, url)
  42. }
  43. } catch {
  44. //
  45. }
  46. }
  47. const goAuth = (wxAppId: string, urlString?: string) => {
  48. // 用户授权
  49. console.log(urlString || window.location.href, 'urlString || window.location.href')
  50. const urlNow = encodeURIComponent(urlString || window.location.href)
  51. console.log(urlNow, 'urlNow')
  52. const scope = 'snsapi_base' //snsapi_userinfo //静默授权 用户无感知
  53. const appid = wxAppId || 'wx8654c671631cfade'
  54. const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${urlNow}&response_type=code&scope=${scope}&state=STATE&connect_redirect=1#wechat_redirect`
  55. window.location.replace(url)
  56. }
  57. // 确认授权
  58. const onAuth = async () => {
  59. try {
  60. await request.post(state.platformApi + '/open/user/updateOpenId', {
  61. data: {
  62. clientType: state.platformType,
  63. code: forms.code,
  64. phone: forms.phone
  65. }
  66. })
  67. window.location.href =
  68. 'https://mp.weixin.qq.com/s?__biz=MzkxMDMwOTI5Nw==&mid=2247485261&idx=1&sn=70c79a832a609bf9fae01c9e90fb4f69&chksm=c12c2593f65bac85d26362bca470f6abc2bfc087d9f4dcf87c00094420bdf5a3acb1b870199b#rd'
  69. } catch {
  70. //
  71. }
  72. }
  73. onMounted(async () => {
  74. // console.log(dayjs(Number(data.time)).subtract(3, 'hour').valueOf())
  75. // 判断是否是微信,只能微信中打开
  76. console.log(import.meta.env)
  77. if (!browser().weixin) {
  78. forms.showPopup = true
  79. return
  80. } else {
  81. //授权
  82. const code = getUrlCode()
  83. if (!code) {
  84. const newUrl =
  85. window.location.origin +
  86. window.location.pathname +
  87. '#' +
  88. route.path +
  89. '?' +
  90. qs.stringify({
  91. ...route.query
  92. })
  93. await getAppIdAndCode(newUrl)
  94. return
  95. } else {
  96. forms.code = code
  97. }
  98. }
  99. // 处理链接上面的参数
  100. try {
  101. const hash = window.location.hash ? window.location.hash.split('?')[1] : ''
  102. const atob = window.atob(decodeURIComponent(hash))
  103. const params = qs.parse(atob)
  104. forms.phone = params.phone
  105. forms.time = params.time
  106. if (!forms.phone) {
  107. forms.popupMessage = '链接已失效'
  108. forms.showPopup = true
  109. return
  110. }
  111. } catch {
  112. //
  113. }
  114. // t: route.query.t, // 过期时间
  115. try {
  116. if (forms.time) {
  117. const { data } = await request.get(
  118. state.platformApi + '/open/paramConfig/queryByParamName',
  119. {
  120. requestType: 'form',
  121. params: {
  122. paramName: 'qr_code_expire_hours'
  123. }
  124. }
  125. )
  126. if (dayjs(Number(forms.time)).add(data.paramValue, 'hour').isBefore(dayjs())) {
  127. forms.popupMessage = '二维码已失效'
  128. forms.showPopup = true
  129. }
  130. }
  131. } catch {
  132. //
  133. }
  134. })
  135. return () => (
  136. <div class={styles.bindWeChat}>
  137. <Image class={styles.largeLogo} src={iconLargeLogo} />
  138. <Image src={iconLogo} class={styles.smallLogo} />
  139. <p class={styles.tips}>同意管乐团获取您的微信信息</p>
  140. <p class={styles.phone}>{forms.phone}</p>
  141. <div class={styles.btnGroup}>
  142. <Button type="primary" size="large" round onClick={onAuth}>
  143. 确认授权
  144. </Button>
  145. </div>
  146. <Popup
  147. v-model:show={forms.showPopup}
  148. round
  149. style={{ width: '88%', marginTop: '-6vh' }}
  150. closeOnClickOverlay={false}
  151. class={styles.wxPopupDialog}
  152. >
  153. <div class={styles.popupContainer}>
  154. <p class={styles.title1}>温馨提示</p>
  155. <p class={styles.popupTips}>{forms.popupMessage}</p>
  156. </div>
  157. </Popup>
  158. </div>
  159. )
  160. }
  161. })