From 724f4c529a8ee3fa0a4f24d0fe55f79b70181a25 Mon Sep 17 00:00:00 2001
From: Tomas <tomasysh@gmail.com>
Date: 星期三, 22 十二月 2021 16:06:55 +0800
Subject: [PATCH] add: auth.service

---
 PAMapp/pages/questionnaire/_agentNo.vue         |    9 ++--
 PAMapp/shared/services/auth.service.ts          |   16 ++++++++
 PAMapp/shared/services/login.service.ts         |   17 ++++----
 PAMapp/components/Consultant/ConsultantList.vue |   28 +++++++++----
 4 files changed, 49 insertions(+), 21 deletions(-)

diff --git a/PAMapp/components/Consultant/ConsultantList.vue b/PAMapp/components/Consultant/ConsultantList.vue
index ce11a46..45e8a0a 100644
--- a/PAMapp/components/Consultant/ConsultantList.vue
+++ b/PAMapp/components/Consultant/ConsultantList.vue
@@ -23,21 +23,25 @@
 </template>
 
 <script lang="ts">
-import { Vue, Component, Prop, namespace } from 'nuxt-property-decorator';
-import { Consultant } from '~/shared/models/consultant.model';
+import { Vue, Component, Prop } from 'nuxt-property-decorator';
 
-const roleStorage = namespace('localStorage');
+import authService from '~/shared/services/auth.service';
+import { Consultant } from '~/shared/models/consultant.model';
 
 @Component
 export default class ConsultantList extends Vue {
-    @Prop() agents!: Consultant[];
-    @Prop() title! : string;
-    @roleStorage.Getter isUserLogin!:boolean;
+    @Prop()
+    agents!: Consultant[];
+
+    @Prop()
+    title! : string;
+
+    isUserLogin = false;
 
     get agentList(){
-        return this.agents.map((agentDto)=>
-            ({...agentDto,customerScore:0,})
-        )
+      return this.agents.map((agentDto)=>
+          ({...agentDto,customerScore:0,})
+      )
     }
 
     get noDataPlaceholder(): string {
@@ -46,6 +50,12 @@
                           : '����撌脤憿批��';
     }
 
+    //////////////////////////////////////////////////////////////////////
+
+    mounted() {
+      this.isUserLogin = authService.isUserLogin();
+    }
+
 }
 </script>
 
diff --git a/PAMapp/pages/questionnaire/_agentNo.vue b/PAMapp/pages/questionnaire/_agentNo.vue
index 83f35e9..69ea619 100644
--- a/PAMapp/pages/questionnaire/_agentNo.vue
+++ b/PAMapp/pages/questionnaire/_agentNo.vue
@@ -143,8 +143,9 @@
 import { getRequestsFromStorage, removeRequestQuestionFromStorage, setRequestsToStorage } from '~/shared/storageRequests';
 import _ from 'lodash';
 
-import queryConsultantService from '~/shared/services/query-consultant.service';
 import appointmentService from '~/shared/services/appointment.service';
+import authService from '~/shared/services/auth.service';
+import queryConsultantService from '~/shared/services/query-consultant.service';
 import { Consultant } from '~/shared/models/consultant.model';
 import { ContactType } from '~/shared/models/enum/ContactType';
 import { Gender } from '~/shared/models/enum/Gender';
@@ -280,7 +281,7 @@
 
     beforeRouteEnter(to: any, from: any, next: any) {
       next(vm => {
-        const isUserLogin = vm.$store.getters['localStorage/isUserLogin'];
+        const isUserLogin = authService.isUserLogin();
         if (from.name === 'login' && !isUserLogin) {
           vm.$router.go(-1);
           return;
@@ -293,7 +294,7 @@
     }
 
     async fetch() {
-      if (this.isUserLogin) {
+      if (authService.isUserLogin()) {
         await this.storeConsultantList();
       };
     }
@@ -451,7 +452,7 @@
                   : appointmentInfo.appointmentDate;
     }
     @Watch('myConsultantList') onMyConsultantListChange() {
-      if (this.isUserLogin && this.myConsultantList.length > 0) {
+      if (authService.isUserLogin() && this.myConsultantList.length > 0) {
           const editAppointment = this.getLatestReserved(this.$route.params.agentNo);
 
           if (editAppointment && editAppointment.agentNo) {
diff --git a/PAMapp/shared/services/auth.service.ts b/PAMapp/shared/services/auth.service.ts
new file mode 100644
index 0000000..6c10d47
--- /dev/null
+++ b/PAMapp/shared/services/auth.service.ts
@@ -0,0 +1,16 @@
+import { Role } from "../models/enum/role";
+
+class AuthService {
+  private idToken = localStorage.getItem('id_token');
+  private currentRole = localStorage.getItem('current_role');
+
+  isAdminLogin(): boolean {
+    return this.currentRole === Role.ADMIN;
+  }
+
+  isUserLogin(): boolean {
+    return this.currentRole === Role.USER;
+  }
+}
+
+export default new AuthService();
diff --git a/PAMapp/shared/services/login.service.ts b/PAMapp/shared/services/login.service.ts
index 9797d52..26bf0f2 100644
--- a/PAMapp/shared/services/login.service.ts
+++ b/PAMapp/shared/services/login.service.ts
@@ -1,31 +1,32 @@
+import { http } from "./httpClient";
+import { AxiosResponse } from 'axios';
+import _ from "lodash";
+
 import { ConsultantLoginInfo } from "../models/ConsultantLoginInfo";
 import { LoginRequest } from "../models/loginRequest.model";
 import { LoginSuccessToken } from "../models/loginSuccessToken.model";
 import { LoginVerify } from "../models/loginVerify.model";
 import { OtpInfo } from "../models/otpInfo.model";
 import { RegisterInfo } from "../models/registerInfo";
-import { http } from "./httpClient";
-import { AxiosResponse } from 'axios';
-import _ from "lodash";
 
 class LoginService {
     /** 憿批恥��-��TP **/
-  sendOtp(loginInfo: LoginRequest):Promise<OtpInfo> {
+  async sendOtp(loginInfo: LoginRequest):Promise<OtpInfo> {
     return http.post('/otp/sendOtp', loginInfo).then( res => res.data );
   }
 
   /** 憿批恥��-撽�TP **/
-  loginVerify(loginVerify: LoginVerify):Promise<LoginSuccessToken>{
+  async loginVerify(loginVerify: LoginVerify):Promise<LoginSuccessToken>{
     return http.post('/otp/verify', loginVerify).then(res => res.data);
   }
 
   /** 憿批恥閮餃�� **/
-  register(registerInfo: RegisterInfo):Promise<LoginSuccessToken>{
+  async register(registerInfo: RegisterInfo):Promise<LoginSuccessToken>{
     return http.post('/otp/register', registerInfo).then(res => res.data);
   }
 
   /** �����Ⅳ���� **/
-  getImgOfVerification():Promise<string>{
+  async getImgOfVerification():Promise<string>{
     return http.get('/login/validate/get_img_code',{ responseType : 'arraybuffer' })
       .then( response => {
         const toBase64 = window.btoa(
@@ -48,4 +49,4 @@
   }
 }
 
-export default new LoginService();
\ No newline at end of file
+export default new LoginService();

--
Gitblit v1.8.0