diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index 949a1b1..21fa598 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -15,6 +15,9 @@ android:theme="@style/Theme.CourseDesign" android:usesCleartextTraffic="true" tools:targetApi="31"> + diff --git a/src/main/java/com/example/coursedesign/Api.java b/src/main/java/com/example/coursedesign/Api.java index 3aa2b95..34f54fd 100644 --- a/src/main/java/com/example/coursedesign/Api.java +++ b/src/main/java/com/example/coursedesign/Api.java @@ -20,6 +20,14 @@ public interface Api { @GET("/system/person/login") Call login(@Header("Authorization") String token, @Query("phone") String username, @Query("password") String password); + //http://127.0.0.1:8080/system/person + @POST("/system/person") + Call addPerson(@Header("Authorization") String token, @Body LoginResult.DataDTO dataDTO); + + //http://127.0.0.1:8080/system/person + @PUT("/system/person") + Call updatePerson(@Header("Authorization") String token, @Body LoginResult.DataDTO dataDTO); + //http://127.0.0.1:8080/system/commodity/list @GET("/system/commodity/list2") Call getCommodityList(@Header("Authorization") String token, @Query("state") String state); diff --git a/src/main/java/com/example/coursedesign/MineFragment.java b/src/main/java/com/example/coursedesign/MineFragment.java index ef20c63..a07a69b 100644 --- a/src/main/java/com/example/coursedesign/MineFragment.java +++ b/src/main/java/com/example/coursedesign/MineFragment.java @@ -46,6 +46,14 @@ public class MineFragment extends Fragment { phone.setText(sharedPreferences.getString("phone", "")); Glide.with(getContext()).load(sharedPreferences.getString("picture", "")).into(picture); + picture.setOnClickListener(new View.OnClickListener(){ + + @Override + public void onClick(View v) { + startActivity(new Intent(getActivity(), MyInformationActivity.class)); + } + }); + myOrder.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { diff --git a/src/main/java/com/example/coursedesign/MyInformationActivity.java b/src/main/java/com/example/coursedesign/MyInformationActivity.java new file mode 100644 index 0000000..ada8687 --- /dev/null +++ b/src/main/java/com/example/coursedesign/MyInformationActivity.java @@ -0,0 +1,107 @@ +package com.example.coursedesign; + +import android.content.SharedPreferences; +import android.os.Bundle; +import android.util.Log; +import android.widget.Button; +import android.widget.EditText; +import android.widget.Toast; + +import androidx.activity.EdgeToEdge; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.graphics.Insets; +import androidx.core.view.ViewCompat; +import androidx.core.view.WindowInsetsCompat; + +import retrofit2.Call; +import retrofit2.Retrofit; +import retrofit2.converter.gson.GsonConverterFactory; + +public class MyInformationActivity extends AppCompatActivity { + + EditText name,phone,password,address,major,age,gender; + Button submit; + LoginResult.DataDTO data; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + EdgeToEdge.enable(this); + setContentView(R.layout.activity_my_information); + ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { + Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); + v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); + return insets; + }); + + name = findViewById(R.id.editTextText6); + phone = findViewById(R.id.editTextPhone2); + password = findViewById(R.id.editTextNumberPassword); + address = findViewById(R.id.editTextText13); + major = findViewById(R.id.editTextText14); + age = findViewById(R.id.editTextNumber2); + gender = findViewById(R.id.editTextText12); + submit = findViewById(R.id.button10); + + SharedPreferences sharedPreferences = getSharedPreferences("user", MODE_PRIVATE); + SharedPreferences.Editor editor = sharedPreferences.edit(); + int userId = sharedPreferences.getInt("userId", 0); + + Retrofit retrofit = new Retrofit.Builder() + .baseUrl("http://10.138.63.204:8080/") + .addConverterFactory(GsonConverterFactory.create()) //返回结果用Gson解析 + .build(); + Api api = retrofit.create(Api.class); + + Call loginCall = api.login("Bearer ", sharedPreferences.getString("phone", ""), sharedPreferences.getString("password", "")); + loginCall.enqueue(new retrofit2.Callback() { + + @Override + public void onResponse(Call call, retrofit2.Response response) { + LoginResult loginResult = response.body(); + if (loginResult != null && loginResult.data != null) { + data = loginResult.data; + name.setText(loginResult.data.name.toString()); + phone.setText(loginResult.data.phone.toString()); + password.setText(loginResult.data.password.toString()); + address.setText(loginResult.data.address.toString()); + major.setText(loginResult.data.major.toString()); + age.setText(loginResult.data.age.toString()); + gender.setText(loginResult.data.gender.toString()); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + + } + }); + + submit.setOnClickListener(v -> { + data.name = name.getText().toString(); + data.phone = phone.getText().toString(); + data.password = password.getText().toString(); + data.address = address.getText().toString(); + data.major = major.getText().toString(); + data.age = Integer.parseInt(age.getText().toString()); + data.gender = gender.getText().toString(); + Call updatePersonCall = api.updatePerson("Bearer ", data); + updatePersonCall.enqueue(new retrofit2.Callback() { + + @Override + public void onResponse(Call call, retrofit2.Response response) { + Result result = response.body(); + if(result.code == 200){ + Toast.makeText(MyInformationActivity.this, "修改个人信息成功", Toast.LENGTH_SHORT).show(); + finish(); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + Log.e("error", t.getMessage()); + } + }); + }); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/coursedesign/RegistrationActivity.java b/src/main/java/com/example/coursedesign/RegistrationActivity.java index 4172c30..fc6e5e8 100644 --- a/src/main/java/com/example/coursedesign/RegistrationActivity.java +++ b/src/main/java/com/example/coursedesign/RegistrationActivity.java @@ -1,8 +1,10 @@ package com.example.coursedesign; import android.os.Bundle; +import android.util.Log; import android.widget.Button; import android.widget.EditText; +import android.widget.Toast; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; @@ -10,6 +12,11 @@ import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Retrofit; +import retrofit2.converter.gson.GsonConverterFactory; + public class RegistrationActivity extends AppCompatActivity { EditText username,phone,password; @@ -34,7 +41,38 @@ public class RegistrationActivity extends AppCompatActivity { // 注册按钮监听 register.setOnClickListener(v -> { + LoginResult.DataDTO dataDTO = new LoginResult.DataDTO(); + dataDTO.name = username.getText().toString(); + dataDTO.phone = phone.getText().toString(); + dataDTO.password = password.getText().toString(); + dataDTO.picture = "https://tse3-mm.cn.bing.net/th/id/OIP-C.lXFxe1bucFZmHU_GG0WbwAHaHa?w=150&h=183&c=7&r=0&o=5&pid=1.7"; + //获取数据 + Retrofit retrofit = new Retrofit.Builder() + .baseUrl("http://10.138.63.204:8080/") + .addConverterFactory(GsonConverterFactory.create()) //返回结果用Gson解析 + .build(); + Api api = retrofit.create(Api.class); + + Call addPersonCall = api.addPerson("Bearer ", dataDTO); + addPersonCall.enqueue(new Callback() { + + @Override + public void onResponse(Call call, retrofit2.Response response) { + Result result = response.body(); + if(result.code == 200){ + Toast.makeText(RegistrationActivity.this, "注册成功", Toast.LENGTH_SHORT).show(); + finish(); + }else{ + Toast.makeText(RegistrationActivity.this, "注册失败", Toast.LENGTH_SHORT).show(); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + Log.e("error", t.getMessage()); + } + }); }); back.setOnClickListener(v -> { diff --git a/src/main/res/layout/activity_my_information.xml b/src/main/res/layout/activity_my_information.xml new file mode 100644 index 0000000..2fe59a8 --- /dev/null +++ b/src/main/res/layout/activity_my_information.xml @@ -0,0 +1,225 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +