1. 程式人生 > >oto餐飲app實時推送訂單給店家程式詳細實現(二)

oto餐飲app實時推送訂單給店家程式詳細實現(二)

文接"oto餐飲app實時訂單追蹤系統架構設計"一文。

    使用者生成一個外賣訂單後,可以以發簡訊的方式告知店家有一個訂單,但是簡訊方式的缺點是非常明顯的,如果想讓使用者和網站知道店家接受訂單和其他的操作就需要網站人工干預,這在智慧手機沒有出來之前,那些外賣網站這樣運營是可以理解的,但是智慧手機都出來這麼多年了,現在的有些外賣網站還是這麼運營我是難以理解。上次我用“餓了麼”下一個外賣訂單,訂單下了後,餓了麼客服立馬打電話過來確認訂單,我不知道是否確認後他們客服再打電話給店家通知訂單。我對這樣的體驗也是非常不滿意的,不滿意這樣的流程而不是服務。

      有點扯遠了。

     5.2.1 使用者app提交訂單業務邏輯層:

  

				order.setHowmuch(total);
				order.setMenu(menuList);
				order.setOrderType(0x001);  //代表的是外賣訂單
				order.setPayType(0x002);    //支付寶網上支付	
				order.setUser_id(1/*Integer.parseInt(mApplication.getmUser().getId())*/);
				//把訂單傳送出去
				OrderServer orderServer = new OrderServer();
				orderServer.dealOrder(order,new AsyncHttpResponseHandler(){
					 @Override
					 public void onSuccess(String response) {
					    	if(!response.equals("0"))
					    	{
					    	    Log.i("TakeawayCommitActivity","提交訂單成功 " + response);
					    	    startActivity(new Intent(TakeawayCommitActivity.this,PaySuccessActivity.class));
					    	    finish();
					    	}
					 }
					@Override
					public void onFailure(Throwable error, String content) {

					}
				});

網路通訊層

package com.cmyy.httpServer;
import com.cmyy.model.Order;
import com.cmyy.util.HttpUtil;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.ResponseHandlerInterface;

//訂單表操作
public class OrderServer {
	//生成訂單
	public void dealOrder(Order order,AsyncHttpResponseHandler responseHandler)
	{		 
		Gson gSon = new Gson();
		String json = gSon.toJson(order);
		String postUrl=HttpUtil.ORDER_ACTION; 
		AsyncHttpClient orderRequest = new AsyncHttpClient();
		//orderRequest.post(postUrl,responseHandler);		
		RequestParams prarms = new RequestParams();
	    prarms.put("req", json);
	    orderRequest.post(postUrl, prarms, responseHandler);
	}
}

通訊協議模型

訂單

package com.cmyy.model;

import java.util.ArrayList;


//訂單列表
public class Order {
    
    private double  howmuch;      //消費金額
    private ArrayList<CommitMenu> menu;     //消費的菜品
    private Bussiness bussiness;//商家
    private String checkCode;   //驗證碼
    private int    orderType;   //訂單型別  訂單分為3中型別:1.外賣  2.預訂  3.團定
    private int    payType;     //付款型別  付款型別分為:1.支付寶  2.網銀  3.飯到付款
    private int    user_id;    //使用者id
    
	public int getUser_id() {
		return user_id;
	}
	public void setUser_id(int user_id) {
		this.user_id = user_id;
	}
	public int getOrderType() {
		return orderType;
	}
	public void setOrderType(int orderType) {
		this.orderType = orderType;
	}
	public int getPayType() {
		return payType;
	}
	public void setPayType(int payType) {
		this.payType = payType;
	}
	public Bussiness getBussiness() {
		return bussiness;
	}
	public void setBussiness(Bussiness bussiness) {
		this.bussiness = bussiness;
	}
	public String getCheckCode() {
		return checkCode;
	}
	public void setCheckCode(String checkCode) {
		this.checkCode = checkCode;
	}
	public double getHowmuch() {
		return howmuch;
	}
	public void setHowmuch(double howmuch) {
		this.howmuch = howmuch;
	}
	public ArrayList<CommitMenu> getMenu() {
		return menu;
	}
	public void setMenu(ArrayList<CommitMenu> menu) {
		this.menu = menu;
	}
    
}
選單
package com.cmyy.model;

import java.io.Serializable;

//餐單列表
public class Menu implements Serializable{
	     private static final long serialVersionUID = 1L;
	     // Fields

		private Integer id;
		private String caiName;
		private String decsrible;
		private String imagesPath;
		private Integer commentCount;
		private Integer bussinessId;
		private Integer count;
		private Integer categorizeId;
		private Integer price;
		private String imagesPath3;
		private String imagesPath5;
		private String imagesPath4;
		private String imagesPath2;
		private String imagesPath1;
		private String categorize;

		// Constructors

		public String getCategorize() {
			return categorize;
		}

		public void setCategorize(String categorize) {
			this.categorize = categorize;
		}

		/** default constructor */
		public Menu() {
		}

		/** minimal constructor */
		public Menu(Integer id) {
			this.id = id;
		}

		/** full constructor */
		public Menu(Integer id, String caiName, String decsrible,
				String imagesPath, Integer commentCount, Integer bussinessId,
				Integer count, Integer categorizeId, Integer price,
				String imagesPath3, String imagesPath5, String imagesPath4,
				String imagesPath2, String imagesPath1) {
			this.id = id;
			this.caiName = caiName;
			this.decsrible = decsrible;
			this.imagesPath = imagesPath;
			this.commentCount = commentCount;
			this.bussinessId = bussinessId;
			this.count = count;
			this.categorizeId = categorizeId;
			this.price = price;
			this.imagesPath3 = imagesPath3;
			this.imagesPath5 = imagesPath5;
			this.imagesPath4 = imagesPath4;
			this.imagesPath2 = imagesPath2;
			this.imagesPath1 = imagesPath1;
		}

		// Property accessors

		public Integer getId() {
			return this.id;
		}

		public void setId(Integer id) {
			this.id = id;
		}

		public String getCaiName() {
			return this.caiName;
		}

		public void setCaiName(String caiName) {
			this.caiName = caiName;
		}

		public String getDecsrible() {
			return this.decsrible;
		}

		public void setDecsrible(String decsrible) {
			this.decsrible = decsrible;
		}

		public String getImagesPath() {
			return this.imagesPath;
		}

		public void setImagesPath(String imagesPath) {
			this.imagesPath = imagesPath;
		}

		public Integer getCommentCount() {
			return this.commentCount;
		}

		public void setCommentCount(Integer commentCount) {
			this.commentCount = commentCount;
		}

		public Integer getBussinessId() {
			return this.bussinessId;
		}

		public void setBussinessId(Integer bussinessId) {
			this.bussinessId = bussinessId;
		}

		public Integer getCount() {
			return this.count;
		}

		public void setCount(Integer count) {
			this.count = count;
		}

		public Integer getCategorizeId() {
			return this.categorizeId;
		}

		public void setCategorizeId(Integer categorizeId) {
			this.categorizeId = categorizeId;
		}

		public Integer getPrice() {
			return this.price;
		}

		public void setPrice(Integer price) {
			this.price = price;
		}

		public String getImagesPath3() {
			return this.imagesPath3;
		}

		public void setImagesPath3(String imagesPath3) {
			this.imagesPath3 = imagesPath3;
		}

		public String getImagesPath5() {
			return this.imagesPath5;
		}

		public void setImagesPath5(String imagesPath5) {
			this.imagesPath5 = imagesPath5;
		}

		public String getImagesPath4() {
			return this.imagesPath4;
		}

		public void setImagesPath4(String imagesPath4) {
			this.imagesPath4 = imagesPath4;
		}

		public String getImagesPath2() {
			return this.imagesPath2;
		}

		public void setImagesPath2(String imagesPath2) {
			this.imagesPath2 = imagesPath2;
		}

		public String getImagesPath1() {
			return this.imagesPath1;
		}

		public void setImagesPath1(String imagesPath1) {
			this.imagesPath1 = imagesPath1;
		}
}

  

5.2.2 web服務端接收訂單

       web服務端接收訂單,生成訂單序列號,寫入資料庫儲存,並將該訂單必要資訊轉發給店家app

       web 伺服器採用j2ee ssh框架。本來電子商務類網站用php應該好一點吧。

package cmyy.web.action.user;

import java.io.IOException;

import org.apache.struts2.ServletActionContext;
import org.ddpush.im.util.StringUtil;
import org.ddpush.im.v1.client.appserver.Pusher;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import cmyy.domain.bussiness.BussinessOrder;
import cmyy.domain.bussiness.Order;
import cmyy.utils.MD5Utils;
import cmyy.web.action.base.BaseAction;
import javax.servlet.http.HttpServletRequest;
//訂單提交
public class OrderAction extends BaseAction{
	private HttpServletRequest req ;
	public void orderSubmit()
	{
		req = ServletActionContext.getRequest();
        Gson gSon = new Gson();
        Order order = gSon.fromJson(req.getParameter("req"), new TypeToken<Order>(){}.getType());
        //轉發給追蹤伺服器去推送給相應的店家
        byte[] bussinessId = null;
		try {
			bussinessId = StringUtil.md5Byte(String.valueOf(order.getBussiness().getId()));
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}  //店家id
        Pusher pusher = null;
        BussinessOrder bussinessOrder = new BussinessOrder();
        bussinessOrder.setOrder_uuid("dajldahgiahlajl");
 
		try {
			//推送伺服器ip地址可以放到配置檔案中,這裡暫時寫定值
			pusher = new Pusher("127.0.0.1",9999, 1000*5);
			boolean result = pusher.push0x20Message(bussinessId,gSon.toJson(bussinessOrder).getBytes()); 
	        if(result){  
	            System.out.println("通用資訊傳送成功");  
	        }else{  
	            System.out.println("傳送失敗!格式有誤");  
	        } 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
		
        //生成訂單號資訊
        try {
			outPrint(req.getParameter("req"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
	}
}

5.2.3 推送伺服器接收訂單並轉發

5.2.4 店家app接收訂單