創(chuàng)建數(shù)據(jù)模型
- 在
/app/models
目錄下創(chuàng)建基礎(chǔ)數(shù)據(jù)模型文件BaseModel.php
肉拓,添加代碼:
<?php
use Phalcon\Mvc\Model;
class BaseModel extends Model {
/**
* 重寫getSource()獲取表名方法濒募, 給表名添加前綴
* @return string|void
*/
public function getSource( $tableName = '' ) {
// 讀取前綴配置
$prefix = Config::instance()->get( 'database.prefix', 'ini' );
if ( $tableName == '' ) {
$tableName = parent::getSource();
}
return $prefix . $tableName;
}
/**
* 新增add方法评凝,成功后會(huì)返回LastInsertId
* @param $item
*
* @return int
*/
public function add( $item ) {
try {
$this->create( $item );
return $this->getWriteConnection()->lastInsertId( $this->getSource() );
} catch ( Exception $e ) {
}
return 0;
}
}
- 重寫函數(shù)
getSource
, 自動(dòng)添加表名前綴。- 新增添加數(shù)據(jù)函數(shù)
add
孽亲,將直接返回新增數(shù)據(jù)行ID迹淌。
- 在
/app/models
目錄下創(chuàng)建用戶數(shù)據(jù)模型文件User.php
,添加代碼:
<?php
/**
* Class User
*/
class User extends BaseModel {
private static $instance;
public static function instance() {
self::$instance = new self();
return self::$instance;
}
/**
* 登錄操作
* 根據(jù)手機(jī)號(hào)獲取用戶信息
* 未找到則新增用戶
*
* @param $user_mobile
*
* @return array
* @throws Exception
*/
public function login( $user_mobile ) {
$model = self::findFirstByUserMobile( $user_mobile );
$user = [];
if ( $model ) {
// 檢查帳號(hào)狀態(tài)
if ( ! $model->user_status ) {
throw new Exception( '帳號(hào)未激活' );
}
// 設(shè)置最后登錄時(shí)間
if ( ! $model->update( [ 'user_uptime' => time() ] ) ) {
throw new Exception( '更新帳號(hào)失敗' );
}
$user['user_token'] = $model->user_token;
} else {
$user['user_token'] = Authentication::instance()->generateToken();
// 初始化帳號(hào)數(shù)據(jù)
$item = [
'user_mobile' => $user_mobile,
'user_token' => $user['user_token'],
'user_status' => 1, // 創(chuàng)建帳號(hào)時(shí)自動(dòng)激活狀態(tài)
'user_addtime' => time(),
'user_uptime' => time(),
];
if ( ! $this->add( $item ) ) {
throw new Exception( '創(chuàng)建帳號(hào)失敗' );
}
}
return $user;
}
}
- 每一個(gè)數(shù)據(jù)模型對(duì)應(yīng)一個(gè)數(shù)據(jù)表:
- 數(shù)據(jù)表命名規(guī)則, 使用小寫字母或數(shù)字湘今,兩個(gè)單詞之前用英文下劃線“_”分隔敢朱。如非必要,盡量不要以數(shù)字開頭摩瞎。
- 使用對(duì)應(yīng)的數(shù)據(jù)表名拴签,去除表名前綴和下劃線,并將以“_”分隔的單詞首字母大寫旗们,作為數(shù)據(jù)模型文件名和類名蚓哩。
- 這里沒有嚴(yán)格按照官方文檔,對(duì)每個(gè)字段都預(yù)定義一個(gè)對(duì)應(yīng)的屬性字段上渴。(并不影響使用)
- 函數(shù)
login
實(shí)現(xiàn)了:
- 驗(yàn)證手機(jī)號(hào)是否已注冊(cè)帳號(hào)
- 已經(jīng)注冊(cè)且狀態(tài)為激活杖剪,更新登陸時(shí)間,并返回
user_token
- 未注冊(cè)驰贷,自動(dòng)創(chuàng)建帳號(hào)盛嘿,并返回
user_token
- 或返回對(duì)應(yīng)錯(cuò)誤信息
實(shí)現(xiàn)登陸接口
打開 /app/controllers/UserController.php
文件, 完善函數(shù)loginAction
:
/**
* 用戶登陸
* @throws Exception
*/
public function loginAction() {
// 驗(yàn)證請(qǐng)求方法是否是POST
$this->isPost();
// 驗(yàn)證請(qǐng)求參數(shù)
XValidationUser::instance()->login( $this->getPost() );
// 獲取登錄信息
$user = User::instance()->login( $this->getPost( 'user_mobile' ) );
if ( $user ) {
Output::instance( $this->response )->success( (object) $user );
} else {
Output::instance( $this->response )->fail( '登錄失敗' );
}
}
到了這一步括袒,函數(shù)
loginAction
其實(shí)就比較簡(jiǎn)單了:
- 調(diào)用POST請(qǐng)求方法驗(yàn)證函數(shù)
- 調(diào)用請(qǐng)求參數(shù)驗(yàn)證函數(shù)
- 調(diào)用獲取登錄信息數(shù)據(jù)函數(shù)
- 輸出用戶信息(這里只輸出了
user_token
字段次兆,可根據(jù)實(shí)際需要,返回相應(yīng)的數(shù)據(jù))
PS: 這次锹锰,我將前面講的驗(yàn)證模塊和數(shù)據(jù)模塊芥炭,都改成了單例模式。大家可下載代碼查看恃慧。
示例代碼下載
鏈接:https://pan.baidu.com/s/1-Myzt8JDQHExjKz2KJxhPQ 密碼:l3lh