Преглед изворни кода

图片下载到本地完成了

张洋 пре 4 година
родитељ
комит
bed3a33a72

+ 1 - 0
.gitignore

@@ -3,3 +3,4 @@
 /.vs
 /bin/Win32/Debug/zhipuzi_pos_windows/log
 /bin/Win32/Debug/zhipuzi_pos_windows/zhipuzi_pos_windows.exe
+/bin/Win32/Debug/zhipuzi_pos_windows/tmp

BIN
bin/Win32/Debug/zhipuzi_pos_windows/db/pos.db


+ 41 - 0
zhipuzi_pos_windows/helper/CLewaimaiString.cpp

@@ -455,4 +455,45 @@ int CLewaimaiString::Replace(std::wstring& strContent, std::wstring strReplace,
     }
 
     return 0;
+}
+
+//整个替换所有的字符串,把strBig钟的strsrc全部替换成strdst
+void CLewaimaiString::string_replace(std::string &strBig, const std::string &strsrc, const std::string &strdst)
+{
+	std::string::size_type pos = 0;
+	std::string::size_type srclen = strsrc.size();
+	std::string::size_type dstlen = strdst.size();
+
+	while ((pos = strBig.find(strsrc, pos)) != std::string::npos)
+	{
+		strBig.replace(pos, srclen, strdst);
+		pos += dstlen;
+	}
+}
+
+//从文件路径或者url中获取文件名
+std::string CLewaimaiString::GetPathOrURLShortName(std::string strFullName)
+{
+	if (strFullName.empty())
+	{
+		return "";
+	}
+
+	string_replace(strFullName, "/", "\\");
+
+	std::string::size_type iPos = strFullName.find_last_of('\\') + 1;
+
+	return strFullName.substr(iPos, strFullName.length() - iPos);
+}
+
+//得到文件路径的目录
+std::string CLewaimaiString::GetPathDir(std::string filePath)
+{
+	std::string dirPath = filePath;
+	size_t p = filePath.find_last_of(L'\\');
+	if (p != -1)
+	{
+		dirPath.erase(p);
+	}
+	return dirPath;
 }

+ 15 - 0
zhipuzi_pos_windows/helper/CLewaimaiString.h

@@ -38,7 +38,22 @@ public:
 
 	static bool isIPAddressValid(const char* pszIPAddr);
 
+	/**
+	 * 把字符串in 以 delim为分隔符,转化为vector,比如 1,2,3,4,5,用逗号分隔,转化为5个元素的vector
+	 */
 	static vector<string> Split(const string& in, const string& delim);
 
+	/**
+	 * 把strContent内容中的strReplace替换为strDest,nNum表示替换几个
+	 */
 	static int Replace(std::wstring& strContent, std::wstring strReplace, std::wstring strDest, int nNum = 1);
+	
+	//整个替换所有的字符串,把strBig钟的strsrc全部替换成strdst
+	static void string_replace(std::string &strBig, const std::string &strsrc, const std::string &strdst);
+
+	//从文件路径或者url中获取文件名
+	static std::string GetPathOrURLShortName(std::string strFullName);
+
+	//得到文件路径的目录
+	static std::string GetPathDir(std::string filePath);
 };

+ 169 - 0
zhipuzi_pos_windows/helper/CSystem.cpp

@@ -1,6 +1,8 @@
 #include "../pch/pch.h"
 #include "CSystem.h"
 
+#include<direct.h>
+
 CSystem::CSystem()
 {
 }
@@ -30,3 +32,170 @@ void CSystem::my_sleep(int second)
 	sleep(second);
 #endif
 }
+
+std::wstring CSystem::getExePath()
+{
+	wchar_t exeFullPath[MAX_PATH]; // Full path
+	std::wstring strPath = L"";
+
+	GetModuleFileName(NULL, exeFullPath, MAX_PATH);
+	strPath = (wstring)exeFullPath;    // Get full path of the file
+
+	return strPath;
+}
+
+std::wstring CSystem::GetProgramDir()
+{
+	wchar_t exeFullPath[MAX_PATH]; // Full path
+	std::wstring strPath = L"";
+
+	GetModuleFileName(NULL, exeFullPath, MAX_PATH);
+	strPath = (wstring)exeFullPath;    // Get full path of the file
+
+	int pos = strPath.find_last_of('\\', strPath.length());
+	return strPath.substr(0, pos);  // Return the directory without the file name
+}
+
+// 程序开机自动启动
+void CSystem::autostart()
+{
+	HKEY hKey;
+	wstring strRegPath = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
+
+	//1、找到系统的启动项
+	if (RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath.c_str(), 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)  ///打开启动项
+	{
+		//2、得到本程序自身的全路径
+		TCHAR strExeFullDir[MAX_PATH];
+		GetModuleFileName(NULL, strExeFullDir, MAX_PATH);
+
+		//3、添加一个子Key,并设置值,"GISRestart"是应用程序名字(不加后缀.exe)
+		RegSetValueEx(hKey, L"智铺子收银软件", 0, REG_SZ, (LPBYTE)strExeFullDir, (lstrlen(strExeFullDir) + 1) * sizeof(TCHAR));
+
+		//4、关闭注册表
+		RegCloseKey(hKey);
+	}
+
+	else
+	{
+		cout << "系统参数错误, 不能随系统启动";
+	}
+}
+
+// 取消开机自动启动
+void CSystem::cancelAutoStart()
+{
+	HKEY hKey;
+	wstring strRegPath = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
+
+	//1、找到系统的启动项
+	if (RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath.c_str(), 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
+	{
+		//2、删除值
+		RegDeleteValue(hKey, L"智铺子收银软件");
+
+		//3、关闭注册表
+		RegCloseKey(hKey);
+	}
+}
+
+bool CSystem::IsAutoStart()
+{
+	HKEY hKey;
+	wstring strRegPath = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
+
+	//1、找到系统的启动项
+	if (RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath.c_str(), 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
+	{
+		char dwValue[256];
+		DWORD dwSzType = REG_SZ;
+		DWORD dwSize = sizeof(dwValue);
+
+		if (::RegQueryValueEx(hKey, _T("智铺子收银软件"), 0, &dwSzType, (LPBYTE)&dwValue, &dwSize) != ERROR_SUCCESS)
+		{
+			return false;
+		}
+
+		//关闭注册表
+		RegCloseKey(hKey);
+
+		return true;
+	}
+
+	return false;
+}
+
+// 判断文件是否存在
+BOOL CSystem::IsFileExist(const wstring& csFile)
+{
+	DWORD dwAttrib = GetFileAttributes(csFile.c_str());
+	return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
+}
+
+// 判断文件夹是否存在
+BOOL CSystem::IsDirExist(const wstring& csDir)
+{
+	DWORD dwAttrib = GetFileAttributes(csDir.c_str());
+	return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 != (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
+}
+
+// 判断文件或文件夹是否存在
+BOOL CSystem::IsPathExist(const wstring& csPath)
+{
+	DWORD dwAttrib = GetFileAttributes(csPath.c_str());
+	return INVALID_FILE_ATTRIBUTES != dwAttrib;
+}
+
+std::string CSystem::GetVersion()
+{
+	std::string r = "";
+
+	UINT sz = GetFileVersionInfoSize(getExePath().c_str(), 0);
+	if (sz != 0)
+	{
+		r.resize(sz, 0);
+		char* pBuf = NULL;
+		pBuf = new char[sz];
+		VS_FIXEDFILEINFO* pVsInfo;
+		if (GetFileVersionInfo(getExePath().c_str(), 0, sz, pBuf))
+		{
+			if (VerQueryValue(pBuf, L"\\", (void**)&pVsInfo, &sz))
+			{
+				sprintf(pBuf, "%d.%d.%d.%d", HIWORD(pVsInfo->dwFileVersionMS), LOWORD(pVsInfo->dwFileVersionMS), HIWORD(pVsInfo->dwFileVersionLS), LOWORD(pVsInfo->dwFileVersionLS));
+				r = pBuf;
+			}
+		}
+		delete pBuf;
+	}
+	return r;
+}
+
+void CSystem::CreateMultiLevel(string dir)
+{
+	if (_access(dir.c_str(), 00) == 0)
+	{
+		return;
+	}
+
+	list <string> dirList;
+	dirList.push_front(dir);
+
+	string curDir = CLewaimaiString::GetPathDir(dir);
+	while (curDir != dir)
+	{
+		if (_access(curDir.c_str(), 00) == 0)
+		{
+			break;
+		}
+
+		dirList.push_front(curDir);
+
+		dir = curDir;
+		curDir = CLewaimaiString::GetPathDir(dir);
+	}
+
+	for (auto it : dirList)
+	{
+		_mkdir(it.c_str());
+	}
+}

+ 10 - 122
zhipuzi_pos_windows/helper/CSystem.h

@@ -13,141 +13,29 @@ public:
     //程序休眠X秒
     static void my_sleep(int second);
 
-	static std::wstring getExePath()
-	{
-		wchar_t exeFullPath[MAX_PATH]; // Full path
-		std::wstring strPath = L"";
+	static std::wstring getExePath();
 
-		GetModuleFileName(NULL, exeFullPath, MAX_PATH);
-		strPath = (wstring)exeFullPath;    // Get full path of the file
-
-		return strPath;
-	}
-
-    static std::wstring GetProgramDir()
-    {
-        wchar_t exeFullPath[MAX_PATH]; // Full path
-        std::wstring strPath = L"";
-
-        GetModuleFileName(NULL, exeFullPath, MAX_PATH);
-        strPath = (wstring)exeFullPath;    // Get full path of the file
-
-        int pos = strPath.find_last_of('\\', strPath.length());
-        return strPath.substr(0, pos);  // Return the directory without the file name
-    }
+    static std::wstring GetProgramDir();
 
     // 程序开机自动启动
-    static void autostart()
-    {
-        HKEY hKey;
-        wstring strRegPath = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
-
-        //1、找到系统的启动项
-        if(RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath.c_str(), 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)  ///打开启动项
-        {
-            //2、得到本程序自身的全路径
-            TCHAR strExeFullDir[MAX_PATH];
-            GetModuleFileName(NULL, strExeFullDir, MAX_PATH);
-
-            //3、添加一个子Key,并设置值,"GISRestart"是应用程序名字(不加后缀.exe)
-            RegSetValueEx(hKey, L"智铺子收银软件", 0, REG_SZ, (LPBYTE)strExeFullDir, (lstrlen(strExeFullDir) + 1) * sizeof(TCHAR));
-
-            //4、关闭注册表
-            RegCloseKey(hKey);
-        }
-
-        else
-        {
-            cout << "系统参数错误, 不能随系统启动";
-        }
-    }
+    static void autostart();
 
     // 取消开机自动启动
-    static void cancelAutoStart()
-    {
-        HKEY hKey;
-        wstring strRegPath = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
-
-        //1、找到系统的启动项
-        if(RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath.c_str(), 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
-        {
-            //2、删除值
-            RegDeleteValue(hKey, L"智铺子收银软件");
-
-            //3、关闭注册表
-            RegCloseKey(hKey);
-        }
-    }
-
-	static bool IsAutoStart()
-	{
-		HKEY hKey;
-		wstring strRegPath = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
-
-		//1、找到系统的启动项
-		if (RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath.c_str(), 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
-		{
-			char dwValue[256];
-			DWORD dwSzType = REG_SZ;
-			DWORD dwSize = sizeof(dwValue);
-
-			if (::RegQueryValueEx(hKey, _T("智铺子收银软件"), 0, &dwSzType, (LPBYTE)&dwValue, &dwSize) != ERROR_SUCCESS)
-			{
-				return false;
-			}
-
-			//关闭注册表
-			RegCloseKey(hKey);
-
-			return true;
-		}
+    static void cancelAutoStart();
 
-		return false;
-	}
+	static bool IsAutoStart();
 
     // 判断文件是否存在
-    static BOOL IsFileExist(const wstring& csFile)
-    {
-        DWORD dwAttrib = GetFileAttributes(csFile.c_str());
-        return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
-    }
+    static BOOL IsFileExist(const wstring& csFile);
 
     // 判断文件夹是否存在
-    static BOOL IsDirExist(const wstring& csDir)
-    {
-        DWORD dwAttrib = GetFileAttributes(csDir.c_str());
-        return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 != (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
-    }
+    static BOOL IsDirExist(const wstring& csDir);
 
     // 判断文件或文件夹是否存在
-    static BOOL IsPathExist(const wstring& csPath)
-    {
-        DWORD dwAttrib = GetFileAttributes(csPath.c_str());
-        return INVALID_FILE_ATTRIBUTES != dwAttrib;
-    }
+    static BOOL IsPathExist(const wstring& csPath);
 
-    static std::string GetVersion()
-    {
-        std::string r = "";
+	static std::string GetVersion();
 
-        UINT sz = GetFileVersionInfoSize(getExePath().c_str(), 0);
-        if(sz != 0)
-        {
-            r.resize(sz, 0);
-            char* pBuf = NULL;
-            pBuf = new char[sz];
-            VS_FIXEDFILEINFO* pVsInfo;
-            if(GetFileVersionInfo(getExePath().c_str(), 0, sz, pBuf))
-            {
-                if(VerQueryValue(pBuf, L"\\", (void**)&pVsInfo, &sz))
-                {
-                    sprintf(pBuf, "%d.%d.%d.%d", HIWORD(pVsInfo->dwFileVersionMS), LOWORD(pVsInfo->dwFileVersionMS), HIWORD(pVsInfo->dwFileVersionLS), LOWORD(pVsInfo->dwFileVersionLS));
-                    r = pBuf;
-                }
-            }
-            delete pBuf;
-        }
-        return r;
-    }
+	static void CreateMultiLevel(string dir);
 };
 

+ 10 - 2
zhipuzi_pos_windows/tool/CSqlite3.cpp

@@ -1032,6 +1032,9 @@ std::vector<CFoodType> CSqlite3::GetFoodtypes(bool is_shouyinji_show)
 	return data;
 }
 
+/**
+ * 如果type_id为0,表示读取所有商品,否则只读取当前type_id的商品
+ */
 std::vector<CFood> CSqlite3::GetFoodByTypeid(std::string type_id, bool is_shouyinji_show)
 {
 	std::vector<CFood> data;
@@ -1040,11 +1043,16 @@ std::vector<CFood> CSqlite3::GetFoodByTypeid(std::string type_id, bool is_shouyi
 
 	if (is_shouyinji_show)
 	{
-		sql = "SELECT * FROM pos_food WHERE status='NORMAL' and is_shouyinji_show = '1';";
+		sql = "SELECT * FROM pos_food WHERE status='NORMAL' and is_shouyinji_show = '1'";
 	}
 	else
 	{
-		sql = "SELECT * FROM pos_food WHERE status='NORMAL';";
+		sql = "SELECT * FROM pos_food WHERE status='NORMAL'";
+	}
+
+	if (type_id != "0")
+	{
+		sql += " AND type_id = '" + type_id + "'";
 	}
 
 	sqlite3_stmt * stmt = NULL;

+ 49 - 0
zhipuzi_pos_windows/wnd/CMainWnd.cpp

@@ -7,6 +7,8 @@
 
 #include "../network/CMessagePush.h"
 
+#include <urlmon.h>
+
 void CMainWnd::Init()
 {
     m_pCloseBtn = static_cast<CButtonUI*>(m_pm.FindControl(_T("closebtn")));
@@ -20,6 +22,53 @@ void CMainWnd::Init()
     //登录成功,启动消息和任务处理
     m_push = new CMessagePush(m_hWnd);
     m_push->Start();
+
+	//启动一个线程,开始同步商品图片
+	std::thread(&CMainWnd::UpdateFoodImage, this).detach();
+}
+
+void CMainWnd::UpdateFoodImage()
+{
+	//先判断并创建临时目录
+	string folderPath = CLewaimaiString::UnicodeToANSI(CSystem::GetProgramDir()) + "\\tmp\\image";
+	std::wstring ws_folderPath = CLewaimaiString::ANSIToUnicode(folderPath);
+	if (!CSystem::IsDirExist(ws_folderPath))
+	{
+		LOG_INFO("folderPath:" << folderPath.c_str() << ",没有找到对应的目录,即将创建");
+		CSystem::CreateMultiLevel(folderPath);
+	}
+
+	CSqlite3 sqlite;
+	std::vector<CFood> foodlist = sqlite.GetFoodByTypeid("0", true);
+	for (std::vector<CFood>::iterator it = foodlist.begin(); it != foodlist.end(); it++)
+	{
+		CFood food = *it;
+
+		if (food.goods_img.size() == 0)
+		{
+			//没有图片,直接跳过
+			continue;
+		}
+		std::wstring ws_goods_img = food.getImageUrl();
+		ws_goods_img += L"!max200"; //下载小图
+
+		wstring imagePath = food.getImageTmpPath();
+		if (CSystem::IsFileExist(imagePath))
+		{
+			//如果图片文件已经存在,直接跳过
+			continue;
+		}
+
+		//图片还不存在,开始下载
+		if (URLDownloadToFile(NULL, ws_goods_img.c_str(), imagePath.c_str(), 0, NULL) == S_OK)
+		{
+			//图片下载成功了,发个消息,更新图片
+		}
+		else
+		{
+			LOG_INFO("URLDownloadToFile Fail,Error"<<GetLastError());
+		}
+	}	
 }
 
 void CMainWnd::SwitchPage(MainPageName name)

+ 2 - 0
zhipuzi_pos_windows/wnd/CMainWnd.h

@@ -45,6 +45,8 @@ public:
     };
 
     void Init();
+
+	void UpdateFoodImage();
 	
 	void SwitchPage(MainPageName name);
 

+ 32 - 0
zhipuzi_pos_windows/zhipuzi/CFood.h

@@ -31,4 +31,36 @@ public:
 	std::string expiration_date;
 	std::string is_weight;
 	std::string member_price_json;
+
+	//获得图片完整的下载地址
+	std::wstring getImageUrl()
+	{
+		if (goods_img.size() == 0)
+		{
+			return L"";
+		}
+
+		std::string url_goods_img = "http://img.zhipuzi.com" + goods_img;
+		std::wstring ws_url_goods_img = CLewaimaiString::UTF8ToUnicode(url_goods_img);
+
+		return ws_url_goods_img;
+	}
+
+	//获取图片的本地临时目录存放路径
+	std::wstring getImageTmpPath()
+	{
+		if (goods_img.size() == 0)
+		{
+			return L"";
+		}
+
+		//纯文件名
+		std::string file_name = CLewaimaiString::GetPathOrURLShortName(goods_img);
+
+		//根据文件名,计算文件临时路径
+		wstring folderPath = CSystem::GetProgramDir() + L"\\tmp\\image\\";
+		wstring imagePath = folderPath + CLewaimaiString::UTF8ToUnicode(file_name);
+
+		return imagePath;
+	}
 };

+ 1 - 1
zhipuzi_pos_windows/zhipuzi_pos_windows.vcxproj

@@ -136,7 +136,7 @@ copy $(ProjectDir)conf\ $(SolutionDir)bin\$(Platform)\$(Configuration)\conf\</Co
       <SubSystem>Windows</SubSystem>
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <AdditionalLibraryDirectories>$(SolutionDir)lib\debug</AdditionalLibraryDirectories>
-      <AdditionalDependencies>dbghelp.lib;winmm.lib;setupapi.lib;AdvAPI32.lib;Shell32.lib;user32.lib;kernel32.lib;Gdi32.lib;libboost_date_time-vc141-mt-sgd-x32-1_70.lib;libboost_regex-vc141-mt-sgd-x32-1_70.lib;sqlite3.lib;DuiLib_ud.lib;log4cplusUD.lib;version.lib;libqrencode.lib;winspool.lib;ole32.lib;oleaut32.lib;uuid.lib;comdlg32.lib;alibabacloud-oss-cpp-sdk.lib;libcurl.lib;libssl.lib;libcrypto.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalDependencies>dbghelp.lib;winmm.lib;setupapi.lib;AdvAPI32.lib;Shell32.lib;user32.lib;kernel32.lib;Gdi32.lib;libboost_date_time-vc141-mt-sgd-x32-1_70.lib;libboost_regex-vc141-mt-sgd-x32-1_70.lib;sqlite3.lib;DuiLib_ud.lib;log4cplusUD.lib;version.lib;libqrencode.lib;winspool.lib;ole32.lib;oleaut32.lib;uuid.lib;comdlg32.lib;alibabacloud-oss-cpp-sdk.lib;libcurl.lib;libssl.lib;libcrypto.lib;urlmon.lib;Wininet.lib;%(AdditionalDependencies)</AdditionalDependencies>
       <IgnoreSpecificDefaultLibraries>LIBCMT</IgnoreSpecificDefaultLibraries>
       <Version>
       </Version>