251 lines
9.7 KiB
PHP
251 lines
9.7 KiB
PHP
<?php
|
|
|
|
namespace app\api\controller;
|
|
|
|
use addons\cms\controller\Diyform;
|
|
use app\common\controller\Api;
|
|
use OSS\OssClient;
|
|
use app\common\controller\Frontend;
|
|
use think\Validate;
|
|
use app\common\model\User;
|
|
/**
|
|
* 示例接口
|
|
*/
|
|
class Government extends Frontend
|
|
{
|
|
|
|
//如果$noNeedLogin为空表示所有接口都需要登录才能请求
|
|
//如果$noNeedRight为空表示所有接口都需要验证权限才能请求
|
|
//如果接口已经设置无需登录,那也就无需鉴权了
|
|
//
|
|
// 无需登录的接口,*表示全部
|
|
protected $noNeedLogin = ['*', '*'];
|
|
// 无需鉴权的接口,*表示全部
|
|
protected $noNeedRight = ['*'];
|
|
|
|
//校验秘钥
|
|
public function checkSign($time,$sign){
|
|
$endTimestamp = strtotime("+300 seconds", $time);
|
|
// var_dump($endTimestamp);exit;
|
|
if(time() > $endTimestamp){
|
|
return false;
|
|
}
|
|
// var_dump($newTimestamp);exit;
|
|
$check_sign = md5($time.'ydool');
|
|
if($check_sign != $sign){
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
/**
|
|
* 修改密码
|
|
*/
|
|
public function changepwd()
|
|
{
|
|
if ($this->request->isPost()) {
|
|
$type = $this->request->post("type");
|
|
$mobile = $this->request->post("mobile");
|
|
|
|
$newpassword = $this->request->post("newpassword");
|
|
$sign = $this->request->post("sign");
|
|
$time = $this->request->post("time");
|
|
$res = $this->checkSign($time,$sign);
|
|
if(!$res){
|
|
return $this->easy_json(400,'fail','error param1');
|
|
}
|
|
// $dialCode = $this->request->post("dialCode");
|
|
|
|
// if (!$newpassword || !$captcha) {
|
|
// $this->error(__('Invalid parameters'));
|
|
// }
|
|
//验证Token
|
|
if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
|
|
return $this->easy_json(400,'fail','Password must be 6 to 30 characters');
|
|
// $this->error(__('Password must be 6 to 30 characters'));
|
|
}
|
|
|
|
$user = \app\common\model\User::getByMobile($mobile);
|
|
if (!$user) {
|
|
return $this->easy_json(400,'fail','User not found');
|
|
// $this->error(__('User not found'));
|
|
}
|
|
//模拟一次登录
|
|
$this->auth->direct($user->id);
|
|
$ret = $this->auth->changepwd($newpassword, '', true);
|
|
if ($ret) {
|
|
return $this->easy_json(200,'success','Reset password successful');
|
|
// $this->success(__('Reset password successful'));
|
|
} else {
|
|
return $this->easy_json(400,'fail',$this->auth->getError());
|
|
// $this->error($this->auth->getError());
|
|
}
|
|
}
|
|
|
|
}
|
|
/**
|
|
* 注册会员
|
|
*/
|
|
public function register()
|
|
{
|
|
if ($this->request->isPost()) {
|
|
$username = $this->request->post('username');
|
|
$password = $this->request->post('password');
|
|
$email = $this->request->post('email');
|
|
$mobile = $this->request->post('mobile', '');
|
|
$sign = $this->request->post("sign");
|
|
$time = $this->request->post("time");
|
|
$res = $this->checkSign($time,$sign);
|
|
// $captcha = $this->request->post('captcha');
|
|
// var_dump($captcha);exit;
|
|
if(!$res){
|
|
return $this->easy_json(400,'fail','error param1');
|
|
}
|
|
if(empty($username) || empty($password) || strlen($username)>50 || strlen($username) < 3 || strlen($password)>30 || strlen($password)<6 || strlen($mobile) != 11){
|
|
// var_dump(12);exit;
|
|
return $this->easy_json(400,'fail','error param');
|
|
}
|
|
|
|
|
|
$rule = [
|
|
'account' => 'require|length:3,50',
|
|
'password' => 'require|length:6,30',
|
|
];
|
|
|
|
$msg = [
|
|
'account.require' => 'Account can not be empty',
|
|
'account.length' => 'Account must be 3 to 50 characters',
|
|
'password.require' => 'Password can not be empty',
|
|
'password.length' => 'Password must be 6 to 30 characters',
|
|
];
|
|
$data = [
|
|
'account' => $username,
|
|
'password' => $password,
|
|
];
|
|
$validate = new Validate($rule, $msg);
|
|
$result = $validate->check($data);
|
|
if (!$result) {
|
|
$message = 'fail';
|
|
// $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
|
|
return $this->easy_json(400,$message,$validate->getError());
|
|
// return false;
|
|
}
|
|
if ($this->auth->register($username, $password, $email, $mobile)) {
|
|
$user = User::get(['username' => $username]);
|
|
return $this->easy_json(200,'success',array('id' => base64_encode($user['id'])));
|
|
} else {
|
|
$message = 'fail';
|
|
if($this->auth->getError() == 'Mobile already exist'){
|
|
$message = '手机号已存在';
|
|
}elseif($this->auth->getError() == 'Username already exist'){
|
|
$message = '用户名已存在';
|
|
}
|
|
return $this->easy_json(400,$message,$this->auth->getError());
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* 会员登录
|
|
*/
|
|
public function login()
|
|
{
|
|
// $this->layout = 'default';
|
|
// $this->view->engine->layout('layout/' . $this->layout);
|
|
// header('Access-Control-Allow-Origin: *');
|
|
// header('Access-Control-Allow-Credentials: true'); // 设置是否允许发送 cookies
|
|
// header('Access-Control-Expose-Headers: *'); //服务器 headers 白名单,可以让客户端进行访问
|
|
// header('Access-Control-Allow-Headers: *');
|
|
// var_dump(base64_encode(1049));exit;
|
|
// var_dump(base64_decode('MTIz'));exit;
|
|
$url = $this->request->request('url', '', 'trim');
|
|
// var_dump(45);exit;
|
|
if ($this->request->isPost()) {
|
|
$username = $this->request->post('username');
|
|
$password = $this->request->post('password');
|
|
$sign = $this->request->post("sign");
|
|
$time = $this->request->post("time");
|
|
$res = $this->checkSign($time,$sign);
|
|
// var_dump($account);exit;
|
|
if(!$res){
|
|
return $this->easy_json(400,'fail','error param1');
|
|
}
|
|
if(empty($username) || empty($password) || strlen($username)>50 || strlen($username) < 3 || strlen($password)>30 || strlen($password)<6){
|
|
// var_dump(12);exit;
|
|
return $this->easy_json(400,'fail','error param');
|
|
}
|
|
|
|
$rule = [
|
|
'account' => 'require|length:3,50',
|
|
'password' => 'require|length:6,30',
|
|
];
|
|
|
|
$msg = [
|
|
'account.require' => 'Account can not be empty',
|
|
'account.length' => 'Account must be 3 to 50 characters',
|
|
'password.require' => 'Password can not be empty',
|
|
'password.length' => 'Password must be 6 to 30 characters',
|
|
];
|
|
$data = [
|
|
'account' => $username,
|
|
'password' => $password,
|
|
];
|
|
$validate = new Validate($rule, $msg);
|
|
$result = $validate->check($data);
|
|
if (!$result) {
|
|
$message = 'fail';
|
|
// $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
|
|
return $this->easy_json(400,$message,$validate->getError());
|
|
// return false;
|
|
}
|
|
// return $this->auth->login($username, $password);
|
|
if ($this->auth->login($username, $password)) {
|
|
$field = Validate::is($username, 'email') ? 'email' : (Validate::regex($username, '/^1\d{10}$/') ? 'mobile' : 'username');
|
|
$user = User::get([$field => $username]);
|
|
return $this->easy_json(200,'success',array('id' => base64_encode($user['id']),'nickname'=>$user['nickname']));
|
|
|
|
// $this->success(__('Logged in successful'), $url ? $url : url('user/index'));
|
|
} else {
|
|
return $this->easy_json(400,'fail',$this->auth->getError());
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private function easy_json($code,$msg,$data)
|
|
{
|
|
$data = [
|
|
'code' => $code,
|
|
'msg' => $msg,
|
|
'data' => $data,
|
|
];
|
|
return json($data);
|
|
|
|
}
|
|
|
|
private function encrypt($data, $key, $cipher = 'aes-256-cbc') {
|
|
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($cipher));
|
|
$encrypted = openssl_encrypt($data, $cipher, $key, 0, $iv);
|
|
return base64_encode($iv . $encrypted);
|
|
}
|
|
|
|
private function decrypt($data, $key, $cipher = 'aes-256-cbc') {
|
|
$data = base64_decode($data);
|
|
$iv = substr($data, 0, openssl_cipher_iv_length($cipher));
|
|
$data = substr($data, openssl_cipher_iv_length($cipher));
|
|
$decrypted = openssl_decrypt($data, $cipher, $key, 0, $iv);
|
|
return $decrypted;
|
|
}
|
|
|
|
|
|
public function delfile()
|
|
{
|
|
$ossClient = new OssClient("LTAI4Fq72VJX1kU4LuqtqD5Z", "fVNcV32xywj0nwaxygq2PpS0aobhKY", "oss-cn-shanghai.aliyuncs.com");
|
|
|
|
$deleteObject = $ossClient->deleteObject("ydool2017", "%E6%AF%94%E7%BF%BC%E9%A3%9E%E8%A1%8C%E7%BD%91//uploads/20231205/9eae1e5dab151ed9246c116717c6c009.png");
|
|
print_r($deleteObject);exit;
|
|
}
|
|
|
|
}
|