18034727885
2026-05-08 0cd2089f05a87e9a2155d169c1c3f654e17ef396
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
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.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 (SPUtils.instance().getString("language")) {
                        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,
    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) { _, _ -> }
}