浏览代码

添加判断

lex 2 年之前
父节点
当前提交
a41c171797

+ 38 - 10
src/student/music-group/layout/login.tsx

@@ -6,7 +6,7 @@ import { setLogin, state } from '@/state'
 import { removeAuth, setAuth } from './utils'
 import styles from './index.module.less'
 import request from '@/helpers/request'
-import { browser } from '@/helpers/utils'
+import { browser, getUrlCode } from '@/helpers/utils'
 
 type loginType = 'PWD' | 'SMS'
 export default defineComponent({
@@ -21,7 +21,8 @@ export default defineComponent({
       countDownTime: 1000 * 120, // 倒计时时间
       // countDownRef: null as any, // 倒计时实例
       imgCodeStatus: false,
-      showPopup: false
+      showPopup: false,
+      code: '' // 授权code码
     }
   },
   computed: {
@@ -36,28 +37,55 @@ export default defineComponent({
     this.directNext()
 
     // 判断是否是微信,只能微信中打开
-    // if (!browser().weixin) {
-    //   this.showPopup = true
-    // } else {
-    //   this.getAppId()
-    // }
+    if (browser().weixin) {
+      // 微信公众号支付
+      //授权
+      const code = getUrlCode()
+      console.log('login mounted code: ' + code)
+      if (!code) {
+        this.getAppIdAndCode()
+      } else {
+        this.code = code
+      }
+    } else {
+      this.showPopup = true
+    }
   },
   methods: {
-    async getAppId() {
+    async getAppIdAndCode() {
       try {
         const { data } = await request.get('/api-student/open/paramConfig/wechatAppId')
-        console.log(data)
+        // 判断是否有微信appId
+        if (data) {
+          this.goAuth(data)
+        }
       } catch {
         //
       }
     },
+    goAuth(wxAppId: string) {
+      // 用户授权
+      const urlNow = encodeURIComponent(window.location.href)
+      const scope = 'snsapi_base' //snsapi_userinfo   //静默授权 用户无感知
+      const appid = wxAppId || 'wx8654c671631cfade'
+      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`
+      window.location.replace(url)
+    },
     directNext() {
       if (state.user.status === 'login' || state.user.status === 'error') {
         const { returnUrl, isRegister, ...rest } = this.$route.query
+        console.log(
+          {
+            ...rest,
+            code: this.code
+          },
+          'jump pre registration'
+        )
         this.$router.replace({
           path: returnUrl as any,
           query: {
-            ...rest
+            ...rest,
+            code: this.code
           }
         })
       }

+ 21 - 2
src/student/music-group/pre-apply/component/apply.tsx

@@ -13,8 +13,9 @@ import {
   Tag
 } from 'vant'
 import { defineComponent, onMounted, reactive } from 'vue'
-import { useRoute } from 'vue-router'
+import { useRoute, useRouter } from 'vue-router'
 import styles from '../index.module.less'
+import { setLogout } from '@/state'
 
 // 乐团交付,乐团停止或关闭,有新的交付团;则不允许报名
 const classList: any = []
@@ -37,7 +38,9 @@ export default defineComponent({
   emits: ['next'],
   setup(props, { slots, attrs, emit }) {
     const route = useRoute()
+    const router = useRouter()
     const state = reactive({
+      code: '' as any, // 微信授权code码
       detail: {} as any, // 学生详情
       currentGrade: [
         { text: '一年级', value: 1 },
@@ -153,7 +156,8 @@ export default defineComponent({
         }
         await request.post('/api-student/orchestraRegister/save', {
           data: {
-            ...params
+            ...params,
+            code: state.code
           }
         })
         setTimeout(() => {
@@ -166,12 +170,27 @@ export default defineComponent({
     }
 
     onMounted(async () => {
+      state.code = route.query.code || ''
+      console.log('pre register code: ' + state.code)
       await getSubjects()
       // 判断学年制
       if (props.schoolSystem === 'sixYearSystem') {
         state.currentGrade.push({ text: '六年级', value: 6 })
       }
       await studentRegister()
+
+      // 判断是否有授权码
+      if (!state.code) {
+        setLogout()
+        const query = {
+          returnUrl: route.path,
+          ...route.query
+        } as any
+        router.replace({
+          path: '/loginMusic',
+          query: query
+        })
+      }
     })
     return () => (
       <>

+ 26 - 0
src/student/music-group/pre-apply/index.module.less

@@ -15,6 +15,32 @@
     }
   }
 
+  .popupContainer {
+    .dialogTitle {
+      i {
+        display: inline-block;
+        width: 4px;
+        height: 14px;
+        background: #ff8057;
+        border-radius: 2px;
+        margin-right: 6px;
+      }
+
+      text-align: left;
+      font-size: 18px;
+      font-weight: 500;
+      color: #333333;
+      line-height: 25px;
+      padding: 20px 0 20px 25px;
+    }
+
+    .popupTips {
+      text-align: center;
+      padding: 15px 0 45px;
+      font-size: 16px;
+    }
+  }
+
   .banner {
     background: url('./images/banner.png') no-repeat center center;
     background-size: cover;

+ 54 - 1
src/student/music-group/pre-apply/index.tsx

@@ -1,5 +1,5 @@
 import { defineComponent, onMounted, reactive, ref, nextTick } from 'vue'
-import { Image, showDialog, Sticky, Tab, Tabs } from 'vant'
+import { Image, Popup, showDialog, Sticky, Tab, Tabs } from 'vant'
 import styles from './index.module.less'
 // import { useRect } from '@vant/use'
 import Apply from './component/apply'
@@ -9,6 +9,7 @@ import { useRoute, useRouter } from 'vue-router'
 
 import { setLogout } from '@/state'
 import request from '@/helpers/request'
+import { browser, getUrlCode } from '@/helpers/utils'
 
 export default defineComponent({
   name: 'pre-apply',
@@ -22,6 +23,8 @@ export default defineComponent({
       registerInfo: {} as any,
       purchase: false, // 购买状态
       register: true // 是否注册
+      // showPopup: false,
+      // code: '' as any
     })
 
     const onNext = async (name: string) => {
@@ -120,12 +123,46 @@ export default defineComponent({
       }
     }
 
+    // const getAppIdAndCode = async () => {
+    //   try {
+    //     const { data } = await request.get('/api-student/open/paramConfig/wechatAppId')
+    //     // 判断是否有微信appId
+    //     if (data) {
+    //       goAuth(data)
+    //     }
+    //   } catch {
+    //     //
+    //   }
+    // }
+    // const goAuth = (wxAppId: string) => {
+    //   // 用户授权
+    //   const urlNow = encodeURIComponent(window.location.href)
+    //   const scope = 'snsapi_base' //snsapi_userinfo   //静默授权 用户无感知
+    //   const appid = wxAppId || 'wx8654c671631cfade'
+    //   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`
+    //   window.location.replace(url)
+    // }
+
     // 先请求接口
     getRegisterStatus()
 
     onMounted(() => {
+      // state.code = route.query.code || ''
       // const { height } = useRect(bannerRef.value)
       // state.heightV = height
+      // 判断是否是微信,只能微信中打开
+      // if (browser().weixin) {
+      //   // 微信公众号支付
+      //   //授权
+      //   const code = getUrlCode()
+      //   if (!code || !state.code) {
+      //     getAppIdAndCode()
+      //   } else {
+      //     state.code = code
+      //   }
+      // } else {
+      //   state.showPopup = true
+      // }
     })
     return () => (
       <div class={styles.preApply}>
@@ -157,6 +194,22 @@ export default defineComponent({
         )}
         {state.tabValue === 'payment' && <Payment onNext={onNext} />}
         {state.tabValue === 'order' && <Order onNext={onNext} />}
+
+        {/* <Popup
+          v-model:show={state.showPopup}
+          round
+          style={{ width: '92%' }}
+          closeOnClickOverlay={false}
+        >
+          <div class={styles.popupContainer}>
+            <div class={styles.dialogTitle}>
+              <i></i>
+              提示
+            </div>
+
+            <p class={styles.popupTips}>请使用微信打开</p>
+          </div>
+        </Popup> */}
       </div>
     )
   }