App.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <script setup>
  2. import { onHide,onLaunch,onShow } from '@dcloudio/uni-app'
  3. import { storeToRefs } from 'pinia'
  4. import { useMainStore } from '@/store/store'
  5. const main = useMainStore()
  6. import { isWeixin,parseQuery } from '@/utils/util'
  7. import cookie from '@/utils/cookie'
  8. import {
  9. userAuthSession,
  10. wechatAuth
  11. } from '@/api/auth'
  12. import { APP_ID } from '@/config'
  13. onLaunch(() => {
  14. console.log('App Launch')
  15. })
  16. onShow(() => {
  17. // 检查用户登录情况
  18. // #ifdef H5
  19. if(isWeixin()){
  20. // H5编译的代码
  21. // 判断是否是微信浏览器
  22. oAuth()
  23. //return;
  24. }
  25. // #endif
  26. // #ifdef MP-WEIXIN
  27. wechatMiniLogin();
  28. // #endif
  29. })
  30. onHide(() => {
  31. console.log('App Hide')
  32. })
  33. const wechatMiniLogin = () => {
  34. //this.$u.toast('登录中');
  35. uni.login({
  36. provider: 'weixin'
  37. }).then(async (res) => {
  38. let data = await userAuthSession({
  39. code: res.code
  40. });
  41. if (data) {
  42. console.log('data.openId:',data.openId)
  43. main.SET_OPENID(data.openId)
  44. if (data.hasOwnProperty('userInfo') && data.accessToken && data.accessToken != '') {
  45. main.SET_MEMBER(data.userInfo);
  46. main.SET_TOKEN(data.accessToken);
  47. }
  48. }
  49. });
  50. }
  51. const getAuthUrl = (appId) => {
  52. // #ifdef H5
  53. // #endif
  54. cookie.set('redirect', window.location.href)
  55. const url = `${location.origin}/h5/#/pages/index/index`
  56. cookie.set('index_url',url)
  57. let redirect_uri = encodeURIComponent(url)
  58. const state = 'STATE'
  59. return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
  60. }
  61. const oAuth = async() => {
  62. const WX_AUTH = 'wx_auth'
  63. return new Promise((resolve, reject) => {
  64. const accessToken = uni.getStorageSync('accessToken');
  65. if (cookie.has('wx_auth') && accessToken && main.isLogin) {
  66. reject()
  67. return
  68. }
  69. const { code } = parseQuery()
  70. if (!code) {
  71. //公众号appid
  72. const appid = APP_ID;
  73. location.href = getAuthUrl(appid)
  74. return
  75. } else {
  76. auth(code)
  77. }
  78. resolve()
  79. }).catch(error => {
  80. console.log(error)
  81. })
  82. }
  83. const auth = async(code) => {
  84. console.log('获取微信授权:',code)
  85. let data = await wechatAuth({'code':code})
  86. cookie.set('wx_auth', code)
  87. if (data) {
  88. main.SET_OPENID(data.openId)
  89. main.SET_MEMBER(data.userInfo);
  90. main.SET_TOKEN(data.accessToken);
  91. let newParams = cookie.get('params')
  92. if(newParams){
  93. uni.navigateTo({
  94. url: '/pages/components/pages/scan/scan'
  95. })
  96. }
  97. }
  98. }
  99. </script>
  100. <style lang="scss">
  101. @import '~@/static/style/app.scss';
  102. //@import 'static/iconfont/iconfont.scss';
  103. //@import url('./static/style/style.less');
  104. @import 'static/style/yshop.css';
  105. // /*每个页面公共css */
  106. // page {
  107. // background-color: #f5f5f5;
  108. // }
  109. </style>