// place_order_dlg.cpp : 实现文件 // #include "stdafx.h" #include "ETradeClient.h" #include "place_order_dlg.h" #include "afxdialogex.h" #include "ETradeClient/utility/win_msg_define.h" #include "ETradeClient/utility/logging.h" #include "etradeclient/utility/url_config.h" #include "etradeclient/utility/application_config.h" #include "etradeclient/browser/session.h" #include "ETradeClient/utility/string_converter.h" #include "ETradeClient/utility/win_msg_define.h" #include <vector> #include "pay_dlg.h" #include "etradeclient/hardware/hardware_cmd.h" #include "etradeclient/boost_patch/property_tree/json_parser.hpp" #include "LoginDialog.h" // PlaceOrderDlg 对话框 IMPLEMENT_DYNAMIC(PlaceOrderDlg, CDialogEx) PlaceOrderDlg::PlaceOrderDlg(CWnd* pParent /*=NULL*/) : CDialogEx(PlaceOrderDlg::IDD, pParent) { line_pen_.CreatePen(PS_SOLID, 1, RGB(0, 0, 0)); edit_brush_.CreateSolidBrush(RGB(245, 247, 250)); comm_id_index_ = 1; product_id_ = 0; stock_amount_ = 0; } PlaceOrderDlg::~PlaceOrderDlg() { } void PlaceOrderDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); DDX_Control(pDX, IDC_GRID_PRODUCT, product_list_); DDX_Control(pDX, IDC_STATIC_OWNER, owner_static_); DDX_Control(pDX, IDC_EDIT_CARD, card_edit_); DDX_Control(pDX, IDC_EDIT_NAME, name_edit_); DDX_Control(pDX, IDC_STATIC_COMM, comm_static_); DDX_Control(pDX, IDC_EDIT_COMM_NUM, comm_num_edit_); DDX_Control(pDX, IDC_EDIT_COMM_NAME, comm_name_edit_); DDX_Control(pDX, IDC_STATIC_UNIT, unit_static_); DDX_Control(pDX, IDC_EDIT_UNIT, unit_edit_); DDX_Control(pDX, IDC_STATIC_REAL_UNIT, real_unit_static); DDX_Control(pDX, IDC_STATIC_HEAVY, heavy_static_); DDX_Control(pDX, IDC_EDIT_HEAVY, heavy_edit_); DDX_Control(pDX, IDC_STATIC_PRICE, price_static_); DDX_Control(pDX, IDC_EDIT_PRICE, price_edit_); DDX_Control(pDX, IDC_STATIC_COUNT, count_static_); DDX_Control(pDX, IDC_EDIT_COUNT, count_edit_); DDX_Control(pDX, IDC_BUTTON_PAY, pay_button_); DDX_Control(pDX, IDC_STATIC_HEJI, total_money_static_); DDX_Control(pDX, IDC_STATIC_TOTAL_MONEY, total_real_money_static_); DDX_Control(pDX, IDC_STATIC_ERROR, error_static_); DDX_Control(pDX, IDC_STATIC_BUYER, buyer_static_); DDX_Control(pDX, IDC_EDIT_BUYER_CARD, buyer_card_edit_); DDX_Control(pDX, IDC_EDIT_BUYER_NAME, buyer_name_edit_); DDX_Control(pDX, IDC_BUTTON_SWIP_CARD, swip_card_button_); } BEGIN_MESSAGE_MAP(PlaceOrderDlg, CDialogEx) ON_WM_SIZE() ON_WM_GETMINMAXINFO() ON_WM_PAINT() ON_WM_CTLCOLOR() ON_BN_CLICKED(IDC_BUTTON_PAY, &PlaceOrderDlg::OnBnClickedButtonPay) ON_COMMAND(ID_ACCELERATOR_S, &PlaceOrderDlg::OnBnClickedButtonPay) ON_COMMAND(ID_ACCELERATOR_TAB, &PlaceOrderDlg::OnAcceleratorTab) ON_COMMAND(ID_ACCELERATOR_ENTER, &PlaceOrderDlg::OnAcceleratorEnter) ON_MESSAGE(WM_ORDER_KILL_FOCUS, &PlaceOrderDlg::OnEditKillFocus) ON_COMMAND(ID_ACCELERATOR_DOWN, &PlaceOrderDlg::OnAcceleratorDown) ON_COMMAND(ID_ACCELERATOR_UP, &PlaceOrderDlg::OnAcceleratorUp) ON_COMMAND(ID_ACCELERATOR_F11, &PlaceOrderDlg::OnBnClickedButtonSwipCard) ON_WM_SETFOCUS() ON_BN_CLICKED(IDC_BUTTON_SWIP_CARD, &PlaceOrderDlg::OnBnClickedButtonSwipCard) ON_MESSAGE(WM_ORDER_DELETE_ORDER, &PlaceOrderDlg::DeleteComm) ON_MESSAGE(WM_CEF_SESSION_EXPIRED, &PlaceOrderDlg::OnSessionExpired) END_MESSAGE_MAP() // PlaceOrderDlg 消息处理程序 BOOL PlaceOrderDlg::OnInitDialog() { CDialogEx::OnInitDialog(); //设置窗口样式 SetCtrlPos(); SetCtrlInitData(); InitList(); acce_ = LoadAccelerators(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_ACCELERATOR_ORDER)); swip_card_button_.SetFocus(); return FALSE; // return TRUE unless you set the focus to a control // 异常: OCX 属性页应返回 FALSE } BOOL PlaceOrderDlg::Create(LPCTSTR lpszTemplateName, CWnd* pParentWnd) { return CDialogEx::Create(lpszTemplateName, pParentWnd); } void PlaceOrderDlg::OnSize(UINT nType, int cx, int cy) { CDialogEx::OnSize(nType, cx, cy); // TODO: 在此处添加消息处理程序代码 } void PlaceOrderDlg::OnGetMinMaxInfo(MINMAXINFO* lpMMI) { CDialogEx::OnGetMinMaxInfo(lpMMI); } BOOL PlaceOrderDlg::PreCreateWindow(CREATESTRUCT& cs) { return CDialogEx::PreCreateWindow(cs); } void PlaceOrderDlg::SetCtrlPos() { const int StaticWidth = 80, StaticHeigth = 35; const int EditWidth = 140, EditHeigth = 35; const int EditWidthShort = 70, StaticWidthShort = 70; CRect ctrl_rect; ctrl_rect.top = 30 + 10; ctrl_rect.bottom = ctrl_rect.top + StaticHeigth; ctrl_rect.left = 30; ctrl_rect.right = ctrl_rect.left + StaticWidth; buyer_static_.MoveWindow(ctrl_rect); ctrl_rect.top -= 10; ctrl_rect.left = ctrl_rect.right; ctrl_rect.right += EditWidth; buyer_card_edit_.MoveWindow(&ctrl_rect); line_point_[2].x = ctrl_rect.right + 3; line_point_[2].y = ctrl_rect.bottom - EditHeigth / 2 - 3; ctrl_rect.left = ctrl_rect.right + 30; ctrl_rect.right = ctrl_rect.left + EditWidth; buyer_name_edit_.MoveWindow(&ctrl_rect); const int ButtonWidth = 120, ButtonHeight = 40; ctrl_rect.left = ctrl_rect.right + 30; ctrl_rect.right = ctrl_rect.left + ButtonWidth; swip_card_button_.MoveWindow(&ctrl_rect); ctrl_rect.top = ctrl_rect.bottom + 30; ctrl_rect.bottom = ctrl_rect.top + StaticHeigth; ctrl_rect.left = 30; ctrl_rect.right = ctrl_rect.left + StaticWidth; owner_static_.MoveWindow(ctrl_rect); ctrl_rect.top -= 10; ctrl_rect.left = ctrl_rect.right; ctrl_rect.right += EditWidth; card_edit_.MoveWindow(&ctrl_rect); line_point_[0].x = ctrl_rect.right + 3; line_point_[0].y = ctrl_rect.bottom - EditHeigth / 2 - 3; ctrl_rect.left = ctrl_rect.right + 30; ctrl_rect.right = ctrl_rect.left + EditWidth; name_edit_.MoveWindow(&ctrl_rect); ctrl_rect.top = ctrl_rect.bottom + 30; ctrl_rect.bottom = ctrl_rect.top + StaticHeigth; ctrl_rect.left = 30; ctrl_rect.right = ctrl_rect.left + StaticWidth; comm_static_.MoveWindow(&ctrl_rect); ctrl_rect.top -= 10; ctrl_rect.left = ctrl_rect.right; ctrl_rect.right += EditWidth; comm_num_edit_.MoveWindow(&ctrl_rect); line_point_[1].x = ctrl_rect.right + 3; line_point_[1].y = ctrl_rect.bottom - EditHeigth / 2 - 3; ctrl_rect.left = ctrl_rect.right + 30; ctrl_rect.right = ctrl_rect.left + EditWidth; comm_name_edit_.MoveWindow(&ctrl_rect); ctrl_rect.top = ctrl_rect.bottom + 30; ctrl_rect.bottom = ctrl_rect.top + StaticHeigth; ctrl_rect.left = 30; ctrl_rect.right = ctrl_rect.left + StaticWidth; unit_static_.MoveWindow(&ctrl_rect); ctrl_rect.top -= 10; ctrl_rect.left = ctrl_rect.right; ctrl_rect.right = ctrl_rect.left + EditWidthShort; unit_edit_.MoveWindow(&ctrl_rect); ctrl_rect.top += 10; ctrl_rect.left = ctrl_rect.right + 10; ctrl_rect.right += StaticWidthShort; real_unit_static.MoveWindow(&ctrl_rect); ctrl_rect.left = ctrl_rect.right + 30; ctrl_rect.right = ctrl_rect.left + StaticWidth; heavy_static_.MoveWindow(&ctrl_rect); ctrl_rect.top -= 10; ctrl_rect.left = ctrl_rect.right; ctrl_rect.right += EditWidth; heavy_edit_.MoveWindow(&ctrl_rect); ctrl_rect.top = ctrl_rect.bottom + 30; ctrl_rect.bottom = ctrl_rect.top + StaticHeigth; ctrl_rect.left = 30; ctrl_rect.right = ctrl_rect.left + StaticWidth; price_static_.MoveWindow(&ctrl_rect); ctrl_rect.top -= 10; ctrl_rect.left = ctrl_rect.right; ctrl_rect.right += EditWidth; price_edit_.MoveWindow(&ctrl_rect); ctrl_rect.top += 10; ctrl_rect.left = ctrl_rect.right + 30; ctrl_rect.right = ctrl_rect.left + StaticWidth; count_static_.MoveWindow(&ctrl_rect); ctrl_rect.top -= 10; ctrl_rect.left = ctrl_rect.right; ctrl_rect.right += EditWidth; count_edit_.MoveWindow(&ctrl_rect); CRect dlg_rect; GetClientRect(&dlg_rect); //ctrl_rect.top += 10; ctrl_rect.left = ctrl_rect.right + 30; ctrl_rect.right = dlg_rect.right; error_static_.MoveWindow(&ctrl_rect); const int TotalMoneyStaticWidth = 80, TotalMoneyStaticHeight = 35; ctrl_rect.top = ctrl_rect.bottom + 20; ctrl_rect.bottom = dlg_rect.bottom - TotalMoneyStaticHeight; ctrl_rect.left = 0; ctrl_rect.right = dlg_rect.right; product_list_.MoveWindow(&ctrl_rect); ctrl_rect.top = ctrl_rect.bottom + 5; ctrl_rect.bottom = dlg_rect.bottom; ctrl_rect.right = dlg_rect.right - TotalMoneyStaticWidth - 100 - ButtonWidth; ctrl_rect.left = ctrl_rect.right - TotalMoneyStaticWidth; total_money_static_.MoveWindow(&ctrl_rect); ctrl_rect.left = ctrl_rect.right; ctrl_rect.right = dlg_rect.right - 10 - ButtonWidth - 5; total_real_money_static_.MoveWindow(&ctrl_rect); ctrl_rect.top -= 5; ctrl_rect.right = dlg_rect.right - 10; ctrl_rect.left = ctrl_rect.right - ButtonWidth; pay_button_.MoveWindow(&ctrl_rect); name_edit_.EnableWindow(FALSE); comm_name_edit_.EnableWindow(FALSE); RECT rect; card_edit_.GetClientRect(&rect); OffsetRect(&rect, 0, 10); card_edit_.SendMessage(EM_SETRECT, 0, (LPARAM)&rect); name_edit_.GetClientRect(&rect); OffsetRect(&rect, 0, 10); name_edit_.SendMessage(EM_SETRECT, 0, (LPARAM)&rect); comm_name_edit_.GetClientRect(&rect); OffsetRect(&rect, 0, 10); comm_name_edit_.SendMessage(EM_SETRECT, 0, (LPARAM)&rect); comm_num_edit_.GetClientRect(&rect); OffsetRect(&rect, 0, 10); comm_num_edit_.SendMessage(EM_SETRECT, 0, (LPARAM)&rect); unit_edit_.GetClientRect(&rect); OffsetRect(&rect, 0, 10); unit_edit_.SendMessage(EM_SETRECT, 0, (LPARAM)&rect); heavy_edit_.GetClientRect(&rect); OffsetRect(&rect, 0, 10); heavy_edit_.SendMessage(EM_SETRECT, 0, (LPARAM)&rect); price_edit_.GetClientRect(&rect); OffsetRect(&rect, 0, 10); price_edit_.SendMessage(EM_SETRECT, 0, (LPARAM)&rect); count_edit_.GetClientRect(&rect); OffsetRect(&rect, 0, 10); count_edit_.SendMessage(EM_SETRECT, 0, (LPARAM)&rect); buyer_card_edit_.GetClientRect(&rect); OffsetRect(&rect, 0, 10); buyer_card_edit_.SendMessage(EM_SETRECT, 0, (LPARAM)&rect); buyer_name_edit_.GetClientRect(&rect); OffsetRect(&rect, 0, 10); buyer_name_edit_.SendMessage(EM_SETRECT, 0, (LPARAM)&rect); buyer_card_edit_.EnableWindow(FALSE); buyer_name_edit_.EnableWindow(FALSE); } void PlaceOrderDlg::OnPaint() { CPaintDC dc(this); // device context for painting CPen *old_pen = dc.SelectObject(&line_pen_); dc.MoveTo(line_point_[0]); dc.LineTo(line_point_[0].x + 24, line_point_[0].y); dc.MoveTo(line_point_[1]); dc.LineTo(line_point_[1].x + 24, line_point_[1].y); dc.MoveTo(line_point_[2]); dc.LineTo(line_point_[2].x + 24, line_point_[2].y); dc.SelectObject(old_pen); CDialogEx::OnPaint(); } HBRUSH PlaceOrderDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor); if (nCtlColor == CTLCOLOR_STATIC) { if (IDC_STATIC_ERROR == pWnd->GetDlgCtrlID()) { pDC->SetTextColor(RGB(200, 0, 0)); } } return hbr; } void PlaceOrderDlg::OnBnClickedButtonPay() { LOG_TRACE(L"提交订单"); SetErrorMsg(CString(L"")); int row_count = product_list_.GetRowCount(); if (row_count == 1) { SetErrorMsg(CString(L"订单信息为空")); return; } struct Comm { std::string product_id; std::string price; std::string amount; std::string unit; std::string weight; }; struct Order { std::string seller_card; std::vector<Comm> comm_vec; }; std::vector<Order> orders_list; Order current_order; for (int row = 1; row < row_count; ++row) { std::string card = wstr_2_str(std::wstring(product_list_.GetItemText(row, 0))); if (card.compare(current_order.seller_card) != 0) { if (row != 1) { orders_list.push_back(current_order); } current_order.comm_vec.clear(); current_order.seller_card = card; } Comm comm_info; CGridCellBase* product_cell = product_list_.GetCell(row, 2); comm_info.product_id = std::to_string(product_cell->product_id); comm_info.price = wstr_2_str(product_list_.GetItemText(row, 3)); comm_info.amount = wstr_2_str(product_list_.GetItemText(row, 4)); comm_info.unit = product_list_.GetItemText(row, 5).Compare(L"斤") == 0 ? "1" : "2"; comm_info.weight = wstr_2_str(product_list_.GetItemText(row, 6)); current_order.comm_vec.push_back(comm_info); } orders_list.push_back(current_order); CString buyer_card; buyer_card_edit_.GetWindowText(buyer_card); CString error_msg; long statement_id = 0; std::string total_money; do { const uint32_t kHTTPOK = 200; WinHttp win_http; auto& url_cfg = URLConfig::Instance(); win_http.ConnectHost(url_cfg.Host(), url_cfg.Port(), url_cfg.IsHttps()); std::string url = url_cfg.GetSubmitOrdersPath(); auto& request = win_http.OpenRequest(WinHttp::Method::JSONPOST, url); std::string post_data = "{\"paymentType\":1,\"cardNo\":\"" + wstr_2_str(buyer_card) + "\",\"orders\":["; for (auto iter_order : orders_list) { post_data += "{\"cardNo\":\"" + iter_order.seller_card + "\",\"products\":["; for (auto iter_product : iter_order.comm_vec) { post_data += "{\"productId\":" + iter_product.product_id + ",\"price\":\"" + iter_product.price + "\",\"amount\":\"" + iter_product.amount + \ "\",\"unit\":" + iter_product.unit + ",\"weight\":\"" + iter_product.weight + "\"},"; } post_data.erase(post_data.end() - 1); post_data += "]},"; } post_data.erase(post_data.end() - 1); post_data += "]}"; request.SetPostData(post_data); if (url_cfg.IsHttps()) { auto& app_cfg = ApplicationConfig::Instance(); request.SetClientCertificate(app_cfg.ClientCertStore(), app_cfg.ClientCertSubject()); } request.SetCookies(Session::Instance().Cookies()); request.Send(); uint32_t status_code = request.GetResponseStatus(); if (kHTTPOK != status_code) { std::string err_msg = "网络请求错误! 错误码: " + std::to_string(status_code); LOG_ERROR(CString(err_msg.c_str()).GetBuffer()); error_msg = err_msg.c_str(); break; } std::string response_header = request.ReadResponseHeader(); std::string response_body = request.ReadResponseBody(); if (response_body.empty()) { LOG_ERROR(L"获取服务器响应数据失败,请确保网络连接正常!"); error_msg = CString(L"获取服务器响应数据失败,请确保网络连接正常!"); break; } namespace PT = boost::property_tree; bool card_res = false; try //Parse the configuration file { int status_index = response_header.find("sessionStatus"); if (std::string::npos == status_index) { LOG_ERROR(L"解析服务器返回的登录结果信息时出错!请确认返回数据不为空,返回的数据格式为正确的Json格式!"); error_msg = CString(L"解析服务器返回的登录结果信息时出错!请确认返回数据不为空,返回的数据格式为正确的Json格式!"); break; } std::string status = response_header.substr(status_index + 15, 1); if (status.compare("0") == 0) { SendMessage(WM_CEF_SESSION_EXPIRED); break; } PT::ptree ptree; std::stringstream ss; ss << response_body; PT::read_json(ss, ptree); if (ptree.get<long>("code") == 0) { PT::ptree statement_data = ptree.get_child("data"); statement_id = statement_data.get<long>("statementId"); total_money = statement_data.get<std::string>("totalMoney"); } else { std::wstring error = str_2_wstr(ptree.get<std::string>("message")); error_msg = error.c_str(); break; } } catch (...) { LOG_ERROR(L"解析服务器返回的登录结果信息时出错!请确认返回数据不为空,返回的数据格式为正确的Json格式!"); error_msg = CString(L"解析服务器返回的登录结果信息时出错!请确认返回数据不为空,返回的数据格式为正确的Json格式!"); break; } } while (0); if (!error_msg.IsEmpty()) { SetErrorMsg(error_msg); } else { EmptyCtrl(true); SetCtrlInitData(); InitList(); comm_id_index_ = 1; comm_vec_.clear(); PayDlg pay_dlg(statement_id, CString(total_money.c_str()), buyer_card_type_); pay_dlg.DoModal(); } } void PlaceOrderDlg::InitList() { product_list_.DeleteAllItems(); product_list_.InsertColumn(L"货主卡号"); product_list_.InsertColumn(L"货主姓名"); product_list_.InsertColumn(L"商品名称"); product_list_.InsertColumn(L"单价"); product_list_.InsertColumn(L"数量"); product_list_.InsertColumn(L"单位"); product_list_.InsertColumn(L"件重(斤/件)"); product_list_.InsertColumn(L"小计/元"); CRect dlg_rect; GetClientRect(&dlg_rect); int column_count = product_list_.GetColumnCount(); for (int index = 0; index < column_count; ++index) { product_list_.SetColumnWidth(index, dlg_rect.Width() / column_count); } product_list_.SetRowHeight(0, 40); CFont font; font.CreatePointFont(105, L"微软雅黑"); product_list_.SetFont(&font); product_list_.SetEditable(FALSE); product_list_.SetFixedRowCount(1); product_list_.SetFixedColumnCount(2); product_list_.SetListMode(TRUE); product_list_.SetSingleColSelection(TRUE); product_list_.SetSingleRowSelection(FALSE); product_list_.SetFixedRowSelection(true); product_list_.SetRowResize(FALSE); product_list_.SetColumnResize(FALSE); product_list_.SetTrackFocusCell(FALSE); } BOOL PlaceOrderDlg::PreTranslateMessage(MSG* pMsg) { if (WM_KEYFIRST <= pMsg->message&&pMsg->message <= WM_KEYLAST) { if (acce_ && ::TranslateAccelerator(m_hWnd, acce_, pMsg)) return TRUE; } return CDialogEx::PreTranslateMessage(pMsg); } void PlaceOrderDlg::OnAcceleratorTab() { } int PlaceOrderDlg::GetCommIndex() { ++comm_id_index_; return comm_id_index_; } void PlaceOrderDlg::OnAcceleratorEnter() { int item_id = GetFocus()->GetDlgCtrlID(); if (item_id == IDC_BUTTON_SWIP_CARD) { OnBnClickedButtonSwipCard(); } else if (item_id == IDC_EDIT_COUNT) { OnEditKillFocus(IDC_EDIT_CARD, NULL); OnEditKillFocus(IDC_EDIT_COMM_NUM, NULL); OnEditKillFocus(IDC_EDIT_UNIT, NULL); OnEditKillFocus(IDC_EDIT_HEAVY, NULL); OnEditKillFocus(IDC_EDIT_PRICE, NULL); OnEditKillFocus(IDC_EDIT_COUNT, NULL); card_edit_.SetFocus(); do { if (card_edit_.IsEditRed() || comm_num_edit_.IsEditRed() || unit_edit_.IsEditRed() || (heavy_edit_.IsWindowEnabled() && heavy_edit_.IsEditRed()) || price_edit_.IsEditRed() || count_edit_.IsEditRed()) { break; } CString text; double price = 0, count = 0; card_edit_.GetWindowText(text); CommInfo comm; comm.comm_id = GetCommIndex(); comm.seller_card = text; text.Empty(); name_edit_.GetWindowText(text); comm.seller_name = text; text.Empty(); comm_name_edit_.GetWindowText(text); comm.comm_name = text; comm.product_id = product_id_; text.Empty(); price_edit_.GetWindowText(text); price = _ttof(text); comm.price = text; text.Empty(); count_edit_.GetWindowText(text); count = _ttof(text); comm.count = text; text.Empty(); real_unit_static.GetWindowText(text); comm.unit = text; text.Empty(); heavy_edit_.GetWindowText(text); comm.weight = text; text.Empty(); double money = price * count * 100; money = std::round(money) / 100; text.Format(_T("%.2f"), money); comm.total_money = text; comm_vec_.push_back(comm); product_list_.DeleteAllItems(); InitList(); for (int index = 0; index != comm_vec_.size(); ++index) { int row_index = product_list_.InsertRow(L"", -1); product_list_.SetItemText(row_index, 0, comm_vec_[index].seller_card, comm_vec_[index].comm_id); product_list_.SetItemText(row_index, 1, comm_vec_[index].seller_name, comm_vec_[index].comm_id); product_list_.SetItemText(row_index, 2, comm_vec_[index].comm_name, comm_vec_[index].comm_id, comm_vec_[index].product_id); product_list_.SetItemText(row_index, 3, comm_vec_[index].price, comm_vec_[index].comm_id); product_list_.SetItemText(row_index, 4, comm_vec_[index].count, comm_vec_[index].comm_id); product_list_.SetItemText(row_index, 5, comm_vec_[index].unit, comm_vec_[index].comm_id); product_list_.SetItemText(row_index, 6, comm_vec_[index].weight, comm_vec_[index].comm_id); product_list_.SetItemText(row_index, 7, comm_vec_[index].total_money, comm_vec_[index].comm_id); for (int index = 0; index < 8; ++index) { product_list_.SetItemFormat(row_index, index, DT_CENTER | DT_VCENTER | DT_SINGLELINE); } } EmptyCtrl(false); SortList(); CString subtotal; double total_price = 0; int list_count = product_list_.GetRowCount(); for (int index = 1; index <= list_count; ++index) { subtotal = product_list_.GetItemText(index, 7); total_price += _ttof(subtotal); } CString total_price_s; total_price_s.Format(L"%.2f", total_price); total_real_money_static_.SetWindowText(total_price_s); } while (0); if (product_list_.GetRowCount() > 1) { pay_button_.EnableWindow(TRUE); } else { pay_button_.EnableWindow(FALSE); } product_list_.Invalidate(TRUE); UpdateWindow(); } else { OnAcceleratorDown(); } } void PlaceOrderDlg::SetCtrlInitData() { EnableCtrl(false); card_edit_.SetWindowTextW(L"8888"); card_edit_.SetCheck(CardNumType); comm_num_edit_.SetCheck(CommNumType); unit_edit_.SetCheck(UnitType); heavy_edit_.SetCheck(HeavyType); price_edit_.SetCheck(PriceType); count_edit_.SetCheck(CountType); card_edit_.SetFocus(); card_edit_.SetSel(4, 4); unit_edit_.SetWindowText(L"1"); heavy_edit_.EnableWindow(FALSE); pay_button_.EnableWindow(FALSE); } void PlaceOrderDlg::SetErrorMsg(CString &error) { error_static_.SetWindowTextW(error); } LRESULT PlaceOrderDlg::OnEditKillFocus(WPARAM wParam, LPARAM lParam) { SetErrorMsg(CString(L"")); int dlg_id = wParam; CString text; GetDlgItem(dlg_id)->GetWindowText(text); switch (dlg_id) { case IDC_EDIT_CARD: { CString name; if (text.IsEmpty()) { card_edit_.SetEditRed(true); break; } bool result = GetCardName(text, name, 2); if (result) { name_edit_.SetWindowText(name); card_edit_.SetEditRed(false); } else { SetErrorMsg(name); card_edit_.SetEditRed(true); } break; } case IDC_EDIT_COMM_NUM: { CString name; if (text.GetLength() < 3) { comm_num_edit_.SetEditRed(true); break; } CString seller_card; card_edit_.GetWindowText(seller_card); bool result = GetCommName(text, seller_card, name, product_id_); if (result) { comm_name_edit_.SetWindowText(name); comm_num_edit_.SetEditRed(false); } else { //SetErrorMsg(name); comm_num_edit_.SetEditRed(true); } break; } case IDC_EDIT_UNIT: { if (text.Compare(L"1") == 0) { real_unit_static.SetWindowTextW(L"斤"); heavy_edit_.SetEditRed(false); heavy_edit_.SetWindowText(L""); heavy_edit_.EnableWindow(FALSE); unit_edit_.SetEditRed(false); } else if (text.Compare(L"2") == 0) { real_unit_static.SetWindowTextW(L"件"); heavy_edit_.EnableWindow(TRUE); unit_edit_.SetEditRed(false); } else { unit_edit_.SetEditRed(true); } break; } case IDC_EDIT_HEAVY: { if (!heavy_edit_.IsWindowEnabled()) { break; } else if(!(_ttof(text) > 0)) { ((CEditBox*)GetDlgItem(dlg_id))->SetEditRed(true); } } case IDC_EDIT_PRICE: { if (text.IsEmpty()) { ((CEditBox*)GetDlgItem(dlg_id))->SetEditRed(true); } else if (text.Right(1).Compare(L".") == 0) { ((CEditBox*)GetDlgItem(dlg_id))->SetEditRed(true); } else if (!(_ttof(text) > 0)) { ((CEditBox*)GetDlgItem(dlg_id))->SetEditRed(true); } else { ((CEditBox*)GetDlgItem(dlg_id))->SetEditRed(false); } break; } case IDC_EDIT_COUNT: { if (text.IsEmpty()) { ((CEditBox*)GetDlgItem(dlg_id))->SetEditRed(true); } else if (text.Right(1).Compare(L".") == 0) { ((CEditBox*)GetDlgItem(dlg_id))->SetEditRed(true); } else if (!(_ttof(text) > 0)) { ((CEditBox*)GetDlgItem(dlg_id))->SetEditRed(true); } else { CString unit; unit_edit_.GetWindowText(unit); CString count; count_edit_.GetWindowText(count); if (unit.Compare(L"1") == 0 && _ttof(count) > stock_amount_) { SetErrorMsg(CString(L"错误信息:库存不足")); ((CEditBox*)GetDlgItem(dlg_id))->SetEditRed(true); } else if (unit.Compare(L"2") == 0) { CString heavy; heavy_edit_.GetWindowText(heavy); if (_ttof(count) * _ttof(heavy) > stock_amount_) { SetErrorMsg(CString(L"错误信息:库存不足")); ((CEditBox*)GetDlgItem(dlg_id))->SetEditRed(true); } else { ((CEditBox*)GetDlgItem(dlg_id))->SetEditRed(false); } } else { ((CEditBox*)GetDlgItem(dlg_id))->SetEditRed(false); } } break; } } UpdateWindow(); return S_OK; } bool PlaceOrderDlg::GetCardName(CString &num, CString &name, int type) { bool result = true; LOG_TRACE(_T("获取卡片姓名!")); do { const uint32_t kHTTPOK = 200; WinHttp win_http; auto& url_cfg = URLConfig::Instance(); win_http.ConnectHost(url_cfg.Host(), url_cfg.Port(), url_cfg.IsHttps()); std::string path = type == 1 ? url_cfg.GetCardNamePath() : url_cfg.GetSellerCardPath(); std::string url = path + "?cardNo=" + wstr_2_str(num); auto& request = win_http.OpenRequest(WinHttp::Method::POST, url); if (url_cfg.IsHttps()) { auto& app_cfg = ApplicationConfig::Instance(); request.SetClientCertificate(app_cfg.ClientCertStore(), app_cfg.ClientCertSubject()); } request.SetCookies(Session::Instance().Cookies()); request.Send(); uint32_t status_code = request.GetResponseStatus(); if (kHTTPOK != status_code) { std::string err_msg = "网络请求错误! 错误码: " + std::to_string(status_code); LOG_ERROR(str_2_wstr(err_msg.c_str())); name = CString(L"网络请求错误!"); result = false; break; } std::string response_header = request.ReadResponseHeader(); std::string response_body = request.ReadResponseBody(); if (response_body.empty() || response_header.empty()) { LOG_ERROR(L"获取服务器响应数据失败,请确保网络连接正常!"); name = CString(L"获取服务器响应数据失败,请确保网络连接正常!"); result = false; break; } namespace PT = boost::property_tree; bool card_res = false; try //Parse the configuration file { int status_index = response_header.find("sessionStatus"); if (std::string::npos == status_index) { LOG_ERROR(L"解析服务器返回的登录结果信息时出错!请确认返回数据不为空,返回的数据格式为正确的Json格式!"); name = CString(L"解析服务器返回的登录结果信息时出错!请确认返回数据不为空,返回的数据格式为正确的Json格式!"); result = false; break; } std::string status = response_header.substr(status_index + 15, 1); if (status.compare("0") == 0) { SendMessage(WM_CEF_SESSION_EXPIRED); result = false; break; } PT::ptree ptree; std::stringstream ss; ss << response_body; PT::read_json(ss, ptree); long res = ptree.get<long>("code"); card_res = res == 0 ? true : false; if (card_res) { PT::ptree card_data = ptree.get_child("data"); name = str_2_wstr(card_data.get<std::string>("name")).c_str(); if (type == 1) { buyer_card_type_ = card_data.get<int>("category"); } } else { std::wstring error_msg = str_2_wstr(ptree.get<std::string>("message")); LOG_ERROR(error_msg); name = error_msg.c_str(); result = false; break; } } catch (...) { LOG_ERROR(L"解析服务器返回的登录结果信息时出错!请确认返回数据不为空,返回的数据格式为正确的Json格式!"); name = CString(L"解析服务器返回的登录结果信息时出错!请确认返回数据不为空,返回的数据格式为正确的Json格式!"); result = false; break; } } while (0); return result; } bool PlaceOrderDlg::GetCommName(CString &num, CString& card, CString &name, long &product_id) { SetErrorMsg(CString(L"")); bool result = true; CString error_msg; LOG_TRACE(_T("获取商品名称!")); do { const uint32_t kHTTPOK = 200; WinHttp win_http; auto& url_cfg = URLConfig::Instance(); win_http.ConnectHost(url_cfg.Host(), url_cfg.Port(), url_cfg.IsHttps()); std::string url = url_cfg.GetCommNamePath() + "?cardNo=" + wstr_2_str(card) + "&shortCode=" + wstr_2_str(num); auto& request = win_http.OpenRequest(WinHttp::Method::POST, url); if (url_cfg.IsHttps()) { auto& app_cfg = ApplicationConfig::Instance(); request.SetClientCertificate(app_cfg.ClientCertStore(), app_cfg.ClientCertSubject()); } request.SetCookies(Session::Instance().Cookies()); request.Send(); uint32_t status_code = request.GetResponseStatus(); if (kHTTPOK != status_code) { std::string err_msg = "网络请求错误! 错误码: " + std::to_string(status_code); LOG_ERROR(str_2_wstr(err_msg.c_str())); error_msg = CString(L"网络请求错误!"); result = false; break; } std::string response_header = request.ReadResponseHeader(); std::string response_body = request.ReadResponseBody(); if (response_body.empty()) { LOG_ERROR(L"获取服务器响应数据失败,请确保网络连接正常!"); error_msg = CString(L"获取服务器响应数据失败,请确保网络连接正常!"); result = false; break; } namespace PT = boost::property_tree; bool card_res = false; try //Parse the configuration file { int status_index = response_header.find("sessionStatus"); if (std::string::npos == status_index) { LOG_ERROR(L"解析服务器返回的登录结果信息时出错!请确认返回数据不为空,返回的数据格式为正确的Json格式!"); name = CString(L"解析服务器返回的登录结果信息时出错!请确认返回数据不为空,返回的数据格式为正确的Json格式!"); result = false; break; } std::string status = response_header.substr(status_index + 15, 1); if (status.compare("0") == 0) { SendMessage(WM_CEF_SESSION_EXPIRED); result = false; break; } PT::ptree ptree; std::stringstream ss; ss << response_body; PT::read_json(ss, ptree); long res = ptree.get<long>("code"); card_res = res == 0 ? true : false; if (card_res) { PT::ptree card_data = ptree.get_child("data"); name = str_2_wstr(card_data.get<std::string>("name")).c_str(); product_id = card_data.get<long>("id"); stock_amount_ = card_data.get<double>("stockAmount"); } else { std::wstring err_msg = str_2_wstr(ptree.get<std::string>("message")); LOG_ERROR(err_msg); error_msg = CString(err_msg.c_str()); result = false; break; } } catch (...) { LOG_ERROR(L"解析服务器返回的登录结果信息时出错!请确认返回数据不为空,返回的数据格式为正确的Json格式!"); name = CString(L"解析服务器返回的登录结果信息时出错!请确认返回数据不为空,返回的数据格式为正确的Json格式!"); result = false; break; } } while (0); if (!error_msg.IsEmpty()) { SetErrorMsg(error_msg); } return result; } void PlaceOrderDlg::OnAcceleratorDown() { int item_id = GetFocus()->GetDlgCtrlID(); switch (item_id) { case IDC_EDIT_CARD: { comm_num_edit_.SetFocus(); break; } case IDC_EDIT_COMM_NUM: { unit_edit_.SetFocus(); unit_edit_.SetSel(0, -1); break; } case IDC_EDIT_UNIT: { CString unit; unit_edit_.GetWindowText(unit); if (unit.IsEmpty()) { heavy_edit_.IsWindowEnabled() ? heavy_edit_.SetFocus() : price_edit_.SetFocus(); } else if (unit.Compare(L"1") == 0) { price_edit_.SetFocus(); } else { heavy_edit_.EnableWindow(TRUE); heavy_edit_.SetFocus(); } break; } case IDC_EDIT_HEAVY: { price_edit_.SetFocus(); break; } case IDC_EDIT_PRICE: { count_edit_.SetFocus(); break; } } } void PlaceOrderDlg::OnAcceleratorUp() { int item_id = GetFocus()->GetDlgCtrlID(); switch (item_id) { case IDC_EDIT_COUNT: { price_edit_.SetFocus(); price_edit_.SetSel(0, -1); break; } case IDC_EDIT_PRICE: { if (heavy_edit_.IsWindowEnabled()) { heavy_edit_.SetFocus(); heavy_edit_.SetSel(0, -1); } else { unit_edit_.SetFocus(); unit_edit_.SetSel(0, -1); } break; } case IDC_EDIT_HEAVY: { unit_edit_.SetFocus(); unit_edit_.SetSel(0, -1); break; } case IDC_EDIT_UNIT: { comm_num_edit_.SetFocus(); comm_num_edit_.SetSel(0, -1); break; } case IDC_EDIT_COMM_NUM: { card_edit_.SetFocus(); card_edit_.SetSel(0, -1); break; } } } void PlaceOrderDlg::OnSetFocus(CWnd* pOldWnd) { CDialogEx::OnSetFocus(pOldWnd); // TODO: 在此处添加消息处理程序代码 } void PlaceOrderDlg::SortList() { product_list_.SortItems(0, TRUE); int row_count = product_list_.GetRowCount(); CString card_num, old_card_num; struct CombineRange { int start_row; int end_row; struct CombineRange() { start_row = 0; end_row = 0; } struct CombineRange(int start, int end) { start_row = start; end_row = end; }; }; std::vector<CombineRange> combine_list; int start_row = 0, end_row = 0; for (int index = 1; index <= row_count; ++index) { card_num = product_list_.GetItemText(index, 0); if (card_num.Compare(old_card_num) == 0) { end_row++; } else { if (start_row != end_row) { combine_list.push_back(CombineRange(start_row, end_row)); } old_card_num = card_num; start_row = index; end_row = start_row; } } int combine_count = combine_list.size(); for (int index = 0; index < combine_count; ++index) { product_list_.MergeCells(combine_list[index].start_row, 0, combine_list[index].end_row, 0); product_list_.MergeCells(combine_list[index].start_row, 1, combine_list[index].end_row, 1); } product_list_.Invalidate(TRUE); } void PlaceOrderDlg::EmptyCtrl(bool is_empty_buyer) { card_edit_.SetWindowText(L"8888"); name_edit_.SetWindowText(L""); comm_num_edit_.SetWindowText(L""); comm_name_edit_.SetWindowText(L""); unit_edit_.SetWindowText(L"1");; real_unit_static.SetWindowText(L"斤"); heavy_edit_.SetWindowText(L""); heavy_edit_.EnableWindow(FALSE); price_edit_.SetWindowText(L""); count_edit_.SetWindowText(L""); if (is_empty_buyer) { buyer_card_edit_.SetWindowTextW(L""); buyer_name_edit_.SetWindowTextW(L""); total_real_money_static_.SetWindowTextW(L"0.00"); } card_edit_.SetSel(4, 4); } void PlaceOrderDlg::OnBnClickedButtonSwipCard() { SetErrorMsg(CString(L"")); EmptyCtrl(true); InitList(); pay_button_.EnableWindow(FALSE); auto dili_card_device = ReadDILIAndBankCardNumCmd(); HardwareCmd::Reply reply = dili_card_device.Execute(""); CString err_msg; do { if (reply.error_code.compare("") != 0) { if (reply.error_code.compare("300") == 0) { err_msg = "打开CPU卡读卡器失败"; } else if (reply.error_code.compare("301") == 0) { err_msg = "无法找到卡片,请放卡到读卡器上(或重新放置卡片)"; } else if (reply.error_code.compare("304") == 0) { err_msg = "读取卡片基本信息失败"; } else if (reply.error_code.compare("501") == 0) { err_msg = "读取银行卡卡号失败"; } else { err_msg = "读卡失败"; } std::wstring log = L"下单时读取买家卡失败,错误码:" + str_2_wstr(reply.error_code); LOG_ERROR(log); buyer_card_edit_.SetWindowText(L""); buyer_name_edit_.SetWindowText(L""); break; } boost::property_tree::ptree card_num_node = reply.data.get_child("chipNo"); std::string card_num = card_num_node.data(); boost::property_tree::ptree card_type_node = reply.data.get_child("type"); std::string card_type = card_type_node.data(); CString card_name; if (!GetCardName(CString(card_num.c_str()), card_name, 1)) { err_msg = card_name; break; } buyer_card_edit_.SetWindowText(CString(card_num.c_str())); buyer_name_edit_.SetWindowText(card_name); EnableCtrl(true); } while (0); if (!err_msg.IsEmpty()) { SetErrorMsg(err_msg); } else { SetErrorMsg(CString(L"")); card_edit_.SetFocus(); card_edit_.SetSel(4, 4); } } void PlaceOrderDlg::EnableCtrl(bool is_enable) { card_edit_.EnableWindow(is_enable); comm_num_edit_.EnableWindow(is_enable); unit_edit_.EnableWindow(is_enable); price_edit_.EnableWindow(is_enable); count_edit_.EnableWindow(is_enable); if (is_enable == false) { card_edit_.SetWindowTextW(L"8888"); name_edit_.SetWindowTextW(L""); comm_num_edit_.SetWindowTextW(L""); comm_name_edit_.SetWindowTextW(L""); unit_edit_.SetWindowTextW(L"1"); price_edit_.SetWindowTextW(L""); count_edit_.SetWindowTextW(L""); } } afx_msg LRESULT PlaceOrderDlg::DeleteComm(WPARAM wParam, LPARAM lParam) { CCellID select_cell = product_list_.GetFocusCell(); int delete_comm_id = product_list_.GetCell(select_cell)->data_id; for (std::vector<CommInfo>::iterator iter = comm_vec_.begin(); iter != comm_vec_.end(); ++iter) { if (iter->comm_id == delete_comm_id) { comm_vec_.erase(iter); break; } } product_list_.DeleteAllItems(); InitList(); for (int index = 0; index != comm_vec_.size(); ++index) { int row_index = product_list_.InsertRow(L"", -1); product_list_.SetItemText(row_index, 0, comm_vec_[index].seller_card, comm_vec_[index].comm_id); product_list_.SetItemText(row_index, 1, comm_vec_[index].seller_name, comm_vec_[index].comm_id); product_list_.SetItemText(row_index, 2, comm_vec_[index].comm_name, comm_vec_[index].comm_id, comm_vec_[index].product_id); product_list_.SetItemText(row_index, 3, comm_vec_[index].price, comm_vec_[index].comm_id); product_list_.SetItemText(row_index, 4, comm_vec_[index].count, comm_vec_[index].comm_id); product_list_.SetItemText(row_index, 5, comm_vec_[index].unit, comm_vec_[index].comm_id); product_list_.SetItemText(row_index, 6, comm_vec_[index].weight, comm_vec_[index].comm_id); product_list_.SetItemText(row_index, 7, comm_vec_[index].total_money, comm_vec_[index].comm_id); for (int index = 0; index < 8; ++index) { product_list_.SetItemFormat(row_index, index, DT_CENTER | DT_VCENTER | DT_SINGLELINE); } } SortList(); CString subtotal; double total_price = 0; int list_count = product_list_.GetRowCount(); for (int index = 1; index <= list_count; ++index) { subtotal = product_list_.GetItemText(index, 7); total_price += _ttof(subtotal); } CString total_price_s; total_price_s.Format(L"%.2f", total_price); total_real_money_static_.SetWindowText(total_price_s); if (product_list_.GetRowCount() > 1) { pay_button_.EnableWindow(TRUE); } else { pay_button_.EnableWindow(FALSE); } product_list_.Invalidate(TRUE); return 0; } LRESULT PlaceOrderDlg::OnSessionExpired(WPARAM wParam, LPARAM lParam) { if (!CLoginDialog(L"当前连接已过期,请重新登录!", this).Launch()) { AfxGetMainWnd()->PostMessage(WM_CLOSE); return 0; } Session::Instance().OnValid(); // If login succeeded, recover the session state to be "not expired". //m_user_msg_monitor.Start(); // Start again after relogged in. return 0; }