终于到了接口编写了,这是我学PHP一直以来到目的
这次围绕这用户表进行的,主要实现了用户表的4个接口:
- 注册
- 登陆
- 修改密码
- 销毁账号
说白了就是数据库的增删改查,和上篇博客没啥区别,不过我们还是开始吧。
1.新建模块
新建一个模块专门用来写接口。
2.新建用户表
首先新建一个用户表存储数据
3.接口编写
注:这些接口是根据我目前的知识量编写的,不是最佳编写方式
接口编写的json返回主要靠一个类去封装
class UserStatus
{
var $status;
var $data;
var $message;
}
1.注册
首先先增加数据,就写注册接口吧。
/**
* 注册(增)
*/
public function signUp($name, $password)
{
$data = ["username" => $name, "password" => $password];
$statu = Db::table("user")->insert($data);
$userstatus = new UserStatus();
if ($statu == 1) {
$userstatus->status = 1;
$userstatus->message = "注册成功";
} else {
$userstatus->status = -1;
$userstatus->message = "注册失败";
}
return json_encode($userstatus);
}
测试接口:
http://127.0.0.1/ThinkphpDemo/public/api/User/signUp?name=zhangsan&password=123456789
http://127.0.0.1/ThinkphpDemo/public/api/User/signUp?name=lisi&password=qqwwee
返回json:{"status":1,"data":null,"message":"\u6ce8\u518c\u6210\u529f"}
数据库情况:
2.登陆
/**
* 登陆(查)
*/
public function signIn($name = '', $password = '')
{
$userstatus = new UserStatus();
try {
$model = Db::table("user")->where('username', $name)->where('password', $password)->find();
if ($model == null) {
$userstatus->status = -4;
$userstatus->message = "no data";
$userstatus->data = $model;
return json_encode($userstatus);
} else {
$userstatus->status = 1;
$userstatus->message = "sccuess";
$userstatus->data = $model;
return json_encode($userstatus);
}
} catch (\think\db\exception\DataNotFoundException $e) {
} catch (\think\db\exception\ModelNotFoundException $e) {
} catch (\think\exception\DbException $e) {
}
}
测试接口:
http://127.0.0.1/ThinkphpDemo/public/api/User/signIn?name=zhangsan&password=123456789
返回json:{"status":1,"data":{"id":"4","username":"zhangsan","password":"123456789"},"message":"sccuess"}
3.修改密码
/**
* 修改密码(改)
*/
public function changePassword($name, $old_password, $new_password)
{
try {
$userstatus = new UserStatus();
$query = Db::table('user')
->where('username', $name)
->where('password', $old_password)
->update(['password' => $new_password]);
$userstatus->status = 1;
$userstatus->message = "sccuess";
$userstatus->data = $query;
return json_encode($userstatus);
} catch (PDOException $e) {
} catch (Exception $e) {
}
}
测试接口:
http://127.0.0.1/ThinkphpDemo/public/api/User/changePassword?name=zhangsan&old_password=123456789&new_password=zhangsan
json返回:{"status":1,"data":1,"message":"sccuess"}
数据库情况:
4.销毁账号
/**
* 销毁账号(删)
*/
public function destroyAccount($name, $password)
{
try {
$userstatus = new UserStatus();
$delete = Db::table('user')
->where('username', $name)
->where('password', $password)
->delete();
if ($delete == 1) {
$userstatus->message = "sccuess";
} else if ($delete == 0) {
$userstatus->message = "no the data";
}
$userstatus->status = 1;
$userstatus->data = $delete;
return json_encode($userstatus);
} catch (PDOException $e) {
} catch (Exception $e) {
}
}
接口测试:
http://127.0.0.1/ThinkphpDemo/public/api/User/destroyAccount?name=zhangsan&password=zhangsan
json返回:{"status":1,"data":1,"message":"sccuess"}
数据库情况:
大概就是这样,其实主要还是用到了数据库的增删改查,这种接口十分粗糙,不过也算一模一样了,总算达成了我最初学习PHP的意愿,但是代码并不精美,所以我还是要继续学习下去。