Ver Fonte

将上传文件更改为promise兼容版本

wangcy há 2 meses atrás
pai
commit
a88c283500

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

@@ -98,10 +98,10 @@ export class YTRequest {
   }
 
   //上传文件
-  static uploadFile(context: Context, fullpath: string, success: (url: string) => void) {
+  static uploadFile(context: Context, fullpath: string, success?: (url: string) => void) {
     const formData = new FormData()
     formData.append('file', fullpath)
-    YTRequest.upPost<ReqString, FormData>('/common/upload', formData, {
+    const promise = YTRequest.upPost<ReqString, FormData>('/common/upload', formData, {
       headers: { 'Content-Type': 'multipart/form-data' },
       context,
       onUploadProgress: (progressEvent: AxiosProgressEvent): void => {
@@ -109,10 +109,14 @@ export class YTRequest {
           Math.ceil(progressEvent.loaded / progressEvent.total * 100) + '%' : '0%', 'uploadFile');
       }
     })
-      .then(res => {
-        const url = res['url']
-        success(url)
-      })
+    promise.then(res => {
+      const url = res['url']
+      success?.(url)
+    })
+
+    return promise.then(res => {
+      return res['url']
+    })
   }
 }
 

+ 31 - 24
features/feature/src/main/ets/view/MainView.ets

@@ -1,6 +1,6 @@
-import { BackgroundPageModifier, DateOption, YTAddressSelectorDialog, YTDateUtil } from "basic"
-import { UnitType } from "basic/src/main/ets/datepicker/DatePickerEnums"
-import { promptAction } from "@kit.ArkUI"
+import { BackgroundPageModifier, DateOption, YTAddressSelectorDialog, YTDateUtil } from 'basic'
+import { UnitType } from 'basic/src/main/ets/datepicker/DatePickerEnums'
+import { promptAction } from '@kit.ArkUI'
 
 @Component
 export struct MainView {
@@ -11,42 +11,49 @@ export struct MainView {
           //以下内容都可以不传 有默认值
           const date: Date = new Date()
           const yTDateDialog = new YTAddressSelectorDialog(this.getUIContext())
-          let dateOption = new DateOption()//选项配置类
-          dateOption.date = date//当前日期  默认今天
-          dateOption.minDate = new Date(2000,0,1)//最小选择的日期 默认10年前的今天
-          dateOption.maxDate = new Date(2048,12,30)//最大选择的日期 默认10年后的今天
-          dateOption.unitType = UnitType.YEAR_MONTH_DAY//日期单位 默认年月日 可选年月、月日等组合
-          dateOption.confirm = (date: Date) => {//确认选择回调函数
-            console.log('确认', YTDateUtil.formatDate( date))
-            promptAction.openToast({message:YTDateUtil.formatDate( date)})
+          let dateOption = new DateOption() //选项配置类
+          dateOption.date = date //当前日期  默认今天
+          dateOption.minDate = new Date(2000, 0, 1) //最小选择的日期 默认10年前的今天
+          dateOption.maxDate = new Date(2048, 12, 30) //最大选择的日期 默认10年后的今天
+          dateOption.unitType = UnitType.YEAR_MONTH_DAY //日期单位 默认年月日 可选年月、月日等组合
+          dateOption.confirm = (date: Date) => { //确认选择回调函数
+            console.log('确认', YTDateUtil.formatDate(date))
+            promptAction.openToast({ message: YTDateUtil.formatDate(date) })
           }
-          dateOption.headerLeftBuilder = wrapBuilder(headerLeftBuilder)//头部左侧按钮 默认返回按钮
-          dateOption.headerRightBuilder = wrapBuilder(headerLeftBuilder)//头部右侧按钮 默认确认按钮
-          dateOption.highlightBackgroundColor = Color.Pink//选中高亮背景色
-          dateOption.highlightBorderColor = Color.Red//选中高亮边框色
-          dateOption.textStyle = {//日期文字样式
-            font:{size:20,weight:400},
-            color:Color.Gray
+          dateOption.headerLeftBuilder = wrapBuilder(headerLeftBuilder) //头部左侧按钮 默认返回按钮
+          dateOption.headerRightBuilder = wrapBuilder(headerLeftBuilder) //头部右侧按钮 默认确认按钮
+          dateOption.highlightBackgroundColor = Color.Pink //选中高亮背景色
+          dateOption.highlightBorderColor = Color.Red //选中高亮边框色
+          dateOption.textStyle = {
+            //日期文字样式
+            font: { size: 20, weight: 400 },
+            color: Color.Gray
           }
-          dateOption.selectedTextStyle = {//选中日期文字样式
-            font:{size:25,weight:400},
-            color:Color.Yellow
+          dateOption.selectedTextStyle = {
+            //选中日期文字样式
+            font: { size: 25, weight: 400 },
+            color: Color.Yellow
           }
-          yTDateDialog.show(dateOption)//设置好配置之后打开日历的函数
+          yTDateDialog.show(dateOption) //设置好配置之后打开日历的函数
         })
+
+      Image('https://hm-static.ytpm.net/upload/20250821/处暑-1755757002591.jpg')
+        .width('100%')
+        .height(300)
     }
     .attributeModifier(new BackgroundPageModifier(true))
 
   }
 }
+
 @Builder
-function headerLeftBuilder(){
+function headerLeftBuilder() {
   Image($r('app.media.app_icon'))
     .width(24)
 }
 
 @Builder
-function headerRightBuilder(){
+function headerRightBuilder() {
   Image($r("app.media.app_icon"))
     .width(24)
 }

+ 2 - 0
products/entry/src/main/ets/entryability/EntryAbility.ets

@@ -108,6 +108,8 @@ export default class EntryAbility extends UIAbility {
       })
       yTPreferences.init(this.context)
       pushMessageUtil.init(this.context)
+      // const res = this.context.resourceManager.getRawFileContentSync('images/types.webp')
+
     });
 
   }

+ 7 - 3
products/entry/src/main/ets/pages/Index.ets

@@ -1,6 +1,7 @@
-import { BackgroundPageModifier, BasicType, YTAvoid, yTRouter, yTToast, YTUserRequest } from 'basic';
+import { BasicType, YTAvoid, yTRouter, yTToast, YTUserRequest } from 'basic';
 import { Mine } from 'user/src/main/ets/views/Mine';
 import { MainView, SecondView, ThirdView } from 'feature';
+import { resourceManager } from '@kit.LocalizationKit';
 
 @Entry
 @Component
@@ -33,13 +34,17 @@ struct Index {
   tabsController: TabsController = new TabsController()
 
   aboutToAppear(): void {
+
+
     yTToast.init({
       context: this.getUIContext(),
       options: { alignment: DialogAlignment.Center, maskColor: '#80000000' }
     })
 
     YTUserRequest.refreshUserInfo((res, err) => {
-      //TODO 在此处发起需要登录的请求
+      if (!err && res) {
+        //TODO 在此处发起需要登录的请求
+      }
     })
   }
 
@@ -85,7 +90,6 @@ struct Index {
       .height('100%')
       .backgroundColor(Color.White)
 
-
     }
     .mode(NavigationMode.Stack)
     .hideToolBar(true)