904118851 11 ماه پیش
والد
کامیت
ef49c8b6e4
4فایلهای تغییر یافته به همراه127 افزوده شده و 0 حذف شده
  1. 20 0
      lualib/tools.lua
  2. 51 0
      service/agentmgr/init.lua
  3. 50 0
      service/dbmgr/init.lua
  4. 6 0
      service/doc/mysql_use.sql

+ 20 - 0
lualib/tools.lua

@@ -442,6 +442,26 @@ M.response_db_get_not_have_file_list_by_list = function(fd,msg_body,isok,tab)
     end
     return M.response(fd,200,cjson.encode({code=10000,msg = "获取被删除的文件!",data=tab}))
 end
+
+M.response_setItem = function(fd,isok)
+    local error_json = '{"code": 10001,   "msg": "保存数据失败" }'
+    if not isok  then
+      return M.response(fd,200,error_json)
+    end
+    return M.response(fd,200,cjson.encode({code=10000,msg = "保存数据成功"}))
+end
+
+M.response_getItem = function(fd,isok,tab)
+    local error_json = '{"code": 10001,   "msg": "获取数据失败" }'
+    if not isok  then
+      return M.response(fd,200,error_json)
+    end
+
+    if tab==nil then
+        return M.response(fd,200,error_json)
+    end
+    return M.response(fd,200,cjson.encode({code=10000,msg = "获取数据成功",data=tab}))
+end
 M.getRandomIndex = function(array)
     local count = #array
     local index = math.random(1,count)

+ 51 - 0
service/agentmgr/init.lua

@@ -1041,6 +1041,57 @@ s.resp.get_not_have_file_list_by_list = function(fd,msg_body,user_data)
         user_id = user_id},fd
     )
 end
+
+
+--存储数据
+s.resp.setItem = function(fd,msg_body,user_data)
+    msg_body = cjson.decode(msg_body)
+    if type(user_data) ~= "table" then
+        return  tools.response(fd, 200, "token error!")
+    end
+    local user_id = user_data.user_id
+    -- tools.dump(msg_body)
+    if user_id==nil then
+        return tools.response(fd,100,"user_id==nil setItem !")
+    end
+    msg_body["user_id"] = user_id
+    local isok ,key =  tools.checkData({"key","base64value"},msg_body)
+    if not isok then
+        return tools.response(fd,200,cjson.encode({code=9001,msg = string.format("缺少字段: %s.", key)}))
+    end
+
+    isok  = skynet.call("dbmgr","lua","on_recv","setItem",{
+        key = msg_body.key,
+        base64value = msg_body.base64value,
+        user_id = user_id},fd
+    )
+end
+
+--获取数据
+s.resp.getItem = function(fd,msg_body,user_data)
+    msg_body = cjson.decode(msg_body)
+    if type(user_data) ~= "table" then
+        return  tools.response(fd, 200, "token error!")
+    end
+    local user_id = user_data.user_id
+    -- tools.dump(msg_body)
+    if user_id==nil then
+        return tools.response(fd,100,"user_id==nil getItem !")
+    end
+    msg_body["user_id"] = user_id
+    local isok ,key =  tools.checkData({"key"},msg_body)
+    if not isok then
+        return tools.response(fd,200,cjson.encode({code=9001,msg = string.format("缺少字段: %s.", key)}))
+    end
+
+    isok  = skynet.call("dbmgr","lua","on_recv","getItem",{
+        key = msg_body.key,
+        user_id = user_id},fd
+    )
+end
+
+
+
 --踢掉
 s.resp.kick = function(fd,msg_body,user_data)
     

+ 50 - 0
service/dbmgr/init.lua

@@ -1907,6 +1907,56 @@ s.resp.get_folder_info = function(msg_body,fd)
         end
     end)
 end
+
+s.resp.setItem = function(_msg_body,_fd)
+    skynet.fork(function(msg_body,fd)
+        local sql = string.format("SELECT user_data FROM user_data_tab WHERE  user_id = %d ",msg_body.user_id)
+        local res = db:query(sql)
+        if res and #res > 0 then
+            local tab = tools.getDbResData(res)
+            if tab.user_data~=nil then
+                local temp_tab = cjson.decode(tab.user_data)
+                temp_tab[msg_body.key] = msg_body.base64value
+
+                sql = string.format("UPDATE user_data_tab SET user_data ='%s' WHERE user_id = %d ",cjson.encode(temp_tab),msg_body.user_id)
+                db:query(sql)
+
+                tools.response_setItem(fd,true)
+            else
+                tools.response_setItem(fd,false)
+            end
+        else
+            local temp_tab = {}
+            temp_tab[msg_body.key] = msg_body.base64value
+            sql = string.format("INSERT INTO user_data_tab (user_id,user_data) VALUES (%d, '%s')",msg_body.user_id,cjson.encode(temp_tab))
+            db:query(sql)
+            tools.response_setItem(fd,true)
+        end
+    end,_msg_body,_fd)
+    return true
+end
+
+s.resp.getItem = function(_msg_body,_fd)
+    skynet.fork(function(msg_body,fd)
+        local sql = string.format("SELECT user_data FROM user_data_tab WHERE  user_id = %d ",msg_body.user_id)
+        local res = db:query(sql)
+        if res and #res > 0 then
+            local tab = tools.getDbResData(res)
+            if tab.user_data~=nil then
+
+                local temp_tab = cjson.decode(tab.user_data)
+
+                tools.response_getItem(fd,true,temp_tab[msg_body.key])
+            else
+                tools.response_getItem(fd,false,nil)
+            end
+        else
+            tools.response_getItem(fd,false,nil)
+        end
+    end,_msg_body,_fd)
+    return true
+end
+
 s.init = function()
     db=mysql.connect({
 		host=runconfig.db_tost,

+ 6 - 0
service/doc/mysql_use.sql

@@ -137,6 +137,12 @@ SELECT * FROM users;  --查询用户表的所有内容
 	primary key (id));
 
 
+	--个人数据
+	create table user_data_tab (  
+	id int not null auto_increment COMMENT '唯一id',
+	user_id int not null COMMENT '用户id',
+	user_data JSON COMMENT '用户数据',
+	primary key (id));
 	--配置