From ed78b6e686d4a7dbbbce8c04908d64e53f54a0dd Mon Sep 17 00:00:00 2001 From: Levi Date: Mon, 23 Dec 2024 11:05:25 +0800 Subject: [PATCH] =?UTF-8?q?Android=E4=BB=A3=E7=A0=8112=E6=9C=8823=E6=97=A5?= =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/AndroidManifest.xml | 8 +- .../coursedesign/AddCommentActivity.java | 80 +++++++ .../java/com/example/coursedesign/Api.java | 28 +++ .../coursedesign/CommodityActivity.java | 198 +++++++++++++++++- .../example/coursedesign/OnSaleFragment.java | 9 +- .../UpdateCommodityMainActivity.java | 127 +++++++++++ src/main/res/layout/activity_add_comment.xml | 82 ++++++++ src/main/res/layout/activity_commodity.xml | 2 +- .../layout/activity_update_commodity_main.xml | 159 ++++++++++++++ 9 files changed, 683 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/example/coursedesign/AddCommentActivity.java create mode 100644 src/main/java/com/example/coursedesign/UpdateCommodityMainActivity.java create mode 100644 src/main/res/layout/activity_add_comment.xml create mode 100644 src/main/res/layout/activity_update_commodity_main.xml diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index 3fe361c..949a1b1 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -5,7 +5,6 @@ + + diff --git a/src/main/java/com/example/coursedesign/AddCommentActivity.java b/src/main/java/com/example/coursedesign/AddCommentActivity.java new file mode 100644 index 0000000..35c4ef2 --- /dev/null +++ b/src/main/java/com/example/coursedesign/AddCommentActivity.java @@ -0,0 +1,80 @@ +package com.example.coursedesign; + +import android.app.Activity; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.util.Log; +import android.widget.Button; +import android.widget.EditText; +import android.widget.TextView; +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 AddCommentActivity extends AppCompatActivity { + + TextView back; + EditText contentET; + Button submit; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + EdgeToEdge.enable(this); + setContentView(R.layout.activity_add_comment); + 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; + }); + + back = findViewById(R.id.textView39); + contentET = findViewById(R.id.editTextText5); + submit = findViewById(R.id.button7); + + SharedPreferences sharedPreferences = getSharedPreferences("comment", Activity.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedPreferences.edit(); + int userId = sharedPreferences.getInt("userId", 0); + int commodityId = sharedPreferences.getInt("commodityId", 0); + + //获取数据 + Retrofit retrofit = new Retrofit.Builder() + .baseUrl("http://10.138.63.204:8080/") + .addConverterFactory(GsonConverterFactory.create()) //返回结果用Gson解析 + .build(); + Api api = retrofit.create(Api.class); + + submit.setOnClickListener(v -> { + String content = contentET.getText().toString(); + CommentListResult.RowsDTO rowsDTO = new CommentListResult.RowsDTO(); + rowsDTO.personid = userId; + rowsDTO.commodityid = commodityId; + rowsDTO.content = content; + Call addCommentCall = api.addComment("Bearer ", rowsDTO); + addCommentCall.enqueue(new retrofit2.Callback() { + @Override + public void onResponse(Call call, retrofit2.Response response) { + Result result = response.body(); + if(result.code == 200){ + Toast.makeText(AddCommentActivity.this, "评论成功", Toast.LENGTH_SHORT).show(); + finish(); + } + } + @Override + public void onFailure(Call call, Throwable t) { + Log.e("error", t.getMessage()); + } + }); + }); + + back.setOnClickListener(v -> finish()); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/coursedesign/Api.java b/src/main/java/com/example/coursedesign/Api.java index d045ad8..3aa2b95 100644 --- a/src/main/java/com/example/coursedesign/Api.java +++ b/src/main/java/com/example/coursedesign/Api.java @@ -36,10 +36,26 @@ public interface Api { @GET("/system/comment/list2") Call getCommentListByCommodityId(@Header("Authorization") String token, @Query("commodityid") Integer commodityId); + //http://127.0.0.1:8080/system/comment + @POST("/system/comment") + Call addComment(@Header("Authorization") String token, @Body CommentListResult.RowsDTO rowsDTO); + //http://127.0.0.1:8080/system/favorite/list2 @GET("/system/favorite/list2") Call getFavoriteListByPersonId(@Header("Authorization") String token, @Query("personid") Integer personId); + //http://127.0.0.1:8080/system/favorite/list3 + @GET("/system/favorite/list3") + Call getFavoriteListByPersonIdAndCommodityId(@Header("Authorization") String token, @Query("personid") Integer personId, @Query("commodityid") Integer commodityId); + + //http://127.0.0.1:8080/system/favorite + @POST("/system/favorite") + Call addFavorite(@Header("Authorization") String token, @Body FavoriteListResult.RowsDTO rowsDTO); + + //http://127.0.0.1:8080/system/favorite/{ids} + @DELETE("system/favorite/{ids}") + Call removeFavorite(@Header("Authorization") String token, @Path("ids") String ids); + //http://127.0.0.1:8080/system/order/list2 @GET("/system/order/list2") Call getOrderListByBuyerId(@Header("Authorization") String token, @Query("buyerid") Integer buyerId); @@ -59,4 +75,16 @@ public interface Api { //http://127.0.0.1:8080/system/commodity/{ids} @DELETE("system/commodity/{ids}") Call removeCommodity(@Header("Authorization") String token, @Path("ids") String ids); + + //http://127.0.0.1:8080/system/order + @POST("/system/order") + Call addOrder(@Header("Authorization") String token, @Body OrderListResult.RowsDTO rowsDTO); + + //http://127.0.0.1:8080/system/favorite/delete + @POST("/system/favorite/delete") + Call removeFavoriteByPersonIdAndCommodityId(@Header("Authorization") String token, @Body FavoriteListResult.RowsDTO rowsDTO); + + //http://127.0.0.1:8080/system/favorite/insert + @POST("/system/favorite/insert") + Call addFavoriteByPersonIdAndCommodityId(@Header("Authorization") String token, @Body FavoriteListResult.RowsDTO rowsDTO); } diff --git a/src/main/java/com/example/coursedesign/CommodityActivity.java b/src/main/java/com/example/coursedesign/CommodityActivity.java index 3f67105..0b2ad6f 100644 --- a/src/main/java/com/example/coursedesign/CommodityActivity.java +++ b/src/main/java/com/example/coursedesign/CommodityActivity.java @@ -10,8 +10,10 @@ import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; +import android.widget.Toast; import androidx.activity.EdgeToEdge; import androidx.annotation.NonNull; @@ -30,6 +32,10 @@ import com.scwang.smart.refresh.layout.SmartRefreshLayout; import com.scwang.smart.refresh.layout.api.RefreshLayout; import com.scwang.smart.refresh.layout.listener.OnRefreshListener; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; + import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -51,8 +57,14 @@ public class CommodityActivity extends AppCompatActivity { TextView descriptionTV; TextView sellerNameTV; ImageView sellerImage; + Button addOrder; + Button addComment; + ImageView favorite; CommentListResult result; + CommodityListResult.RowsDTO rowsDTO; + FavoriteListResult.RowsDTO rowsDTO2; + Boolean isFavorite = false; @Override protected void onCreate(Bundle savedInstanceState) { @@ -79,10 +91,17 @@ public class CommodityActivity extends AppCompatActivity { classificationTV = findViewById(R.id.textView34); sellerImage = findViewById(R.id.imageView10); commodityImage = findViewById(R.id.imageView9); + addOrder = findViewById(R.id.button5); + addComment = findViewById(R.id.button4); + favorite = findViewById(R.id.imageView11); SharedPreferences sharedPreferences = getSharedPreferences("commodity", Activity.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); - int id = sharedPreferences.getInt("id", 0); + int commodityId = sharedPreferences.getInt("id", 0); + + SharedPreferences sharedPreferences2 = getSharedPreferences("user", Activity.MODE_PRIVATE); + SharedPreferences.Editor editor2 = sharedPreferences.edit(); + int userId = sharedPreferences2.getInt("id", 0); //获取数据 Retrofit retrofit = new Retrofit.Builder() @@ -91,14 +110,15 @@ public class CommodityActivity extends AppCompatActivity { .build(); Api api = retrofit.create(Api.class); - Call commodityListCall = api.getCommodityListById("Bearer ", id, null); + //获取商品信息 + Call commodityListCall = api.getCommodityListById("Bearer ", commodityId, null); commodityListCall.enqueue(new retrofit2.Callback() { @Override public void onResponse(Call call, retrofit2.Response response) { CommodityListResult result = response.body(); if(result.code == 200){ - CommodityListResult.RowsDTO rowsDTO = result.rows.get(0); + rowsDTO = result.rows.get(0); commodityNameTV.setText(rowsDTO.name); priceTV.setText("¥" + rowsDTO.price); descriptionTV.setText(rowsDTO.description); @@ -116,13 +136,13 @@ public class CommodityActivity extends AppCompatActivity { } }); - Call commentListCall = api.getCommentListByCommodityId("Bearer ", id); + //获取评论信息 + Call commentListCall = api.getCommentListByCommodityId("Bearer ", commodityId); commentListCall.enqueue(new retrofit2.Callback() { @Override public void onResponse(Call call, retrofit2.Response response) { result = response.body(); - Log.e("result", result.toString()); if(result.code == 200){ myAdapter.notifyDataSetChanged(); } @@ -130,7 +150,32 @@ public class CommodityActivity extends AppCompatActivity { @Override public void onFailure(Call call, Throwable t) { + Log.e("error", t.getMessage()); + } + }); + //获取收藏信息 + Call favoriteListCall = api.getFavoriteListByPersonIdAndCommodityId("Bearer ", userId, commodityId); + favoriteListCall.enqueue(new retrofit2.Callback() { + + @Override + public void onResponse(Call call, retrofit2.Response response) { + FavoriteListResult result = response.body(); + if(result.code == 200){ + if(result.rows.size() == 0){ + isFavorite = false; + favorite.setSelected(false); + }else{ + isFavorite = true; + rowsDTO2 = result.rows.get(0); + favorite.setSelected(true); + } + } + } + + @Override + public void onFailure(Call call, Throwable t) { + Log.e("error", t.getMessage()); } }); @@ -142,7 +187,7 @@ public class CommodityActivity extends AppCompatActivity { @Override public void onRefresh(@NonNull RefreshLayout refreshLayout) { - Call commentListCall = api.getCommentListByCommodityId("Bearer ", id); + Call commentListCall = api.getCommentListByCommodityId("Bearer ", commodityId); commentListCall.enqueue(new retrofit2.Callback() { @Override @@ -163,6 +208,147 @@ public class CommodityActivity extends AppCompatActivity { } }); + addOrder.setOnClickListener(new View.OnClickListener() { + + @Override + public void onClick(View v) { + + OrderListResult.RowsDTO rowsDTO2 = new OrderListResult.RowsDTO(); + rowsDTO2.buyerid = userId; + rowsDTO2.commodityid = commodityId; + rowsDTO2.status = "未发货"; + // 获取当前时间的毫秒数 + long currentTimeMillis = System.currentTimeMillis(); + // 将毫秒数转换为Date对象 + Date date = new Date(currentTimeMillis); + // 定义日期格式 + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()); + // 格式化Date对象为字符串 + rowsDTO2.time = sdf.format(date); + + Call addOrderCall = api.addOrder("Bearer ", rowsDTO2); + addOrderCall.enqueue(new retrofit2.Callback() { + + @Override + public void onResponse(Call call, retrofit2.Response response) { + Result result = response.body(); + if(result.code == 200){ + Toast.makeText(CommodityActivity.this, "下单成功", Toast.LENGTH_SHORT).show(); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + Log.e("error", t.getMessage()); + } + }); + + rowsDTO.state = "售出"; + Call updateCommodityCall = api.updateCommodity("Bearer ", rowsDTO); + updateCommodityCall.enqueue(new retrofit2.Callback() { + + @Override + public void onResponse(Call call, retrofit2.Response response) { + Result result = response.body(); + if(result.code == 200){ + Toast.makeText(CommodityActivity.this, "修改商品状态成功", Toast.LENGTH_SHORT).show(); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + Log.e("error", t.getMessage()); + } + }); + finish(); + } + }); + + addComment.setOnClickListener(new View.OnClickListener(){ + + @Override + public void onClick(View v) { + + SharedPreferences sharedPreferences = getSharedPreferences("comment", Activity.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedPreferences.edit(); + editor.putInt("userId", userId); + editor.putInt("commodityId", commodityId); + editor.commit(); + startActivity(new Intent(CommodityActivity.this, AddCommentActivity.class)); + } + }); + + favorite.setOnClickListener(new View.OnClickListener(){ + + @Override + public void onClick(View v) { + if(isFavorite){ + Call removeFavoriteCall = api.removeFavoriteByPersonIdAndCommodityId("Bearer ", rowsDTO2); + removeFavoriteCall.enqueue(new retrofit2.Callback() { + + @Override + public void onResponse(Call call, retrofit2.Response response) { + Result result = response.body(); + if(result.code == 200){ + Toast.makeText(CommodityActivity.this, "取消收藏成功", Toast.LENGTH_SHORT).show(); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + Log.e("error", t.getMessage()); + } + }); + isFavorite = false; + favorite.setSelected(false); + }else { + + //添加收藏 + FavoriteListResult.RowsDTO rowsDTO = new FavoriteListResult.RowsDTO(); + rowsDTO.personid = userId; + rowsDTO.commodityid = commodityId; + Call addFavoriteCall = api.addFavoriteByPersonIdAndCommodityId("Bearer ", rowsDTO); + addFavoriteCall.enqueue(new retrofit2.Callback() { + + @Override + public void onResponse(Call call, retrofit2.Response response) { + Result result = response.body(); + if(result.code == 200){ + Toast.makeText(CommodityActivity.this, "添加收藏成功", Toast.LENGTH_SHORT).show(); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + Log.e("error", t.getMessage()); + } + }); + + //获取收藏信息 + Call favoriteListCall = api.getFavoriteListByPersonIdAndCommodityId("Bearer ", userId, commodityId); + favoriteListCall.enqueue(new retrofit2.Callback() { + + @Override + public void onResponse(Call call, retrofit2.Response response) { + FavoriteListResult result = response.body(); + if(result.code == 200){ + if(result.rows.size() != 0){ + rowsDTO2 = result.rows.get(0); + } + } + } + + @Override + public void onFailure(Call call, Throwable t) { + Log.e("error", t.getMessage()); + } + }); + isFavorite = true; + favorite.setSelected(true); + } + } + }); + back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { diff --git a/src/main/java/com/example/coursedesign/OnSaleFragment.java b/src/main/java/com/example/coursedesign/OnSaleFragment.java index ba80666..ca234b3 100644 --- a/src/main/java/com/example/coursedesign/OnSaleFragment.java +++ b/src/main/java/com/example/coursedesign/OnSaleFragment.java @@ -1,6 +1,7 @@ package com.example.coursedesign; import android.app.Activity; +import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; @@ -190,7 +191,6 @@ public class OnSaleFragment extends Fragment { public void onResponse(Call call, retrofit2.Response response) { Result result = response.body(); - Log.e("error", result.msg); if(result != null && result.code == 200){ Toast.makeText(getActivity(), "删除成功", Toast.LENGTH_SHORT).show(); notifyDataSetChanged(); @@ -212,7 +212,12 @@ public class OnSaleFragment extends Fragment { int adapterPosition = holder.getAdapterPosition(); if(adapterPosition != RecyclerView.NO_POSITION){ CommodityListResult.RowsDTO rowsDTO = result.rows.get(adapterPosition); - + SharedPreferences sharedPreferences = getActivity().getSharedPreferences("commodity", Activity.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedPreferences.edit(); + editor.clear(); + editor.putInt("id", rowsDTO.id); + editor.commit(); + startActivity(new Intent(getActivity(), UpdateCommodityMainActivity.class)); } } }); diff --git a/src/main/java/com/example/coursedesign/UpdateCommodityMainActivity.java b/src/main/java/com/example/coursedesign/UpdateCommodityMainActivity.java new file mode 100644 index 0000000..02d4c14 --- /dev/null +++ b/src/main/java/com/example/coursedesign/UpdateCommodityMainActivity.java @@ -0,0 +1,127 @@ +package com.example.coursedesign; + +import android.app.Activity; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.AdapterView; +import android.widget.Button; +import android.widget.EditText; +import android.widget.Spinner; +import android.widget.TextView; +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.Callback; +import retrofit2.Retrofit; +import retrofit2.converter.gson.GsonConverterFactory; + +public class UpdateCommodityMainActivity extends AppCompatActivity { + + TextView back; + EditText nameET, priceET, descriptionET; + Spinner spinner; + Button addCommodityBtn; + + CommodityListResult.RowsDTO commodity = new CommodityListResult.RowsDTO(); + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + EdgeToEdge.enable(this); + setContentView(R.layout.activity_update_commodity_main); + 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; + }); + + back = findViewById(R.id.textView53); + nameET = findViewById(R.id.editTextText10); + priceET = findViewById(R.id.editTextNumber3); + descriptionET = findViewById(R.id.editTextText11); + spinner = findViewById(R.id.spinner5); + addCommodityBtn = findViewById(R.id.button13); + + SharedPreferences sharedPreferences = getSharedPreferences("commodity", Activity.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedPreferences.edit(); + commodity.id = sharedPreferences.getInt("id", 0); + + //设置item的被选择的监听 + spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + //当item被选择后调用此方法 + @Override + public void onItemSelected(AdapterView parent, View view, int position, long id) { + //获取我们所选中的内容 + commodity.classification = parent.getItemAtPosition(position).toString(); + } + //只有当patent中的资源没有时,调用此方法 + @Override + public void onNothingSelected(AdapterView parent) { + + } + }); + + //获取数据 + Retrofit retrofit = new Retrofit.Builder() + .baseUrl("http://10.138.63.204:8080/") + .addConverterFactory(GsonConverterFactory.create()) //返回结果用Gson解析 + .build(); + Api api = retrofit.create(Api.class); + + Call commodityListCall = api.getCommodityListById("Bearer ", commodity.id, null); + commodityListCall.enqueue(new Callback() { + + @Override + public void onResponse(Call call, retrofit2.Response response) { + CommodityListResult result = response.body(); + if(result.code == 200){ + commodity = result.rows.get(0); + nameET.setText(commodity.name); + priceET.setText(commodity.price.toString()); + descriptionET.setText(commodity.description); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + + } + }); + + addCommodityBtn.setOnClickListener(v -> { + commodity.name = nameET.getText().toString(); + commodity.price = Double.parseDouble(priceET.getText().toString()); + commodity.description = descriptionET.getText().toString(); + commodity.classification = spinner.getSelectedItem().toString(); + + Call updateCommodityCall = api.updateCommodity("Bearer ", commodity); + updateCommodityCall.enqueue(new Callback() { + + @Override + public void onResponse(Call call, retrofit2.Response response) { + Result result = response.body(); + if(result.code == 200){ + Toast.makeText(UpdateCommodityMainActivity.this, "修改商品信息成功", Toast.LENGTH_SHORT).show(); + finish(); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + Log.e("error", t.getMessage()); + } + }); + + }); + + back.setOnClickListener(v -> finish()); + } +} \ No newline at end of file diff --git a/src/main/res/layout/activity_add_comment.xml b/src/main/res/layout/activity_add_comment.xml new file mode 100644 index 0000000..479fc24 --- /dev/null +++ b/src/main/res/layout/activity_add_comment.xml @@ -0,0 +1,82 @@ + + + + + + + + + +