parent
12febe759f
commit
fd9a7289cc
@ -1,11 +1,24 @@
|
||||
package com.orangesale.cn.mapper;
|
||||
|
||||
import com.orangesale.cn.entity.OrangeShoppingCart;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author zhijun
|
||||
* @Date 2019/12/13
|
||||
*/
|
||||
@Mapper
|
||||
public interface ShoppingCartMapper {
|
||||
// 新增商品到购物车
|
||||
public int insertCart(OrangeShoppingCart cart);
|
||||
|
||||
// 购物车商品数量
|
||||
public int updateCart(OrangeShoppingCart cart);
|
||||
|
||||
// 查询购物车中所有商品
|
||||
public List<OrangeShoppingCart> selectAll();
|
||||
|
||||
public List<OrangeShoppingCart> selectByUserId(Integer UserId);
|
||||
}
|
||||
|
@ -1,12 +1,34 @@
|
||||
package com.orangesale.cn.service;
|
||||
|
||||
import com.orangesale.cn.entity.OrangeShoppingCart;
|
||||
import com.orangesale.cn.mapper.ShoppingCartMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author zhijun
|
||||
* @Date 2019/12/13
|
||||
*/
|
||||
@Service
|
||||
public class ShoppingCartService {
|
||||
|
||||
@Autowired
|
||||
private ShoppingCartMapper shoppingCartMapper;
|
||||
|
||||
public int insertCart(OrangeShoppingCart cart){
|
||||
return shoppingCartMapper.insertCart(cart);
|
||||
}
|
||||
|
||||
public int updateCart(OrangeShoppingCart cart){
|
||||
return shoppingCartMapper.updateCart(cart);
|
||||
}
|
||||
|
||||
public List<OrangeShoppingCart> selectAll(){
|
||||
return shoppingCartMapper.selectAll();
|
||||
}
|
||||
|
||||
public List<OrangeShoppingCart> selectByUserId(Integer userId){
|
||||
return shoppingCartMapper.selectByUserId(userId);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.orangesale.cn.mapper.ShoppingCartMapper">
|
||||
|
||||
<insert id="insertCart">
|
||||
insert into orange_shoppingcart values (null,#{userId},#{productId},#{num})
|
||||
</insert>
|
||||
|
||||
|
||||
<update id="updateCart">
|
||||
update orange_shoppingcart
|
||||
set id=#{id}
|
||||
</update>
|
||||
<select id="selectAll" resultType="com.orangesale.cn.entity.OrangeShoppingCart">
|
||||
select * from orange_shoppingcart
|
||||
</select>
|
||||
|
||||
|
||||
<select id="selectByUserId" resultType="com.orangesale.cn.entity.OrangeShoppingCart">
|
||||
select * from orange_shoppingcart where user_id = #{userId}
|
||||
</select>
|
||||
</mapper>
|
@ -0,0 +1,81 @@
|
||||
package com.example.test.adapter;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.BaseAdapter;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.example.test.R;
|
||||
import com.example.test.entity.cart;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class cartAdapter extends BaseAdapter {
|
||||
private List<Bitmap> bitmaps;
|
||||
private List<String> names;
|
||||
private List<Double> prices;
|
||||
private List<Integer> nums;
|
||||
private Context context;
|
||||
|
||||
public cartAdapter(List<Bitmap> bitmaps, List<String> names, List<Double> prices, List<Integer> nums, Context context) {
|
||||
this.bitmaps = bitmaps;
|
||||
this.names = names;
|
||||
this.prices = prices;
|
||||
this.nums = nums;
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return bitmaps.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getItem(int i) {
|
||||
List<Object> list = new ArrayList<>();
|
||||
list.add(bitmaps.get(i));
|
||||
list.add(names.get(i));
|
||||
list.add(prices.get(i));
|
||||
list.add(nums.get(i));
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getItemId(int i) {
|
||||
return i;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int i, View convertView, ViewGroup viewGroup) {
|
||||
viewHolder holder;
|
||||
if(convertView==null){
|
||||
holder = new viewHolder();
|
||||
convertView = LayoutInflater.from(context).inflate(R.layout.cart_listview_item,null);
|
||||
holder.ivImg = convertView.findViewById(R.id.cart_good_img);
|
||||
holder.tvName = convertView.findViewById(R.id.cart_good_name);
|
||||
holder.tvPrice = convertView.findViewById(R.id.cart_good_price);
|
||||
holder.tvNum = convertView.findViewById(R.id.cart_good_num);
|
||||
convertView.setTag(holder);
|
||||
}else {
|
||||
holder = (viewHolder) convertView.getTag();
|
||||
}
|
||||
|
||||
// holder.ivImg.setImageResource(imgs[position]);
|
||||
holder.ivImg.setImageBitmap(bitmaps.get(i));
|
||||
holder.tvName.setText(names.get(i));
|
||||
holder.tvPrice.setText(prices.get(i)+"");
|
||||
holder.tvNum.setText(nums.get(i)+"");
|
||||
return convertView;
|
||||
}
|
||||
static class viewHolder{
|
||||
ImageView ivImg;
|
||||
TextView tvName;
|
||||
TextView tvPrice;
|
||||
TextView tvNum;
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package com.example.test.entity;
|
||||
|
||||
public class cart {
|
||||
private Integer id;
|
||||
private Integer userId;
|
||||
private Integer productId;
|
||||
private Integer num;
|
||||
|
||||
public cart(Integer id, Integer userId, Integer productId, Integer num) {
|
||||
this.id = id;
|
||||
this.userId = userId;
|
||||
this.productId = productId;
|
||||
this.num = num;
|
||||
}
|
||||
|
||||
public cart() {
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Integer getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(Integer userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public Integer getProductId() {
|
||||
return productId;
|
||||
}
|
||||
|
||||
public void setProductId(Integer productId) {
|
||||
this.productId = productId;
|
||||
}
|
||||
|
||||
public Integer getNum() {
|
||||
return num;
|
||||
}
|
||||
|
||||
public void setNum(Integer num) {
|
||||
this.num = num;
|
||||
}
|
||||
}
|
@ -1,33 +1,108 @@
|
||||
package com.example.test.fragment;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.ListView;
|
||||
|
||||
import com.example.test.R;
|
||||
import com.example.test.activity.homeActivity;
|
||||
import com.example.test.adapter.cartAdapter;
|
||||
import com.example.test.entity.cart;
|
||||
import com.example.test.entity.product;
|
||||
import com.example.test.netrequest.cartOkHttp;
|
||||
import com.example.test.netrequest.productOkHttp;
|
||||
import com.example.test.utils.Address;
|
||||
|
||||
public class navigationCartFragment extends Fragment {
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class navigationCartFragment extends Fragment {
|
||||
private View view;
|
||||
private View cartView;
|
||||
private homeActivity activity;
|
||||
private static List<Bitmap> bitmaps = new ArrayList<>();
|
||||
private static List<String> names = new ArrayList<>();
|
||||
private static List<Double> prices = new ArrayList<>();
|
||||
private static List<Integer> nums = new ArrayList<>();
|
||||
//加载动画 有东西的购物车 空的购物车
|
||||
private int[] pages={R.layout.navigation_cart_loading,R.layout.navigation_cart, R.layout.navigation_cart_empty};
|
||||
private static int index=0;
|
||||
private ListView listView;
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
// Inflate the layout for this fragment
|
||||
View view = inflater.inflate(R.layout.navigation_cart, container, false);
|
||||
view = inflater.inflate(pages[index], container, false);
|
||||
cartView = inflater.inflate(pages[1],null);
|
||||
activity = (homeActivity) getActivity();
|
||||
new Thread(getCartItem).start();
|
||||
if(index==1){
|
||||
initListview();
|
||||
}else if(index==2){
|
||||
goShopping();
|
||||
}
|
||||
return view;
|
||||
}
|
||||
|
||||
private void goShopping() {
|
||||
Button button = view.findViewById(R.id.navigation_cart_shopping);
|
||||
button.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
homeActivity activity = (homeActivity)getActivity();
|
||||
activity.initFragment(new navigationGoodFragment());
|
||||
activity.changeFragment(new navigationGoodFragment());
|
||||
}
|
||||
});
|
||||
return view;
|
||||
}
|
||||
|
||||
public void initListview(){
|
||||
if(listView == null) {
|
||||
listView = (ListView) cartView.findViewById(R.id.navigation_cart_listview);
|
||||
}
|
||||
listView.setAdapter(new cartAdapter(bitmaps,names,prices,nums,activity));
|
||||
}
|
||||
|
||||
|
||||
Runnable getCartItem = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
cartOkHttp cartOkHttp1 = new cartOkHttp();
|
||||
productOkHttp productOkHttp1 = new productOkHttp();
|
||||
try {
|
||||
int userId = (Integer)activity.getBundle().get("id");
|
||||
List<cart> carts = cartOkHttp1.selectByUserId(userId);
|
||||
if(carts.size()==0){
|
||||
index = 2;
|
||||
activity.changeFragment(new navigationCartFragment());
|
||||
//跳转到购物车为空页面
|
||||
}else{
|
||||
// 将获取到的数据分别添加到集合中
|
||||
for (cart cart : carts) {
|
||||
product p = productOkHttp1.getProductById(cart.getProductId());
|
||||
names.add(p.getName());
|
||||
String url = Address.getImgIp(p.getImgUrl());
|
||||
Bitmap img = productOkHttp1.getImg(url);
|
||||
bitmaps.add(img);
|
||||
prices.add(p.getPrice());
|
||||
nums.add(cart.getNum());
|
||||
}
|
||||
index = 1;
|
||||
activity.changeFragment(new navigationCartFragment());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
package com.example.test.netrequest;
|
||||
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson.TypeReference;
|
||||
import com.example.test.entity.cart;
|
||||
import com.example.test.utils.Address;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import okhttp3.ConnectionPool;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class cartOkHttp {
|
||||
private OkHttpClient okHttpClient = new OkHttpClient();
|
||||
public List<cart> selectAll() throws IOException {
|
||||
Request request = new Request.Builder()
|
||||
.url("http://" + Address.ip + ":8081/orange/shoppingCart/search")
|
||||
.build();
|
||||
Response response = okHttpClient.newCall(request).execute();
|
||||
JSONObject jsonObject = JSON.parseObject(response.body().string());
|
||||
String data = jsonObject.getString("data");
|
||||
List<cart> carts = JSON.parseObject(data, new TypeReference<List<cart>>() {
|
||||
});
|
||||
return carts;
|
||||
}
|
||||
|
||||
public List<cart> selectByUserId(int userId) throws IOException {
|
||||
RequestBody requestBody = RequestBody.create(userId+"",MediaType.parse("application/json;charset=UTF-8"));
|
||||
Request request = new Request.Builder()
|
||||
.url("http://" + Address.ip + ":8081/orange/shoppingCart/searchByUserId")
|
||||
.post(requestBody)
|
||||
.build();
|
||||
Response response = okHttpClient.newCall(request).execute();
|
||||
JSONObject jsonObject = JSON.parseObject(response.body().string());
|
||||
String data = jsonObject.getString("data");
|
||||
List<cart> carts = JSON.parseObject(data, new TypeReference<List<cart>>() {
|
||||
});
|
||||
return carts;
|
||||
}
|
||||
|
||||
public boolean insertCart(cart c) throws IOException {
|
||||
String data = JSONObject.toJSONString(c);
|
||||
RequestBody requestBody = RequestBody.create(data,MediaType.parse("application/json;charset=UTF-8"));
|
||||
Request request = new Request.Builder()
|
||||
.url("http://"+Address.ip+":8081/orange/shoppingCart/insert")
|
||||
.post(requestBody)
|
||||
.build();
|
||||
Response response = okHttpClient.newCall(request).execute();
|
||||
JSONObject jsonObject = JSON.parseObject(response.body().string());
|
||||
return jsonObject.getBoolean("flag");
|
||||
}
|
||||
|
||||
public boolean updateCart(cart c) throws IOException {
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
String data = JSONObject.toJSONString(c);
|
||||
RequestBody requestBody = RequestBody.create(data,MediaType.parse("application/json;charset=UTF-8"));
|
||||
Request request = new Request.Builder()
|
||||
.url("http://"+Address.ip+":8081/orange/shoppingCart/update")
|
||||
.post(requestBody)
|
||||
.build();
|
||||
Response response = client.newCall(request).execute();
|
||||
JSONObject jsonObject = JSON.parseObject(response.body().string());
|
||||
return jsonObject.getBoolean("flag");
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.example.test.utils;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
public class Address {
|
||||
public static String ip = "192.168.64.1";
|
||||
public static String getImgIp(String url){
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String[] result = url.split(" ");
|
||||
for (int i = 0; i < result.length; i++) {
|
||||
if(i==1){
|
||||
sb.append(ip).append(result[i]);
|
||||
}else{
|
||||
sb.append(result[i]);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
After Width: | Height: | Size: 167 B |
After Width: | Height: | Size: 161 B |
Before Width: | Height: | Size: 891 B After Width: | Height: | Size: 775 B |
@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".utils.address">
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
@ -0,0 +1,53 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="@color/white">
|
||||
<ImageView
|
||||
android:id="@+id/cart_good_img"
|
||||
android:layout_width="100dp"
|
||||
android:layout_height="120dp"/>
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="120dp"
|
||||
android:orientation="vertical">
|
||||
<TextView
|
||||
android:id="@+id/cart_good_name"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="50dp"
|
||||
android:textSize="30dp"
|
||||
android:gravity="center"/>
|
||||
<TextView
|
||||
android:id="@+id/cart_good_price"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="30dp"
|
||||
android:textSize="20dp"
|
||||
android:gravity="center"/>
|
||||
<LinearLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="220dp">
|
||||
<ImageButton
|
||||
android:id="@+id/cart_good_add"
|
||||
android:layout_width="20dp"
|
||||
android:layout_height="20dp"
|
||||
android:src="@drawable/add"
|
||||
android:layout_gravity="center"
|
||||
android:background="@color/white"/>
|
||||
<TextView
|
||||
android:id="@+id/cart_good_num"
|
||||
android:layout_width="40dp"
|
||||
android:layout_height="40dp"
|
||||
android:textSize="25dp"
|
||||
android:gravity="center"/>
|
||||
<ImageButton
|
||||
android:id="@+id/cart_good_minus"
|
||||
android:layout_width="20dp"
|
||||
android:layout_height="20dp"
|
||||
android:layout_gravity="center"
|
||||
android:src="@drawable/reduce"
|
||||
android:background="@color/white"/>
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
@ -0,0 +1,55 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="40dp"
|
||||
android:background="@color/orange">
|
||||
<ImageView
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:layout_gravity="center"
|
||||
android:src="@drawable/arrow_right"
|
||||
android:rotation="180"/>
|
||||
<TextView
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="match_parent"
|
||||
android:text="购物车"
|
||||
android:layout_weight="10"
|
||||
android:gravity="center"
|
||||
android:textColor="@color/white"
|
||||
android:textSize="20dp"/>
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:orientation="vertical"
|
||||
android:paddingTop="150dp">
|
||||
<ImageView
|
||||
android:layout_width="100dp"
|
||||
android:layout_height="100dp"
|
||||
android:src="@drawable/cart"
|
||||
android:layout_marginBottom="10dp"
|
||||
android:layout_gravity="center"/>
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="购物车是空的~"
|
||||
android:textSize="20dp"
|
||||
android:layout_marginBottom="20dp"
|
||||
android:layout_gravity="center"/>
|
||||
<Button
|
||||
android:id="@+id/navigation_cart_shopping"
|
||||
android:layout_width="150dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="去逛逛"
|
||||
android:textSize="20dp"
|
||||
android:background="@color/orange"
|
||||
android:layout_gravity="center"/>
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
@ -0,0 +1,16 @@
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:paddingLeft="@dimen/activity_horizontal_margin"
|
||||
android:paddingRight="@dimen/activity_horizontal_margin"
|
||||
android:paddingTop="@dimen/activity_vertical_margin"
|
||||
android:paddingBottom="@dimen/activity_vertical_margin"
|
||||
tools:context=".MainActivity"
|
||||
android:background="@color/white">
|
||||
|
||||
<com.mingle.widget.LoadingView
|
||||
android:id="@+id/loadView"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent"/>
|
||||
</RelativeLayout>
|
Loading…
Reference in new issue