前言
上一篇文章中寫了lua對接水仙后臺,不過發現不能支持圖片上傳,比如修改頭像等,通過修改,支持了
api封裝
require "import"
import "http"
shuixian={
post=function(moduleName,method,postdata,filedata,call)
local url="http://shuixian.ltd/main/api/"
if(filedata=https://www.cnblogs.com/bushrose/archive/2023/02/25/=nil) then
content,cookie,code,header=http.post(url..moduleName.."/"..method..".php",postdata)
call(code,content,cookie,header)
else
content,cookie,code,header=http.upload(url..moduleName.."/"..method..".php",postdata,filedata)
call(code,content,cookie,header)
end
end
}
食用方法
- 匯入模塊 或將上面代碼復制到自己的代碼中,復制代碼就不需要匯入
import "shuixian"
- 呼叫方法
shuixian.api(模塊名,方法名,提交引數(可為表或字串),檔案引數(可為表或字串),回傳引數)
例子:以用戶系統-用戶注冊為例
shuixian.api("user","register",postdata,function(code,content,cookie,header))
--[[
在水仙后臺找到對應系統對應方法的提交介面,如用戶修改介面:
http://shuixian.ltd/main/api/user/user_set.php
則上面方法的模塊名和方法名如下:
http://shuixian.ltd/main/api/{模塊名}/{方法名}.php
postdata說明:
以用戶系統-用戶修改介面為例,官方示例如下:
提交引數{
admin=管理員賬號
user=用戶賬號
password=用戶密碼
project=要修改的資訊(例:name、signatrue、password、head、wallet)
例如:
project=name(修改昵稱)
name=要修改的昵稱
project=signatrue(修改簽名)
signatrue=要修改的簽名
project=password(修改密碼)
new_password=要修改的新密碼
project=head(修改頭像)
project=wallet(扣除用戶積分)
num=扣除的數量
project=mail(修改郵箱)
mail=新郵箱
code=驗證碼
}
-- 以修改頭像為例
postdata=https://www.cnblogs.com/bushrose/archive/2023/02/25/{
admin="管理員賬號",
user="注冊賬號",
password="用戶密碼",
project="head"
}
filedata=https://www.cnblogs.com/bushrose/archive/2023/02/25/{
head="/storage/emulated/0/Pictures/Image.jpg" -- 頭像圖片的路徑
}
回傳引數說明:
code:回應代碼,2xx表示成功,4xx表示請求錯誤,5xx表示服務器錯誤,-1表示出錯
content:內容,如果code為-1,則為出錯資訊
cookie:服務器回傳的用戶身份識別資訊
header:服務器回傳的頭資訊
決議回傳內容:
使用cjson等json包決議
result=cjson.decode(content)
if(result.code==1) then
print("注冊成功")
elseif(result.code==0) then
print("注冊失敗-"..result.msg)
else
print("程式錯誤-"..result.msg)
end
]]
- 測驗案例
import "cjson"
import "http"
shuixian={
post=function(moduleName,method,postdata,filedata,call)
local url="http://shuixian.ltd/main/api/"
if(filedata=https://www.cnblogs.com/bushrose/archive/2023/02/25/=nil) then
content,cookie,code,header=http.post(url..moduleName.."/"..method..".php",postdata)
call(code,content,cookie,header)
else
content,cookie,code,header=http.upload(url..moduleName.."/"..method..".php",postdata,filedata)
call(code,content,cookie,header)
end
end
}
postdata=https://www.cnblogs.com/bushrose/archive/2023/02/25/{
admin="自己在水仙后臺的管理員賬號,也就是qq號",
user="注冊賬號",
password="",
project="head",
key="在后臺修改配置的key"
}
-- key 看是否在個人后臺開啟,如開啟在postdata中添加引數即可
-- 無檔案上傳的介面,filedata引數傳nil即可
filedata=https://www.cnblogs.com/bushrose/archive/2023/02/25/{
head="/storage/emulated/0/Pictures/Image.jpg" -- 頭像圖片的路徑
}
shuixian.api("user","user_set",postdata,filedata,function(code,content,cookie,header)
if(code==200) then
local result=cjson.decode(content)
if(result.code==1) then
print("修改成功") -- 撰寫自己的業務處理代碼
elseif(result.code==0) then
print("修改失敗-"..result.msg) -- 撰寫自己的業務處理代碼
else
print("程式錯誤-"..result.msg) -- 撰寫自己的業務處理代碼
end
else
end
end)
--[[
回傳引數的多層級的json,如舉報記錄介面,查看檔案回傳如下引數:
回傳引數{
code=回應碼(0失敗,1成功,-1程式錯誤)
msg=回應資訊
data{
id=id
user=舉報用戶
name=用戶昵稱
head=用戶頭像
content=舉報內容
time=舉報時間
state=受理狀態
result=受理內容
}
}
如:
獲取舉報用戶,result.data.user即可
]]
總結
對接回傳json格式資料的api,就是一個json決議的操作,通過軟體自帶的網路請求模塊即可完成我們的需求,
本文由【產品經理不是經理】gzh同步發布,歡迎關注
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/545043.html
標籤:其他
上一篇:高并發系統設計之快取
下一篇:PHP 并發方案建議
