18034727885
2026-08-20 97da117f2d2a6425039b6a4cd6dcf5a6601041e1
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package com.igo.user.network
 
import android.util.Log
import cn.sinata.xldutils.activity.BaseActivity
import cn.sinata.xldutils.data.ResultData
import cn.sinata.xldutils.defaultScheduler
import cn.sinata.xldutils.fragment.BaseFragment
import cn.sinata.xldutils.rxutils.ResultDataSubscriber
import cn.sinata.xldutils.utils.LanguageUtil
import cn.sinata.xldutils.utils.SPUtils
import com.igo.user.utils.Const
import io.reactivex.Flowable
 
/**
 * Created on 2018/4/12.
 */
inline fun <reified O, I : ResultData<O>> Flowable<I>.request(
    activity: BaseActivity,
    showToast: Boolean = true,
    showDialog: Boolean = false,
    crossinline success: (msg: String?, t: O?) -> Unit,
    crossinline error: (code: Int, msg: String) -> Unit
) {
    if (showDialog) {
        activity.showDialog()
    }
    this.defaultScheduler().subscribe(object : ResultDataSubscriber<O>(activity) {
        override fun onSuccess(msg: String?, data: O?) {
            activity.dismissDialog()
            success(msg, data)
        }
 
        override fun isShowToast() = showToast
 
        override fun onError(code: Int, msg: String) {
            activity.dismissDialog()
            if (code == 600) {//token无效
                super.onError(
                    code, when (LanguageUtil.getLanguage()) {
                        Const.Language.CHINESE -> "您的身份已过期,请重新登录"
                        Const.Language.FRENCH -> "Votre identité a expiré, veuillez vous reconnecter"
                        else -> "Your identity has expired, please log in again"
                    }
                )
                SPUtils.instance().remove(Const.User.TOKEN).remove(Const.User.USER_ID)
                    .remove(Const.User.NO_PHONE).remove(Const.User.EMAIL).apply()
                activity.gotoLogin()
            } else {
                super.onError(code, msg)
                error(code, msg)
            }
        }
    })
}
 
inline fun <reified O, I : ResultData<O>> Flowable<I>.request(
    activity: BaseActivity,
    crossinline showToast: (code: Int, msg: String) -> Boolean,
    showDialog: Boolean = false,
    crossinline success: (msg: String?, t: O?) -> Unit,
    crossinline error: (code: Int, msg: String) -> Unit
) {
    if (showDialog) {
        activity.showDialog()
    }
    this.defaultScheduler().subscribe(object : ResultDataSubscriber<O>(activity) {
        override fun onSuccess(msg: String?, data: O?) {
            activity.dismissDialog()
            success(msg, data)
        }
 
        override fun onError(code: Int, msg: String) {
            activity.dismissDialog()
            if (code == 600) {//token无效
                super.onError(
                    code, when (LanguageUtil.getLanguage()) {
                        Const.Language.CHINESE -> "您的身份已过期,请重新登录"
                        Const.Language.FRENCH -> "Votre identité a expiré, veuillez vous reconnecter"
                        else -> "Your identity has expired, please log in again"
                    }
                )
                SPUtils.instance().remove(Const.User.TOKEN).remove(Const.User.USER_ID)
                    .remove(Const.User.NO_PHONE).remove(Const.User.EMAIL).apply()
                activity.gotoLogin()
            } else {
                if (showToast.invoke(code, msg)) {
                    super.onError(code, msg)
                }
                error(code, msg)
            }
        }
    })
}
 
inline fun <reified O, I : ResultData<O>> Flowable<I>.request(
    activity: BaseActivity,
    showToast: Boolean = true,
    crossinline success: (msg: String?, t: O?) -> Unit,
    crossinline error: (code: Int, msg: String) -> Unit
) {
    request(activity, showToast, false, success, error)
}
 
inline fun <reified O, I : ResultData<O>> Flowable<I>.request(
    activity: BaseActivity,
    showToast: Boolean = true,
    showDialog: Boolean = false,
    crossinline success: (msg: String?, t: O?) -> Unit
) {
    request(activity, showToast, showDialog, success) { code: Int, msg: String ->
        Log.e("xxxx", "code:${code},msg:${msg}")
    }
}
 
inline fun <reified O, I : ResultData<O>> Flowable<I>.requestByF(
    fragment: BaseFragment,
    showToast: Boolean = true,
    showDialog: Boolean = false,
    crossinline success: (msg: String?, t: O?) -> Unit,
    crossinline error: (code: Int, msg: String) -> Unit
) {
    if (showDialog) {
        fragment.showDialog()
    }
    this.defaultScheduler().subscribe(object : ResultDataSubscriber<O>(fragment) {
        override fun onSuccess(msg: String?, data: O?) {
            fragment.dismissDialog()
            success(msg, data)
        }
 
        override fun isShowToast() = showToast
 
        override fun onError(code: Int, msg: String) {
            fragment.dismissDialog()
            if (code == 600) {//token无效
//                super.onError(code, "您的身份已过期,请重新登录")
                SPUtils.instance().remove(Const.User.TOKEN).remove(Const.User.USER_ID)
                    .remove(Const.User.NO_PHONE).remove(Const.User.EMAIL).apply()
                fragment.gotoLogin()
            } else {
                super.onError(code, msg)
                error(code, msg)
            }
        }
    })
}
 
inline fun <reified O, I : ResultData<O>> Flowable<I>.requestByF(
    fragment: BaseFragment,
    showToast: Boolean = true,
    crossinline success: (msg: String?, t: O?) -> Unit,
    crossinline error: (code: Int, msg: String) -> Unit
) {
    requestByF(fragment, showToast, false, success, error)
}
 
inline fun <reified O, I : ResultData<O>> Flowable<I>.requestByF(
    fragment: BaseFragment,
    showToast: Boolean = true,
    crossinline success: (msg: String?, t: O?) -> Unit
) {
    requestByF(fragment, showToast, success) { _, _ -> }
}