ITPub博客

首页 > 应用开发 > IT综合 > 直播电商源码,实现服务器用户登录回应

直播电商源码,实现服务器用户登录回应

IT综合 作者:云豹科技晓彤 时间:2021-07-22 17:22:16 0 删除 编辑

二  服务器回应

总的流程大致这样:

cb_api_resp中,直播电商源码服务器返回登录信息,

在,g_ui.callback(cbitem->hWnd, cbitem->uMsg, cbitem->wParam, p); 处理服务器返回的登录信息,

根据登录信息,发送 WM_LAVAAGNT_MSG 自定义消息,在窗口自定义消息中,具体处理登录反馈 on_login_resp

1) 总的直播电商源码服务器回应处理

static void	cb_api_resp(void* param, int code, CHttpResponseParse* resp)
{
	CUICallbackItem* cbitem = (CUICallbackItem*)param;
	if (cbitem->uMsg == WPARAM_DOWNLOAD_FILE) {
		g_downMgr.cb_down_resp(param, code, resp);
		return;
	}
	const char* p = NULL;
	CBinBuf bf;
	if (resp) {
		if (resp->m_header.m_code == 302) {
			CBinBuf bfV;
			p = resp->m_header.GetHeaderValue("location",bfV);
			bf.Format("{");
			int bComma = 0;
			CJsonObj::append_json_item(bf, "location", p, bComma);
			bf.AppendFormat("}");
			p = bf.c_str();
		}
		else {
			int	len;
			p = resp->GetRawContent(bf, len);
		}
	}
	g_ui.callback(cbitem->hWnd, cbitem->uMsg, cbitem->wParam, p);
	delete cbitem;
}

直播电商源码登录回应

    return xnw_api_request("/v1/weibo/do_login", form,  g_ui.GetHwnd());

    因为登录时,向服务器发出的请求是这么写的,服务器处理返回时,也带一个窗口句柄信息,由这个窗口负责处理。

static void	cb_api_resp(void* param, int code, CHttpResponseParse* resp)
{
	CUICallbackItem* cbitem = (CUICallbackItem*)param;  
 
 
     ...
}
 
    class CUICallbackItem
{
public:
	HWND	hWnd;  
	UINT	uMsg;
	WPARAM	wParam;
	char	url[128];
 
	CUICallbackItem(HWND h, UINT u, WPARAM w=NULL, const char* _url=NULL) {
		hWnd = h;
		uMsg = u;
		wParam = w;
		strcpyN(url, _url, sizeof(url));
	}
};

 因为登录传送的是g_ui窗口,所以应该由g_ui窗口负责登录信息处理。

  这里注意:

            CThreadToUI::DefWndProc中,有一个登录处理:on_login_resp(wParam, (CJsonObj*)lParam);

            CThreadToUI::callback中也有一个登录处理:if (hWnd == m_hWnd && !code),

            如果是g_ui窗口,且body存在的话,则登录成功,

                                         body不存在,则登录失败。

            不管成功与失败,都调用login_resp,在login_resp中,向窗口发送WM_LAVAAGNT_MSG, 在消息窗口处理函数DefWndProc中,处理这个消息,对应于登录消息,就是处理on_login_resp(wParam, (CJsonObj*)lParam);

 

static CThreadToUI			g_ui;
static CXnwDownloadMgr		g_downMgr;
 
 
void CThreadToUI::callback(HWND hWnd, int code, WPARAM wParam, const char* body)
{
	if (hWnd == m_hWnd && !code) {
		if (body) {
			CJsonObj json(body);
			int err = json.getInt("errcode", 0);
			std::string s, sUid,name;
			if (err != 0) {
				json.getString("msg", s);
				login_resp(XNW_ERR_API_AUTH, new CJsonObj(body));
				return;
			}
			json.getString("account", name);
			json.getString("uid", sUid);
			json.getString("passport", s);
			login_resp(XNW_ERR_OK);
 
			// if dynamic code login ok.
			std::string sPasswd;
			json.getString("oldpasswd", sPasswd);
			if (sPasswd.length()) {
				xnw_saveAccount(name.c_str(), sPasswd.c_str());
			}
 
			if (g_myLavaAgnt) {
				g_myLavaAgnt->Login(sUid.c_str(), s.c_str(), "<d pc=\"1\"/>", 0, 1, 2);
			}
		}
		else {
			login_resp(XNW_ERR_API);
		}
		return;
	}
	CJsonObj* p = NULL;
	if (body) {
		p = new CJsonObj(body);
 
		{
			std::wstring bodyS = nbase::UTF8ToUTF16(body);
			std::string bodyA = MyString::wstos(bodyS);
 
			m_lavaError.WriteSuccessFuc("-------------服务器传来信息如下:-----------------\n");
			m_lavaError.WriteSuccessFuc(bodyA);
		}
 
	}
	if (!callui(hWnd, code, (LPARAM)p)) {
		if(p) delete p;
	}
	else{
		std::string info;
		CHAR capital[256];
		info.append("窗口:");
		GetWindowTextA(hWnd, capital, 256);
		info.append(capital);
 
		m_lavaError.WriteSuccessFuc(info, handleMsg);
	}
 
 
}

根据服务器返回的直播电商源码登录信息, 发送窗口消息,WM_LAVAAGNT_MSG

	void login_resp(int code,CJsonObj* p=NULL) {
		callui(m_hWnd, code, (LPARAM)p);
	}
 
	int callui(HWND hWnd, WPARAM wParam, LPARAM lParam=NULL) {
		for (int n = 3; n > 0; --n) {
			if (PostMessage(hWnd, WM_LAVAAGNT_MSG, wParam, lParam))
			{
				return 1;
			}
		}
		return 0;
	}

在窗口消息中,具体处理登录反馈:on_login_resp

class CThreadToUI
	: public CWndX
{
	LRESULT DefWndProc(UINT uMsg, WPARAM wParam, LPARAM lParam) {
		if (uMsg == WM_LAVAAGNT_MSG) {
			if (wParam == WPARAM_OPEN_LIVE) {
				CJsonObj* pJson = (CJsonObj*)lParam;
				if (pJson) {
					std::string s1,s2;
					pJson->getString("push", s1);
					pJson->getString("name", s2);
					show_liveroom(s1,s2);
					delete pJson;
				}
				return 0;
			}
			else if (wParam == WPARAM_OPEN_PLAYER) {
				CJsonObj* pJson = (CJsonObj*)lParam;
				if (pJson) {
					std::string s1, s2;
					pJson->getString("pull", s1);
					pJson->getString("name", s2);
					show_player(s1, s2);
					delete pJson;
				}
				return 0;
			}
			else {
				on_login_resp(wParam, (CJsonObj*)lParam);
			}
		}
		return DefWindowProc(m_hWnd, uMsg, wParam, lParam);
	}
	int callui(HWND hWnd, WPARAM wParam, LPARAM lParam=NULL) {
		for (int n = 3; n > 0; --n) {
			if (PostMessage(hWnd, WM_LAVAAGNT_MSG, wParam, lParam))
			{
				return 1;
			}
		}
		return 0;
	}
public:
	HWND GetHwnd() const {
		return m_hWnd;
	}
	void login_resp(int code,CJsonObj* p=NULL) {
		callui(m_hWnd, code, (LPARAM)p);
	}
	void callback(HWND hWnd, int code, WPARAM wParam, const char* body);
};

具体的直播电商源码登录处理

void on_login_resp(int code,CJsonObj* pJson)
{
	LoginForm* form = (LoginForm*)nim_comp::WindowsManager::GetInstance()->GetWindow(LoginForm::kClassName, LoginForm::kClassName);
	if (form) {
		std::string s;
		if (pJson) {
			pJson->getString("msg", s);
			delete pJson;
		}
		form->OnLoginResult(code,s);
		if (code == XNW_ERR_PRESENCE_OK) {
			//show_courselist();
 
			show_myClass();
			form->ShowWindow(false);
			form->Close(0);
		}
		else if (code == XNW_ERR_UPDATA)   //新版本强制更新 -9999
		{
			pJson->getString("msg", s);
			//
			//
		}
	}
}

声明:本文由云豹科技转发自 清水映朝阳博客,如有侵权请联系作者删除


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70002045/viewspace-2782777/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
音视频软件开发相关知识科普账号

注册时间:2021-06-11

  • 博文量
    163
  • 访问量
    60336