Explorar el Código

Merge remote-tracking branch 'origin/master'

raojiaolong@163.com hace 2 años
padre
commit
b643e66627

+ 2 - 0
mhotel/src/com/happy/Model/weixin/WeiXinUtil.java

@@ -4,7 +4,9 @@ public class WeiXinUtil {
 
     // 诚欣公众号
     public static String appid_c = "wxa5cc2178ff34bc8e";
+    public static String ip = "171.34.223.11";
     public static String screct_c = "7e6b036298b718da6d1afe4ae5364a1f";
+    public static String key = "密钥设置";
     // 诚欣测试号
     /*public static String appid_c = "wx5fcbd4d313aa4c15";
     public static String screct_c = "a72db0c99cde741eb391af69067aacfa";*/

+ 1 - 1
mhotel/src/com/happy/Until/Enum/OrderEnum.java

@@ -1,5 +1,5 @@
 package com.happy.Until.Enum;
 
 public enum OrderEnum {
-    间, 晚,房间已满,有空房;
+    间, 晚,房间已满,有空房,微信支付;
 }

+ 39 - 30
mhotel/src/com/happy/Until/ResponseUtil.java

@@ -1,5 +1,7 @@
 package com.happy.Until;
 
+import com.happy.Until.Enum.B;
+import com.happy.constant.ResultStatusCode;
 import com.happy.dto.IPage;
 import net.sf.json.JSONObject;
 import org.apache.poi.ss.usermodel.Workbook;
@@ -33,25 +35,29 @@ public class ResponseUtil {
 		respone.setContentType("application/json;charset=utf-8");
 
 		JSONObject resultJson = new JSONObject();
+		JSONObject dataJson = new JSONObject();
+		resultJson.put(B.CODE, ResultStatusCode.OK.getMsg());
+		resultJson.put(B.DATA, dataJson);
+
 		if (listPage == null || listPage.size() == 0) {
-			resultJson.put("rows", "");
-			resultJson.put("total", 0);
+			dataJson.put("rows", "");
+			dataJson.put("total", 0);
 		} else {
-			resultJson.put("code", 200);
-			resultJson.put("rows", rows);
+			dataJson.put("code", 200);
+			dataJson.put("rows", rows);
 			// int total = listAll.size();
-			resultJson.put("total", total);// 总记录数
+			dataJson.put("total", total);// 总记录数
 			int totalPage = total % rows == 0 ? (total / rows) : (total / rows) + 1;// 总页数
-			resultJson.put("totalPage", totalPage);
-			resultJson.put("currentPage", page);// 当前页
-			resultJson.put("numPerPage", rows);// 每页数
-			resultJson.put("nextPage", totalPage - page == 0 ? page : page + 1);// 下一页
-			resultJson.put("previousPage", page - 0 == 1 ? page : page - 1);// 上一页
-			resultJson.put("hasPreviousPage", true);// 有上一页
-			resultJson.put("hasNextPage", true);// 有下一页
-			resultJson.put("firstPage", true);// 首页
-			resultJson.put("lastPage", true);// 尾页
-			resultJson.put("listPage", listPage);
+			dataJson.put("totalPage", totalPage);
+			dataJson.put("currentPage", page);// 当前页
+			dataJson.put("numPerPage", rows);// 每页数
+			dataJson.put("nextPage", totalPage - page == 0 ? page : page + 1);// 下一页
+			dataJson.put("previousPage", page - 0 == 1 ? page : page - 1);// 上一页
+			dataJson.put("hasPreviousPage", true);// 有上一页
+			dataJson.put("hasNextPage", true);// 有下一页
+			dataJson.put("firstPage", true);// 首页
+			dataJson.put("lastPage", true);// 尾页
+			dataJson.put("listPage", listPage);
 
 		}
 
@@ -70,25 +76,28 @@ public class ResponseUtil {
 		respone.setContentType("application/json;charset=utf-8");
 
 		JSONObject resultJson = new JSONObject();
+		JSONObject dataJson = new JSONObject();
+		resultJson.put(B.CODE, ResultStatusCode.OK.getStatus());
+		resultJson.put(B.DATA, dataJson);
 		if (iPage.getPageList() == null || iPage.getPageList().size() == 0) {
-			resultJson.put("rows", "");
-			resultJson.put("total", 0);
+			dataJson.put("rows", "");
+			dataJson.put("total", 0);
 		} else {
-			resultJson.put("code", 200);
-			resultJson.put("rows", iPage.getRows());
+			dataJson.put("code", 200);
+			dataJson.put("rows", iPage.getRows());
 			// int total = listAll.size();
-			resultJson.put("total", iPage.getTotal());// 总记录数
+			dataJson.put("total", iPage.getTotal());// 总记录数
 			int totalPage = iPage.getTotal() % iPage.getRows() == 0 ? (iPage.getTotal() / iPage.getRows()) : (iPage.getTotal() / iPage.getRows()) + 1;// 总页数
-			resultJson.put("totalPage", totalPage);
-			resultJson.put("currentPage", iPage.getPage());// 当前页
-			resultJson.put("numPerPage", iPage.getRows());// 每页数
-			resultJson.put("nextPage", totalPage - iPage.getPage() == 0 ? iPage.getPage() : iPage.getPage() + 1);// 下一页
-			resultJson.put("previousPage", iPage.getPage() - 0 == 1 ? iPage.getPage() : iPage.getPage() - 1);// 上一页
-			resultJson.put("hasPreviousPage", true);// 有上一页
-			resultJson.put("hasNextPage", true);// 有下一页
-			resultJson.put("firstPage", true);// 首页
-			resultJson.put("lastPage", true);// 尾页
-			resultJson.put("pageList",iPage.getPageList()); // 分页数据
+			dataJson.put("totalPage", totalPage);
+			dataJson.put("currentPage", iPage.getPage());// 当前页
+			dataJson.put("numPerPage", iPage.getRows());// 每页数
+			dataJson.put("nextPage", totalPage - iPage.getPage() == 0 ? iPage.getPage() : iPage.getPage() + 1);// 下一页
+			dataJson.put("previousPage", iPage.getPage() - 0 == 1 ? iPage.getPage() : iPage.getPage() - 1);// 上一页
+			dataJson.put("hasPreviousPage", true);// 有上一页
+			dataJson.put("hasNextPage", true);// 有下一页
+			dataJson.put("firstPage", true);// 首页
+			dataJson.put("lastPage", true);// 尾页
+			dataJson.put("pageList",iPage.getPageList()); // 分页数据
 		}
 
 		PrintWriter out;

+ 242 - 72
mhotel/src/com/happy/action/AppBookingAction.java

@@ -1,15 +1,23 @@
 package com.happy.action;
 
 import com.happy.Model.Booking;
+import com.happy.Model.weixin.PayResult;
 import com.happy.Model.weixin.Users;
+import com.happy.Model.weixin.WechatUnifiedOrder;
+import com.happy.Model.weixin.WeiXinUtil;
 import com.happy.Until.*;
 import com.happy.Until.Enum.B;
 import com.happy.Until.Enum.OrderEnum;
-import com.happy.common.http.Message;
+import com.happy.Until.Enum.PayEnum;
+import com.happy.Until.weixin.EndPay;
+import com.happy.Until.weixin.JaxbUtil;
+import com.happy.Until.weixin.PayWxUtil;
+import com.happy.Until.weixin.TongYiReturn;
+import com.happy.common.wx.WxUtil;
 import com.happy.constant.ResultStatusCode;
 import com.happy.service.AppBookService;
 import com.happy.service.BookService;
-import com.happy.service.HotelService;
+import com.happy.service.UserService;
 import com.opensymphony.xwork2.ActionSupport;
 import net.sf.json.JSONObject;
 import org.apache.struts2.ServletActionContext;
@@ -18,6 +26,11 @@ import org.apache.struts2.interceptor.ServletRequestAware;
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import java.io.BufferedReader;
+import java.io.PrintWriter;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * 订单进去Action请求交互
@@ -30,6 +43,8 @@ public class AppBookingAction extends ActionSupport implements ServletRequestAwa
     @Resource
     public BookService bookService;
     @Resource
+    public UserService userService;
+    @Resource
     public AppBookService appBookService;
 
     public int page; // 当前页
@@ -53,7 +68,8 @@ public class AppBookingAction extends ActionSupport implements ServletRequestAwa
         this.bookingId = bookingId;
     }
 
-    public String bookingId; // 订单Id
+    private String bookingId; // 订单Id
+    private String open_id; // 用户open_Id
 
     public HttpServletRequest getRequest() {
         return request;
@@ -119,81 +135,78 @@ public class AppBookingAction extends ActionSupport implements ServletRequestAwa
      * userId; // 下单人id
      * 用户点击订房操作,生成订单
      */
-    public String createOrder()
-    {
+    public String createOrder() {
         // 插入订单数据到表中
         JSONObject jsonObject = new JSONObject();
-        if (Func.checkNull(houseId) ||Func.checkNull(startTime) ||Func.checkNull(endTime)
-                ||Func.checkNull(houseOrderNumber) ||Func.checkNull(userName) ||Func.checkNull(userPhone) ||Func.checkNull(userId))
-        {
+        if (Func.checkNull(houseId) || Func.checkNull(startTime) || Func.checkNull(endTime)
+                || Func.checkNull(houseOrderNumber) || Func.checkNull(userName) || Func.checkNull(userPhone) || Func.checkNull(userId)) {
             jsonObject.put(B.CODE, ResultStatusCode.BAD_REQUEST.getStatus());
-            jsonObject.put(B.MESSAGE,ResultStatusCode.BAD_REQUEST.getMsg());
-            ResponseUtil.writeJson(ServletActionContext.getResponse(),jsonObject.toString());
+            jsonObject.put(B.MESSAGE, ResultStatusCode.BAD_REQUEST.getMsg());
+            ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
             return null;
         }
 
         // 将时间统一转化为 yyyy/MM/dd
-        String startDate = DateUtil.parseDateToStr((Func.parseDate(startTime)),DateUtil.Time_Formatter_Day);
-        String endDate = DateUtil.parseDateToStr((Func.parseDate(endTime)),DateUtil.Time_Formatter_Day);
+        String startDate = DateUtil.parseDateToStr((Func.parseDate(startTime)), DateUtil.Time_Formatter_Day);
+        String endDate = DateUtil.parseDateToStr((Func.parseDate(endTime)), DateUtil.Time_Formatter_Day);
 
         // 创建订单之前,先验证是否有房间
         String backMessage = appBookService.varificationHouse(houseId, startDate, endDate);
-        if(Func.checkNull(backMessage))
-        {
+        if (Func.checkNull(backMessage)) {
             jsonObject.put(B.CODE, ResultStatusCode.CANNTBOOKING.getStatus());
-            jsonObject.put(B.MESSAGE,backMessage);
-            ResponseUtil.writeJson(ServletActionContext.getResponse(),jsonObject.toString());
+            jsonObject.put(B.MESSAGE, backMessage);
+            ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
             return null;
         }
 
-        String mess = appBookService.createOrder(houseId, startDate, endDate, houseOrderNumber,userName,userPhone, userId);
-        if (!Func.checkNull(mess))
-        {
+        String mess = appBookService.createOrder(houseId, startDate, endDate, houseOrderNumber, userName, userPhone, userId);
+        if (!Func.checkNull(mess)) {
             jsonObject.put(B.CODE, ResultStatusCode.BAD_REQUEST.getStatus());
-            jsonObject.put(B.MESSAGE,mess);
-            ResponseUtil.writeJson(ServletActionContext.getResponse(),jsonObject.toString());
+            jsonObject.put(B.MESSAGE, mess);
+            ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
             return null;
         }
 
         jsonObject.put(B.CODE, ResultStatusCode.OK.getStatus());
-        ResponseUtil.writeJson(ServletActionContext.getResponse(),jsonObject.toString());
+        jsonObject.put(B.MESSAGE, ResultStatusCode.OK.getMsg());
+        ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
         return null;
     }
 
     /**
      * 再次预订
+     *
      * @return
      */
-    public String reCreateOrder()
-    {
+    public String reCreateOrder() {
         // 插入订单数据到表中
         JSONObject jsonObject = new JSONObject();
-        if (Func.checkNull(houseId) ||Func.checkNull(startTime) ||Func.checkNull(endTime)
-                ||Func.checkNull(houseOrderNumber) ||Func.checkNull(userName) ||Func.checkNull(userPhone) ||Func.checkNull(userId))
-        {
+        if (Func.checkNull(houseId) || Func.checkNull(startTime) || Func.checkNull(endTime)
+                || Func.checkNull(houseOrderNumber) || Func.checkNull(userName) || Func.checkNull(userPhone) || Func.checkNull(userId)) {
             jsonObject.put(B.CODE, ResultStatusCode.BAD_REQUEST.getStatus());
-            jsonObject.put(B.MESSAGE,ResultStatusCode.BAD_REQUEST.getMsg());
-            ResponseUtil.writeJson(ServletActionContext.getResponse(),jsonObject.toString());
+            jsonObject.put(B.MESSAGE, ResultStatusCode.BAD_REQUEST.getMsg());
+            ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
             return null;
         }
 
         // 将时间统一转化为 yyyy/MM/dd
-        String startDate = DateUtil.parseDateToStr((Func.parseDate(startTime)),DateUtil.Time_Formatter_Day);
-        String endDate = DateUtil.parseDateToStr((Func.parseDate(endTime)),DateUtil.Time_Formatter_Day);
+        String startDate = DateUtil.parseDateToStr((Func.parseDate(startTime)), DateUtil.Time_Formatter_Day);
+        String endDate = DateUtil.parseDateToStr((Func.parseDate(endTime)), DateUtil.Time_Formatter_Day);
 
         // 创建订单之前,先验证是否有房间
         String backMessage = appBookService.varificationHouse(houseId, startDate, endDate);
-        if(Func.checkNull(backMessage))
-        {
+        if (Func.checkNull(backMessage)) {
             jsonObject.put(B.CODE, ResultStatusCode.CANNTBOOKING.getStatus());
-            jsonObject.put(B.MESSAGE,backMessage);
-            ResponseUtil.writeJson(ServletActionContext.getResponse(),jsonObject.toString());
+            jsonObject.put(B.MESSAGE, backMessage);
+            ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
             return null;
         }
 
-        appBookService.reCreateOrder(houseId, startTime, endTime, houseOrderNumber,userName,userPhone, userId);
+        appBookService.reCreateOrder(houseId, startTime, endTime, houseOrderNumber, userName, userPhone, userId);
+
         jsonObject.put(B.CODE, ResultStatusCode.OK.getStatus());
-        ResponseUtil.writeJson(ServletActionContext.getResponse(),jsonObject.toString());
+        jsonObject.put(B.MESSAGE, ResultStatusCode.OK.getMsg());
+        ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
         return null;
     }
 
@@ -201,105 +214,262 @@ public class AppBookingAction extends ActionSupport implements ServletRequestAwa
      * bookingId 订单id
      * 取消订单功能:
      * //1待支付,2已支付,3待入住,4已入住,5已消费,6支付超时,7已取消,8已退单,9已退款
+     *
      * @return
      */
-    public String cancelBooking(){
-        if (Func.checkNull(bookingId))
+    public String cancelBooking() {
+        if (Func.checkNull(getBookingId()))
             return null;
 
         JSONObject jsonObject = new JSONObject();
 
-        String mess = bookService.cancelBooking(bookingId);
-        if (!Func.checkNull(mess))
-        {
+        String mess = bookService.cancelBooking(getBookingId());
+        if (!Func.checkNull(mess)) {
             jsonObject.put(B.CODE, ResultStatusCode.BAD_REQUEST.getStatus());
-            jsonObject.put(B.MESSAGE,mess);
-            ResponseUtil.writeJson(ServletActionContext.getResponse(),jsonObject.toString());
+            jsonObject.put(B.MESSAGE, mess);
+            ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
             return null;
         }
 
         jsonObject.put(B.CODE, ResultStatusCode.OK.getStatus());
-        jsonObject.put(B.MESSAGE,ResultStatusCode.OK.getMsg());
-        ResponseUtil.writeJson(ServletActionContext.getResponse(),jsonObject.toString());
+        jsonObject.put(B.MESSAGE, ResultStatusCode.OK.getMsg());
+        ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
         return null;
     }
 
     /**
      * bookingId 订单id
      * 删除订单功能(假删除)
+     *
      * @return
      */
-    public String delBooking(){
-        if (Func.checkNull(bookingId))
+    public String delBooking() {
+        if (Func.checkNull(getBookingId()))
             return null;
 
         JSONObject jsonObject = new JSONObject();
 
-        String mess = bookService.delBooking(bookingId);
-        if (!Func.checkNull(mess))
-        {
+        String mess = bookService.delBooking(getBookingId());
+        if (!Func.checkNull(mess)) {
             jsonObject.put(B.CODE, ResultStatusCode.BAD_REQUEST.getStatus());
-            jsonObject.put(B.MESSAGE,mess);
-            ResponseUtil.writeJson(ServletActionContext.getResponse(),jsonObject.toString());
+            jsonObject.put(B.MESSAGE, mess);
+            ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
             return null;
         }
 
         jsonObject.put(B.CODE, ResultStatusCode.OK.getStatus());
-        jsonObject.put(B.MESSAGE,ResultStatusCode.OK.getMsg());
-        ResponseUtil.writeJson(ServletActionContext.getResponse(),jsonObject.toString());
+        jsonObject.put(B.MESSAGE, ResultStatusCode.OK.getMsg());
+        ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
         return null;
     }
 
 
-
     /**
-     *验证是否有房间
+     * 验证是否有房间
      * houseId, 房型id
      * startTime, 开始时间
      * endTime 结束时间
+     *
      * @return
      */
-    public String varificationHouse()
-    {
+    public String varificationHouse() {
         JSONObject jsonObject = new JSONObject();
-        if (Func.checkNull(houseId) ||Func.checkNull(startTime) ||Func.checkNull(endTime))
-        {
+        if (Func.checkNull(houseId) || Func.checkNull(startTime) || Func.checkNull(endTime)) {
             jsonObject.put(B.CODE, ResultStatusCode.BAD_REQUEST.getStatus());
-            jsonObject.put(B.MESSAGE,ResultStatusCode.BAD_REQUEST.getMsg());
-            ResponseUtil.writeJson(ServletActionContext.getResponse(),jsonObject.toString());
+            jsonObject.put(B.MESSAGE, ResultStatusCode.BAD_REQUEST.getMsg());
+            ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
             return null;
         }
 
-        String backMessage = appBookService.varificationHouse( houseId, startTime, endTime);
+        String backMessage = appBookService.varificationHouse(houseId, startTime, endTime);
 
-        if (!Func.checkNull(backMessage))
-        {
+        if (!Func.checkNull(backMessage)) {
             jsonObject.put(B.CODE, ResultStatusCode.BAD_REQUEST.getStatus());
-            jsonObject.put(B.MESSAGE,backMessage);
-            ResponseUtil.writeJson(ServletActionContext.getResponse(),jsonObject.toString());
+            jsonObject.put(B.MESSAGE, backMessage);
+            ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
             return null;
         }
 
         jsonObject.put(B.CODE, ResultStatusCode.OK.getStatus());
         jsonObject.put(B.MESSAGE, OrderEnum.有空房.toString());
-        ResponseUtil.writeJson(ServletActionContext.getResponse(),jsonObject.toString());
+        ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
         return null;
     }
 
 
     /**
+     * open_id
+     * bookingId
      * 用户支付操作
+     *
      * @return
      */
-    public String pay()
-    {
+    public String pay() {
+        JSONObject resultjson = new JSONObject();
+        if (Func.checkNull(this.open_id) || Func.checkNull(this.bookingId)) {
+            resultjson.put(B.CODE, ResultStatusCode.BAD_REQUEST.getStatus());
+            resultjson.put(B.MESSAGE, ResultStatusCode.BAD_REQUEST.getMsg());
+            ResponseUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
+            return null;
+        }
 
+        Users users = userService.queryByOpenid(open_id); // 用户信息
+        Booking booking = bookService.getById(Func.parseInt(bookingId)); // 订单信息
+        if (users == null || booking == null) {
+            resultjson.put(B.CODE, ResultStatusCode.BAD_REQUEST.getStatus());
+            resultjson.put(B.MESSAGE, ResultStatusCode.BAD_REQUEST.getMsg());
+            ResponseUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
+            return null;
+        }
 
+        try {
+            // 支付费用
+            int totalPrice = (int) booking.getHouseTotalPrice();
+            WechatUnifiedOrder w = new WechatUnifiedOrder();
+            w.setAppid(WeiXinUtil.appid_c);
+            w.setAttach("chuanghai");
+            w.setBody("chuanghai");
+            w.setMch_id(WeiXinUtil.account);
+            w.setNonce_str(PayWxUtil.getNonceStr());// 随机支付串
+            w.setNotify_url(WeiXinUtil.ip_h + "/abkpayResulet.action");// 支付结果回调地址
+            w.setOpenid(this.open_id);
+            String orderNo = booking.getOrderNum(); // 订单编号
+            w.setOut_trade_no(orderNo);
+            w.setSpbill_create_ip(WeiXinUtil.ip);
+            w.setTotal_fee(totalPrice);
+            w.setTrade_type("JSAPI");
+            Map<String, String> params = new HashMap<String, String>();
+            params.put("attach", w.getAttach());
+            params.put("appid", w.getAppid());
+            params.put("mch_id", w.getMch_id());
+            params.put("nonce_str", w.getNonce_str());
+            params.put("body", w.getBody());
+            params.put("out_trade_no", w.getOut_trade_no());
+            params.put("total_fee", w.getTotal_fee() + "");
+            params.put("spbill_create_ip", w.getSpbill_create_ip());
+            params.put("notify_url", w.getNotify_url());
+            params.put("trade_type", w.getTrade_type());
+            params.put("openid", w.getOpenid());
+            w.setSign(PayWxUtil.getSign(params, WeiXinUtil.key));
+            params.put("sign", w.getSign());
+            String retXml = JaxbUtil.getRequestXml(params);
+            String msg = HttpUtils.post("https://api.mch.weixin.qq.com/pay/unifiedorder", retXml);
+            System.out.println(msg);
+            // 支付失败
+            if (msg.indexOf("FAIL") > -1) {
+                resultjson.put("code", "205");
+                resultjson.put("message", "支付异常205");
+                ResUtil.write(resultjson, ServletActionContext.getResponse());
+                return null;
+            }
+
+            JaxbUtil requestBinder = new JaxbUtil(TongYiReturn.class, JaxbUtil.CollectionWrapper.class);
+            TongYiReturn to = requestBinder.fromXml(msg);
+            // 支付成功
+            if (to.getReturn_code().equals("SUCCESS") && to.getResult_code().equals("SUCCESS")) {
+                EndPay pay = new EndPay();
+                pay.setAppId(WeiXinUtil.appid_c);
+                pay.setSignType("MD5");
+                pay.setTimeStamp(System.currentTimeMillis() / 1000 + "");
+                pay.setPrepay_id(to.getPrepay_id());
+                pay.setNonceStr(PayWxUtil.getNonceStr());
+                Map<String, String> requestMap = new HashMap<String, String>();
+                requestMap.put("appId", pay.getAppId());
+                requestMap.put("timeStamp", pay.getTimeStamp());
+                requestMap.put("nonceStr", pay.getNonceStr());
+                requestMap.put("package", "prepay_id=" + pay.getPrepay_id());
+                requestMap.put("signType", "MD5");
+                pay.setPaySign(PayWxUtil.getSign(requestMap, WeiXinUtil.key));
+                // 返回支付参数
+                resultjson.put("code", 200);
+                resultjson.put("message", "返回成功");
+                resultjson.put("data", pay);
+                ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
+
+                // 保存支付信息
+                booking.setUpdateTime(DateUtil.getFormatPaternDate(new Date()));
+                booking.setOrderStatus(Func.parseStr(PayEnum.TWO.getNum()));
+                booking.setPayTime(DateUtil.getFormatPaternDate(new Date()));
+                booking.setPayAccount(totalPrice);
+                booking.setPayWay(OrderEnum.微信支付.toString());
+                bookService.updateBooking(booking);
+
+                resultjson.put(B.CODE, ResultStatusCode.OK.getStatus());
+                resultjson.put(B.MESSAGE, "支付成功");
+                ResponseUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
+            }
+        } catch (Exception e) {
+            resultjson.put(B.CODE, ResultStatusCode.BAD_REQUEST.getStatus());
+            resultjson.put(B.MESSAGE, e.getMessage());
+            ResponseUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
+        }
+        return null;
+    }
+
+    /**
+     *  支付成功后回调,推送消息给商家
+     *
+     */
+    public String payResulet(){
+        System.out.println("进入支付回调====");
+        JSONObject resultjson = new JSONObject();
+        try{
+            BufferedReader reader = null;// BufferedReader 字符输入流
+            reader = request.getReader();
+            String line = "";
+            String xmlString = null;
+            StringBuilder inputString = new StringBuilder();
+            while ((line = reader.readLine()) != null) {
+                inputString.append(line);
+            }
+            xmlString = inputString.toString();
+            request.getReader().close();
+            JaxbUtil requestBinder = new JaxbUtil(PayResult.class, PayResult.class);
+            PayResult result = requestBinder.fromXml(xmlString);// Xml到Java,用fromXML()方法
+            String order_num = result.getOut_trade_no();
+            System.out.println("支付成功回调,订单号:"+order_num);
+            Booking booking = bookService.queryByOrder(order_num);
+            // 支付成功
+            if (result.getResult_code().equals("SUCCESS") && booking!=null && booking.getOrderStatus().equals("1")) {
+                // 保存支付信息
+                booking.setUpdateTime(DateUtil.getFormatPaternDate(new Date()));
+                booking.setOrderStatus(Func.parseStr(PayEnum.TWO.getNum()));
+                booking.setPayTime(DateUtil.getFormatPaternDate(new Date()));
+                booking.setPayWay(OrderEnum.微信支付.toString());
+                int m = bookService.updateBooking(booking);
+                if (m>0){
+                    String returnMsg = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
+                    HttpServletResponse response = ServletActionContext.getResponse();
+                    response.setContentType("text/html;charset=utf-8");
+                    PrintWriter out = response.getWriter();
+                    out.println(returnMsg);
+                    out.flush();
+                    out.close(); // 发送成功消息给商家
+                }
+                return null;
+            }
+
+            // 推送消息给商家 TODO
+        } catch (Exception e){
+            resultjson.put("message", "支付失败");
+            resultjson.put("code", 205);
+            ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
+            return null;
+        }
+        resultjson.put("message", "支付失败");
+        resultjson.put("code", 205);
+        ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
         return null;
     }
 
 
 
 
+    public String getOpen_id() {
+        return open_id;
+    }
 
-}
+    public void setOpen_id(String open_id) {
+        this.open_id = open_id;
+    }
+}

+ 2 - 4
mhotel/src/com/happy/action/AppHomePageAction.java

@@ -111,13 +111,9 @@ public class AppHomePageAction extends ActionSupport implements ServletRequestAw
      */
     public String homePage()
     {
-        // 第一次进入首页,获取用户信息
-        //getUserInfo();
-
         // 获取民宿列表
         IPage iPage = appHomePageService.getHotelList(queryValue,page,rows);
         ResponseUtil.writeJsonIPage(ServletActionContext.getResponse(),iPage);
-
         return null;
     }
 
@@ -144,6 +140,7 @@ public class AppHomePageAction extends ActionSupport implements ServletRequestAw
         Hotel hotel = appHomePageService.getHotelAndHouseByHotelId(hotelId,queryStartTime, queryEndTime);
 
         // 通过时间区间查询房间信息,并带好是否有房标识给前台
+        jsonObject.put(B.CODE,ResultStatusCode.OK.getStatus());
         jsonObject.put(B.DATA,ResultUtil.ok(hotel));
         ResponseUtil.writeJson(ServletActionContext.getResponse(),jsonObject.toString());
         return null;
@@ -168,6 +165,7 @@ public class AppHomePageAction extends ActionSupport implements ServletRequestAw
 
         Hotel hotel = hotelService.getById(Func.parseInt(hotelId));
 
+        jsonObject.put(B.CODE,ResultStatusCode.OK.getStatus());
         jsonObject.put(B.DATA,ResultUtil.ok(hotel));
         ResponseUtil.writeJson(ServletActionContext.getResponse(),jsonObject.toString());
         return null;

+ 72 - 4
mhotel/src/com/happy/action/AppMePageAction.java

@@ -1,11 +1,14 @@
 package com.happy.action;
 
+import com.baidubce.model.User;
+import com.google.gson.Gson;
 import com.happy.Model.weixin.Users;
+import com.happy.Model.weixin.Wechat_userinfo;
+import com.happy.Model.weixin.WeiXinUtil;
+import com.happy.Until.*;
 import com.happy.Until.Enum.B;
 import com.happy.Until.Enum.DataType;
-import com.happy.Until.Func;
-import com.happy.Until.ResponseUtil;
-import com.happy.Until.TimeExchange;
+import com.happy.common.http.HttpsClient;
 import com.happy.constant.ResultStatusCode;
 import com.happy.service.BookService;
 import com.happy.service.UserService;
@@ -17,6 +20,8 @@ import org.apache.struts2.interceptor.ServletRequestAware;
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import java.net.URLEncoder;
+import java.util.Date;
 import java.util.List;
 
 /**
@@ -44,6 +49,7 @@ public class AppMePageAction extends ActionSupport implements ServletRequestAwar
     private String headPhoto; // 用户头像地址
     private String contactId; // 相关联系人的id
     public String status;
+    private String code; // 授权前端给用户code
 
     private String userCode; // 第一次进来获取用户的code
     private String queryValue; // 首页查寻搜索关键词
@@ -119,7 +125,6 @@ public class AppMePageAction extends ActionSupport implements ServletRequestAwar
      */
     public void getUserContactList()
     {
-//        String userId = "15461"; // 用户id
         // 通过用户id查询所有订单列表数据
         if (Func.checkNull(userId))
             return;
@@ -251,6 +256,61 @@ public class AppMePageAction extends ActionSupport implements ServletRequestAwar
     }
 
 
+    /**
+     * 用户登录小程序授权
+     */
+    public void authorizationUser()
+    {
+        if (Func.checkNull(this.code))
+            return;
+
+        String OPEN_ID = "";
+        JSONObject resultJson = new JSONObject();
+        // 此处授权后的回调地址,主要获取token和openid
+        String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="
+                + WeiXinUtil.appid_c
+                + "&secret="
+                + WeiXinUtil.screct_c
+                + "&code="
+                + this.getCode() + "&grant_type=authorization_code";
+
+            // String json = HttpUtils.get(requestUrl);
+            String json = HttpsClient.sendPost(requestUrl, "");
+            Wechat_userinfo result = new Gson().fromJson(json, Wechat_userinfo.class);
+            OPEN_ID = result.getOpenid();
+            if (Func.checkNull(OPEN_ID))
+            {
+                resultJson.put(B.CODE,ResultStatusCode.BAD_REQUEST.getStatus());
+                resultJson.put(B.MESSAGE,"未获取到OpenId");
+                ResponseUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
+            }
+
+            /*
+             *  手机号前端过来,昵称、省份、性别等其他信息已保存的result中,
+             * 查询用户openid是否存在,存在就返回信息给前端,不存在就保存到数据库
+             * */
+            Users users = userService.queryByOpenid(OPEN_ID);
+            if (users == null)
+            {
+                users = new Users();
+                users.setUser_name(userName);
+                users.setHeadPhoto(headPhoto);
+                users.setOpenid(OPEN_ID);
+                users.setCreate_time(DateUtil.getFormatPaternDate(new Date()));
+                users.setDataType(DataType.游客数据.toString());
+                userService.insert(users); // 写入用户数据
+
+                users = userService.queryByOpenid(OPEN_ID);
+            }
+
+            resultJson.put(B.CODE,ResultStatusCode.OK.getStatus());
+            resultJson.put(B.MESSAGE,ResultStatusCode.OK.getMsg());
+            resultJson.put(B.DATA,users);
+            ResponseUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
+            return ;
+    }
+
+
 
 
     public String getUserCode() {
@@ -324,4 +384,12 @@ public class AppMePageAction extends ActionSupport implements ServletRequestAwar
     public void setHeadPhoto(String headPhoto) {
         this.headPhoto = headPhoto;
     }
+
+    public String getCode() {
+        return code;
+    }
+
+    public void setCode(String code) {
+        this.code = code;
+    }
 }

+ 3 - 0
mhotel/src/com/happy/common/http/Message2.java

@@ -6,6 +6,9 @@ import com.google.gson.reflect.TypeToken;
 import com.happy.Model.Message.Mp_template_msg;
 import java.util.HashMap;
 
+/**
+ * 公众号,推送信息,推给商家信息
+ */
 public class Message2 {
 
     /** 小程序推送消息到公众号

+ 14 - 15
mhotel/src/com/happy/dao/impl/UserDaoImpl.java

@@ -3,6 +3,7 @@ package com.happy.dao.impl;
 import com.happy.Model.Booking;
 import com.happy.Model.weixin.Users;
 import com.happy.Until.Func;
+import com.happy.Until.UUIDUtil;
 import com.happy.dao.UserDao;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.jdbc.core.BeanPropertyRowMapper;
@@ -36,7 +37,7 @@ public class UserDaoImpl implements UserDao {
         sps.addValue("user_phone", users.getUser_phone());
         sps.addValue("user_zz", users.getUser_zz());
         sps.addValue("openid", users.getOpenid());
-        sps.addValue("create_time", users.getCreate_time());
+        sps.addValue("create_time", UUIDUtil.getNewDate());
         sps.addValue("remark", users.getRemark());
         sps.addValue("identity_type", users.getIdentity_type());
         sps.addValue("contact_id", users.getContactId());
@@ -95,63 +96,61 @@ public class UserDaoImpl implements UserDao {
     {
         if (!Func.checkNull(users.getUser_name()))
         {
-            stringBuffer.append(" user_name=:user_name ");
+            stringBuffer.append(" user_name=:user_name, ");
             msp.addValue("user_name", users.getUser_name());
         }
         if (!Func.checkNull(users.getCard_number()))
         {
-            stringBuffer.append(" card_number=:card_number ");
+            stringBuffer.append(" card_number=:card_number, ");
             msp.addValue("card_number", users.getCard_number());
         }
         if (!Func.checkNull(users.getUser_phone()))
         {
-            stringBuffer.append(" user_phone=:user_phone ");
+            stringBuffer.append(" user_phone=:user_phone ,");
             msp.addValue("user_phone", users.getUser_phone());
         }
         if (!Func.checkNull(users.getUser_zz()))
         {
-            stringBuffer.append(" user_zz=:user_zz ");
+            stringBuffer.append(" user_zz=:user_zz ,");
             msp.addValue("user_zz", users.getUser_zz());
         }
         if (!Func.checkNull(users.getOpenid()))
         {
-            stringBuffer.append(" openid=:openid ");
+            stringBuffer.append(" openid=:openid ,");
             msp.addValue("openid", users.getOpenid());
         }
         if (!Func.checkNull(users.getRemark()))
         {
-            stringBuffer.append(" remark=:remark ");
+            stringBuffer.append(" remark=:remark ,");
             msp.addValue("remark", users.getRemark());
         }
         if (!Func.checkDblZero(users.getIdentity_type()))
         {
-            stringBuffer.append(" identity_type=:identity_type ");
+            stringBuffer.append(" identity_type=:identity_type ,");
             msp.addValue("identity_type", users.getIdentity_type());
         }
         if (!Func.checkNull(users.getContactId()))
         {
-            stringBuffer.append(" contact_id=:contact_id ");
+            stringBuffer.append(" contact_id=:contact_id ,");
             msp.addValue("contact_id", users.getContactId());
         }
         if (!Func.checkNull(users.getContactName()))
         {
-            stringBuffer.append(" contact_id=:contact_id ");
+            stringBuffer.append(" contact_id=:contact_id ,");
             msp.addValue("contact_id", users.getContactId());
         }
         if (!Func.checkNull(users.getDataType()))
         {
-            stringBuffer.append(" data_type=:data_type ");
+            stringBuffer.append(" data_type=:data_type ,");
             msp.addValue("data_type", users.getDataType());
         }
         if (!Func.checkNull(users.getHeadPhoto()))
         {
-            stringBuffer.append(" head_photo=:head_photo ");
+            stringBuffer.append(" head_photo=:head_photo ,");
             msp.addValue("head_photo", users.getHeadPhoto());
         }
 
-
-
-
+        stringBuffer.append(" remark=''");
     }
 
 

+ 3 - 4
mhotel/src/com/happy/service/impl/AppBookImplService.java

@@ -11,6 +11,7 @@ import com.happy.Until.Enum.OrderEnum;
 import com.happy.Until.Enum.PayEnum;
 import com.happy.Until.Func;
 import com.happy.Until.TimeExchange;
+import com.happy.common.wx.WxUtil;
 import com.happy.constant.ResultStatusCode;
 import com.happy.dao.BookDao;
 import com.happy.dto.IPage;
@@ -86,8 +87,7 @@ public class AppBookImplService implements AppBookService {
         booking.setOrderLiveTime(orderLiveTime);
 
         // 订单信息
-        Integer orderNum = TimeExchange.StringToTimestamp(TimeExchange.getDate());// 订单号
-        booking.setOrderNum(Func.parseStr(orderNum));
+        booking.setOrderNum(Func.parseStr(WxUtil.mchOrderNo()));// 订单号
         booking.setOrderStatus(Func.parseStr(PayEnum.ONE.getNum())); // 1待支付状态
         booking.setCreateTime(DateUtil.getFormatPaternDate(DateUtil.getCurrentDate()));
         int i = bookService.insertBooking(booking);
@@ -146,8 +146,7 @@ public class AppBookImplService implements AppBookService {
         booking.setOrderLiveTime(orderLiveTime);
 
         // 订单信息
-        Integer orderNum = TimeExchange.StringToTimestamp(TimeExchange.getDate());// 订单号
-        booking.setOrderNum(Func.parseStr(orderNum));
+        booking.setOrderNum(Func.parseStr(WxUtil.mchOrderNo()));// 订单号
         booking.setOrderStatus(Func.parseStr(PayEnum.ONE.getNum())); // 待支付状态
         booking.setCreateTime(DateUtil.getFormatPaternDate(DateUtil.getCurrentDate()));
         bookService.insertBooking(booking);