CustomRoleHead = CustomRoleHead or BaseClass() function CustomRoleHead:__init() if CustomRoleHead.Instance ~= nil then error("attempt to create singleton(CustomRoleHead) twice!") return end CustomRoleHead.Instance = self self.image_buff_list = {} self.cache_data_state = 0 self.sent_data_state = false local scene_change = function() self:UpdateData() end GlobalEventSystem:Bind(SceneEventType.SCENE_CHANGED, scene_change) -- local onPhonePhotoLoad = function(image_path) -- local sent_succeed = function(result, image) -- if result then -- Message.show("图片上传成功") -- GlobalEventSystem:Fire(EventName.ROLEHEAD_UPLOAD_SUCCEED) -- else -- Message.show("图片上传失败,请再次尝试") -- end -- end -- local mainrole_vo = RoleManager.Instance.mainRoleInfo -- if mainrole_vo then -- CustomRoleHead:getInstance():Sent(sent_succeed, mainrole_vo.picture_ver + 1) -- end -- end -- GlobalEventSystem:Bind(EventName.PHONE_PHOTO_LOADBACK, onPhonePhotoLoad) end function CustomRoleHead:getInstance() if CustomRoleHead.Instance == nil then CustomRoleHead.New() end return CustomRoleHead.Instance end -- ref_tar目标 -- raw_image图片容器 -- role_id 角色ID -- role_career 角色职业 -- role_sex性别 -- role_turn转身数 -- out_server 已不用 可以传 nil -- head_ver 角色头像版本号 -- picture 角色头像路径,如无可传 nil -- is_big是否大图 -- local_picture_id 本地图标的id -- is_ear --是否露头 function CustomRoleHead:Create(ref_tar, raw_image, role_id, role_career, role_sex, role_turn, out_server, head_ver, picture, is_big, local_picture_id, is_ear) local call_func = function(result, image) if ref_tar._use_delete_method then return end if result and image and not IsNull(raw_image) then raw_image.texture = image else logWarn("load imagse is null") end end if role_id == "" or not role_id then logWarn("CustomRoleHead role_id is error") return end -- 兼容聊天AI做的容错,去掉外层的职业性别和转生的判断 role_career = role_career and role_career ~= 0 and role_career or 1 role_sex = role_sex and role_sex ~= 0 and role_sex or 1 role_turn = role_turn and role_turn ~= 0 and role_turn or 1 local now_role_ver = head_ver or "" local has_load = false local load_default_res = function() if has_load then return end has_load = true local cfg = Config.Transfercareer[role_career .. "@" .. role_turn] if not cfg then print("no Transfercareer by roleid:" .. role_id) return end --玩家默认职业相片 加载后可常存 local default_call = function(obj) if obj and obj[0] then call_func(true, obj[0], role_id) else call_func(false, nil, role_id) end end local res_name local ab_name -- if is_big then res_name = "head_icon_" .. cfg.image_id ab_name = "icon_head_icon_" .. res_name -- else -- if is_ear then -- res_name = "head_ear_" .. cfg.icon -- else -- res_name = "head_circle_" .. cfg.icon -- end -- ab_name = "icon_head_" .. res_name -- end lua_resM:loadTexture(self, ab_name, {res_name .. ".png"}, default_call, ASSETS_LEVEL.LOW) end local load_local_picture_res = function() if has_load or not local_picture_id then return end has_load = true --玩家默认职业相片 加载后可常存 local default_call = function(obj) if obj and obj[0] then call_func(true, obj[0], role_id) else call_func(false, nil, role_id) end end local res_name = "" local ab_name = "" -- if is_big then res_name = "head_icon_" .. local_picture_id ab_name = "icon_head_icon_" .. res_name -- else -- if is_ear then -- res_name = "head_ear_" .. local_picture_id -- else -- res_name = "head_circle_" .. local_picture_id -- end -- ab_name = "icon_head_" .. res_name -- end lua_resM:loadTexture(self, ab_name, {res_name .. ".png"}, default_call, ASSETS_LEVEL.LOW) end -- if (now_role_ver == "" or now_role_ver == 0 or picture == "") and (not local_picture_id or local_picture_id == "" or local_picture_id == 0) then -- load_default_res() -- return -- elseif local_picture_id and (local_picture_id ~= 0 and local_picture_id ~= "") then if local_picture_id and (local_picture_id ~= 0 and local_picture_id ~= "") then load_local_picture_res() return else -- setTimeout(load_default_res, 3) load_default_res() end -- local playerInfo = LoginController.Instance:GetPlatUserInfo() -- local server_id = out_server or playerInfo.server_id -- local role_info = role_id -- local role_info_cache = role_info .. now_role_ver -- self.cache_data_state = self.cache_data_state + 1 -- local res_callback = function(is_succ, is_cache, buffer) -- local result = false -- if is_succ and buffer then -- has_load = true -- if not is_cache then -- self.image_buff_list[role_info_cache] = buffer -- end -- result = true -- end -- if call_func then -- call_func(result, buffer, role_id) -- end -- self.cache_data_state = self.cache_data_state - 1 -- --下载成功 ,头像是自己的,只存拍照和相册的 -- if is_succ and role_id == RoleManager.Instance:GetMainRoleId() and (not local_picture_id or local_picture_id == 0 or local_picture_id == "") then -- GlobalEventSystem:Fire(EventName.SAVE_CUSTOME_HEAD) -- end -- end -- local res_path, res_name = UtilGetRolePicPath(role_id, server_id, now_role_ver) -- local save_path = res_path .. res_name -- local role_image = self.image_buff_list[role_info_cache] -- if role_image then -- print("cache role head") -- res_callback(true, true, role_image) -- return -- end -- local read_local_date = function(result) -- if not result then -- return -- end -- local load_func = function(image) -- if image then -- res_callback(true, false, image) -- else -- res_callback(false, false, image) -- end -- end -- Util.LoadRawTexture(save_path, load_func) -- end -- local is_exsit = false -- is_exsit = Util.FileExists(save_path) -- if is_exsit then -- print("local role head") -- read_local_date(true) -- return -- end -- local url = ClientConfig.rolehead_download_url .. "img/" .. role_info .. ".jpg" -- print("download role head") -- HttpUtil.HttpDownLoad(url, save_path, read_local_date, timeout) end function CustomRoleHead:Sent(sent_callback, head_ver) local playerInfo = LoginController.Instance:GetPlatUserInfo() if not playerInfo then return end self.sent_data_state = true local mainrole_vo = RoleManager.Instance:GetMainRoleVo() local get_param = { accname = tostring(playerInfo.account), platname = ClientConfig.plat_name, server = tostring(playerInfo.server_id), player_id = mainrole_vo.role_id, game = "mrsd", } local res_path, res_name = UtilGetRolePicPath(mainrole_vo.role_id, nil, head_ver) local sent_back_func = function(is_succ, data) local load_succeed = false local buffer = nil local call_back = function() if sent_callback then sent_callback(load_succeed, buffer) end self.sent_data_state = false end if is_succ then local json_obj = JsonToTable(data) if json_obj then local ret = json_obj["ret"] if tonumber(ret) == 0 then local load_func = function(image) if image then load_succeed = true buffer = image end call_back(load_succeed, buffer) end Util.LoadRawTexture(res_path .. res_name, load_func) return elseif tonumber(ret) == 2 then --len字节 Message.show("上传图片数据大小超过限制,请重新上传") return end end end call_back(load_succeed, buffer) end HttpUtil.HttpByteBuffSent(ClientConfig.rolehead_url .. "api/upload.php", get_param, res_path .. res_name, sent_back_func) end function CustomRoleHead:ComplainRoleHead(img_name) if img_name == "" or not img_name then Message.show("玩家头像数据异常") return end local sent_back_func = function(is_succ, data) -- local json_obj = JsonToTable(data) -- if is_succ and json_obj then -- local ret = json_obj["ret"] -- if tonumber(ret) == 0 then -- return -- end -- end if is_succ then Message.show("举报已经收到,感谢你的反馈") else Message.show("举报失败") end end local ticket = "414de19367d6eea5ee1014fa75917aa4" local now_method = "set_img_status" local cur_time = tostring(os.time()) local post_param = { time = cur_time, method = now_method, sign = string.lower(Util.md5(ticket .. cur_time .. now_method)), game = "xjhx", imgname = img_name, } HttpUtil.HttpPostRequest(ClientConfig.rolehead_url.."api/api.php", post_param, sent_back_func, 1000) end function CustomRoleHead:UpdateData() if self.cache_data_state > 0 or self.sent_data_state then return end for role_id, buffer in pairs(self.image_buff_list) do if buffer then destroy(buffer) self.image_buff_list[role_id] = nil end end self.image_buff_list = {} end