瀏覽代碼

feat: 新增多个请求 URL 和请求方法, 新增 oaid 管理类,防止在请求时拿不到oaid

YuJing 1 月之前
父節點
當前提交
3f6b2e3904

+ 17 - 6
commons/basic/src/main/ets/apis/YTRequest.ets

@@ -9,13 +9,15 @@ import axios, {
 } from '@ohos/axios';
 import { ContextHelper, IBestToast, ReqString, YTLog } from '../../../../Index';
 import { AppStorageKeyCollect } from '../constants';
+import { BasicType } from '../models';
 import { userInfo } from '../models/UserInfo';
+import { ytOaidUtils } from '../utils/YtOaidUtils';
 import { yTRouter } from '../utils/YTRouter';
 
 
-export const baseURL: string = 'https://hm.ytpm.net/prod-activity'
+// export const baseURL: string = 'https://hm.ytpm.net/prod-activity'
 
-// export const baseURL: string = 'http://192.168.1.160:48103'
+export const baseURL: string = 'http://192.168.1.20:25923/'
 
 export const instance = axios.create({
   baseURL,
@@ -23,12 +25,22 @@ export const instance = axios.create({
 })
 
 // 添加请求拦截器
-instance.interceptors.request.use((config: InternalAxiosRequestConfig) => {
+instance.interceptors.request.use(async (config: InternalAxiosRequestConfig) => {
 
   // 对请求数据做点什么
-  if (AppStorage.get<string>(AppStorageKeyCollect.TOKEN)) {
-    config.headers.Authorization = AppStorage.get<string>(AppStorageKeyCollect.TOKEN)
+  // if (AppStorage.get<string>(AppStorageKeyCollect.TOKEN)) {
+  //   config.headers.Authorization = AppStorage.get<string>(AppStorageKeyCollect.TOKEN)
+  // }
+
+
+  if ((config.method == 'get' || config.method == 'delete') && config.url) {
+    //TODO 这里看后端怎么写的 目前这个格式有点奇怪
+    // config.url += AppStorage.get("OAID")
+    config.params.oaid = await ytOaidUtils.getOaid()
+  } else {
+    config.data.oaid = await ytOaidUtils.getOaid()
   }
+
   YTLog.info(config.data, '请求参数')
 
   return config;
@@ -119,4 +131,3 @@ export class YTRequest {
   }
 }
 
-

+ 2 - 1
commons/basic/src/main/ets/utils/ContextHelper.ets

@@ -13,6 +13,7 @@ import {
 } from '../../../../Index'
 import { identifier } from '@kit.AdsKit'
 import { BusinessError } from '@kit.BasicServicesKit'
+import { ytOaidUtils } from './YtOaidUtils'
 
 export class ContextHelper {
   private static declare _UIContext: UIContext
@@ -84,7 +85,7 @@ export class ContextHelper {
                   } else {
                     const oaid: string = data;
                     jHStartAd.init(ContextHelper.UIAbilityContext, oaid)
-                    AppStorage.setOrCreate('OAID', oaid)
+                    ytOaidUtils.setOaid(oaid)
                     YTLog.info(`succeeded in getting oaid by callback , oaid: ${oaid}`);
                   }
                 });

+ 26 - 0
commons/basic/src/main/ets/utils/YtOaidUtils.ets

@@ -0,0 +1,26 @@
+
+class YtOaidUtils{
+  private messageQueue: ESObject[] =[]
+
+  getOaid(){
+    let oaid = AppStorage.get<string>('OAID')
+    if(!oaid) {
+      let p = new Promise<string>((resolve, reject) => {
+        this.messageQueue.push({resolve, reject})
+      })
+      return p
+    } else {
+      return Promise.resolve(oaid)
+    }
+  }
+
+  setOaid(oaid: string) {
+    AppStorage.setOrCreate('OAID', oaid)
+    this.messageQueue.forEach((item: ESObject) => {
+      item.resolve(oaid)
+    })
+    this.messageQueue = []
+  }
+}
+
+export const ytOaidUtils = new YtOaidUtils()

+ 27 - 0
features/feature/src/main/ets/apis/ApiUrl.ets

@@ -0,0 +1,27 @@
+export class ApiUrl {
+  /**
+   * @description 新增学生信息
+   * @method POST
+   */
+  static addStudent = '/api/class/student/add';
+  /**
+   * @description 删除学生信息
+   * @method POST
+   */
+  static deleteStudent = '/api/class/student/delete';
+  /**
+   * @description 修改学生信息
+   * @method POST
+   */
+  static updateStudent = '/api/class/student/edit';
+  /**
+   * @description 获取学生信息
+   * @method GET
+   */
+  static getStudentDetail = '/api/class/student/getStudentInfo';
+  /**
+   * @description 获取学生列表
+   * @method POST
+   */
+  static getStudentList = '/api/class/student/list';
+}

+ 32 - 0
features/feature/src/main/ets/apis/ClassAffairsApi.ets

@@ -0,0 +1,32 @@
+import { YTRequest } from "basic"
+import { pageQuery, pageQueryResp, studentInfo } from "../model/Index"
+import { ApiUrl } from "./ApiUrl"
+
+class ClassAffairsApi {
+  // 新增学生信息
+  addStudent(param: ESObject): Promise<ESObject>{
+    return YTRequest.post(ApiUrl.addStudent, param)
+  }
+
+  // 删除学生信息
+  deleteStudent(param: ESObject): Promise<ESObject> {
+    return YTRequest.post(ApiUrl.deleteStudent, param)
+  }
+
+  // 修改学生信息
+  updateStudent(param: ESObject): Promise<ESObject> {
+    return YTRequest.post(ApiUrl.updateStudent, param)
+  }
+
+  // 获取学生信息
+  getStudentDetail(param: ESObject): Promise<ESObject> {
+    return YTRequest.get(ApiUrl.getStudentDetail, param)
+  }
+
+  // 获取学生列表
+  getStudentList(param: pageQuery): Promise<pageQueryResp<studentInfo[]>> {
+    return YTRequest.post(ApiUrl.getStudentList, param)
+  }
+}
+
+export const classAffairApi = new ClassAffairsApi()

+ 42 - 1
features/feature/src/main/ets/model/Index.ets

@@ -4,4 +4,45 @@ export class undoData {
   arr?: string[]
   arr2?: Array<string[]>
   columnMode?: number
-}
+}
+
+/************* 请求接口用  *********************/
+
+// 学生信息
+export class studentInfo {
+  defaultParam?: string
+  gender?: number
+  limit?: number
+  name?: string
+  oaid?: string
+  page?: number
+  pageSizes?: number
+  total?: number
+  updateParam?: string
+  // 新增字段
+  createBy?: string
+  createTime?: string
+  updateBy?: string
+  updateTime?: string
+  studentId?: string
+}
+
+// 分页查询数据类型 - 发起
+export class pageQuery {
+  page?: number
+  pageSizes?: number
+}
+
+// 分页查询数据类型 - 返回
+export class pageQueryResp<T> {
+  records?: T[]
+  total?: string
+  size?: string
+  current?: string
+  orders?: string[]
+  optimizeCountSql?: boolean
+  searchCount?: boolean
+  maxLimit?: string
+  countId?: string
+  pages?: string
+}

+ 5 - 0
features/feature/src/main/ets/view/MainView.ets

@@ -88,6 +88,11 @@ export struct MainView {
     .padding({top: this.vm.safeTop })
     .justifyContent(FlexAlign.Center)
     .alignItems(HorizontalAlign.Center)
+    .onVisibleAreaChange([0, 1], (e, r) => {
+      if(e && r == 1){
+        this.vm._getStudentList()
+      }
+    })
   }
 
   // 圆型容器

+ 8 - 5
features/feature/src/main/ets/viewModel/MainViewModel.ets

@@ -1,4 +1,6 @@
 import { YTAvoid, yTRouter } from "basic"
+import { classAffairApi } from "../apis/ClassAffairsApi"
+import { pageQuery } from "../model/Index"
 import { iRouter } from "../utils/RouterUtils"
 
 @ObservedV2
@@ -12,6 +14,11 @@ export class MainViewModel{
   @Trace girlNum: number = 10
   @Trace isEmpty: boolean = true
 
+  query: pageQuery = {
+    page: 1,
+    pageSizes: 10
+  }
+
   // 线性渐变
   linear: LinearGradientOptions = {
     colors: [ ['#A9A8FF', 0.01], ['#EBF7FF', 0.3] ],
@@ -19,8 +26,6 @@ export class MainViewModel{
 
   constructor() {
     this.safeTop = AppStorage.get(YTAvoid.SAFE_TOP_KEY) as number
-
-    this._getStudentList()
   }
 
 
@@ -57,9 +62,7 @@ export class MainViewModel{
 
   // 获取学生列表数据
   async _getStudentList() {
-    // setTimeout(()=> {
-      this.studentList = [ 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 2, 3 ]
-    // }, 2000)
+    classAffairApi.getStudentList(this.query)
   }