riku
2025-10-17 fbae5f3ea74727ccadc48314a864a1ea0099a945
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package cn.flightfeather.thirdappmodule.module.login;
 
 
import static android.graphics.PorterDuff.Mode.SRC_IN;
 
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
 
import com.ping.greendao.gen.DaoSession;
import com.ping.greendao.gen.UserinfoDao;
 
import cn.flightfeather.thirdappmodule.CommonApplication;
import cn.flightfeather.thirdappmodule.R;
import cn.flightfeather.thirdappmodule.activity.LoginActivity;
import cn.flightfeather.thirdappmodule.bean.entity.Userinfo;
import cn.flightfeather.thirdappmodule.module.MainActivity;
import cn.flightfeather.thirdappmodule.util.push.LoginInitializer;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.Body;
import retrofit2.http.POST;
 
/**
 * A simple {@link Fragment} subclass.
 */
public class CardFragment extends Fragment implements View.OnClickListener {
    private Button mbtn_login;
    protected TextView mtv_name;
    protected ImageView miv_photo;
    protected static String USER_TYPE = "type";
    private int type = 0;
    private int position = 0;
    protected static String POSITION = "position";
    protected String FFUSER = "飞羽环保";
    protected String ADMIN = "主管部门";
    protected String POLLUTION = "企业";
 
    private EditText met_UserName;
    private EditText met_PassWord;
    private String accountName;
    private String password;
    private int lastPos;
    private UserinfoDao useinfoDao;
    private DaoSession daoSession;
    private CommonApplication application;
 
    private int positionCurrent;
 
    public CardFragment() {
        // Required empty public constructor
    }
 
    public static CardFragment newInstance(int type, int position) {
        CardFragment cardFragment = new CardFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(USER_TYPE, type);
        bundle.putInt(POSITION, position);
        cardFragment.setArguments(bundle);
        return cardFragment;
    }
 
 
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //读取登录用户信息
        SharedPreferences sharedPre = getContext().getSharedPreferences("config", getContext().MODE_PRIVATE);
        accountName = sharedPre.getString("username", "");
        password = sharedPre.getString("password", "");
        lastPos = sharedPre.getInt("pos", 0);
 
 
        application = (CommonApplication) getActivity().getApplication();
    }
 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_card, container, false);
        mbtn_login = (Button) view.findViewById(R.id.btn_longin);
        mtv_name = (TextView) view.findViewById(R.id.tv_name);
        miv_photo = (ImageView) view.findViewById(R.id.iv_user_photo);
        met_UserName = (EditText) view.findViewById(R.id.et_login_card_name);
        met_PassWord = (EditText) view.findViewById(R.id.et_login_card_password);
        mbtn_login.setScaleX(0);
        mbtn_login.setScaleY(0);
 
        mbtn_login.setOnClickListener(this);
        PorterDuffColorFilter porterDuffColorFilter = new PorterDuffColorFilter(getResources().getColor(R.color.lightGreen), SRC_IN);
 
        final Bundle bundle = getArguments();
        if (bundle != null) {
            type = bundle.getInt(USER_TYPE);
            position = bundle.getInt(POSITION);
            if (position%3 == lastPos%3) {
                met_UserName.setText(accountName);
                met_PassWord.setText(password);
            }
            if (position == 0) {
                showLoginButton();
            }
            setUserTag(type);
        }
 
        return view;
    }
 
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
 
        final Bundle bundle = getArguments();
        int position = bundle.getInt(POSITION);
        if (lastPos == position) {
            LoginActivity la = (LoginActivity) getActivity();
            la.setCurFragment(lastPos);
        }
    }
 
    protected void setUserTag(int type) {
        if (type == 1) {
            mtv_name.setText(FFUSER);
            miv_photo.setImageResource(R.drawable.app_icon);
        } else if (type == 2) {
            mtv_name.setText(ADMIN);
            miv_photo.setImageResource(R.drawable.icon_people);
        } else if (type == 3) {
            mtv_name.setText(POLLUTION);
            miv_photo.setImageResource(R.drawable.icon_pollution2);
        }
    }
 
    //显示登录按钮动画
    public void showLoginButton() {
        mbtn_login.setScaleX(0);
        mbtn_login.setScaleY(0);
        mbtn_login.animate().setDuration(300).scaleX(1).scaleY(1);
    }
 
    //隐藏登陆按钮动画
    public void hideLoginButtion() {
        mbtn_login.setScaleX(1);
        mbtn_login.setScaleY(1);
        mbtn_login.animate().setDuration(300).scaleX(0).scaleY(0);
    }
 
    //修改登录按钮颜色
    public void changeLoginColor(String color) {
        mbtn_login.setBackgroundDrawable(tintDrawable(mbtn_login.getBackground(),
                ColorStateList.valueOf(Color.parseColor(color))));
    }
 
    @Override
    public void onResume() {
 
        super.onResume();
    }
 
    @Override
    public void onPause() {
        super.onPause();
 
    }
 
    public static Drawable tintDrawable(Drawable drawable, ColorStateList colors) {
        final Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTintList(wrappedDrawable, colors);
        return wrappedDrawable;
    }
 
    //保存登录信息方法
    public static void saveLoginInfo(Context context, String username, String password, int positionCurrent) {
        //获取SharedPreferences对象
        SharedPreferences sharedPre = context.getSharedPreferences("config", context.MODE_PRIVATE);
        //获取Editor对象
        SharedPreferences.Editor editor = sharedPre.edit();
        //设置参数
        editor.putString("username", username);
        editor.putString("password", password);
        editor.putInt("pos", positionCurrent);
        //提交
        editor.apply();
    }
 
    private void login(String accountName, String password, final int type){
        Retrofit retrofit = application.getRetrofit();
        TaskService taskService = retrofit.create(TaskService.class);
 
        Userinfo userinfo = new Userinfo();
        userinfo.setAcountname(accountName);
        userinfo.setPassword(password);
        Call<Userinfo> call = taskService.login(userinfo);
        call.enqueue(new Callback<Userinfo>() {
            @Override
            public void onResponse(Call<Userinfo> call, Response<Userinfo> response) {
                if (response.body() != null
                        && (
                                (response.body().getGuid().length() > 0 && type == response.body().getUsertypeid())
                                        || response.body().getUsertypeid() == 0)
                        )
                {
 
                    Userinfo user = response.body();
                    application.setCurrentUser(user);
                    useinfoDao.deleteAll();
                    useinfoDao.insert(user);
                    LoginInitializer.INSTANCE.onLoginStatusCheck(getContext(), true, accountName);
                    Toast.makeText(application, "登录成功", Toast.LENGTH_SHORT).show();
                    loginSuccess();
                    getActivity().finish();
                } else {
                    Toast.makeText(application, "登录失败", Toast.LENGTH_SHORT).show();
                }
            }
 
            @Override
            public void onFailure(Call<Userinfo> call, Throwable t) {
                Toast.makeText(application, "网络链接失败", Toast.LENGTH_SHORT).show();
            }
        });
    }
 
    protected void loginSuccess() {
        startActivity(new Intent(getActivity(), MainActivity.class));
//        startActivity(new Intent(getActivity(), TestActivity.class));
    }
 
    //登录按钮的点击事件
    @Override
    public void onClick(View v) {
 
        Toast.makeText(application, "登录中", Toast.LENGTH_SHORT).show();
        daoSession =application.getDaoSession();
        useinfoDao = daoSession.getUserinfoDao();
 
        accountName = met_UserName.getText().toString();
        password = met_PassWord.getText().toString();
 
//        accountName = "linli2494";
//        password = "abc123456";
//        startActivity(new Intent(getActivity(), MainActivity.class));
//        getActivity().finish();
        if (true){
            if (true){
                saveLoginInfo(getContext(), accountName, password, position);
                new Thread(){
                    @Override
                    public void run() {
                        login(accountName,password, type);
                    }
                }.run();
 
            }else {
                Toast.makeText(getContext(), "用户名密码错误", Toast.LENGTH_SHORT).show();
            }
        }else {
            Toast.makeText(getContext(), "请输入完整", Toast.LENGTH_SHORT).show();
        }
 
    }
 
    interface TaskService{
        @POST("userinfo/login/")
        Call<Userinfo> login(@Body Userinfo userinfo);
    }
}