car4s/application/api/controller/Index.php
孟凡懂 dc5d9598be
All checks were successful
continuous-integration/drone/push Build is passing
更新导入功能更完善
2022-06-21 04:25:24 +08:00

79 lines
2.2 KiB
PHP

<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\common\model\user4s\Category;
use app\common\model\user4s\Level;
/**
* 首页接口
*/
class Index extends Api
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
protected function arrayToXml($arr, $dom = 0, $item = 0)
{
if (!$dom) {
$dom = new \DOMDocument("1.0");
}
if (!$item) {
$item = $dom->createElement("root");
$dom->appendChild($item);
}
foreach ($arr as $key => $val) {
$itemx = $dom->createElement(is_string($key) ? $key : "item");
$item->appendChild($itemx);
if (!is_array($val)) {
$text = $dom->createTextNode($val);
$itemx->appendChild($text);
} else {
$this->arrayToXml($val, $dom, $itemx);
}
}
return $dom->saveXML();
}
/**
* 首页
*
*/
public function index($type)
{
$type = $this->request->get("type");
$this->level_model = new \app\common\model\user4s\Level;
$all_levels = $this->level_model->where('status', 1)->column('id,name');
$this->cate_model = new \app\common\model\user4s\Category;
$all_cates = $this->cate_model->where('status', 1)->column('id,name');
header('Content-Type:application/xml; charset=utf-8');
$level=array();
foreach ($all_levels as $key => $value) {
$level[] = array(
'id' => $key,
'name' => $value,
);
}
$category=array();
foreach ($all_cates as $key => $value) {
$category[] = array(
'id' => $key,
'name' => $value,
);
}
$data = array('level' => $level,'category' => $category);
if($type==1){
$data = $level;
}elseif($type==2){
$data = $category;
}else{
$data = array('level' => $level,'category' => $category);
}
$req = $this->arrayToXml($data);
//var_dump($req);
echo $req;
exit();
//$this->success('请求成功', ['all_levels' => $all_levels,'all_category' => $all_cates]);
}
}