Sfoglia il codice sorgente

feat: 新增请求类别和文件上传

王家程 10 mesi fa
parent
commit
0232831652

+ 2 - 1
.env.development

@@ -2,13 +2,14 @@
  # @Author: wjc
  # @Date: 2023-05-30 10:50:23
  # @LastEditors: wjc
- # @LastEditTime: 2024-06-05 11:04:36
+ # @LastEditTime: 2024-07-01 15:01:53
  # @Description: 
 ### 
 VITE_ENV = 'development'
 
 # base api
 VITE_APP_BASE_API = 'https://wy-test.huiguanjia.cn/9ad134a361f8d778/'
+VITE_APP_UPLOAD_API = 'https://wy-test.huiguanjia.cn/9ad134a361f8d778/'
 
 VITE_AMAPIOS_KEY = "96447224d7ce6a099f9b100c14b8584a"
 VITE_AMAP_ANDROID_KEY = "27b7d28dab0c4857b9177c7a59cf33f2"

+ 1 - 0
.env.production

@@ -2,6 +2,7 @@ VITE_ENV = 'production'
 
 # base api
 VITE_APP_BASE_API = 'https://wy.huiguanjia.cn/9ad134a361f8d778/'
+VITE_APP_UPLOAD_API = 'https://wy.huiguanjia.cn/9ad134a361f8d778/'
 
 VITE_AMAPIOS_KEY = "96447224d7ce6a099f9b100c14b8584a"
 VITE_AMAP_ANDROID_KEY = "27b7d28dab0c4857b9177c7a59cf33f2"

+ 2 - 1
.env.test

@@ -2,13 +2,14 @@
  # @Author: wjc
  # @Date: 2024-06-05 10:30:53
  # @LastEditors: wjc
- # @LastEditTime: 2024-06-05 11:04:41
+ # @LastEditTime: 2024-07-01 15:02:05
  # @Description: 
 ### 
 VITE_ENV = 'test'
 
 # base api
 VITE_APP_BASE_API = 'https://wy-test.huiguanjia.cn/9ad134a361f8d778/'
+VITE_APP_UPLOAD_API = 'https://wy-test.huiguanjia.cn/9ad134a361f8d778/'
 
 VITE_AMAPIOS_KEY = "96447224d7ce6a099f9b100c14b8584a"
 VITE_AMAP_ANDROID_KEY = "27b7d28dab0c4857b9177c7a59cf33f2"

+ 3 - 8
src/api/userApi.ts

@@ -2,7 +2,7 @@
  * @Author: wjc
  * @Date: 2024-06-12 10:05:12
  * @LastEditors: wjc
- * @LastEditTime: 2024-06-17 15:09:11
+ * @LastEditTime: 2024-07-01 11:46:02
  * @Description:
  */
 import { request } from '@/utils/request'
@@ -10,9 +10,7 @@ import { BasicRes } from '@/models/requestTypes'
 import { ILogin, LoginRes, Employees } from '@/models/userTypes'
 
 export function login(data: ILogin) {
-  return request<BasicRes<LoginRes>>({
-    url: 'auth/login',
-    method: 'POST',
+  return request.post<BasicRes<LoginRes>>('auth/login', {
     header: {
       entcode: data.entCode,
     },
@@ -24,10 +22,7 @@ export function login(data: ILogin) {
 }
 
 export function logout() {
-  return request<BasicRes<string>>({
-    url: 'auth/logout',
-    method: 'GET',
-  })
+  return request.get<BasicRes<string>>('auth/logout')
 }
 
 export function getUserInfo(data: string) {

+ 10 - 12
src/pages/index/index.vue

@@ -2,7 +2,7 @@
  * @Author: wjc
  * @Date: 2019-08-22 19:41:20
  * @LastEditors: wjc
- * @LastEditTime: 2024-06-26 15:08:03
+ * @LastEditTime: 2024-07-01 10:37:01
  * @Description: 
 -->
 <template>
@@ -12,21 +12,19 @@
       <text class="title">{{ title }}</text>
     </view>
     <view :class="`i-custom-${icon}`" class="wh-32px text-32px"></view>
+    <view>
+      <view>svg icon</view>
+      <MIcon :name="icon" :size="16"></MIcon>
+    </view>
     <u-button type="primary" text="确定"></u-button>
   </view>
 </template>
 
-<script>
-  export default {
-    data() {
-      return {
-        title: 'unocss 自定义图标',
-        icon: 'anl',
-      }
-    },
-    onLoad() {},
-    methods: {},
-  }
+<script setup lang="ts">
+  import MIcon from '@/components/MIcon/index.vue'
+
+  const title = ref('自定义图标')
+  const icon = ref('anl')
 </script>
 
 <style lang="scss">

+ 2 - 2
src/pages/mine/index.vue

@@ -2,12 +2,12 @@
  * @Author: wjc
  * @Date: 2024-06-17 16:02:59
  * @LastEditors: wjc
- * @LastEditTime: 2024-06-25 16:03:00
+ * @LastEditTime: 2024-06-28 15:39:17
  * @Description: 
 -->
 <template>
   <MCard>
-    <view>我的</view>
+    <view>{{ userStore.userInfo.name }}</view>
   </MCard>
   <up-button type="primary" class="btn-primary" @click="handleConfirmLogout">退出登录</up-button>
   <up-modal :show="logoutShow">

+ 2 - 1
src/stores/modules/userStore.ts

@@ -2,7 +2,7 @@
  * @Author: wjc
  * @Date: 2024-06-05 17:13:30
  * @LastEditors: wjc
- * @LastEditTime: 2024-06-25 15:15:56
+ * @LastEditTime: 2024-07-01 14:59:31
  * @Description:
  */
 import { defineStore } from 'pinia'
@@ -21,6 +21,7 @@ export const useUserStore = defineStore('user', {
       storageUserId: '',
     }
   },
+  // persist: true,
   persist: {
     paths: ['isPrivacyShowed', 'isInstall', 'storageUserId', 'storageLoginInfo'],
   },

+ 90 - 1
src/utils/request/index.ts

@@ -2,7 +2,7 @@
  * @Author: wjc
  * @Date: 2024-06-05 10:21:23
  * @LastEditors: wjc
- * @LastEditTime: 2024-06-25 15:12:03
+ * @LastEditTime: 2024-07-01 15:24:39
  * @Description:
  */
 import type { App } from 'vue'
@@ -73,3 +73,92 @@ export const request = <T>(options: IRequestOptions) => {
     })
   })
 }
+
+request.get = <T>(url: string, options: Omit<IRequestOptions, 'url'> = {}) => {
+  return request<T>({
+    method: 'GET',
+    url,
+    ...options,
+  })
+}
+
+request.post = <T>(url: string, options: Omit<IRequestOptions, 'url'> = {}) => {
+  return request<T>({
+    method: 'POST',
+    url,
+    ...options,
+  })
+}
+
+request.put = <T>(url: string, options: Omit<IRequestOptions, 'url'> = {}) => {
+  return request<T>({
+    method: 'PUT',
+    url,
+    ...options,
+  })
+}
+
+request.del = <T>(url: string, options: Omit<IRequestOptions, 'url'> = {}) => {
+  return request<T>({
+    method: 'DELETE',
+    url,
+    ...options,
+  })
+}
+
+export const uploadFile = <T>({ tempFilePath, formData, loading, error, data }) => {
+  uni.uploadFile({
+    url: import.meta.env.VITE_APP_UPLOAD_API,
+    filePath: tempFilePath,
+    name: 'file',
+    formData,
+    success: (uploadFileRes) => {
+      data.value = uploadFileRes.data as T
+    },
+    fail: (err) => {
+      error.value = true
+    },
+    complete: () => {
+      loading.value = false
+    },
+  })
+}
+
+request.uploadFile = <T>(formData: Record<string, any> = {}, count = 1) => {
+  const loading = ref(false)
+  const error = ref(false)
+  const data = ref<T>()
+
+  // #ifdef MP-WEIXIN
+  // 微信小程序从基础库 2.21.0 开始, wx.chooseImage 停止维护,请使用 uni.chooseMedia 代替。
+  uni.chooseMedia({
+    count: 1,
+    mediaType: ['image'],
+    success: (res) => {
+      loading.value = true
+      const tempFilePath = res.tempFiles[0].tempFilePath
+      uploadFile<T>({ tempFilePath, formData, data, error, loading })
+    },
+    fail: (err) => {
+      console.error('uni.chooseMedia err->', err)
+      error.value = true
+    },
+  })
+  // #endif
+  // #ifndef MP-WEIXIN
+  uni.chooseImage({
+    count,
+    success: (res) => {
+      loading.value = true
+      const tempFilePath = res.tempFilePaths[0]
+      uploadFile<T>({ tempFilePath, formData, data, error, loading })
+    },
+    fail: (err) => {
+      console.error('uni.chooseImage err->', err)
+      error.value = true
+    },
+  })
+  // #endif
+
+  return { loading, error, data }
+}

+ 1 - 0
types/components.d.ts

@@ -11,6 +11,7 @@ declare module 'vue' {
     MCard: typeof import('./../src/components/MCard/index.vue')['default']
     MContainer: typeof import('./../src/components/MContainer/index.vue')['default']
     MFooter: typeof import('./../src/components/MFooter/index.vue')['default']
+    MIcon: typeof import('./../src/components/MIcon/index.vue')['default']
     MPage: typeof import('./../src/components/MPage/index.vue')['default']
     RouterLink: typeof import('vue-router')['RouterLink']
     RouterView: typeof import('vue-router')['RouterView']