riku
2025-07-02 3013b813e5df6977c0be921928f73b1a3adde290
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package cn.flightfeather.thirdappmodule;
 
import android.app.Application;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ping.greendao.gen.DaoMaster;
import com.ping.greendao.gen.DaoSession;
import com.ping.greendao.gen.UserinfoDao;
 
import org.greenrobot.greendao.query.QueryBuilder;
 
import java.io.File;
 
import cn.flightfeather.thirdappmodule.bean.entity.Userinfo;
import cn.flightfeather.thirdappmodule.common.net.RetrofitFactory;
import cn.flightfeather.thirdappmodule.util.GlobalConfig;
import cn.flightfeather.thirdappmodule.util.crashreport.MyCrashHandler;
import cn.flightfeather.thirdappmodule.util.notification.MyNotificationChannel;
import cn.flightfeather.thirdappmodule.util.tbs.Tbs;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
 
/**
 * Created by note_ff_1602 on 2018/1/10.
 */
 
public class CommonApplication extends Application {
//    public String ROOT_URL_RELEASE = "http://47.100.191.150:9005/";
    public String ROOT_URL_RELEASE = "https://fyami.com.cn:447/";
    public String ROOT_URL_RELEASE_IMAGE = ROOT_URL_RELEASE + "images/";
 
    public String ROOT_URL ="http://192.168.0.103:9001/";
//    public String ROOT_URL_IMAGE = ROOT_URL + "images/";
    public String ROOT_URL_IMAGE = ROOT_URL_RELEASE + "images/";
 
    // 飞羽环境系统后端接口ip
//    public String ROOT_URL_RELEASE_2 = "http://47.100.191.150:9006/";
    public String ROOT_URL_RELEASE_2 = "https://fyami.com.cn/";
    public String ROOT_URL_RELEASE_IMAGE_2 = ROOT_URL_RELEASE_2 + "images/";
 
 
    private Retrofit retrofit;
    private Retrofit retrofitImage;
    private DaoSession daoSession;
    private Userinfo currentUser;
    protected boolean released = true;
    //在程序打开时联网获取预置数据
    private boolean loadPreDataWhenOpen = true;
 
    private static CommonApplication instance;
 
    @Override
    public void onCreate() {
        super.onCreate();
 
        //初始化网络请求
        RetrofitFactory.init(this);
 
        new MyCrashHandler(this).init();
 
        //x5内核初始化
        Tbs.Companion.init(this);
 
        //Android8.0后注册通知通道
        MyNotificationChannel.Companion.init(this);
 
        //初始化移动推送
//        PushService.Companion.init(this);
 
        if (instance == null) {
            instance = this;
        }
    }
 
    public static CommonApplication getInstance() {
        return instance;
    }
 
    /**
     * 获取数据传输接口调用的根地址
     */
    public Retrofit getRetrofit(){
 
        if (retrofit ==null){
            Gson gson = new GsonBuilder()
                    .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
                    .create();
 
            if (released){
                retrofit = new Retrofit.Builder()
                        .baseUrl(ROOT_URL_RELEASE)
                        .addConverterFactory(GsonConverterFactory.create(gson))
                        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                        .build();
            }else {
                retrofit = new Retrofit.Builder()
                        .baseUrl(ROOT_URL)
                        .addConverterFactory(GsonConverterFactory.create(gson))
                        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                        .build();
            }
 
        }
        return retrofit;
    }
 
    /**
     * 获取图片传输接口调用的根地址
     */
    public Retrofit getRetrofitImage(){
 
        if (retrofitImage ==null){
            Gson gson = new GsonBuilder()
                    .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
                    .create();
            if (released){
                retrofitImage = new Retrofit.Builder()
                        .baseUrl(ROOT_URL_RELEASE_IMAGE)
                        .addConverterFactory(GsonConverterFactory.create(gson))
                        .build();
            }else {
                retrofitImage = new Retrofit.Builder()
                        .baseUrl(ROOT_URL_IMAGE)
                        .addConverterFactory(GsonConverterFactory.create(gson))
                        .build();
            }
        }
        return retrofitImage;
    }
 
    /**
     * 获取数据库Session
     */
    public DaoSession getDaoSession() {
        if (daoSession ==null) {
            File path = new File(Environment.getExternalStorageDirectory(), "FlightFeather/third_new.db");
            if(!path.exists())
                path.getParentFile().mkdirs();
 
            //创建数据库shop.db"
            DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(getApplicationContext(), path.getAbsolutePath(), null);
            //获取可写数据库
            SQLiteDatabase db = helper.getWritableDatabase();
            //获取数据库对象
            DaoMaster daoMaster = new DaoMaster(db);
            daoSession = daoMaster.newSession();
 
            QueryBuilder.LOG_SQL = true;
            QueryBuilder.LOG_VALUES = true;
        }
 
        return daoSession;
    }
 
    /**
     * 获取当前用户对象
     */
    public Userinfo getCurrentUser() {
        if (currentUser ==null){
            UserinfoDao userinfoDao = this.getDaoSession().getUserinfoDao();
            String userId = GlobalConfig.getInstance().getUserId();
            if (userId.equals(GlobalConfig.DEFAULT_USER)) {
                currentUser = userinfoDao.queryBuilder().unique();
            } else {
                currentUser = userinfoDao.queryBuilder().where(UserinfoDao.Properties.Guid.eq(userId)).unique();
            }
        }
        return currentUser;
    }
 
    public void setCurrentUser(Userinfo currentUser) {
        GlobalConfig.getInstance().setUserId(currentUser.getGuid());
        this.currentUser = currentUser;
    }
 
    /**
     * app启动时判断是否加载初始化数据完成
     */
    public boolean isLoadPreDataWhenOpen() {
        return loadPreDataWhenOpen;
    }
 
    public void setLoadPreDataWhenOpen(boolean loadPreDataWhenOpen) {
        this.loadPreDataWhenOpen = loadPreDataWhenOpen;
    }
 
}