前言
上一篇文章中寫了lua對(duì)接水仙后臺(tái)捕透,不過發(fā)現(xiàn)不能支持圖片上傳,比如修改頭像等碴萧,通過修改乙嘀,支持了
api封裝
require "import"
import "http"
shuixian={
post=function(moduleName,method,postdata,filedata,call)
local url="http://shuixian.ltd/main/api/"
if(filedata==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
}
食用方法
- 導(dǎo)入模塊 或?qū)⑸厦娲a復(fù)制到自己的代碼中,復(fù)制代碼就不需要導(dǎo)入
import "shuixian"
- 調(diào)用方法
shuixian.api(模塊名,方法名,提交參數(shù)(可為表或字符串),文件參數(shù)(可為表或字符串),返回參數(shù))
例子:以用戶系統(tǒng)-用戶注冊(cè)為例
shuixian.api("user","register",postdata,function(code,content,cookie,header))
--[[
在水仙后臺(tái)找到對(duì)應(yīng)系統(tǒng)對(duì)應(yīng)方法的提交接口破喻,如用戶修改接口:
http://shuixian.ltd/main/api/user/user_set.php
則上面方法的模塊名和方法名如下:
http://shuixian.ltd/main/api/{模塊名}/{方法名}.php
postdata說(shuō)明:
以用戶系統(tǒng)-用戶修改接口為例虎谢,官方示例如下:
提交參數(shù){
admin=管理員賬號(hào)
user=用戶賬號(hào)
password=用戶密碼
project=要修改的信息(例:name、signatrue曹质、password婴噩、head、wallet)
例如:
project=name(修改昵稱)
name=要修改的昵稱
project=signatrue(修改簽名)
signatrue=要修改的簽名
project=password(修改密碼)
new_password=要修改的新密碼
project=head(修改頭像)
project=wallet(扣除用戶積分)
num=扣除的數(shù)量
project=mail(修改郵箱)
mail=新郵箱
code=驗(yàn)證碼
}
-- 以修改頭像為例
postdata={
admin="管理員賬號(hào)",
user="注冊(cè)賬號(hào)",
password="用戶密碼",
project="head"
}
filedata={
head="/storage/emulated/0/Pictures/Image.jpg" -- 頭像圖片的路徑
}
返回參數(shù)說(shuō)明:
code:響應(yīng)代碼羽德,2xx表示成功几莽,4xx表示請(qǐng)求錯(cuò)誤,5xx表示服務(wù)器錯(cuò)誤宅静,-1表示出錯(cuò)
content:內(nèi)容章蚣,如果code為-1,則為出錯(cuò)信息
cookie:服務(wù)器返回的用戶身份識(shí)別信息
header:服務(wù)器返回的頭信息
解析返回內(nèi)容:
使用cjson等json包解析
result=cjson.decode(content)
if(result.code==1) then
print("注冊(cè)成功")
elseif(result.code==0) then
print("注冊(cè)失敗-"..result.msg)
else
print("程序錯(cuò)誤-"..result.msg)
end
]]
- 測(cè)試案例
import "cjson"
import "http"
shuixian={
post=function(moduleName,method,postdata,filedata,call)
local url="http://shuixian.ltd/main/api/"
if(filedata==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={
admin="自己在水仙后臺(tái)的管理員賬號(hào)姨夹,也就是qq號(hào)",
user="注冊(cè)賬號(hào)",
password="",
project="head",
key="在后臺(tái)修改配置的key"
}
-- key 看是否在個(gè)人后臺(tái)開啟纤垂,如開啟在postdata中添加參數(shù)即可
-- 無(wú)文件上傳的接口,filedata參數(shù)傳nil即可
filedata={
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("修改成功") -- 編寫自己的業(yè)務(wù)處理代碼
elseif(result.code==0) then
print("修改失敗-"..result.msg) -- 編寫自己的業(yè)務(wù)處理代碼
else
print("程序錯(cuò)誤-"..result.msg) -- 編寫自己的業(yè)務(wù)處理代碼
end
else
end
end)
--[[
返回參數(shù)的多層級(jí)的json磷账,如舉報(bào)記錄接口峭沦,查看文檔返回如下參數(shù):
返回參數(shù){
code=響應(yīng)碼(0失敗,1成功够颠,-1程序錯(cuò)誤)
msg=響應(yīng)信息
data{
id=id
user=舉報(bào)用戶
name=用戶昵稱
head=用戶頭像
content=舉報(bào)內(nèi)容
time=舉報(bào)時(shí)間
state=受理狀態(tài)
result=受理內(nèi)容
}
}
如:
獲取舉報(bào)用戶熙侍,result.data.user即可
]]
總結(jié)
對(duì)接返回json格式數(shù)據(jù)的api榄鉴,就是一個(gè)json解析的操作履磨,通過軟件自帶的網(wǎng)絡(luò)請(qǐng)求模塊即可完成我們的需求。
本文由【產(chǎn)品經(jīng)理不是經(jīng)理】 gzh 同步發(fā)布庆尘,歡迎關(guān)注