18034727885
2026-04-27 3a4c1bbb2cf4fb1cdda8a5061b0674f30e7456b4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package com.example.basecore.base
 
import androidx.lifecycle.ViewModel
import com.example.basecore.net.CODE_EXPIRED
import com.example.basecore.net.ex.ExceptionHandle
import com.example.basecore.net.http.BaseListResponse
import com.example.basecore.net.http.BaseResponseBean
import com.example.basecore.net.http.OssBaseResponse
import com.example.basecore.net.http.Result
import com.example.basecore.util.LogUtils
 
 
import kotlinx.coroutines.coroutineScope
import java.lang.Exception
 
/**
 * @Description  A ViewModel is alive longer than Activity or Fragment,
 * it should not reference a View, or any class that might hold a context reference to an Activity.
 *
 */
open class BaseViewModel : ViewModel() {
 
    suspend fun <T : Any> safeCall(
        call: suspend () -> Result<T>
    ): Result<T> {
        return try {
            call()
        } catch (e: Exception) {
            val ex = ExceptionHandle.handleException(e)
            Result.Error(ex)
        }
    }
 
 
    suspend fun <T : Any> execute(response: BaseResponseBean<T>): Result<T> {
 
        LogUtils.d("Thread = ${Thread.currentThread()}")
        return coroutineScope {
            when {
                response.isOk -> {
                    Result.Success(response.data)
                }
 
                response.isExpired -> {
                    Result.Expired(
                        CODE_EXPIRED
                    )
                }
 
                else -> {
                    Result.Error(
                        ExceptionHandle.ResponseThrowable(
                            Exception(response.errorMsg),
                            601
                        )
                    )
                }
            }
        }
    }
 
 
    suspend fun <T : Any> callList(response: BaseListResponse<T>): Result<List<T>> {
 
        return coroutineScope {
            when {
                response.isOk -> {
                    Result.Success(response.data)
                }
 
                response.isExpired -> {
                    Result.Expired(
                        CODE_EXPIRED
                    )
                }
 
                else -> {
                    Result.Error(
                        ExceptionHandle.ResponseThrowable(
                            Exception(response.errorMsg),
                            601
                        )
                    )
                }
            }
        }
    }
 
 
    suspend fun <T : Any> callNoResult(response: BaseResponseBean<T>): Result<T> {
 
        return coroutineScope {
            when {
                response.isOk -> {
                    Result.SuccessNone(response.status)
                }
 
                response.isExpired -> {
                    Result.Expired(
                        CODE_EXPIRED
                    )
                }
 
                else -> {
                    Result.Error(
                        ExceptionHandle.ResponseThrowable(
                            Exception(response.errorMsg),
                            601
                        )
                    )
                }
            }
        }
    }
 
 
    suspend fun callOSSToken(response: OssBaseResponse): Result<OssBaseResponse> {
 
        return coroutineScope {
            when {
                response.isOk -> {
                    Result.Success(response)
                }
 
                else -> {
                    Result.Error(
                        ExceptionHandle.ResponseThrowable(
                            Exception("未知错误"),
                            601
                        )
                    )
                }
            }
        }
    }
 
}