diff --git a/app/src/main/java/com/c202101020117/Action/Action3.java b/app/src/main/java/com/c202101020117/Action/Action3.java index 3bbd157..5ac974f 100644 --- a/app/src/main/java/com/c202101020117/Action/Action3.java +++ b/app/src/main/java/com/c202101020117/Action/Action3.java @@ -2,15 +2,27 @@ package com.c202101020117.Action; import androidx.appcompat.app.AppCompatActivity; +import android.app.Activity; +import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; +import android.widget.Toast; +import com.c202101020117.AppConctroller; +import com.c202101020117.Login; import com.c202101020117.R; +import com.c202101020117.package1.DrugInfo; +import com.c202101020117.package1.RYApi; +import com.c202101020117.package1.Result; import com.gyf.immersionbar.ImmersionBar; +import retrofit2.Call; +import retrofit2.Retrofit; +import retrofit2.converter.gson.GsonConverterFactory; + public class Action3 extends AppCompatActivity { @@ -27,6 +39,9 @@ public class Action3 extends AppCompatActivity { setContentView(R.layout.activity_action3); ImmersionBar.with(this).init(); + //添加Activity到容器中 + AppConctroller.addActivity(this); + back = findViewById(R.id.textView33); text1 = findViewById(R.id.editTextText30); text2 = findViewById(R.id.editTextText32); @@ -55,7 +70,62 @@ public class Action3 extends AppCompatActivity { confirm.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { + DrugInfo drugInfo = new DrugInfo(); + int id = sharedPreferences.getInt("id", 0); + String dno = text1.getText() + ""; + String MFD = text2.getText() + ""; + String EXP = text3.getText() + ""; + int Quantity = Integer.parseInt(text4.getText() + ""); + drugInfo.id = id; + drugInfo.dno = dno; + drugInfo.MFD = MFD; + drugInfo.EXP = EXP; + drugInfo.quantity = Quantity; + + SharedPreferences sharedPreferences = getSharedPreferences("user", Activity.MODE_PRIVATE); + String token = sharedPreferences.getString("token", null); + + //获取数据 + Retrofit retrofit = new Retrofit.Builder() + .baseUrl("http://10.138.63.204:8080/") + .addConverterFactory(GsonConverterFactory.create()) //返回结果用Gson解析 + .build(); + RYApi api = retrofit.create(RYApi.class); + + Call updateDrugCall = api.updateDrug("Bearer " + token, drugInfo); + updateDrugCall.enqueue(new retrofit2.Callback() { + @Override + public void onResponse(Call call, retrofit2.Response response) { + Result result = response.body(); + if (result.code == 200) { + Toast.makeText(Action3.this, "修改成功", Toast.LENGTH_LONG).show(); + Intent intent = new Intent(Action3.this, Action6.class); + startActivity(intent); + }else{ + Toast.makeText(Action3.this, "修改失败", Toast.LENGTH_LONG).show(); + } + if (result.code == 401){ + Toast.makeText(Action3.this, "请重新登录", Toast.LENGTH_LONG).show(); + Intent intent = new Intent(Action3.this, Login.class); + startActivity(intent); + AppConctroller.exit(); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + + } + }); + } + }); + + back.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + AppConctroller.removeActivity(Action3.this); + finish(); } }); } diff --git a/app/src/main/java/com/c202101020117/Fragment/HomeFragment.java b/app/src/main/java/com/c202101020117/Fragment/HomeFragment.java index 1621980..2cd61f9 100644 --- a/app/src/main/java/com/c202101020117/Fragment/HomeFragment.java +++ b/app/src/main/java/com/c202101020117/Fragment/HomeFragment.java @@ -11,7 +11,9 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; +import android.widget.Toast; +import com.c202101020117.AppConctroller; import com.c202101020117.Login; import com.c202101020117.R; import com.c202101020117.package1.RYApi; @@ -56,6 +58,12 @@ public class HomeFragment extends Fragment { nickNameTV.setText("用户名:" + userInfoResult.user.nickName); phonenumberTv.setText("手机号:" + userInfoResult.user.phonenumber); } + if(userInfoResult.code == 401){ + Toast.makeText(getActivity(), "登录过期,请重新登录", Toast.LENGTH_SHORT).show(); + Intent intent = new Intent(getActivity(), Login.class); + startActivity(intent); + getActivity().finish(); + } } } diff --git a/app/src/main/java/com/c202101020117/package1/Api.java b/app/src/main/java/com/c202101020117/package1/Api.java index e2541fa..d0f9849 100644 --- a/app/src/main/java/com/c202101020117/package1/Api.java +++ b/app/src/main/java/com/c202101020117/package1/Api.java @@ -3,6 +3,7 @@ package com.c202101020117.package1; import retrofit2.Call; import retrofit2.http.GET; import retrofit2.http.POST; +import retrofit2.http.PUT; import retrofit2.http.Query; //定义网络请求的方法 diff --git a/app/src/main/java/com/c202101020117/package1/RYApi.java b/app/src/main/java/com/c202101020117/package1/RYApi.java index 0283d04..a125597 100644 --- a/app/src/main/java/com/c202101020117/package1/RYApi.java +++ b/app/src/main/java/com/c202101020117/package1/RYApi.java @@ -45,6 +45,10 @@ public interface RYApi { @GET("/system/adrug/expiringlist") Call getExpiringDrugList(@Header("Authorization") String token); + //http://127.0.0.1:8080/system/quantity + @PUT("/system/quantity") + Call updateDrug(@Header("Authorization") String token, @Body DrugInfo drugInfo); + //http://127.0.0.1:8080/system/quantity @POST("/system/quantity") Call addDrug(@Header("Authorization") String token, @Body DrugInfo drugInfo);