|
@@ -2,7 +2,7 @@
|
|
* @Author: wjc
|
|
* @Author: wjc
|
|
* @Date: 2024-06-05 10:21:23
|
|
* @Date: 2024-06-05 10:21:23
|
|
* @LastEditors: wjc
|
|
* @LastEditors: wjc
|
|
- * @LastEditTime: 2024-06-25 15:12:03
|
|
|
|
|
|
+ * @LastEditTime: 2024-07-01 15:24:39
|
|
* @Description:
|
|
* @Description:
|
|
*/
|
|
*/
|
|
import type { App } from 'vue'
|
|
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 }
|
|
|
|
+}
|