package cn.flightfeather.thirdapp.fragment;
|
|
|
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.thirdapp.CommonApplication;
|
import cn.flightfeather.thirdapp.R;
|
import cn.flightfeather.thirdapp.activity.LoginActivity;
|
import cn.flightfeather.thirdapp.bean.Userinfo;
|
import cn.flightfeather.thirdapp.module.MainActivity;
|
import retrofit2.Call;
|
import retrofit2.Callback;
|
import retrofit2.Response;
|
import retrofit2.Retrofit;
|
import retrofit2.http.Body;
|
import retrofit2.http.POST;
|
|
import static android.graphics.PorterDuff.Mode.SRC_IN;
|
|
/**
|
* A simple {@link Fragment} subclass.
|
*/
|
public class CardFragment extends Fragment implements View.OnClickListener {
|
private Button mbtn_login;
|
private TextView mtv_name;
|
private ImageView miv_photo;
|
private static String USER_TYPE = "type";
|
private int type = 0;
|
private int position = 0;
|
private static String POSITION = "position";
|
private String FFUSER = "飞羽环保";
|
private String ADMIN = "主管部门";
|
private 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();
|
}
|
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);
|
}
|
}
|
|
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);
|
}
|
}
|
|
//显示登录按钮动画
|
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);
|
Toast.makeText(application, "登录成功", Toast.LENGTH_SHORT).show();
|
startActivity(new Intent(getActivity(), MainActivity.class));
|
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();
|
}
|
});
|
}
|
|
//登录按钮的点击事件
|
@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);
|
}
|
}
|