主頁 >  其他 > Linux專案:音樂播放器

Linux專案:音樂播放器

2021-09-19 10:19:22 其他

文章目錄

  • 1.專案介紹
  • 2.前端代碼
      • 1.httplib快速搭建一個http服務器
      • 2.B/S雙方的資料互動選擇JSON資料格式,http請求和回應的正文中采用jsoncpp開源庫
      • 3.前段的js代碼向發送ajax請求
  • 三、服務端代碼
      • 1.搭建一個music_player這樣的一個類
      • 2.增加注冊按鈕,并且點擊注冊按鈕跳轉到注冊界面
      • 3.服務端代碼當中呼叫mysql-c api連接資料庫,進行操作
      • 4.登錄查詢用戶是否存在校驗郵箱和密碼
      • 5.通過命令列引數獲取ip地址和埠號資訊
      • 6.保存登錄的會話資訊session,使用md5摘要保護登錄資訊,否則造成直接跳轉list.html界面
      • 7.用哈希表保存多個用戶的資訊
      • 8.資料庫存放音樂相關資訊,通過json串回傳給瀏覽器
      • 9.查詢音樂、喜歡的音樂、添加喜歡的音樂

1.專案介紹

音樂播放器擁有注冊登錄功能,查看所有的音樂串列,也可以聽音樂,也可以將音樂添加到自己的喜歡串列,

2.前端代碼

1.httplib快速搭建一個http服務器

在這里插入圖片描述

     1	#include<stdio.h>
     2	#include<iostream>
     3	
     4	#include<string.h>
     5	#include"httplib.h"
     6	
     7	using namespace httplib;
     8	using namespace std;
     9	
    10	int g_val=100;
    11	
    12	void Get_CallBackFunc(const Request& req, Response& resp)
    13	{
    14	    printf("%d\n",g_val);
    15	    cout<<req.method<<endl;
    16	    printf("i am Get_CallBackFunc\n");
    17	    
    18	    const char* lp="<html><h2>hello world!</h2></html>";
    19	    resp.set_content(lp,strlen(lp),"text/html");
    20	}
    21	
    22	
    23	
    24	int main()
    25	{
    26	    Server http_svr;
    27	    int a=10;
    28	
    29	    http_svr.Get("/abc",Get_CallBackFunc);
    30	    http_svr.Post("/111",[a](const Request& req,Response& resp){
    31	            printf("a:%d\n",a);
    32	
    33	    });
    34	    //直接訪問web里面的檔案
    35	    //邏輯根目錄指定為web檔案夾
    36	    http_svr.set_mount_point("/","./web");
    37	    http_svr.listen("0.0.0.0",21010);
    38	
    39	    return 0;
    40	}

在這里插入圖片描述
在這里插入圖片描述

2.B/S雙方的資料互動選擇JSON資料格式,http請求和回應的正文中采用jsoncpp開源庫

在這里插入圖片描述
在這里插入圖片描述

     1	#include<stdio.h>
     2	#include<iostream>
     3	#include<jsoncpp/json/json.h>
     4	
     5	using namespace std;
     6	
     7	int main()
     8	{
     9	
    10	    Json::Value v1;
    11	    v1["id"]=1;
    12	    v1["name"]="yyw";
    13	    cout<<v1<<endl;
    14	
    15	    Json::Value v2;
    16	    v2["id"]=2;
    17	    v2["name"]="yy";
    18	    cout<<v2<<endl;
    19	
    20	    Json::Value v3;
    21	    v3["v1"]=v1;
    22	    v3["v2"]=v2;
    23	    cout<<v3<<endl;
    24	
    25	    Json::Value v4;
    26	    v4.append(v1);
    27	    v4.append(v2);
    28	    cout<<v4<<endl;
    29	
    30	    for(int i=0;i<2;i++)
    31	    {
    32	        cout<<v4[i]["id"]<<":"<<v4[i]["name"]<<endl;
    33	    }
    34	
    35	    //序列化
    36	    Json::FastWriter w;
    37	    string json_str=w.write(v4);
    38	    cout<<json_str<<endl;
    39	    //反序列化
    40	    Json::Reader r;
    41	    Json::Value vv;
    42	    r.parse(json_str,vv);
    43	    cout<<vv<<endl;
    44	    return 0;
    45	}

JSON資料格式有三種顯示方法:平鋪、嵌套、陣列形式,
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
序列化和反序列化:
在這里插入圖片描述

在這里插入圖片描述

3.前段的js代碼向發送ajax請求

在這里插入圖片描述

#include<stdio.h>
#include<iostream>

#include<jsoncpp/json/json.h>
#include<string.h>
#include"httplib.h"

using namespace httplib;
using namespace std;

int g_val=100;

void Get_CallBackFunc(const Request& req, Response& resp)
{
    printf("%d\n",g_val);
    cout<<req.method<<endl;
    printf("i am Get_CallBackFunc\n");
    
    const char* lp="<html><h2>hello world!</h2></html>";
    resp.set_content(lp,strlen(lp),"text/html");
}



int main()
{
    Server http_svr;

    http_svr.Get("/abc",Get_CallBackFunc);
    //發送post請求/login,回呼這個運算式
    http_svr.Post("/login",[](const Request& req,Response& resp){
            cout<<req.body<<endl; //列印正文資訊

            Json::Value resp_json;
            resp_json["login_status"]=true;  //瀏覽器回傳登錄狀態
            
            //序列化
            //系列化成完整的字串的內容放到回應的正文中
            Json::FastWriter w;
            resp.body= w.write(resp_json);
            //告訴瀏覽器回傳的內容就是json串,對json決議
            resp.set_header("Content-Type","application/json");
    });
    //直接訪問web里面的檔案
    //邏輯根目錄指定為web檔案夾
    http_svr.set_mount_point("/","./web");
    http_svr.listen("0.0.0.0",21010);

    return 0;
}

在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述

三、服務端代碼

在這里插入圖片描述

1.搭建一個music_player這樣的一個類

在這里插入圖片描述

     1	#pragma once
     2	#include"database.hpp"
     3	#include<stdio.h>
     4	#include<iostream>
     5	#include<string>
     6	#include"httplib.h"
     7	
     8	#include<jsoncpp/json/json.h>
     9	
    10	using namespace std;
    11	
    12	using namespace httplib;
    13	
    14	#define MUSIC_SVR_IP "0.0.0.0"
    15	#define MUSIC_SVR_PORT 18989
    16	
    17	class MusicServer
    18	{
    19	    public:
    20	        MusicServer()
    21	        {
    22	            svr_ip_=MUSIC_SVR_IP;
    23	            svr_port_=MUSIC_SVR_PORT;
    24	            db_svr_=NULL;
    25	        }
    26	        ~MusicServer()
    27	        {
    28	
    29	        }
    30	
    31	        //1.初始化當前類接
    32	        int InitMusicServer(string ip=MUSIC_SVR_IP,uint16_t port=MUSIC_SVR_PORT)
    33	        {
    34	            svr_ip_=ip;
    35	            svr_port_=port;
    36	
    37	            db_svr_=new DataBaseSvr("1.14.165.138","yy","123","music_svr");
    38	            if(db_svr_==NULL)
    39	            {
    40	                return -1;
    41	            }
    42	            return 0;
    43	        }
    44	        //2.啟動服務的介面
    45	        int StartMusicServer()
    46	        {
    47	            //1.注冊個若干個http請求的對應的回呼函式
    48	            /*
    49	             *請求:{"name":"yy","passwd":"123","email":"123@qq.com","phonenum":"1231"}
    50	             *回應:{"register_status":"xxxx"}
    51	             *
    52	             *
    53	             * */
    54	            http_svr_.Post("/register",[this](const Request& req,Response& resq){
    55	                    cout<<req.body<<endl;
    56	                    //1.需要將瀏覽器中的資料持久化(保存在資料庫中) 
    57	                    //
    58	                    //1.將用戶提交的資料進行反序列化,拿到一個json物件
    59	                    Json::Reader r;
    60	                    Json::Value v;
    61	                    r.parse(req.body,v);
    62	
    63	                    cout<<v["name"]<<endl;
    64	                    cout<<v["passwd"]<<endl;
    65	                    cout<<v["email"]<<endl;
    66	                    cout<<v["phonenum"]<<endl;
    67	
    68	                    db_svr_->InsertUserInfo(v);
    69	                    });
    70	
    71	            http_svr_.Post("/login",[](const Request& req,Response& resq){
    72	                /*
    73	                 *1.
    74	                 *2.
    75	                 *3.
    76	                 * */
    77	            cout<<req.body<<endl;
    78	
    79	            });
    80	
    81	            //2.設定http服務器靜態路徑(邏輯根目錄)
    82	            http_svr_.set_mount_point("/","./web");
    83	            //3.監聽起來
    84	            http_svr_.listen(svr_ip_.c_str(),svr_port_);
    85	        }
    86	    private:
    87	        //httplib 當中的server物件
    88	        Server http_svr_;
    89	
    90	        string svr_ip_;
    91	        uint16_t svr_port_;
    92	
    93	        //資料庫操作模塊
    94	        DataBaseSvr* db_svr_;
    95	};

2.增加注冊按鈕,并且點擊注冊按鈕跳轉到注冊界面

    54	            http_svr_.Post("/register",[this](const Request& req,Response& resq){
    55	                    cout<<req.body<<endl;
    56	                    //1.需要將瀏覽器中的資料持久化(保存在資料庫中) 
    57	                    //
    58	                    //1.將用戶提交的資料進行反序列化,拿到一個json物件
    59	                    Json::Reader r;
    60	                    Json::Value v;
    61	                    r.parse(req.body,v);
    62	
    63	                    cout<<v["name"]<<endl;
    64	                    cout<<v["passwd"]<<endl;
    65	                    cout<<v["email"]<<endl;
    66	                    cout<<v["phonenum"]<<endl;
    67	
    68	                    db_svr_->InsertUserInfo(v);
    69	                    });

3.服務端代碼當中呼叫mysql-c api連接資料庫,進行操作

在這里插入圖片描述

    1	#pragma once
     2	#include<stdio.h>
     3	#include<unistd.h>
     4	#include<iostream>
     5	#include<string>
     6	
     7	#include<mysql/mysql.h>
     8	#include<jsoncpp/json/json.h>
     9	using namespace std;
    10	class DataBaseSvr
    11	{
    12	    public:
    13	        DataBaseSvr(const string& db_host,const string& db_user,const string& db_password,const string& db_name,unsigned int db_port=3306)
    14	        {
    15	            mysql_init(&mysql_);
    16	            db_host_=db_host;
    17	            db_user_=db_user;
    18	            db_password_=db_password;
    19	            db_port_=db_port;
    20	            db_name_=db_name;
    21	        }
    22	        ~DataBaseSvr()
    23	        {
    24	
    25	        }
    26	
    27	        int ConnectToMysql()
    28	        {
    29	            if(!mysql_real_connect(&mysql_,db_host_.c_str(),db_user_.c_str(),db_password_.c_str(),db_name_.c_str(),db_port_,NULL,CLIENT_FOUND_ROWS))
    30	            {
    31	                cout<<mysql_error(&mysql_)<<endl;
    32	                return -1;
    33	            }
    34	            return 0;
    35	        }
    36	
    37	        //提交上來的物件
    38	        int InsertUserInfo(const Json::Value& v)
    39	        {
    40	            //1.連接資料庫
    41	            if(ConnectToMysql()<0)
    42	            {
    43	                return -1;
    44	            }
    45	            //2.組織sql語
    46	            
    47	            string name=v["name"].asString();
    48	            string password=v["password"].asString();
    49	            string email=v["email"].asString();
    50	            string phonenum=v["phonenum"].asString();
    51	
    52	#define INSERT_USER_INFO  "insert into sys_user(user_name,password,email,phone_num) values('%s','%s','%s','%s');"
    53	
    54	            char sql[1024]={0};
    55	            snprintf(sql,sizeof(sql)-1,INSERT_USER_INFO,name.c_str(),password.c_str(),email.c_str(),phonenum.c_str());
    56	            cout<<"sql:"<<sql<<endl;
    57	
    58	            //3.繼續執行sql
    59	            //4.回傳插入結果給呼叫者
    60	        }
    61	    private:
    62	        MYSQL mysql_;
    63	
    64	        string db_host_;
    65	        string db_user_;
    66	        string db_password_;
    67	        string db_name_;
    68	        unsigned int db_port_;
    69	};

4.登錄查詢用戶是否存在校驗郵箱和密碼

在這里插入圖片描述

	//查詢用戶是否存在
   105	       int QueryUserExist(const Json::Value& v)
   106	        {
   107	           /* //0 鏈接
   108	            if(ConnectToMysql()<0)
   109	            {
   110	                return -1;
   111	            }*/
   112	            //1.從json物件當中決議出來 郵箱和密碼
   113	
   114	            string email = v["email"].asString();
   115	            string password = v["password"].asString();
   116	
   117	            //2.使用郵箱作為查詢條件在sys_user表當中進行查詢(如果沒有查找到用戶,則回傳)
   118	#define QUERY_USER "select * from sys_user where email='%s';"
   119	            char sql[1024] = {0};
   120	            snprintf(sql, sizeof(sql) - 1, QUERY_USER, email.c_str());
   121	            cout << sql << endl;
   122	                    
   123	       		 //3.針對查詢的結果當中的密碼, 進行比對
   124	            MYSQL_RES* res = NULL;
   125	            if(ExecuteSql(sql, &res) == false)
   126	            {
   127	                return -2;
   128	            }
   129	            
   130	            //3.1 針對結果集進行操作, 判斷下,結果集當中的行數是否等于1
   131	            //    等于1, 繼續往下執行
   132	            //    不等1 , 直接回傳
   133	            //my_ulonglong STDCALL mysql_num_rows(MYSQL_RES *res);
   134	            if(mysql_num_rows(res) != 1)
   135	            {
   136	                cout << "No Data for sql!, sql is " << sql <<endl;
   137	                mysql_free_result(res);
   138	                return -3;
   139	            }
   140	
   141	            //3.2 在結果集當中獲取一行資料
   142	            //MYSQL_ROW   STDCALL mysql_fetch_row(MYSQL_RES *result);
   143	            //     如果重復呼叫msyql_fetch_row這個函式, 默認是取下一行的資料
   144	            MYSQL_ROW row = mysql_fetch_row(res);
   145	
   146	            //3.3 比對密碼
   147	            string db_password = row[2];
   148	            if(db_password != password )
   149	            {
   150	                return -4;
   151	            }
   152	            
   153	            mysql_free_result(res);
   154	            //4.回傳查詢結果
   155	            return atoi(row[0]); 
   156	 		}

5.通過命令列引數獲取ip地址和埠號資訊

     1	#include"music_player.hpp"
     2	void Usage()
     3	{
     4	    cout << "./MusicServer [command key] [command value] ..." << endl;
     5	    cout << "   " << "-ip : svr listen ip" << endl;
     6	    cout << "   " << "-port : svr listen port" << endl;
     7	    cout << "   " << "-db_ip : msyql server ip address" << endl;
     8	    cout << "   " << "-db_port : mysql port" << endl;
     9	    cout << "   " << "-db_user : mysql user" << endl;
    10	    cout << "   " << "-db_passwd : mysql password" << endl;
    11	    cout << "   " << "-db_db : which database " << endl;
    12	    cout << "   " << "-h : show usage" << endl;
    13	}
    14	
    15	//通過命令列引數獲取ip地址和埠號之類的資訊
    16	int main(int argc, char* argv[])
    17	{
    18	    if(argc == 2 && strcmp(argv[1], "-h") == 0)
    19	    {
    20	        Usage();
    21	        exit(1);
    22	    }
    23	    string svr_ip, db_ip, db_user, db_db, db_passwd;
    24	    uint16_t svr_port, db_port;
    25	    for(int i = 0; i < argc; i++)
    26	    {
    27	        if(strcmp(argv[i], "-ip") == 0 && i + 1 < argc)
    28	        {
    29	            svr_ip = argv[i + 1];
    30	        }
    31	        else if(strcmp(argv[i], "-port") == 0 && i + 1 < argc)
    32	        {
    33	            svr_port = atoi(argv[i + 1]);
    34	        }
    35	        else if(strcmp(argv[i], "-db_ip") == 0 && i + 1 < argc)
    36	        {
    37	            db_ip = argv[i + 1];
    38	        }
    39	        else if(strcmp(argv[i], "-db_port") == 0 && i + 1 < argc)
    40	        {
    41	            db_port = atoi(argv[i + 1]);
    42	        }
    43	        else if(strcmp(argv[i], "-db_user") == 0 && i + 1 < argc)
    44	        {
    45	            db_user = argv[i + 1];
    46	        }
    47	        else if(strcmp(argv[i], "-db_db") == 0 && i + 1 < argc)
    48	        {
    49	            db_db = argv[i + 1];
    50	        }
    51	        else if(strcmp(argv[i], "-db_passwd") == 0 && i + 1 < argc)
    52	        {
    53	            db_passwd = argv[i + 1];
    54	        }
    55	}
    56		cout<<"please check info:"<<endl;
    57	    cout << "svr_ip: " << svr_ip << endl;
    58	    cout << "svr_pot: " << svr_port << endl;
    59	    cout << "db_ip: " << db_ip << endl;
    60	    cout << "db_port: " << db_port << endl;
    61	    cout << "db_user: " << db_user << endl;
    62	    //cout << "db_passwd: " << db_passwd << endl;
    63	    cout << "db_db: " << db_db << endl;
    64	
    65	    //重新輸出ip
    66		int select  = 0;
    67	    cout << "enter check result: currect input 1, error input 0;" << endl;
    68	    cout << "[input select]: ";
    69	    cin >> select;
    70	    switch(select)
    71	    {
    72	        case 0:
    73	            cout << "plase retry start server.." << endl;
    74	            break;
    75	        case 1:
    76	            {
    77	                MusicServer* ms = new MusicServer();
    78	                if(ms == NULL)
    79	                {
    80	                    return -1;
    81	                }
    82	
    83	                if(ms->InitMusicServer(db_ip, db_port, db_user, db_passwd, db_db, svr_ip, svr_port) < 0)
    84	                {
    85	                    return -2;
    86	                }
    87	                ms->StartMusicServer();
    88	                delete ms;
    89	                break;
    90	            }
    91	        default:
    92	            printf("input select num error, please input 1(currect) or 0(error)\n");
    93	            break;
    94	    }
    95	
    96	    return 0;
    97	}

6.保存登錄的會話資訊session,使用md5摘要保護登錄資訊,否則造成直接跳轉list.html界面

在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述

     1	#pragma once
     2	#include <stdio.h>
     3	#include <unistd.h>
     4	#include <string.h>
     5	#include <openssl/md5.h>
     6	#include <iostream>
     7	#include <string>
     8	#include <unordered_map>
     9	
    10	#include <jsoncpp/json/json.h>
    11	#include "httplib.h"
    12	
    13	using namespace std;
    14	using namespace httplib;
    15	
    16	typedef enum UserStatus
    17	{
    18	    //不在線
    19	    OFFLINE = 0,
    20	    //在線狀態
    21	    ONLINE,
    22	}USER_STATUS;
    23	
    24	
    25	//針對與登錄的用戶創建的會話資訊
    26	
    27	class Session
    28	{
    29	    public:
    30	        Session()
    31	        {
    32	
    33	        }
    34	
    35	        Session(const Json::Value& v, int user_id)
    36	        {
    37	            user_id_ = user_id;
    38	
    39	            real_str_ += v["email"].asString();
    40	            real_str_ += v["password"].asString();
    41	
    42	            us_status_ = ONLINE;
    43	        }
    44	
    45	        ~Session()
    46	        {
    47	
    48	        }
    49	
    50	
    51	        bool CalcMd5()
    52	        {
    53	            MD5_CTX ctx;
    54	            MD5_Init(&ctx);
    55	
    56	            //int MD5_Update(MD5_CTX *c, const void *data, unsigned long len);
    57	            if(MD5_Update(&ctx, real_str_.c_str(), real_str_.size()) != 1)
    58	            {
    59	                return false;
    60	            }
    61	
    62	            //int MD5_Final(unsigned char *md, MD5_CTX *c);
    63	            //md ==> 16位元組
    64	            unsigned char md5[16] = { 0 };
    65	            if(MD5_Final(md5, &ctx) != 1)
    66	            {
    67	                return false;
    68	            }
    69	
    70	            char tmp[2] = {0};
    71	            char buf[32] = {0};
    72	            for(int i = 0; i < 16; i++)
    73	            {
    74	                sprintf(tmp, "%02x", md5[i]);
    75	                strncat(buf, tmp, 2);
    76	            }
    77	
    78	            session_id_ = buf;
    79	            cout << "session_id_" << session_id_ << endl;
    80	            return true;
    81	        }
    82	
    83	        string& GetSessionID()
    84	        {
    85	            //1.計算sessionID
    86	            CalcMd5();
    87	            //2.回傳session_id
    88	            return session_id_;
    89	        }
    90	
    91	        int GetUserId()
    92	        {
    93	            return user_id_;
    94	        }
    95	
    96	    private:
    97	        string session_id_;
    98	
    99	        //生成會話ID的源字串
   100	        string real_str_;
   101	
   102	        int user_id_;
   103	
   104	        UserStatus us_status_;
   105	};

7.用哈希表保存多個用戶的資訊

在這里插入圖片描述
在這里插入圖片描述

   107	//哈希查找多用戶的時刻更快
   108	class AllSessionInfo
   109	{
   110	    public:
   111	        AllSessionInfo()
   112	        {
   113	            all_sess_map_.clear();
   114	            pthread_mutex_init(&map_lock_, NULL);
   115	        }
   116	
   117	        ~AllSessionInfo()
   118	        {
   119	            pthread_mutex_destroy(&map_lock_);
   120	        }
   121	
   122	        void InsertSessionInfo(const string session_id, const Session sess)
   123	        {
   124	            pthread_mutex_lock(&map_lock_);
   125	            all_sess_map_.insert(make_pair(session_id, sess));
   126	            pthread_mutex_unlock(&map_lock_);
   127	        }
   128	
   129	        int CheckSession(const Request& req)
   130	        {
   131	            /*
   132	             * 1.通過http請求頭當中的 Cookie欄位, 獲取對應的value(會話ID)
   133	             * 2.通過會話ID, 在all_sess_map_當中查找, 是否有對應的會話資訊
   134	             * */
   135	            string sess_id = req.get_header_value("Cookie");
   136	
   137	            pthread_mutex_lock(&map_lock_);
   138	            auto iter = all_sess_map_.find(sess_id);
   139	            if(iter == all_sess_map_.end())
   140	            {
   141	                pthread_mutex_unlock(&map_lock_);
   142	                return -1;
   143	            }
   144	            int user_id = iter->second.GetUserId();
   145	            pthread_mutex_unlock(&map_lock_);
   146	            return user_id;
   147	        }
   148	    private:
   149	        unordered_map<string, Session> all_sess_map_;
   150	        pthread_mutex_t map_lock_;
   151	};

在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述

8.資料庫存放音樂相關資訊,通過json串回傳給瀏覽器

在這里插入圖片描述

	        int GetMusic(string sql, Json::Value& resp_json)
   161	        {
   162	            MYSQL_RES* res = NULL;
   163	            if(ExecuteSql(sql, &res) == false)
   164	            {
   165	                return -2;
   166	            }
   167	
   168	            int row_nums = mysql_num_rows(res);
   169	            if(row_nums <= 0)
   170	            {
   171	                printf("No data: sql is \"%s\"", sql.c_str());
   172	                mysql_free_result(res);
   173	                return -3;
   174	            }
   175	
   176	            MYSQL_ROW row = mysql_fetch_row(res);
   177	            Json::Value music_value;
   178	            while(row != NULL)
   179	            {
   180	                //1.保存資料到json當中
   181	                Json::Value tmp;
   182	                tmp["id"] = row[0];
   183	                tmp["title"] = row[1];
   184	                tmp["singer"] = row[2];
   185	                tmp["url"] = row[3];
   186	
   187	                music_value.append(tmp);
   188	                row = mysql_fetch_row(res);
   189	            }
   190	
   191	            resp_json["music"] = music_value;
   192	            mysql_free_result(res);
   193	            return 0;
   194	        }
   195	
   196	
   197	        int GetAllMusic(Json::Value& resp_json)
   198	        {
   199	            /*
   200	             * 1.組織查詢的sql陳述句
   201	             * 2.呼叫ExecuteSql函式執行sql陳述句
   202	             * 3.檢查結果集當中的行數
   203	             * 4.將獲取到的結果集, 進行遍歷操作
   204	             * 4.組織json物件
   205	             * */
   206	            const char* sql = "select * from music;";
   207	            return GetMusic(sql, resp_json);
   208	        }
   209	
   210	       
   211	
   212	        int InsertLoveMusic(const Request& req, int user_id)
   213	        {
   214	            /*
   215	             * 1.通過req當中的正文資訊, 獲取music_id
   216	             * 2.使用music_id, user_id組織sql陳述句
   217	             * 3.執行sql陳述句
   218	             * */
   219	            Json::Reader r;
   220	            Json::Value v;
   221	            r.parse(req.body, v);
   222	
   223	            int music_id = v["music_id"].asInt();
   224	
   225	#define INSERT_LOVE_MUSIC "insert into love_music(user_id, music_id) values(%d, %d);"
   226	            char sql[1024] = {0};
   227	            snprintf(sql, sizeof(sql) - 1, INSERT_LOVE_MUSIC, user_id, music_id);
   228	
   229	            if(ExecuteSql(sql) == false)
   230	            {
   231	                return -1;
   232	            }
   233	            return 1;
   234	        }
   235	
   236	        int GetLoveMusic(int user_id, Json::Value& resp_json)
   237	        {
   238	            /*
   239	             * 1.組織查詢sql
   240	             * 2.呼叫GetMusic函式
   241	             * */
   242	#define GET_LOVE_MUSIC "select * from music where music_id in(select music_id from love_music where user_id=%d);"
   243	            char sql[1024] = {0}; 
   244	            snprintf(sql, sizeof(sql) - 1, GET_LOVE_MUSIC, user_id);
   245	            return GetMusic(sql, resp_json);
   246	        }

9.查詢音樂、喜歡的音樂、添加喜歡的音樂

在這里插入圖片描述

				http_svr_.Get("/findMusic", [this](const Request& req, Response& resp){
   137						/*
   138						 * 1.會話校驗, 從當前http請求的請求體當中拿到 Cookie對應的value值, 會話ID
   139						 *   1.1 通過會話ID, 在all_sess_當中查找是否有對應的會話
   140						 *      找到了, 則認為該用戶是登錄用戶, 就可以做后續查找音樂的操作
   141						 *      沒有找到, 則認為該用戶是非登錄用戶, 則回傳status為-1
   142						 * 2.通過資料庫模塊在資料表music當中查找音樂資訊, 則將查找到的音樂資訊組織成為json串
   143						 * 3.組織應答
   144						 * */
   145						Json::Value resp_json;
   146						int user_id = all_sess_->CheckSession(req);
   147						resp_json["status"] = user_id;
   148						if(user_id > 0)
   149						{
   150						/* 1.會話校驗成功的邏輯
   151						 * 2.訪問資料庫, 獲取到資料表music的音樂資訊
   152						 */
   153						db_svr_->GetAllMusic(resp_json); 
   154						}
   155	
   156						Json::FastWriter w;
   157						resp.body = w.write(resp_json);
   158						resp.set_header("Content-Type", "application/json");
   159				});	 
   160	
   161				http_svr_.Post("/loveMusic", [this](const Request& req, Response& resp){
   162						Json::Value resp_json;
   163						int user_id = all_sess_->CheckSession(req);
   164						resp_json["status"] = user_id;
   165						if(user_id > 0)
   166						{
   167						cout << "user_id: " << user_id << ", req正文資訊:" <<  req.body << endl;
   168						resp_json["status"] = db_svr_->InsertLoveMusic(req, user_id);
   169						}
   170	
   171						Json::FastWriter w;
   172						resp.body = w.write(resp_json);
   173						resp.set_header("Content-Type", "application/json");
   174						});
   175	
   176				http_svr_.Get("/findLoveMusic", [this](const Request& req, Response& resp){
   177						/*
   178						 * 1.會話校驗,通過會話校驗, 如果成功就能獲取當前請求的用戶ID
   179						 * 2.查資料庫, 當前用戶喜歡的音樂
   180						 * 3.組織json, 將用戶喜歡的音樂回傳給瀏覽器
   181						 * */
   182						Json::Value resp_json;
   183						int user_id = all_sess_->CheckSession(req);
   184						resp_json["status"] = user_id;
   185						if(user_id > 0)
   186						{
   187						//去資料庫當中查詢喜歡的音樂
   188						db_svr_->GetLoveMusic(user_id, resp_json);
   189						}
   190	
   191						Json::FastWriter w;
   192						resp.body = w.write(resp_json);
   193						resp.set_header("Content-Type", "application/json");
   194						}); 
   195				//2.設定http服務器靜態路徑(邏輯根目錄)
   196				http_svr_.set_mount_point("/","./web");
   197				//3.監聽起來
   198				http_svr_.listen(svr_ip_.c_str(),svr_port_);
   199			}

在這里插入圖片描述

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/301282.html

標籤:其他

上一篇:拿到這份Java面試檔案“狂刷”2周,成功拿到阿里P6+的offer!

下一篇:和老婆的一次真實對話

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 網閘典型架構簡述

    網閘架構一般分為兩種:三主機的三系統架構網閘和雙主機的2+1架構網閘。 三主機架構分別為內端機、外端機和仲裁機。三機無論從軟體和硬體上均各自獨立。首先從硬體上來看,三機都用各自獨立的主板、記憶體及存盤設備。從軟體上來看,三機有各自獨立的作業系統。這樣能達到完全的三機獨立。對于“2+1”系統,“2”分為 ......

    uj5u.com 2020-09-10 02:00:44 more
  • 如何從xshell上傳檔案到centos linux虛擬機里

    如何從xshell上傳檔案到centos linux虛擬機里及:虛擬機CentOs下執行 yum -y install lrzsz命令,出現錯誤:鏡像無法找到軟體包 前言 一、安裝lrzsz步驟 二、上傳檔案 三、遇到的問題及解決方案 總結 前言 提示:其實很簡單,往虛擬機上安裝一個上傳檔案的工具 ......

    uj5u.com 2020-09-10 02:00:47 more
  • 一、SQLMAP入門

    一、SQLMAP入門 1、判斷是否存在注入 sqlmap.py -u 網址/id=1 id=1不可缺少。當注入點后面的引數大于兩個時。需要加雙引號, sqlmap.py -u "網址/id=1&uid=1" 2、判斷文本中的請求是否存在注入 從文本中加載http請求,SQLMAP可以從一個文本檔案中 ......

    uj5u.com 2020-09-10 02:00:50 more
  • Metasploit 簡單使用教程

    metasploit 簡單使用教程 浩先生, 2020-08-28 16:18:25 分類專欄: kail 網路安全 linux 文章標簽: linux資訊安全 編輯 著作權 metasploit 使用教程 前言 一、Metasploit是什么? 二、準備作業 三、具體步驟 前言 Msfconsole ......

    uj5u.com 2020-09-10 02:00:53 more
  • 游戲逆向之驅動層與用戶層通訊

    驅動層代碼: #pragma once #include <ntifs.h> #define add_code CTL_CODE(FILE_DEVICE_UNKNOWN,0x800,METHOD_BUFFERED,FILE_ANY_ACCESS) /* 更多游戲逆向視頻www.yxfzedu.com ......

    uj5u.com 2020-09-10 02:00:56 more
  • 北斗電力時鐘(北斗授時服務器)讓網路資料更精準

    北斗電力時鐘(北斗授時服務器)讓網路資料更精準 北斗電力時鐘(北斗授時服務器)讓網路資料更精準 京準電子科技官微——ahjzsz 近幾年,資訊技術的得了快速發展,互聯網在逐漸普及,其在人們生活和生產中都得到了廣泛應用,并且取得了不錯的應用效果。計算機網路資訊在電力系統中的應用,一方面使電力系統的運行 ......

    uj5u.com 2020-09-10 02:01:03 more
  • 【CTF】CTFHub 技能樹 彩蛋 writeup

    ?碎碎念 CTFHub:https://www.ctfhub.com/ 筆者入門CTF時時剛開始刷的是bugku的舊平臺,后來才有了CTFHub。 感覺不論是網頁UI設計,還是題目質量,賽事跟蹤,工具軟體都做得很不錯。 而且因為獨到的金幣制度的確讓人有一種想去刷題賺金幣的感覺。 個人還是非常喜歡這個 ......

    uj5u.com 2020-09-10 02:04:05 more
  • 02windows基礎操作

    我學到了一下幾點 Windows系統目錄結構與滲透的作用 常見Windows的服務詳解 Windows埠詳解 常用的Windows注冊表詳解 hacker DOS命令詳解(net user / type /md /rd/ dir /cd /net use copy、批處理 等) 利用dos命令制作 ......

    uj5u.com 2020-09-10 02:04:18 more
  • 03.Linux基礎操作

    我學到了以下幾點 01Linux系統介紹02系統安裝,密碼啊破解03Linux常用命令04LAMP 01LINUX windows: win03 8 12 16 19 配置不繁瑣 Linux:redhat,centos(紅帽社區版),Ubuntu server,suse unix:金融機構,證券,銀 ......

    uj5u.com 2020-09-10 02:04:30 more
  • 05HTML

    01HTML介紹 02頭部標簽講解03基礎標簽講解04表單標簽講解 HTML前段語言 js1.了解代碼2.根據代碼 懂得挖掘漏洞 (POST注入/XSS漏洞上傳)3.黑帽seo 白帽seo 客戶網站被黑帽植入劫持代碼如何處理4.熟悉html表單 <html><head><title>TDK標題,描述 ......

    uj5u.com 2020-09-10 02:04:36 more
最新发布
  • 2023年最新微信小程式抓包教程

    01 開門見山 隔一個月發一篇文章,不過分。 首先回顧一下《微信系結手機號資料庫被脫庫事件》,我也是第一時間得知了這個訊息,然后跟蹤了整件事情的經過。下面是這起事件的相關截圖以及近日流出的一萬條資料樣本: 個人認為這件事也沒什么,還不如關注一下之前45億快遞資料查詢渠道疑似在近日復活的訊息。 訊息是 ......

    uj5u.com 2023-04-20 08:48:24 more
  • web3 產品介紹:metamask 錢包 使用最多的瀏覽器插件錢包

    Metamask錢包是一種基于區塊鏈技術的數字貨幣錢包,它允許用戶在安全、便捷的環境下管理自己的加密資產。Metamask錢包是以太坊生態系統中最流行的錢包之一,它具有易于使用、安全性高和功能強大等優點。 本文將詳細介紹Metamask錢包的功能和使用方法。 一、 Metamask錢包的功能 數字資 ......

    uj5u.com 2023-04-20 08:47:46 more
  • vulnhub_Earth

    前言 靶機地址->>>vulnhub_Earth 攻擊機ip:192.168.20.121 靶機ip:192.168.20.122 參考文章 https://www.cnblogs.com/Jing-X/archive/2022/04/03/16097695.html https://www.cnb ......

    uj5u.com 2023-04-20 07:46:20 more
  • 從4k到42k,軟體測驗工程師的漲薪史,給我看哭了

    清明節一過,盲猜大家已經無心上班,在數著日子準備過五一,但一想到銀行卡里的余額……瞬間心情就不美麗了。最近,2023年高校畢業生就業調查顯示,本科畢業月平均起薪為5825元。調查一出,便有很多同學表示自己又被平均了。看著這一資料,不免讓人想到前不久中國青年報的一項調查:近六成大學生認為畢業10年內會 ......

    uj5u.com 2023-04-20 07:44:00 more
  • 最新版本 Stable Diffusion 開源 AI 繪畫工具之中文自動提詞篇

    🎈 標簽生成器 由于輸入正向提示詞 prompt 和反向提示詞 negative prompt 都是使用英文,所以對學習母語的我們非常不友好 使用網址:https://tinygeeker.github.io/p/ai-prompt-generator 這個網址是為了讓大家在使用 AI 繪畫的時候 ......

    uj5u.com 2023-04-20 07:43:36 more
  • 漫談前端自動化測驗演進之路及測驗工具分析

    隨著前端技術的不斷發展和應用程式的日益復雜,前端自動化測驗也在不斷演進。隨著 Web 應用程式變得越來越復雜,自動化測驗的需求也越來越高。如今,自動化測驗已經成為 Web 應用程式開發程序中不可或缺的一部分,它們可以幫助開發人員更快地發現和修復錯誤,提高應用程式的性能和可靠性。 ......

    uj5u.com 2023-04-20 07:43:16 more
  • CANN開發實踐:4個DVPP記憶體問題的典型案例解讀

    摘要:由于DVPP媒體資料處理功能對存放輸入、輸出資料的記憶體有更高的要求(例如,記憶體首地址128位元組對齊),因此需呼叫專用的記憶體申請介面,那么本期就分享幾個關于DVPP記憶體問題的典型案例,并給出原因分析及解決方法。 本文分享自華為云社區《FAQ_DVPP記憶體問題案例》,作者:昇騰CANN。 DVPP ......

    uj5u.com 2023-04-20 07:43:03 more
  • msf學習

    msf學習 以kali自帶的msf為例 一、msf核心模塊與功能 msf模塊都放在/usr/share/metasploit-framework/modules目錄下 1、auxiliary 輔助模塊,輔助滲透(埠掃描、登錄密碼爆破、漏洞驗證等) 2、encoders 編碼器模塊,主要包含各種編碼 ......

    uj5u.com 2023-04-20 07:42:59 more
  • Halcon軟體安裝與界面簡介

    1. 下載Halcon17版本到到本地 2. 雙擊安裝包后 3. 步驟如下 1.2 Halcon軟體安裝 界面分為四大塊 1. Halcon的五個助手 1) 影像采集助手:與相機連接,設定相機引數,采集影像 2) 標定助手:九點標定或是其它的標定,生成標定檔案及內參外參,可以將像素單位轉換為長度單位 ......

    uj5u.com 2023-04-20 07:42:17 more
  • 在MacOS下使用Unity3D開發游戲

    第一次發博客,先發一下我的游戲開發環境吧。 去年2月份買了一臺MacBookPro2021 M1pro(以下簡稱mbp),這一年來一直在用mbp開發游戲。我大致分享一下我的開發工具以及使用體驗。 1、Unity 官網鏈接: https://unity.cn/releases 我一般使用的Apple ......

    uj5u.com 2023-04-20 07:40:19 more