thinkphp-登入接口示例

2016-10-02  本文已影响83人  AdrianRD

接着上一个笔记编写第一个后台接口

新建数据表

新建数据表 编辑列

编写后台接口

在控制器Controller目录中新建名为_A_mysql的php文件,此文件的功能是php操作mysql的常用代码,这里封装成一个文件,为了减少代码的重复,和方便修改mysql的登入信息。

_A_mysql.php代码如下

<?php
    $con = mysql_connect("localhost","root","xiao1234");
    if (!$con){die('Could not connect:'.mysql_error());}
    mysql_select_db("zero",$con);
?>

AccountController.class.php代码如下

<?php
namespace Home\Controller;
use Think\Controller;
header("Content-Type: text/html;charset=utf-8");
header('Access-Control-Allow-Origin:*');//允许跨域


class AccountController extends Controller { 
    public function login(){
        $name = $_GET['name']; // 获取参数
        $password = $_GET['password']; // 获取参数

        $data = $this -> mysql_select($name);

        if(!$data['password'] || $data['password'] !== $password){
            $re->state = 0;  
        }else{
            $re->state = 1;
            $re->data->id = $data['id'];
        }
        $this->ajaxReturn($re);
    }
    
    public function addAccount(){
        $name = $_GET['name']; // 获取参数
        $password = $_GET['password']; // 获取参数
        $result = $this -> mysql_insert($name,$password);
        $this->ajaxReturn($result);
    }

    public function deleteAccount(){
        $name = $_GET['name']; // 获取参数
        $result = $this -> mysql_delete($name);
        $this->ajaxReturn($result);
    }

    public function setPassword(){
        $name = $_GET['name']; // 获取参数
        $password = $_GET['password']; // 获取参数
        $result = $this -> mysql_updata($name,$password);
        $this->ajaxReturn($result);
    }

    public function mysql_insert($name,$password){
        if(json_encode($this->mysql_select($name))=='false'){//是否重名
            include '_A_mysql.php';
            mysql_query("INSERT INTO db_account (name, password) 
                VALUES ('{$name}', '{$password}')");
            mysql_close($con);
            $re = 1;
        }else{
            $re = 0;
        }
        return $re;
    }

    public function mysql_delete($name){
        include '_A_mysql.php';
        mysql_query("DELETE FROM db_account WHERE name='{$name}'");
        mysql_close($con);
        return;
    }

    public function mysql_updata($name,$password){
        include '_A_mysql.php';
        mysql_query("UPDATE db_account SET password = '{$password}'
            WHERE name = '{$name}' ");
        mysql_close($con);
        return;
    }

    public function mysql_select($name){
        include '_A_mysql.php';
        $sql = "SELECT * FROM db_account WHERE name='{$name}'";
        $result = mysql_query($sql);
        $row = mysql_fetch_array($result);//获取一行数据
        mysql_close($con);
        return $row;
    }
}

访问接口

在浏览器中输入

http://localhost/zero/index.php/Home/Account/addAccount?name=adrian&password=angel

网页访问接口

在打开mysql管理工具,可以看到已经多了一条数据

插入数据

ajax请求

打开包含有jqery.js的html页面的控制台

输入如下js代码

$.ajax({
    type: "GET",
    url: "http://localhost/zero/index.php/Home/Account/addAccount?",
    data: {
        name:'simon',
        password:'simon',
    },
    dataType: "json",
    success: function(result){
        console.log('成功回调',result);
    },
    error: function(result){
        console.log('失败回调',result);
    }
});
Paste_Image.png

打开mysql管理工具,可以看到又多了一条记录

ajax请求
上一篇下一篇

猜你喜欢

热点阅读