westdc-zf1/application/module/Open/App.php

193 lines
4.1 KiB
PHP
Raw Permalink Normal View History

<?php
namespace Open;
use \Helpers\View as view;
use \Helpers\dbh;
use \Open\Open as open;
use \Open\Listener\AppListener as Listener;
class App
{
public $config;
public $db;
public $table;
public $checkFiled = array('phone','realname','unit','address');
public function __construct($db = NULL,$auth = NULL)
{
if(empty($db))
{
$this->db = \Zend_Registry::get('db');
}else{
$this->db = $db;
}
$this->config = \Zend_Registry::get('config');
$Listener = new Listener();
@$this->events()->attachAggregate($Listener);
$this->table = new \Helpers\Table();
}
public function events(\Zend_EventManager_EventCollection $events = NULL)
{
if ($events !== NULL) {
$this->events = $events;
} elseif ($this->events === NULL) {
$this->events = new \Zend_EventManager_EventManager(__CLASS__);
}
return $this->events;
}
public function appStatus()
{
return array(
-1 => '关闭',
0 => '测试',
1 => '启用'
);
}
//检查用户资料完整性
public function checkinfo()
{
$uid = view::User('id');
$sql = "SELECT * FROM ".$this->table->member. " WHERE id=$uid";
$rs = $this->db->query($sql);
$row = $rs->fetch();
foreach($this->checkFiled as $v)
{
if(empty($row[$v]))
{
return "请完善个人信息";
}
}
return true;
}
//获得用户创建的app
public function getUserApp($uid = 0)
{
if(empty($uid))
{
$uid = view::User('id');
}
$sql = "SELECT * FROM ".$this->table->oauth_clients." WHERE user_id=".$uid ." ORDER BY id DESC";
$rs = $this->db->query($sql);
$rows = $rs->fetchAll();
return $rows;
}
//获得某个App的信息
public function getAppInfo($id)
{
if(empty($id))
{
return false;
}
$sql = "SELECT * FROM ".$this->table->oauth_clients." WHERE id=".$id;
$rs = $this->db->query($sql);
$rows = $rs->fetch();
return $rows;
}
//收集创建应用的参数
public function getAppCreateParam(\Zend_Controller_Request_Abstract $request = NULL)
{
$request = new \Zend_Controller_Request_Http();
$data = array(
'subject' => trim($request->getParam('subject')),
'client_domain' => trim($request->getParam('client_domain')),
'redirect_uri' => trim($request->getParam('redirect_uri')),
'status' => (int)$request->getParam('status')
);
return $data;
}//getAppCreateParam
//添加或者编辑
public function appCreate($id = NULL)
{
if(!empty($id))
{
if(!is_numeric($id) || $id<1)
{
return "参数错误";
}
}
$data = $this->getAppCreateParam();
$params = compact('data');
$results = $this->events()->trigger('app.checkParam', $this, $params);
$cache_data = $results->bottom();
if($cache_data !== true)
{
return $cache_data;
}
if(!empty($id))
{
$params = compact('data','id');
}
$results = $this->events()->trigger('app.processData', $this, $params);
$data = $results->bottom();
$dbh = new dbh();
if(empty($id))
{
$id = $dbh->insert($this->table->oauth_clients,$data,true);
if($id > 0)
{
$results = $this->events()->trigger('app.created', $this, compact("data","id"));
$cache_data = $results->bottom();
return $id;
}else{
return "应用创建中发生错误,请重试!";
}
}else{
$status = $dbh->update($this->table->oauth_clients,$data," id=$id ");
if($status)
{
$results = $this->events()->trigger('app.eidted', $this, compact("data","id"));
$cache_data = $results->bottom();
return true;
}else{
return "应用编辑中发生错误,请重试";
}
}
}//appCreate
//删除App
public function delete($id)
{
if(!is_numeric($id) || $id<1)
{
return "参数错误";
}
try{
$sql = "DELETE FROM {$this->table->oauth_clients} WHERE id=$id";
$rs = $this->db->exec($sql);
if($rs)
{
return true;
}else{
return "删除失败";
}
}catch(Excaption $e){
return "服务器处理中遇到错误";
}
}//delete
}