直播源码中的电商商城功能实现的具体步骤
如今直播带货平台大热,搭建直播带货平台成为许多公司的开发重点。既然是直播带货平台,就意味着系统和带货相关,电商商城就是搭建直播带货平台必不可少的一部分。电商商城的实现,不仅可以为平台带去收益,也能满足用户引流变现的需求,接下来我们就看看,电商商城具体是怎么实现的。
一、商城功能介绍
既然想要搭建直播带货平台的商城功能,那么我们需要先了解电商商城有哪些基本功能。
我们可以通过首页下方的功能栏,打开商城页面。在商城中,各种商品通过列表的方式进行展示,点击商品可以查看详情,也可以下单购买商品。下单付款后,订单进入待发货状态,商家收到下单消息后可自行安排发货。发货后商品进入待收货状态,用户收到商品后,可以评价商品或发起退货。
用户可通过注册成为商家,商家进行商品的发布和管理,售出商品后可以将账户中的金额提现。商家发布商品时,可以设置商品的视频图片,标题,简介,规格等内容,也可进行修改和下架。
二、实现电商商城
既然我们知道了大体功能,就可以搭建直播带货平台的商城了。
部分代码如下:
public class MainMallViewHolder extends AbsMainViewHolder implements OnItemClickListener<GoodsSimpleBean>, View.OnClickListener { private CommonRefreshView mRefreshView; private MainMallAdapter mAdapter; private Banner mBanner; private View mBannerWrap; private boolean mBannerNeedUpdate; private List<BannerBean> mBannerList; private List<GoodsHomeClassBean> mClassList; private RecyclerView mRecyclerViewClass; private boolean mClassShowed; private View mScrollIndicator; private int mDp25; public MainMallViewHolder(Context context, ViewGroup parentView) { super(context, parentView); } @Override protected int getLayoutId() { return R.layout.view_main_mall; } @Override public void init() { setStatusHeight(); findViewById(R.id.btn_search).setOnClickListener(this); mRefreshView = findViewById(R.id.refreshView); mRefreshView.setEmptyLayoutId(R.layout.view_no_data_main_mall); GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 2, GridLayoutManager.VERTICAL, false); gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { @Override public int getSpanSize(int position) { if (position == 0) { return 2; } return 1; } }); mRefreshView.setLayoutManager(gridLayoutManager); ItemDecoration decoration = new ItemDecoration(mContext, 0x00000000, 10, 0); decoration.setOnlySetItemOffsetsButNoDraw(true); mRefreshView.setItemDecoration(decoration); mAdapter = new MainMallAdapter(mContext); mAdapter.setOnItemClickListener(this); mRefreshView.setRecyclerViewAdapter(mAdapter); mRefreshView.setDataHelper(new CommonRefreshView.DataHelper<GoodsSimpleBean>() { @Override public RefreshAdapter<GoodsSimpleBean> getAdapter() { return null; } @Override public void loadData(int p, HttpCallback callback) { MainHttpUtil.getHomeGoodsList(p, callback); } @Override public List<GoodsSimpleBean> processData(String[] info) { JSONObject obj = JSON.parseObject(info[0]); mBannerNeedUpdate = false; List<BannerBean> bannerList = JSON.parseArray(obj.getString("slide"), BannerBean.class); if (bannerList != null && bannerList.size() > 0) { if (mBannerList == null || mBannerList.size() != bannerList.size()) { mBannerNeedUpdate = true; } else { for (int i = 0; i < mBannerList.size(); i++) { BannerBean bean = mBannerList.get(i); if (bean == null || !bean.isEqual(bannerList.get(i))) { mBannerNeedUpdate = true; break; } } } } mBannerList = bannerList; mClassList = JSON.parseArray(obj.getString("shoptwoclass"), GoodsHomeClassBean.class); return JSON.parseArray(obj.getString("list"), GoodsSimpleBean.class); } @Override public void onRefreshSuccess(List<GoodsSimpleBean> list, int listCount) { showBanner(); showClass(); } @Override public void onRefreshFailure() { } @Override public void onLoadMoreSuccess(List<GoodsSimpleBean> loadItemList, int loadItemCount) { } @Override public void onLoadMoreFailure() { } }); View headView = mAdapter.getHeadView(); mScrollIndicator = headView.findViewById(R.id.scroll_indicator); mDp25 = DpUtil.dp2px(25); mBannerWrap = headView.findViewById(R.id.banner_wrap); mBanner = (Banner) headView.findViewById(R.id.banner); mBanner.setImageLoader(new ImageLoader() { @Override public void displayImage(Context context, Object path, ImageView imageView) { ImgLoader.display(mContext, ((BannerBean) path).getImageUrl(), imageView); } }); mBanner.setOnBannerListener(new OnBannerListener() { @Override public void OnBannerClick(int p) { if (mBannerList != null) { if (p >= 0 && p < mBannerList.size()) { BannerBean bean = mBannerList.get(p); if (bean != null) { String link = bean.getLink(); if (!TextUtils.isEmpty(link)) { WebViewActivity.forward(mContext, link, false); } } } } } }); mRecyclerViewClass = headView.findViewById(R.id.recyclerView_class); mRecyclerViewClass.setOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); } @Override public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { if (mScrollIndicator != null) { mScrollIndicator.setTranslationX(mDp25 * computeScrollPercent()); } } }); } /** * 计算滑动的百分比 */ private float computeScrollPercent() { if (mRecyclerViewClass != null) { int extent = mRecyclerViewClass.computeHorizontalScrollExtent(); int range = mRecyclerViewClass.computeHorizontalScrollRange(); float offset = mRecyclerViewClass.computeHorizontalScrollOffset(); //已经滚动的百分比 0~1 float percent = offset / (range - extent); if (percent > 1) { percent = 1; } return percent; } return 0; } private void showBanner() { if (mBanner == null || mBannerWrap == null) { return; } if (mBannerList == null || mBannerList.size() == 0) { mBannerWrap.setVisibility(View.GONE); return; } if (mBannerNeedUpdate) { mBanner.update(mBannerList); } } private void showClass() { if (mRecyclerViewClass == null) { return; } if (mClassList == null || mClassList.size() == 0) { mRecyclerViewClass.setVisibility(View.GONE); return; } if (mClassShowed) { return; } mClassShowed = true; int size = mClassList.size(); if (size <= 12) { mRecyclerViewClass.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false)); } else { mRecyclerViewClass.setLayoutManager(new GridLayoutManager(mContext, 2, GridLayoutManager.HORIZONTAL, false)); } MainMallClassAdapter adapter = new MainMallClassAdapter(mContext, mClassList); mRecyclerViewClass.setAdapter(adapter); } @Override public void onItemClick(GoodsSimpleBean bean, int position) { GoodsDetailActivity.forward(mContext, bean.getId(), false, bean.getType()); } @Override public void loadData() { if (isFirstLoadData() && mRefreshView != null) { mRefreshView.initData(); } } @Override public void onDestroy() { MainHttpUtil.cancel(MainHttpConsts.GET_HOME_GOODS_LIST); super.onDestroy(); } @Override public void onClick(View v) { if(!((AbsActivity)mContext).checkLogin()){ return; } int id = v.getId(); if (id == R.id.btn_search) { MallSearchActivity.forward(mContext); } } }
云豹就是使用这段代码,在搭建直播带货系统时,实现商城的部分功能。本文到这里就结束了,有想了解如何搭建直播带货平台的,欢迎和我讨论。
声明:以上内容为云豹科技原创,未经作者本人同意,禁止转载,否则将追究相关法律责任。