## **开发说明**
基于CodeIgniter框架开发。
#### **控制器开发说明**
**接口控制器命名规则**
get方式:`接口名_get()`
post方式:`接口名_post()`
**非接口控制器命名规则**
`_` + ` 控制器名`,并使用`private`修饰
```
class User extends REST_Controller
{
function __construct()
{
// Construct the parent class
parent::__construct();
}
// 用户登录,post方式
// 对应api接口:http://host/api/user/login
public function login_post()
{
// 验证签名
$this->perform_sign_auth([
'username', 'password'
]);
// 获取参数
$username = $this->post('username');
$password = $this->post('password');
// ....
if(true){
// ....
$this->output_success($some_data);
}
// 错误输出
$this->output_error(20001, '用户名或密码错误');
}
// 获取用户数据,get方式
// 对应api接口:http://host/api/user/info
public function info_get()
{
}
// 非接口控制器
private function _get_key($key)
{
}
}
```
#### **验证签名方法**
使用`perform_sign_auth()`验证接口签名。一般情况下每个接口都应**首先**调用此方法来验证签名是否正确。
```
/**
* 验证签名
* @param $fields 参与验证字段
* @return bool
* @throws Exception
*/
$this->perform_sign_auth([
'username', 'password'
]);
```
#### **参数读取方法**
```
/**
* get参数获取
* @param string $key 参数名,为空获取所有get参数值
* @param boolean $xss_clean 是否应用XSS筛选
*/
$this->get($key = NULL, $xss_clean = NULL);
/**
* post参数获取
* @param string $key 参数名,为空获取所有post参数值
* @param boolean $xss_clean 是否应用XSS筛选
*/
$this->post($key = NULL, $xss_clean = NULL);
```
#### **数据输出方法**
```
/**
* 输出成功信息
* @param array $data 数据
* @param array $extra 额外数据
*/
$this->output_success($data, $extra = []);
/**
* 输出错误信息
* @param $code 错误代码
* @param $msg 错误信息
*/
$this->output_error($code, $msg);
```
#### **数据库操作**
系统本身的`$this->db`代表接口系统的数据库操作对象,若要操作业务数据库,需先配置业务数据库连接信息。
配置文件地址:`config/database.php`,文件中对应`$db['business']`数组配置。
**业务数据库调用**
```
// 使用 $this->bizdb 对象,操作同 $this->db
$this->bizdb->insert
$this->bizdb->query
```
#### **错误码命名规则**
错误码`5`位数,前`2`位表示模块代码,后`3`位表示具体错误数字。
1 开头的是全局错误,其他模块前2位依次递增,如:20001,30001
