2021-01-30 20:59:12 +08:00
< ? php
2021-07-26 17:41:59 +08:00
/**
* @ copyright Copyright ( c ) 2021 勾股工作室
2021-11-24 17:17:29 +08:00
* @ license https :// opensource . org / licenses / Apache - 2.0
2022-02-21 12:47:48 +08:00
* @ link https :// blog . gougucms . com
2021-07-26 17:41:59 +08:00
*/
2021-01-30 20:59:12 +08:00
declare ( strict_types = 1 );
namespace app\admin\controller ;
use app\admin\BaseController ;
use app\admin\model\AdminLog ;
use app\admin\validate\AdminCheck ;
2023-01-29 17:23:27 +08:00
use OSS\Core\OssException ;
use OSS\OssClient ;
2021-01-30 20:59:12 +08:00
use think\exception\ValidateException ;
use think\facade\Db ;
use think\facade\Session ;
class Api extends BaseController
{
//上传文件
public function upload ()
{
$param = get_params ();
2022-05-31 22:01:51 +08:00
//var_dump($param);exit;
$sourse = 'file' ;
if ( isset ( $param [ 'sourse' ])){
$sourse = $param [ 'sourse' ];
}
if ( $sourse == 'file' || $sourse == 'tinymce' ){
if ( request () -> file ( 'file' )){
$file = request () -> file ( 'file' );
}
else {
return to_assign ( 1 , '没有选择上传文件' );
}
}
else {
if ( request () -> file ( 'editormd-image-file' )) {
$file = request () -> file ( 'editormd-image-file' );
} else {
return to_assign ( 1 , '没有选择上传文件' );
}
}
2021-01-30 20:59:12 +08:00
// 获取上传文件的hash散列值
$sha1 = $file -> hash ( 'sha1' );
$md5 = $file -> hash ( 'md5' );
$rule = [
'image' => 'jpg,png,jpeg,gif' ,
'doc' => 'doc,docx,ppt,pptx,xls,xlsx,pdf' ,
'file' => 'zip,gz,7z,rar,tar' ,
2022-06-20 00:13:28 +08:00
'video' => 'mpg,mp4,mpeg,avi,wmv,mov,flv,m4v' ,
2021-01-30 20:59:12 +08:00
];
2022-06-20 00:13:28 +08:00
$fileExt = $rule [ 'image' ] . ',' . $rule [ 'doc' ] . ',' . $rule [ 'file' ] . ',' . $rule [ 'video' ];
2021-01-30 20:59:12 +08:00
//1M=1024*1024=1048576字节
2022-06-20 00:13:28 +08:00
$fileSize = 100 * 1024 * 1024 ;
2021-01-30 20:59:12 +08:00
if ( isset ( $param [ 'type' ]) && $param [ 'type' ]) {
$fileExt = $rule [ $param [ 'type' ]];
}
if ( isset ( $param [ 'size' ]) && $param [ 'size' ]) {
$fileSize = $param [ 'size' ];
}
$validate = \think\facade\Validate :: rule ([
'image' => 'require|fileSize:' . $fileSize . '|fileExt:' . $fileExt ,
]);
$file_check [ 'image' ] = $file ;
if ( ! $validate -> check ( $file_check )) {
2021-09-09 08:33:05 +08:00
return to_assign ( 1 , $validate -> getError ());
2021-01-30 20:59:12 +08:00
}
// 日期前綴
$dataPath = date ( 'Ym' );
$use = 'thumb' ;
2023-01-29 17:23:27 +08:00
$accessKeyId = " LTAI5t7mhH3ij2cNWs1zhPmv " ; ;
$accessKeySecret = " gqo2wMpvi8h5bDBmCpMje6BaiXvcPu " ;
$endpoint = " oss-cn-chengdu.aliyuncs.com " ;
try {
$ossClient = new OssClient ( $accessKeyId , $accessKeySecret , $endpoint );
} catch ( OssException $e ) {
return to_assign ( 1 , $e -> getMessage ());
}
$bucket = " lihai001 " ;
$object = 'storage/' . $dataPath . '/' . $md5 . '.jpg' ;
// $filename = \think\facade\Filesystem::disk('public')->putFile($dataPath, $file, function () use ($md5) {
// return $md5;
// });
try {
$filename = $ossClient -> uploadFile ( $bucket , $object , $file );
} catch ( OssException $e ) {
return to_assign ( 1 , $e -> getMessage ());
}
2021-01-30 20:59:12 +08:00
if ( $filename ) {
//写入到附件表
$data = [];
$path = get_config ( 'filesystem.disks.public.url' );
2023-01-29 17:23:27 +08:00
$data [ 'filepath' ] = $filename [ 'info' ][ 'url' ];
2021-01-30 20:59:12 +08:00
$data [ 'name' ] = $file -> getOriginalName ();
$data [ 'mimetype' ] = $file -> getOriginalMime ();
$data [ 'fileext' ] = $file -> extension ();
$data [ 'filesize' ] = $file -> getSize ();
2023-01-29 17:23:27 +08:00
$data [ 'filename' ] = $object ;
2021-01-30 20:59:12 +08:00
$data [ 'sha1' ] = $sha1 ;
$data [ 'md5' ] = $md5 ;
$data [ 'module' ] = \think\facade\App :: initialize () -> http -> getName ();
$data [ 'action' ] = app ( 'request' ) -> action ();
$data [ 'uploadip' ] = app ( 'request' ) -> ip ();
$data [ 'create_time' ] = time ();
$data [ 'user_id' ] = get_login_admin ( 'id' ) ? get_login_admin ( 'id' ) : 0 ;
if ( $data [ 'module' ] = 'admin' ) {
//通过后台上传的文件直接审核通过
$data [ 'status' ] = 1 ;
$data [ 'admin_id' ] = $data [ 'user_id' ];
$data [ 'audit_time' ] = time ();
}
$data [ 'use' ] = request () -> has ( 'use' ) ? request () -> param ( 'use' ) : $use ; //附件用处
$res [ 'id' ] = Db :: name ( 'file' ) -> insertGetId ( $data );
$res [ 'filepath' ] = $data [ 'filepath' ];
$res [ 'name' ] = $data [ 'name' ];
$res [ 'filename' ] = $data [ 'filename' ];
add_log ( 'upload' , $data [ 'user_id' ], $data );
2022-05-31 22:01:51 +08:00
if ( $sourse == 'editormd' ){
//editormd编辑器上传返回
return json ([ 'success' => 1 , 'message' => '上传成功' , 'url' => $data [ 'filepath' ]]);
}
else if ( $sourse == 'tinymce' ){
//tinymce编辑器上传返回
return json ([ 'success' => 1 , 'message' => '上传成功' , 'location' => $data [ 'filepath' ]]);
}
else {
//普通上传返回
return to_assign ( 0 , '上传成功' , $res );
}
2021-01-30 20:59:12 +08:00
} else {
2021-09-09 08:33:05 +08:00
return to_assign ( 1 , '上传失败,请重试' );
2021-01-30 20:59:12 +08:00
}
}
2022-02-21 12:47:48 +08:00
2021-01-30 20:59:12 +08:00
//获取权限树所需的节点列表
public function get_rule ()
{
$rule = get_admin_rule ();
$group = [];
if ( ! empty ( get_params ( 'id' ))) {
$group = get_admin_group_info ( get_params ( 'id' ))[ 'rules' ];
}
$list = create_tree_list ( 0 , $rule , $group );
2021-09-09 08:33:05 +08:00
return to_assign ( 0 , '' , $list );
2021-01-30 20:59:12 +08:00
}
//获取关键字
public function get_keyword_cate ()
{
$keyword = get_keywords ();
2021-09-09 08:33:05 +08:00
return to_assign ( 0 , '' , $keyword );
2021-01-30 20:59:12 +08:00
}
2021-07-26 17:41:59 +08:00
2021-04-12 21:28:00 +08:00
//获取话题
public function get_topics_cate ()
{
$topic = get_topics ();
2021-09-09 08:33:05 +08:00
return to_assign ( 0 , '' , $topic );
2021-04-12 21:28:00 +08:00
}
2021-01-30 20:59:12 +08:00
//清空缓存
public function cache_clear ()
{
2021-02-19 19:07:41 +08:00
\think\facade\Cache :: clear ();
2021-09-09 08:33:05 +08:00
return to_assign ( 0 , '系统缓存已清空' );
2021-01-30 20:59:12 +08:00
}
//发送测试邮件
public function email_to ( $email )
{
$name = empty ( get_config ( 'webconfig.admin_title' )) ? '系统' : get_config ( 'webconfig.admin_title' );
if ( send_email ( $email , " 一封来自 { $name } 的测试邮件。 " )) {
2021-09-09 08:33:05 +08:00
return to_assign ( 0 , '发送成功,请注意查收' );
2021-01-30 20:59:12 +08:00
}
2021-09-09 08:33:05 +08:00
return to_assign ( 1 , '发送失败' );
2021-01-30 20:59:12 +08:00
}
//修改个人信息
public function edit_personal ()
{
return view ( 'admin/edit_personal' , [
'admin' => get_login_admin (),
]);
}
//保存个人信息修改
public function personal_submit ()
{
2021-02-22 23:43:11 +08:00
if ( request () -> isAjax ()) {
2021-01-30 20:59:12 +08:00
$param = get_params ();
try {
validate ( AdminCheck :: class ) -> scene ( 'editPersonal' ) -> check ( $param );
} catch ( ValidateException $e ) {
// 验证失败 输出错误信息
2021-09-09 08:33:05 +08:00
return to_assign ( 1 , $e -> getError ());
2021-01-30 20:59:12 +08:00
}
unset ( $param [ 'username' ]);
$uid = get_login_admin ( 'id' );
Db :: name ( 'Admin' ) -> where ([
'id' => $uid ,
]) -> strict ( false ) -> field ( true ) -> update ( $param );
$session_admin = get_config ( 'app.session_admin' );
Session :: set ( $session_admin , Db :: name ( 'admin' ) -> find ( $uid ));
return to_assign ();
}
}
//修改密码
public function edit_password ()
{
return view ( 'admin/edit_password' , [
'admin' => get_login_admin (),
]);
}
//保存密码修改
public function password_submit ()
{
2021-02-22 23:43:11 +08:00
if ( request () -> isAjax ()) {
2021-01-30 20:59:12 +08:00
$param = get_params ();
try {
validate ( AdminCheck :: class ) -> scene ( 'editpwd' ) -> check ( $param );
} catch ( ValidateException $e ) {
// 验证失败 输出错误信息
2021-09-09 08:33:05 +08:00
return to_assign ( 1 , $e -> getError ());
2021-01-30 20:59:12 +08:00
}
$admin = get_login_admin ();
if ( set_password ( $param [ 'old_pwd' ], $admin [ 'salt' ]) !== $admin [ 'pwd' ]) {
2021-09-09 08:33:05 +08:00
return to_assign ( 1 , '旧密码不正确!' );
2021-01-30 20:59:12 +08:00
}
unset ( $param [ 'username' ]);
$param [ 'salt' ] = set_salt ( 20 );
$param [ 'pwd' ] = set_password ( $param [ 'pwd' ], $param [ 'salt' ]);
Db :: name ( 'Admin' ) -> where ([
'id' => $admin [ 'id' ],
]) -> strict ( false ) -> field ( true ) -> update ( $param );
$session_admin = get_config ( 'app.session_admin' );
Session :: set ( $session_admin , Db :: name ( 'admin' ) -> find ( $admin [ 'id' ]));
return to_assign ();
}
}
2021-02-23 13:52:07 +08:00
// 测试邮件发送
2021-07-26 17:41:59 +08:00
public function email_test ()
{
2021-02-23 15:43:11 +08:00
$sender = get_params ( 'email' );
2022-08-25 17:13:28 +08:00
//检查是否邮箱格式
$validate = \think\facade\Validate :: rule ([
'email' => 'email'
]);
$data = [
'email' => $sender
];
if ( ! $validate -> check ( $data )) {
return to_assign ( 1 , $validate -> getError ());
}
$email_config = \think\facade\Db :: name ( 'config' ) -> where ( 'name' , 'email' ) -> find ();
2021-02-23 15:43:11 +08:00
$config = unserialize ( $email_config [ 'content' ]);
$content = $config [ 'template' ];
2021-02-23 13:52:07 +08:00
//所有项目必须填写
2021-02-23 15:43:11 +08:00
if ( empty ( $config [ 'smtp' ]) || empty ( $config [ 'smtp_port' ]) || empty ( $config [ 'smtp_user' ]) || empty ( $config [ 'smtp_pwd' ])) {
2021-09-09 08:33:05 +08:00
return to_assign ( 1 , '请完善邮件配置信息!' );
2021-02-23 13:52:07 +08:00
}
2021-07-26 17:41:59 +08:00
$send = send_email ( $sender , '测试邮件' , $content );
2021-02-23 13:52:07 +08:00
if ( $send ) {
2021-09-09 08:33:05 +08:00
return to_assign ( 0 , '邮件发送成功!' );
2021-02-23 13:52:07 +08:00
} else {
2021-09-09 08:33:05 +08:00
return to_assign ( 1 , '邮件发送失败!' );
2021-02-23 13:52:07 +08:00
}
}
2021-01-30 20:59:12 +08:00
//首页获取
public function get_admin_list ()
{
$content = Db :: name ( 'Admin' )
-> where ([ 'status' => 1 ])
-> order ( 'id desc' )
-> limit ( 10 )
-> select () -> toArray ();
2021-07-26 17:41:59 +08:00
$res [ 'data' ] = $content ;
2021-09-09 08:33:05 +08:00
return table_assign ( 0 , '' , $res );
2021-01-30 20:59:12 +08:00
}
//首页获取最新10位用户
public function get_user_list ()
{
$list = Db :: name ( 'User' )
-> where ([ 'status' => 1 ])
-> order ( 'id desc' )
-> limit ( 10 )
-> select () -> toArray ();
2021-07-26 17:41:59 +08:00
foreach ( $list as $key => $val ) {
2022-06-14 00:31:05 +08:00
$list [ $key ][ 'last_login_time' ] = date ( 'Y-m-d H:i:s' , $val [ 'last_login_time' ]);
2021-01-30 20:59:12 +08:00
}
2021-07-26 17:41:59 +08:00
$res [ 'data' ] = $list ;
2021-09-09 08:33:05 +08:00
return table_assign ( 0 , '' , $res );
2021-01-30 20:59:12 +08:00
}
//首页文章
public function get_article_list ()
{
$list = Db :: name ( 'Article' )
2022-06-14 00:31:05 +08:00
-> field ( 'a.id,a.title,a.read,a.status,a.create_time,c.title as cate_title' )
2021-01-30 20:59:12 +08:00
-> alias ( 'a' )
2022-08-25 17:13:28 +08:00
-> join ( 'article_cate c' , 'a.cate_id = c.id' )
-> where ([ 'a.delete_time' => 0 ])
2021-01-30 20:59:12 +08:00
-> order ( 'a.id desc' )
-> limit ( 10 )
-> select () -> toArray ();
2021-07-26 17:41:59 +08:00
foreach ( $list as $key => $val ) {
2022-06-14 00:31:05 +08:00
$list [ $key ][ 'create_time' ] = date ( 'Y-m-d H:i' , $val [ 'create_time' ]);
2021-07-26 17:41:59 +08:00
}
$res [ 'data' ] = $list ;
2021-09-09 08:33:05 +08:00
return table_assign ( 0 , '' , $res );
2021-01-30 20:59:12 +08:00
}
//系统操作日志
public function log_list ()
2021-07-26 17:41:59 +08:00
{
return view ( 'admin/log_list' );
2021-01-30 20:59:12 +08:00
}
2021-07-26 17:41:59 +08:00
//获取系统操作日志
2021-01-30 20:59:12 +08:00
public function get_log_list ()
2021-07-26 17:41:59 +08:00
{
$param = get_params ();
2021-01-30 20:59:12 +08:00
$log = new AdminLog ();
$content = $log -> get_log_list ( $param );
2021-09-09 08:33:05 +08:00
return table_assign ( 0 , '' , $content );
2021-07-26 17:41:59 +08:00
}
2023-01-31 17:09:14 +08:00
2023-01-31 16:39:11 +08:00
public function getbytype (){
$type = get_params ( " type " );
2023-02-01 11:30:54 +08:00
$flag = get_params ( " flag " );
2023-02-13 15:06:59 +08:00
$id = get_params ( " id " );
if ( $flag == 'add' ){
$www [] = [ 'pid' , '<>' , 0 ];
}
if ( $flag == 'edit' ){
$www = [];
}
if ( $id ){
$arr = Db :: table ( 'fa_category' ) -> where ( 'id' , $id ) -> find ();
if ( $arr && $arr [ 'pid' ] == 0 ){
$www [] = [ 'pid' , '<>' , 0 ];
}
$www [] = [ 'id' , '<>' , $id ];
}
2023-01-31 16:39:11 +08:00
$where [ 'type' ] = $type ;
$where [ 'status' ] = 'normal' ;
2023-02-13 15:06:59 +08:00
$list = Db :: table ( 'fa_category' ) -> field ( 'id,pid,type,name' ) -> where ( $where ) -> where ( $www ) -> order ( 'weigh asc,id asc' ) -> select () -> toArray ();
2023-02-01 11:30:54 +08:00
// 添加无
$wu [ 'id' ] = 0 ;
$wu [ 'pid' ] = 0 ;
$wu [ 'type' ] = 0 ;
$wu [ 'name' ] = '无' ;
array_unshift ( $list , $wu );
2023-02-13 15:06:59 +08:00
// halt($list);
2023-01-31 16:39:11 +08:00
return to_assign ( 0 , '' , $list );
}
2021-01-30 20:59:12 +08:00
2023-02-03 11:21:28 +08:00
public function reply (){
$data = get_params ();
$this -> adminInfo = get_admin ( $data [ 'admin_id' ]);
if ( $this -> adminInfo [ 'user_id' ] > 0 ){
$useraddress = Db :: table ( 'fa_szxc_information_useraddress' ) -> where ( 'admin_id' , $this -> adminInfo [ 'id' ]) -> where ( 'status' , 1 ) -> find ();
$input = [];
if ( $useraddress ) {
$input [ 'county' ] = $useraddress [ 'area_id' ];
$input [ 'township' ] = $useraddress [ 'street_id' ];
$input [ 'village' ] = $useraddress [ 'village_id' ];
}
$input [ 'user_id' ] = $this -> adminInfo [ 'user_id' ];
$input [ 'add_time' ] = date ( 'Y-m-d H:i:s' );
$input [ 'content' ] = $data [ 'reply' ];
$input [ 'vote_id' ] = $data [ 'id' ];
$res = Db :: table ( 'fa_article_comment' ) -> strict ( false ) -> field ( true ) -> insert ( $input );
if ( $res ){
Db :: table ( 'fa_article' ) -> where ( 'id' , $input [ 'vote_id' ]) -> update ([ 'is_solve' => 1 ]);
return to_assign ();
} else {
return to_assign ( 1 , '操作失败' );
}
} else {
return to_assign ( 1 , '没有绑定前端用户,无法回复' );
}
}
2023-02-07 17:10:23 +08:00
public function tongji (){
$mmm = $www = [];
$post = get_params ();
//镇农产品需求量分析(事业单位、企业、居民)
$date = 'month' ;
$num_5 = Db :: connect ( 'shop' ) -> table ( 'eb_store_order' )
-> field ( 'from_unixtime(unix_timestamp(create_time),\'%m-%d\') as time, count(DISTINCT order_id) as total' )
-> group ( 'time' )
-> order ( 'time ASC' )
-> whereBetween ( 'create_time' , [ date ( 'Y-m-d H:i:s' , strtotime ( 'first Day of this month 00:00:00' )), date ( 'Y-m-d H:i:s' , strtotime ( 'first Day of next month 00:00:00 -1second' ))])
-> select () -> toarray ();
$day_time = array_column ( $num_5 , 'time' );
$total = array_column ( $num_5 , 'total' );
//农产品市场行情分析、显示
$list [ 'num_6' ] = 6 ;
//农产品市内需求量
$list [ 'num_7' ] = 7 ;
$visitUser = $this -> dateVisitUserNum ( $date , '' );
$orderUser = $this -> orderUserNum ( $date , null , '' );
$orderPrice = $this -> orderPrice ( $date , null , '' );
$payOrderUser = $this -> orderUserNum ( $date , 1 , '' );
$payOrderPrice = $this -> orderPrice ( $date , 1 , '' );
// halt($payOrderUser);
// $userRate = $payOrderUser ? bcdiv($payOrderPrice, $payOrderUser, 2) : 0;
// $orderRate = $visitUser ? bcdiv($orderUser, $visitUser, 2) : 0;
// $payOrderRate = $orderUser ? bcdiv($payOrderUser, $orderUser, 2) : 0;
$result = compact ( 'day_time' , 'total' , 'visitUser' , 'orderUser' , 'orderPrice' , 'payOrderUser' , 'payOrderPrice' );
$result = [ 'data' => $result ];
return table_assign ( 0 , '' , $result );
}
public function dateVisitUserNum ( $date , $merId = null )
{
return Db :: connect ( 'shop' ) -> table ( 'eb_user_visit' ) -> alias ( 'A' ) -> join ( 'eb_store_product B' , 'A.type_id = B.product_id' ) -> when ( $date , function ( $query , $date ) {
getModelTime ( $query , $date , 'A.create_time' );
}) -> when ( $merId , function ( $query , $merId ) {
$query -> where ( 'B.mer_id' , $merId );
}) -> where ( 'A.type' , 'product' ) -> group ( 'uid' ) -> count ();
}
public function orderUserNum ( $date , $paid = null , $merId = null )
{
return Db :: connect ( 'shop' ) -> table ( 'eb_store_order' ) -> when ( $paid , function ( $query , $paid ) {
$query -> where ( 'paid' , $paid );
}) -> when ( $merId , function ( $query , $merId ) {
$query -> where ( 'mer_id' , $merId );
}) -> when ( $date , function ( $query , $date ) use ( $paid ) {
if ( ! $paid ) {
getModelTime ( $query , $date );
// $query->where(function ($query) use ($date) {
// $query->where(function ($query) use ($date) {
// $query->where('paid', 1);
// getModelTime($query, $date, 'pay_time');
// })->whereOr(function ($query) use ($date) {
// $query->where('paid', 0);
// getModelTime($query, $date);
// });
// });
} else
getModelTime ( $query , $date , 'pay_time' );
}) -> group ( 'uid' ) -> count ();
}
public function orderPrice ( $date , $paid = null , $merId = null )
{
return Db :: connect ( 'shop' ) -> table ( 'eb_store_order' ) -> when ( $paid , function ( $query , $paid ) {
$query -> where ( 'paid' , $paid );
}) -> when ( $merId , function ( $query , $merId ) {
$query -> where ( 'mer_id' , $merId );
}) -> when ( $date , function ( $query , $date ) use ( $paid ) {
if ( ! $paid ) {
$query -> where ( function ( $query ) use ( $date ) {
$query -> where ( function ( $query ) use ( $date ) {
$query -> where ( 'paid' , 1 );
getModelTime ( $query , $date , 'pay_time' );
}) -> whereOr ( function ( $query ) use ( $date ) {
$query -> where ( 'paid' , 0 );
getModelTime ( $query , $date );
});
});
} else
getModelTime ( $query , $date , 'pay_time' );
}) -> sum ( 'pay_price' );
}
2023-02-11 10:11:28 +08:00
public function streetList (){
$data = get_params ();
$id = $data [ 'id' ];
$this -> adminInfo = get_admin ( $data [ 'admin_id' ]);
if ( $this -> adminInfo [ 'user_id' ] > 0 ){ //不是超级管理员
$www [ 'user_id' ] = $this -> adminInfo [ 'user_id' ];
$user_address = Db :: table ( 'fa_szxc_information_useraddress' ) -> where ( $www ) -> find ();
if ( $user_address [ 'auth_range' ] == 3 || $user_address [ 'auth_range' ] == 4 ){
$where = [];
} else {
$where [ 'street_code' ] = $user_address [ 'street_id' ];
}
$select = Db :: table ( 'fa_geo_street' ) -> where ( 'area_code' , $id ) -> where ( $where ) -> field ( 'street_id id,street_code code,street_name name' ) -> select ();
} else {
$select = Db :: table ( 'fa_geo_street' ) -> where ( 'area_code' , $id ) -> field ( 'street_id id,street_code code,street_name name' ) -> select ();
}
$select = [ 'data' => $select ];
return table_assign ( 0 , '' , $select );
}
2023-02-07 17:10:23 +08:00
2023-02-03 11:21:28 +08:00
2021-01-30 20:59:12 +08:00
}