feat: init project codebase
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
use Restserver\Libraries\REST_Controller;
|
||||
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
|
||||
// This can be removed if you use __autoload() in config.php OR use Modular Extensions
|
||||
/** @noinspection PhpIncludeInspection */
|
||||
//To Solve File REST_Controller not found
|
||||
require APPPATH . 'libraries/REST_Controller.php';
|
||||
require APPPATH . 'libraries/Format.php';
|
||||
|
||||
class Table extends REST_Controller
|
||||
{
|
||||
|
||||
function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->load->model('Api_model');
|
||||
// $this->load->model('Record_model');
|
||||
// $this->load->model('Dept_model', 'Dept');
|
||||
// $this->config->load('config', true);
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
$this->load->view('login_view');
|
||||
}
|
||||
|
||||
public function testapi()
|
||||
{
|
||||
echo "test api ok...";
|
||||
}
|
||||
|
||||
public function phpinfo()
|
||||
{
|
||||
phpinfo();
|
||||
}
|
||||
|
||||
public function testdb()
|
||||
{
|
||||
$this->load->database();
|
||||
$query = $this->db->query("show tables");
|
||||
var_dump($query);
|
||||
var_dump($query->result());
|
||||
var_dump($query->row_array());
|
||||
// 有结果表明数据库连接正常 reslut() 与 row_array 结果有时不太一样
|
||||
// 一般加载到时model里面使用。
|
||||
}
|
||||
|
||||
|
||||
function list_get()
|
||||
{
|
||||
|
||||
$items = [
|
||||
["id" => 1,
|
||||
"title" => "www",
|
||||
"status" => "draft",
|
||||
"author" => "qiaokun",
|
||||
"display_time" => "",
|
||||
"pageviews" => 300
|
||||
],
|
||||
["id" => 3,
|
||||
"title" => "bbb",
|
||||
"status" => "bbbb",
|
||||
"author" => "乔锟",
|
||||
"display_time" => "",
|
||||
"pageviews" => 300
|
||||
],
|
||||
];
|
||||
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"data" => [
|
||||
"items" => $items
|
||||
]
|
||||
];
|
||||
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
|
||||
}
|
||||
|
||||
function goods_get()
|
||||
{
|
||||
|
||||
$items = array(
|
||||
array('id' => 1, 'title' => 'iphone 7 ', 'price' => 399, 'num' => 1),
|
||||
array('id' => 2, 'title' => 'hdcms 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 3, 'title' => 'aaaas 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 4, 'title' => 'iphone 7 ', 'price' => 399, 'num' => 1),
|
||||
array('id' => 5, 'title' => 'hdcms 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 6, 'title' => 'aaaas 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 7, 'title' => 'iphone 7 ', 'price' => 399, 'num' => 1),
|
||||
array('id' => 8, 'title' => 'hdcms 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 9, 'title' => 'aaaas 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 10, 'title' => 'iphone 7 ', 'price' => 399, 'num' => 1),
|
||||
array('id' => 11, 'title' => 'hdcms 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 31, 'title' => 'aaaas 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 13, 'title' => 'iphone 7 ', 'price' => 399, 'num' => 1),
|
||||
array('id' => 24, 'title' => 'hdcms 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 35, 'title' => 'aaaas 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 19, 'title' => 'iphone 7 ', 'price' => 399, 'num' => 1),
|
||||
array('id' => 22, 'title' => 'hdcms 7 ', 'price' => 2000, 'num' => 2),
|
||||
array('id' => 33, 'title' => 'aaaas 7 ', 'price' => 2000, 'num' => 2),
|
||||
);
|
||||
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"data" => [
|
||||
"items" => $items
|
||||
]
|
||||
];
|
||||
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* End of file welcome.php */
|
||||
/* Location: ./application/controllers/welcome.php */
|
||||
@@ -0,0 +1,325 @@
|
||||
<?php
|
||||
|
||||
use Restserver\Libraries\REST_Controller;
|
||||
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
|
||||
// This can be removed if you use __autoload() in config.php OR use Modular Extensions
|
||||
/** @noinspection PhpIncludeInspection */
|
||||
//To Solve File REST_Controller not found
|
||||
require APPPATH . 'libraries/REST_Controller.php';
|
||||
require APPPATH . 'libraries/Format.php';
|
||||
|
||||
//require APPPATH . 'libraries/kindeditor/php/JSON.php';
|
||||
|
||||
class Uploadimg extends REST_Controller
|
||||
{
|
||||
|
||||
function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->load->model('Api_model');
|
||||
}
|
||||
|
||||
|
||||
public function testapi_get()
|
||||
{
|
||||
echo "test api ok...";
|
||||
|
||||
echo APPPATH . "\n";
|
||||
echo SELF . "\n";
|
||||
echo BASEPATH . "\n";
|
||||
echo FCPATH . "\n";
|
||||
echo SYSDIR . "\n";
|
||||
var_dump($this->config->item('rest_language'));
|
||||
var_dump($this->config->item('language'));
|
||||
|
||||
var_dump($this->config);
|
||||
}
|
||||
|
||||
public function upload_post()
|
||||
{
|
||||
$uploadDir = FCPATH . 'uploads/';
|
||||
$id = 'T' . $this->POST('identify');
|
||||
$php_path = dirname(__FILE__) . '/';//dirname($_SERVER['DOCUMENT_ROOT']); dirname(__FILE__)
|
||||
// $php_url = "";//dirname($_SERVER['HTTP_HOST']) . '/';//PHP_SELF
|
||||
$php_url = $_SERVER['HTTP_HOST'] . '/'; //PHP_SELF
|
||||
|
||||
$save_path = $uploadDir;
|
||||
//文件保存目录URL
|
||||
$save_url = $php_url . 'uploads/';
|
||||
// nginx服务器端修改绝对路径
|
||||
//$save_url = "http://172.30.3.11/static/home/kindeditor/attached/";
|
||||
|
||||
// var_dump($save_path);
|
||||
// var_dump($save_url);
|
||||
// string(46) "D:\Q\code\vue\CodeIgniter-3.1.10\uploads\imgs\"
|
||||
// string(28) "www.cirest.com:8889/uploads/"
|
||||
|
||||
|
||||
//定义允许上传的文件扩展名
|
||||
$ext_arr = array(
|
||||
'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),
|
||||
'flash' => array('swf', 'flv'),
|
||||
'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'),
|
||||
'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2'),
|
||||
);
|
||||
//最大文件大小 10M 默认是1M
|
||||
$max_size = 10000000;
|
||||
|
||||
$save_path = realpath($save_path) . '/';
|
||||
$save_path = str_replace('\\', '/', $save_path);
|
||||
|
||||
//PHP上传失败
|
||||
if (!empty($_FILES['file']['error'])) {
|
||||
switch ($_FILES['file']['error']) {
|
||||
case '1':
|
||||
$error = '超过php.ini允许的大小。';
|
||||
break;
|
||||
case '2':
|
||||
$error = '超过表单允许的大小。';
|
||||
break;
|
||||
case '3':
|
||||
$error = '图片只有部分被上传。';
|
||||
break;
|
||||
case '4':
|
||||
$error = '请选择图片。';
|
||||
break;
|
||||
case '6':
|
||||
$error = '找不到临时目录。';
|
||||
break;
|
||||
case '7':
|
||||
$error = '写文件到硬盘出错。';
|
||||
break;
|
||||
case '8':
|
||||
$error = 'File upload stopped by extension。';
|
||||
break;
|
||||
case '999':
|
||||
default:
|
||||
$error = '未知错误。';
|
||||
}
|
||||
$this->alert($error);
|
||||
}
|
||||
|
||||
//有上传文件时
|
||||
if (empty($_FILES) === false) {
|
||||
//原文件名
|
||||
$file_name = $_FILES['file']['name'];
|
||||
//服务器上临时文件名
|
||||
$tmp_name = $_FILES['file']['tmp_name'];
|
||||
//文件大小
|
||||
$file_size = $_FILES['file']['size'];
|
||||
//检查文件名
|
||||
if (!$file_name) {
|
||||
$this->alert("请选择文件。");
|
||||
}
|
||||
//检查目录
|
||||
if (@is_dir($save_path) === false) {
|
||||
$this->alert("上传目录不存在。");
|
||||
}
|
||||
//检查目录写权限
|
||||
if (@is_writable($save_path) === false) {
|
||||
$this->alert("上传目录没有写权限。");
|
||||
}
|
||||
//检查是否已上传
|
||||
if (@is_uploaded_file($tmp_name) === false) {
|
||||
$this->alert("上传失败。");
|
||||
}
|
||||
//检查文件大小
|
||||
if ($file_size > $max_size) {
|
||||
$this->alert("上传文件大小超过限制(<10M)。");
|
||||
}
|
||||
//检查目录名
|
||||
$dir_name = empty($_GET['dir']) ? 'image' : trim($_GET['dir']);
|
||||
if (empty($ext_arr[$dir_name])) {
|
||||
$this->alert("目录名不正确。");
|
||||
}
|
||||
//获得文件扩展名
|
||||
$temp_arr = explode(".", $file_name);
|
||||
$file_ext = array_pop($temp_arr);
|
||||
$file_ext = trim($file_ext);
|
||||
$file_ext = strtolower($file_ext);
|
||||
|
||||
//检查扩展名
|
||||
if (!in_array($file_ext, $ext_arr[$dir_name])) {
|
||||
$this->alert("上传文件扩展名是不允许的扩展名。\n只允许" . implode(",", $ext_arr[$dir_name]) . "格式。");
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* 以 T+身份证号作为临时目录 , 以身份证作为正式目录
|
||||
*/
|
||||
$identify = empty($id) ? '' : trim($id);
|
||||
|
||||
if ($identify == '') {
|
||||
echo "Invalid session identify.";
|
||||
exit;
|
||||
}
|
||||
//创建文件夹
|
||||
if ($dir_name !== '') {
|
||||
$save_path .= $dir_name . "/" . $identify . "/";
|
||||
$save_url .= $dir_name . "/" . $identify . "/";
|
||||
if (!file_exists($save_path)) {
|
||||
mkdir($save_path, 0777, true); // true 允许创建多级目录
|
||||
}
|
||||
}
|
||||
$ymd = date("Ym");
|
||||
$save_path .= $ymd . "/";
|
||||
$save_url .= $ymd . "/";
|
||||
if (!file_exists($save_path)) {
|
||||
mkdir($save_path);
|
||||
}
|
||||
//新文件名
|
||||
$new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $file_ext;
|
||||
//移动文件
|
||||
$file_path = $save_path . $new_file_name;
|
||||
if (move_uploaded_file($tmp_name, $file_path) === false) {
|
||||
$this->alert("上传文件失败。");
|
||||
}
|
||||
@chmod($file_path, 0644);
|
||||
$file_url = $save_url . $new_file_name;
|
||||
|
||||
header('Content-type: text/html; charset=UTF-8');
|
||||
// Insert file information in the database
|
||||
// $insert = $db->query("INSERT INTO files (file_name, uploaded_on) VALUES ('".$fileName."', NOW())");
|
||||
$link = "http://" . $file_url;
|
||||
// http://www.cirest.com:8889/uploads/image/T410000000000000000/201902/20190228071354_96833.png
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"error" => 0,
|
||||
"message" => "上传成功",
|
||||
"link" => $link,
|
||||
"filepath" => preg_replace('/^http.*uploads/', '/uploads', $link)
|
||||
];
|
||||
|
||||
echo json_encode($message);
|
||||
// $this->set_response($message, REST_Controller::HTTP_OK);
|
||||
// alert里面使用时 exit() 产生的是空,或字符串,使用原生的json_encode返回统一的字符串,在客户端在统一处理成对象
|
||||
}
|
||||
}
|
||||
|
||||
public function delimg_post()
|
||||
{
|
||||
$php_path = dirname(__FILE__) . '/';//dirname($_SERVER['DOCUMENT_ROOT']); dirname(__FILE__)
|
||||
|
||||
//文件保存目录路径
|
||||
$save_path = $php_path . '../../../../';
|
||||
|
||||
$save_path = realpath($save_path) . '/';
|
||||
$save_path = str_replace('\\', '/', $save_path);
|
||||
// var_dump($save_path);
|
||||
// "D:/Q/code/vue/CodeIgniter-3.1.10/"
|
||||
|
||||
$DelFileName = $this->POST('filename');
|
||||
$DelFileType = $this->POST('isdir');
|
||||
|
||||
$FilePath = $save_path . $DelFileName;
|
||||
|
||||
// var_dump($FilePath);return;
|
||||
|
||||
if ($DelFileType == 'F') {
|
||||
if (!is_file($FilePath)) {
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"data" => "文件不存在 - " . $DelFileName,
|
||||
"message" => "文件不存在 - " . $DelFileName
|
||||
];
|
||||
|
||||
echo json_encode($message);
|
||||
|
||||
} else {
|
||||
|
||||
if (!unlink($FilePath)) {
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"data" => "Error deleting " . $FilePath,
|
||||
"message" => "Error deleting " . $FilePath
|
||||
];
|
||||
echo json_encode($message);
|
||||
|
||||
} else {
|
||||
// 必须返回code 由于前端vue封装的 request 请求,返回数据对code进行判断
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"data" => '服务器删除成功!',
|
||||
"message" => '服务器删除成功!'
|
||||
];
|
||||
echo json_encode($message);
|
||||
}
|
||||
}
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($DelFileType == 'D') {
|
||||
if (!@rmdir($FilePath)) {
|
||||
echo "文件夹 " . $FilePath . " 不为空,不能删除!";
|
||||
} else {
|
||||
echo "succeed";
|
||||
}
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
private function alert($msg)
|
||||
{
|
||||
header('Content-type: text/html; charset=UTF-8');
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"error" => 1,
|
||||
"message" => $msg
|
||||
];
|
||||
echo json_encode($message);
|
||||
exit();
|
||||
// var_dump($message);
|
||||
// $this->set_response($message, REST_Controller::HTTP_OK);
|
||||
// die();
|
||||
}
|
||||
|
||||
|
||||
public function onsubmit_post()
|
||||
{
|
||||
$identify = $this->POST('identify');
|
||||
$phone = $this->POST('phone');
|
||||
$idinfo = $this->POST('idinfo');
|
||||
$bankinfo = $this->POST('bankinfo');
|
||||
// $data = [
|
||||
// 'identify' => $identify,
|
||||
// 'phone' => $phone,
|
||||
// 'idinfo' => $idinfo,
|
||||
// 'check' => '待审核'
|
||||
// ];
|
||||
|
||||
// 写入数据库表 身份证号,手机号,证件照,文件路径等
|
||||
$where = [
|
||||
'identify' => $identify,
|
||||
'phone' => $phone,
|
||||
];
|
||||
|
||||
$data = [
|
||||
'idinfo' => $idinfo,
|
||||
'bankinfo' => $bankinfo,
|
||||
'check' => '待审核'
|
||||
];
|
||||
|
||||
$result = $this->Api_model->saveEdit('upload_tbl', $data, $where);
|
||||
|
||||
if ($result) {
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"message" => '写入数据库表成功,请请待审核通知!',
|
||||
"data" => array_merge($where, $data)
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
} else {
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"message" => '写入数据库表失败!',
|
||||
"data" => array_merge($where, $data)
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* End of file welcome.php */
|
||||
/* Location: ./application/controllers/welcome.php */
|
||||
@@ -0,0 +1,559 @@
|
||||
<?php
|
||||
|
||||
use Restserver\Libraries\REST_Controller;
|
||||
|
||||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||||
|
||||
// This can be removed if you use __autoload() in config.php OR use Modular Extensions
|
||||
/** @noinspection PhpIncludeInspection */
|
||||
//To Solve File REST_Controller not found
|
||||
require APPPATH . 'libraries/REST_Controller.php';
|
||||
require APPPATH . 'libraries/Format.php';
|
||||
|
||||
class User extends REST_Controller
|
||||
{
|
||||
|
||||
function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->load->model('Api_model');
|
||||
// $this->load->model('Record_model');
|
||||
// $this->load->model('Dept_model', 'Dept');
|
||||
// $this->config->load('config', true);
|
||||
}
|
||||
|
||||
public function index_get()
|
||||
{
|
||||
$this->load->view('login_view');
|
||||
}
|
||||
|
||||
public function testapi_get()
|
||||
{
|
||||
echo "test api ok...";
|
||||
|
||||
echo APPPATH . "\n";
|
||||
echo SELF . "\n";
|
||||
echo BASEPATH . "\n";
|
||||
echo FCPATH . "\n";
|
||||
echo SYSDIR . "\n";
|
||||
var_dump($this->config->item('rest_language'));
|
||||
var_dump($this->config->item('language'));
|
||||
|
||||
var_dump($this->config);
|
||||
|
||||
}
|
||||
|
||||
public function phpinfo_get()
|
||||
{
|
||||
phpinfo();
|
||||
}
|
||||
|
||||
public function testdb_get()
|
||||
{
|
||||
$this->load->database();
|
||||
$query = $this->db->query("show tables");
|
||||
var_dump($query);
|
||||
var_dump($query->result());
|
||||
var_dump($query->row_array());
|
||||
// 有结果表明数据库连接正常 reslut() 与 row_array 结果有时不太一样
|
||||
// 一般加载到时model里面使用。
|
||||
}
|
||||
|
||||
|
||||
/* Helper Methods */
|
||||
/**
|
||||
* 生成 token
|
||||
* @param
|
||||
* @return string 40个字符
|
||||
*/
|
||||
private function _generate_token()
|
||||
{
|
||||
do {
|
||||
// Generate a random salt
|
||||
$salt = base_convert(bin2hex($this->security->get_random_bytes(64)), 16, 36);
|
||||
|
||||
// If an error occurred, then fall back to the previous method
|
||||
if ($salt === FALSE) {
|
||||
$salt = hash('sha256', time() . mt_rand());
|
||||
}
|
||||
|
||||
$new_key = substr($salt, 0, config_item('rest_key_length'));
|
||||
} while ($this->_token_exists($new_key));
|
||||
|
||||
return $new_key;
|
||||
}
|
||||
|
||||
/* Private Data Methods */
|
||||
|
||||
private function _token_exists($token)
|
||||
{
|
||||
return $this->rest->db
|
||||
->where('token', $token)
|
||||
->count_all_results('auth') > 0;
|
||||
}
|
||||
|
||||
private function _insert_token($token, $data)
|
||||
{
|
||||
$data['token'] = $token;
|
||||
$data['date_created'] = function_exists('now') ? now() : time();
|
||||
|
||||
return $this->rest->db
|
||||
->set($data)
|
||||
->insert('auth');
|
||||
}
|
||||
|
||||
private function _update_token($token, $data)
|
||||
{
|
||||
return $this->rest->db
|
||||
->where('token', $token)
|
||||
->update('auth', $data);
|
||||
}
|
||||
|
||||
|
||||
function login_post()
|
||||
{
|
||||
$username = $this->post('username'); // POST param
|
||||
$password = $this->post('password'); // POST param
|
||||
// var_dump($username);
|
||||
// var_dump($password);
|
||||
$input_account = $username;
|
||||
$input_password = md5($password);
|
||||
// $result = $this->Api_model->app_user_login_validate($input_account, $input_password);
|
||||
// 用户名密码正确 生成token 返回
|
||||
if (1) {
|
||||
$token = $this->_generate_token();
|
||||
// "token" => $token
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"data" => [
|
||||
"token" => "admin-token"
|
||||
]
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
} else {
|
||||
$message = [
|
||||
"code" => 60204,
|
||||
"message" => 'Account and password are incorrect.'
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
}
|
||||
}
|
||||
|
||||
// 根据token拉取用户信息 get
|
||||
function info_get()
|
||||
{
|
||||
// $result = $this->some_model();
|
||||
$result['success'] = TRUE;
|
||||
|
||||
// 获取用户信息成功
|
||||
if ($result['success']) {
|
||||
$info = [
|
||||
"roles" => ["editor"],
|
||||
"introduction" => "I am a super administrator",
|
||||
"avatar" => "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
|
||||
"name" => "Super Admin",
|
||||
"identify" => "410000000000000000",
|
||||
"phone" => "13633838282"
|
||||
];
|
||||
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"data" => $info,
|
||||
"_SERVER" => $_SERVER,
|
||||
"_GET" => $_GET
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
} else {
|
||||
$message = [
|
||||
"code" => 50008,
|
||||
"message" => 'Login failed, unable to get user details.'
|
||||
];
|
||||
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function logout_post()
|
||||
{
|
||||
$message = [
|
||||
"code" => 20000,
|
||||
"data" => 'success'
|
||||
];
|
||||
$this->set_response($message, REST_Controller::HTTP_OK);
|
||||
}
|
||||
|
||||
function login()
|
||||
{
|
||||
$this->SET_HEADER; // 设置php CI 处理 CORS 自定义头部
|
||||
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] == 'POST') { // 只处理post请求,否则options请求 500错误
|
||||
$json_params = file_get_contents('php://input');
|
||||
$data = json_decode($json_params, true);
|
||||
|
||||
if (!empty($data)) {
|
||||
if (!empty($data['username']) && !empty($data['password'])) {
|
||||
$username = $data['username'];
|
||||
$password = $data['password'];
|
||||
$input_account = $username;
|
||||
$input_password = md5($password);
|
||||
// $results = $this->phpIonicLoginAuthValidateLogin($username, $password);
|
||||
$result = $this->Api_model->app_user_login_validate($input_account, $input_password);
|
||||
|
||||
// $token=$_SERVER['x-auth-token'];
|
||||
// 用户名密码正确 生成token 返回
|
||||
$token = $this->createToken(10000);
|
||||
|
||||
$data = array(
|
||||
"code" => 20000,
|
||||
"data" => array(
|
||||
"token" => "admin-token"
|
||||
// "token" => $token
|
||||
),
|
||||
"params" => $json_params
|
||||
);
|
||||
|
||||
echo json_encode($data);
|
||||
// 用户名密码不正确
|
||||
// return {
|
||||
// code:
|
||||
// 60204,
|
||||
// message: 'Account and password are incorrect.'
|
||||
// }
|
||||
|
||||
|
||||
if ($result['success']) {
|
||||
echo json_encode($this->saveLoginInfo($result['userinfo']));
|
||||
} else {
|
||||
// 校验失败,写入token
|
||||
$this->output->set_status_header(300);
|
||||
echo '{"success": false,"message": "用户名或密码错误","jump":"","user":"' . $username . '"}';
|
||||
}
|
||||
|
||||
} else {
|
||||
$results = array(
|
||||
"result" => "Error - data incomplete!",
|
||||
);
|
||||
|
||||
$jsonData = json_encode($results);
|
||||
echo $jsonData;
|
||||
}
|
||||
} else { // no data post
|
||||
$results = array(
|
||||
"result" => "Error - no data!",
|
||||
);
|
||||
$jsonData = json_encode($results);
|
||||
echo $jsonData;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 根据token拉取用户信息 get
|
||||
function info()
|
||||
{
|
||||
$this->SET_HEADER; // 设置php CI 处理 CORS 自定义头部
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] == 'GET') { // 只处理post请求,否则options请求 500错误
|
||||
$json_params = file_get_contents('php://input');
|
||||
$data = json_decode($json_params, true);
|
||||
|
||||
// $token=$_SERVER['x-auth-token'];
|
||||
|
||||
// 获取用户信息成功
|
||||
$info = array(
|
||||
"roles" => array(
|
||||
"admin"
|
||||
),
|
||||
"introduction" => "I am a super administrator",
|
||||
"avatar" => "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
|
||||
"name" => "Super Admin",
|
||||
);
|
||||
|
||||
echo json_encode(
|
||||
array(
|
||||
"code" => 20000,
|
||||
"data" => $info,
|
||||
"_SERVER" => $_SERVER,
|
||||
"_GET" => $_GET
|
||||
)
|
||||
);
|
||||
|
||||
// 获取用户信息失败
|
||||
// return {
|
||||
// code: 50008,
|
||||
// message: 'Login failed, unable to get user details.'
|
||||
// }
|
||||
// echo json_encode(
|
||||
// array(
|
||||
// "code" => 50008,
|
||||
// "message" => "Login failed, unable to get user details."
|
||||
// )
|
||||
// );
|
||||
|
||||
return;
|
||||
|
||||
if (!empty($data)) {
|
||||
if (!empty($data['username']) && !empty($data['password'])) {
|
||||
$username = $data['username'];
|
||||
$password = $data['password'];
|
||||
$input_account = $username;
|
||||
$input_password = md5($password);
|
||||
// $results = $this->phpIonicLoginAuthValidateLogin($username, $password);
|
||||
$result = $this->Api_model->app_user_login_validate($input_account, $input_password);
|
||||
|
||||
if ($result['success']) {
|
||||
echo json_encode($this->saveLoginInfo($result['userinfo']));
|
||||
} else {
|
||||
// 校验失败,写入token
|
||||
$this->output->set_status_header(300);
|
||||
echo '{"success": false,"message": "用户名或密码错误","jump":"","user":"' . $username . '"}';
|
||||
}
|
||||
|
||||
} else {
|
||||
$results = array(
|
||||
"result" => "Error - data incomplete!",
|
||||
);
|
||||
|
||||
$jsonData = json_encode($results);
|
||||
echo $jsonData;
|
||||
}
|
||||
} else { // no data post
|
||||
$results = array(
|
||||
"result" => "Error - no data!",
|
||||
);
|
||||
$jsonData = json_encode($results);
|
||||
echo $jsonData;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function logout()
|
||||
{
|
||||
$this->SET_HEADER; // 设置php CI 处理 CORS 自定义头部
|
||||
if ($_SERVER["REQUEST_METHOD"] == 'POST') { // 只处理post请求,否则options请求 500错误
|
||||
echo json_encode(array(
|
||||
"code" => 20000,
|
||||
"data" => 'sucess'
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* For 微信认证及全登录时保留登录日志信息
|
||||
*/
|
||||
function saveLoginInfo($userinfo)
|
||||
{
|
||||
$token = md5($userinfo["name"] . date('y-m-d H:i:s', time()));
|
||||
$arr2 = array('token' => $token);
|
||||
$userinfo["token"] = $token;
|
||||
$this->Bas->saveAdd(
|
||||
'auth',
|
||||
array(
|
||||
'token' => $token,
|
||||
'expiredAt' => date('Y-m-d H:i:s', strtotime('+1 day')),
|
||||
'onlineIp' => $this->input->ip_address(),
|
||||
'userLoginInfo' => json_encode($userinfo),
|
||||
'creatorId' => $userinfo["name"],
|
||||
'createdAt' => date('Y-m-d H:i:s')
|
||||
)
|
||||
);
|
||||
|
||||
// 返回信息
|
||||
$results = array(
|
||||
"success" => true,
|
||||
"message" => "APP登陆成功",
|
||||
"user" => $userinfo,
|
||||
'session' => $_SESSION,
|
||||
);
|
||||
|
||||
$this->Bas->saveEdit('userinfo', array('LASTLOGIN' => date('y-m-d H:i:s', time()), 'LASTIP' => $this->input->ip_address()), array('USERNAME' => $userinfo["name"]));
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行CURL请求,并封装返回对象
|
||||
*/
|
||||
private function execCURL($ch)
|
||||
{
|
||||
$response = curl_exec($ch);
|
||||
$error = curl_error($ch);
|
||||
$result = array('header' => '',
|
||||
'content' => '',
|
||||
'curl_error' => '',
|
||||
'http_code' => '',
|
||||
'last_url' => '');
|
||||
|
||||
if ($error != "") {
|
||||
$result['curl_error'] = $error;
|
||||
return $result;
|
||||
}
|
||||
|
||||
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
|
||||
$result['header'] = str_replace(array("\r\n", "\r", "\n"), "<br/>", substr($response, 0, $header_size));
|
||||
$result['content'] = substr($response, $header_size);
|
||||
$result['http_code'] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
$result['last_url'] = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
|
||||
$result["base_resp"] = array();
|
||||
$result["base_resp"]["ret"] = $result['http_code'] == 200 ? 0 : $result['http_code'];
|
||||
$result["base_resp"]["err_msg"] = $result['http_code'] == 200 ? "ok" : $result["curl_error"];
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* GET 请求
|
||||
* @param string $url
|
||||
*/
|
||||
private function http_get($url)
|
||||
{
|
||||
$oCurl = curl_init();
|
||||
if (stripos($url, "https://") !== FALSE) {
|
||||
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
|
||||
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
|
||||
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
|
||||
}
|
||||
curl_setopt($oCurl, CURLOPT_URL, $url);
|
||||
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($oCurl, CURLOPT_VERBOSE, 1);
|
||||
curl_setopt($oCurl, CURLOPT_HEADER, 1);
|
||||
|
||||
// $sContent = curl_exec($oCurl);
|
||||
// $aStatus = curl_getinfo($oCurl);
|
||||
$sContent = $this->execCURL($oCurl);
|
||||
curl_close($oCurl);
|
||||
|
||||
return $sContent;
|
||||
}
|
||||
|
||||
/**
|
||||
* POST 请求
|
||||
* @param string $url
|
||||
* @param array $param
|
||||
* @param boolean $post_file 是否文件上传
|
||||
* @return string content
|
||||
*/
|
||||
private function http_post($url, $param, $post_file = false)
|
||||
{
|
||||
$oCurl = curl_init();
|
||||
|
||||
if (stripos($url, "https://") !== FALSE) {
|
||||
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
|
||||
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
|
||||
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
|
||||
}
|
||||
if (PHP_VERSION_ID >= 50500 && class_exists('\CURLFile')) {
|
||||
$is_curlFile = true;
|
||||
} else {
|
||||
$is_curlFile = false;
|
||||
if (defined('CURLOPT_SAFE_UPLOAD')) {
|
||||
curl_setopt($oCurl, CURLOPT_SAFE_UPLOAD, false);
|
||||
}
|
||||
}
|
||||
|
||||
if ($post_file) {
|
||||
if ($is_curlFile) {
|
||||
foreach ($param as $key => $val) {
|
||||
if (isset($val["tmp_name"])) {
|
||||
$param[$key] = new \CURLFile(realpath($val["tmp_name"]), $val["type"], $val["name"]);
|
||||
} else if (substr($val, 0, 1) == '@') {
|
||||
$param[$key] = new \CURLFile(realpath(substr($val, 1)));
|
||||
}
|
||||
}
|
||||
}
|
||||
$strPOST = $param;
|
||||
} else {
|
||||
$strPOST = json_encode($param);
|
||||
}
|
||||
|
||||
curl_setopt($oCurl, CURLOPT_URL, $url);
|
||||
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($oCurl, CURLOPT_POST, true);
|
||||
curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
|
||||
curl_setopt($oCurl, CURLOPT_VERBOSE, 1);
|
||||
curl_setopt($oCurl, CURLOPT_HEADER, 1);
|
||||
|
||||
// $sContent = curl_exec($oCurl);
|
||||
// $aStatus = curl_getinfo($oCurl);
|
||||
|
||||
$sContent = $this->execCURL($oCurl);
|
||||
curl_close($oCurl);
|
||||
|
||||
return $sContent;
|
||||
}
|
||||
|
||||
function weixinAuth()
|
||||
{
|
||||
session_start();
|
||||
if ($_SERVER["REQUEST_METHOD"] == 'OPTIONS') {
|
||||
echo "options";
|
||||
die();
|
||||
}
|
||||
// $corpId = "wwxxxxxxxx";
|
||||
// $agentId = "100000";
|
||||
// $appSecret = "fsdfsfsdf";
|
||||
// $localAuthUrl = "http://dj.xxx.com:7000/hotcode/";
|
||||
|
||||
if (!array_key_exists("code", $_REQUEST)) {
|
||||
$redirectUri = urlencode("http://yw.cttha.com/ksh/get-corp-weixin-code.html?redirect_uri=" . urlencode($localAuthUrl));
|
||||
$authUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $corpId . "&redirect_uri=" . $redirectUri . "&response_type=code&scope=snsapi_privateinfo&agentid=" . $agentId . "&state=STATE#wechat_redirect";
|
||||
echo json_encode(array("success" => false, "authUrl" => $authUrl));
|
||||
die();
|
||||
}
|
||||
$getCorpAccessTokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" . $corpId . "&corpsecret=" . $appSecret;
|
||||
$accessToken = "";
|
||||
if (false && $_SESSION["weixinAuth_accessToken"] && $_SESSION["weixinAuth_tokenTime"] && $_SESSION["weixinAuth_tokenExpires"] && time() - intval($_SESSION["weixinAuth_tokenTime"]) < intval($_SESSION["tokenExpires"])) {
|
||||
$accessToken = $_SESSION["weixinAuth_accessToken"];
|
||||
} else {
|
||||
$tokenInfo = $this->http_get($getCorpAccessTokenUrl);
|
||||
$tokenInfo = json_decode($tokenInfo["content"], true);
|
||||
if ($tokenInfo["errcode"] == 0) {
|
||||
$accessToken = $tokenInfo["access_token"];
|
||||
$_SESSION["weixinAuth_accessToke"] = $accessToken;
|
||||
$_SESSION["weixinAuth_tokenTime"] = time();
|
||||
$_SESSION["weixinAuth_tokenExpires"] = $tokenInfo["expires_in"];
|
||||
} else {
|
||||
echo json_encode(array("success" => false, "msg" => $tokenInfo["errmsg"] ? $tokenInfo["errmsg"] : "企业认证失败!"));
|
||||
die();
|
||||
}
|
||||
}
|
||||
$getUserIdUrl = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=" . $accessToken . "&code=" . $_REQUEST["code"];
|
||||
$ajaxUserIdInfo = $this->http_get($getUserIdUrl);
|
||||
// var_dump($ajaxUserIdInfo);die();
|
||||
$userIdInfo = json_decode($ajaxUserIdInfo["content"], true);
|
||||
if ($userIdInfo["errcode"] == 0) {
|
||||
if (array_key_exists("OpenId", $userIdInfo)) {
|
||||
echo json_encode(array("success" => false, "msg" => "不是企业成员!请联系企业管理员,添加您的账号的企业通讯录!"));
|
||||
die();
|
||||
// next(U.error("不是企业成员!请联系企业管理员,添加您的账号的企业通讯录!"));
|
||||
} else if (array_key_exists("UserId", $userIdInfo)) {
|
||||
$getUserInfoUrl = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserdetail?access_token=" . $accessToken;
|
||||
// 44468cd93cdfefb8a7f911b5e1f7dfd0
|
||||
$data = array("user_ticket" => $userIdInfo["user_ticket"]);
|
||||
$ajaxUserInfo = $this->http_post($getUserInfoUrl, $data);
|
||||
$userInfo = json_decode($ajaxUserInfo["content"], true);
|
||||
if ($userInfo["errcode"] == 0) {
|
||||
$user = $this->Api_model->getUserByWxUserId($userIdInfo["UserId"]);
|
||||
if ($user["success"]) {
|
||||
echo json_encode($this->saveLoginInfo($user['userinfo']));
|
||||
die();
|
||||
} else {
|
||||
$_SESSION["wxUserInfo"] = $userInfo;
|
||||
echo json_encode(array("sessionid" => session_id(), "status" => -1, "success" => false, "msg" => "此微信账号(" . $userInfo["name"] . ")没有与系统账号关联,请用您的账号密码登录一次,完成首次绑定!"));
|
||||
die();
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
echo json_encode(array("success" => false, "msg" => $userInfo["errmsg"]));
|
||||
die();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
echo json_encode(array("success" => false, "msg" => $userIdInfo["errmsg"]));
|
||||
die();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* End of file welcome.php */
|
||||
/* Location: ./application/controllers/welcome.php */
|
||||
Reference in New Issue
Block a user