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 ;
2023-03-17 17:33:00 +08:00
use think\facade\Cache ;
2021-01-30 20:59:12 +08:00
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-03-17 17:33:00 +08:00
/**
* 后台首页获取支付等数据
* @ return mixed
* @ author xaboy
* @ day 2020 / 6 / 25
*/
public function main ()
{
$res = Cache :: store ( 'file' ) -> remember ( self :: class . '@main' , function () {
$today = $this -> mainGroup ( 'today' );
$yesterday = $this -> mainGroup ( 'yesterday' );
$lastWeek = $this -> mainGroup ( date ( 'Y-m-d' , strtotime ( '- 7day' )));
$lastWeekRate = [];
foreach ( $lastWeek as $k => $item ) {
$lastWeekRate [ $k ] = $this -> getRate ( $item , $today [ $k ], 4 );
}
return compact ( 'today' , 'yesterday' , 'lastWeekRate' );
}, 2000 + random_int ( 600 , 1200 ));
// $jyq=Db::connect('shop')->table('eb_product_order_log')->where('area_id',510502)->cache(true)->sum('product_price');
// $lmtq=Db::connect('shop')->table('eb_product_order_log')->where('area_id',510504)->cache(true)->sum('product_price');
// $region=[
// 'jyq'=>$jyq,
// 'lmtq'=>$lmtq,
// 'nxq'=>0,
// 'lx'=>0,
// 'hjx'=>0,
// 'xyx'=>0,
// 'glx'=>0,
// ];
// $jyq1=Db::connect('shop')->table('eb_product_order_log')->where('area_id',510502)->where('mer_type_id',4)->cache(true)->sum('product_price');
// $lmtq1=Db::connect('shop')->table('eb_product_order_log')->where('area_id',510504)->where('mer_type_id',4)->cache(true)->sum('product_price');
// $personal=[
// 'jyq'=>$jyq1,
// 'lmtq'=>$lmtq1,
// 'nxq'=>0,
// 'lx'=>0,
// 'hjx'=>0,
// 'xyx'=>0,
// 'glx'=>0,
// ];
// $res['region']=$region;
// $res['personal']=$personal;
$result = [ 'data' => $res ];
return table_assign ( 0 , '' , $result );
}
/**
* @ param $date
* @ return array
* @ author xaboy
* @ day 2020 / 6 / 25
*/
protected function mainGroup ( $date , $merId = null )
{
$payPrice = getModelTime ( Db :: connect ( 'shop' ) -> table ( 'eb_store_order' ) -> where ( 'paid' , 1 ) -> when ( $merId , function ( $query , $merId ) {
$query -> where ( 'mer_id' , $merId );
}), $date , 'pay_time' ) -> sum ( 'pay_price' );
$payUser = getModelTime ( Db :: connect ( 'shop' ) -> table ( 'eb_store_order' ) -> where ( 'paid' , 1 ) -> when ( $merId , function ( $query , $merId ) {
$query -> where ( 'mer_id' , $merId );
}), $date , 'pay_time' ) -> group ( 'uid' ) -> count ();
$userNum = ( float ) Db :: connect ( 'shop' ) -> table ( 'eb_user' ) -> when ( $date , function ( $query , $date ) {
getModelTime ( $query , $date , 'create_time' );
}) -> count ();
$storeNum = ( float ) Db :: connect ( 'shop' ) -> table ( 'eb_merchant' ) -> where ( 'is_del' , 0 ) -> when ( $date , function ( $query , $date ) {
getModelTime ( $query , $date );
}) -> count ();
$visitUserNum = ( float ) 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 ();
$visitNum = ( float ) Db :: connect ( 'shop' ) -> table ( 'eb_user_visit' ) -> when ( $date , function ( $query , $date ) {
getModelTime ( $query , $date , 'create_time' );
}) -> whereIn ( 'type' , [ 'page' , 'smallProgram' ]) -> count ();
return compact ( 'payPrice' , 'userNum' , 'storeNum' , 'visitUserNum' , 'visitNum' , 'payUser' );
}
/**
* @ param $last
* @ param $today
* @ param int $scale
* @ return int | string | null
* @ author xaboy
* @ day 2020 / 6 / 25
*/
protected function getRate ( $last , $today , $scale = 2 )
{
if ( $last == $today )
return 0 ;
else if ( $last == 0 )
return $today ;
else if ( $today == 0 )
return - $last ;
else
return ( float ) bcdiv ( bcsub (( string ) $today , ( string ) $last , $scale ), ( string ) $last , $scale );
}
/**
* @ param 后台首页获取订单数据
* @ return mixed
* @ author xaboy
* @ day 2020 / 6 / 25
*/
public function order ()
{
2023-02-07 17:10:23 +08:00
2023-03-17 17:33:00 +08:00
$date = get_params ( 'date' ) ? ? 'lately7' ;
$merId = get_params ( 'mer_id' ) ? ? '' ;
$res = Cache :: remember ( self :: class . '@order' . $merId . $date , function () use ( $merId , $date ) {
if ( $date == 'year' ) {
$m = date ( 'm' , time ());
$time [] = $m ;
do {
$time [] = '0' . ( $m - 1 );
$m -- ;
} while ( $m > 1 );
$time = array_reverse ( $time );
} else {
$time = getDatesBetweenTwoDays ( getStartModelTime ( $date ), date ( 'Y-m-d' ));
}
$list = $this -> orderGroupNum ( $date , $merId ) -> toArray ();
$list = array_combine ( array_column ( $list , 'day' ), $list );
$data = [];
foreach ( $time as $item ) {
$data [] = [
'day' => $item ,
'total' => $list [ $item ][ 'total' ] ? ? 0 ,
'user' => $list [ $item ][ 'user' ] ? ? 0 ,
'pay_price' => $list [ $item ][ 'pay_price' ] ? ? 0
];
}
return $data ;
}, 2000 + random_int ( 600 , 1200 ));
$result [ 'day' ] = array_column ( $res , 'day' );
$result [ 'total' ] = array_column ( $res , 'total' );
$result [ 'user' ] = array_column ( $res , 'user' );
$result [ 'pay_price' ] = array_column ( $res , 'pay_price' );
// halt($result);
$result = [ 'data' => $result ];
return table_assign ( 0 , '' , $result );
}
public function orderGroupNum ( $date , $merId = null )
{
$field = Db :: raw ( 'sum(pay_price) as pay_price,count(*) as total,count(distinct uid) as user,pay_time,from_unixtime(unix_timestamp(pay_time),\'%m-%d\') as `day`' );
if ( $date == 'year' ){
$field = Db :: raw ( 'sum(pay_price) as pay_price,count(*) as total,count(distinct uid) as user,pay_time,from_unixtime(unix_timestamp(pay_time),\'%m\') as `day`' );
}
$query = Db :: connect ( 'shop' ) -> table ( 'eb_store_order' ) -> field ( $field )
-> where ( 'paid' , 1 ) -> when ( $date , function ( $query , $date ) {
getModelTime ( $query , $date , 'pay_time' );
}) -> when ( $merId , function ( $query , $merId ) {
$query -> where ( 'mer_id' , $merId );
});
return $query -> order ( 'pay_time ASC' ) -> group ( 'day' ) -> select ();
}
/**
* @ param 后台首页获取用户数据
* @ return \think\response\Json
* @ author xaboy
* @ day 2020 / 9 / 24
*/
public function user ()
{
$date = get_params ( 'date' ) ? ? 'today' ;
$merId = get_params ( 'mer_id' ) ? ? '' ;
$res = Cache :: store ( 'file' ) -> remember ( self :: class . '@user' . $merId . $date , function () use ( $merId , $date ) {
$visitUser = 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 ();
$paid = 0 ;
$orderUser = 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 ();
$orderPrice = 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' );
$paid = 1 ;
$payOrderUser = 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 ();
$payOrderPrice = 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' );
$userRate = $payOrderUser ? bcdiv (( string ) $payOrderPrice , ( string ) $payOrderUser , 2 ) : 0 ;
$orderRate = $visitUser ? bcdiv (( string ) $orderUser , ( string ) $visitUser , 2 ) : 0 ;
$payOrderRate = $orderUser ? bcdiv (( string ) $payOrderUser , ( string ) $orderUser , 2 ) : 0 ;
return compact ( 'visitUser' , 'orderUser' , 'orderPrice' , 'payOrderUser' , 'payOrderPrice' , 'payOrderRate' , 'userRate' , 'orderRate' );
}, 2000 + random_int ( 600 , 1200 ));
$result = [ 'data' => $res ];
return table_assign ( 0 , '' , $result );
}
/**
* @ param 用户统计饼状图
* @ return mixed
* @ author xaboy
* @ day 2020 / 6 / 25
*/
public function userRate ()
{
$date = get_params ( 'date' ) ? ? 'today' ;
$merId = get_params ( 'mer_id' ) ? ? '' ;
$res = Cache :: store ( 'file' ) -> remember ( self :: class . '@userRate' . $merId . $date , function () use ( $merId , $date ) {
$paid = 1 ;
$uids = 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 ) {
getModelTime ( $query , $date , 'pay_time' );
}) -> group ( 'uid' ) -> field ( Db :: raw ( 'uid,sum(pay_price) as pay_price,count(order_id) as total' )) -> select () -> toArray ();
$ids = array_column ( $uids , 'uid' );
$userPayCount = Db :: connect ( 'shop' ) -> table ( 'eb_user' ) -> whereIn ( 'uid' , $ids ) -> column ( 'pay_count' , 'uid' );
$user = count ( $uids );
$oldUser = 0 ;
$totalPrice = 0 ;
$oldTotalPrice = 0 ;
foreach ( $uids as $uid ) {
$totalPrice = bcadd (( string ) $uid [ 'pay_price' ], ( string ) $totalPrice , 2 );
if (( $userPayCount [ $uid [ 'uid' ]] ? ? 0 ) > $uid [ 'total' ]) {
$oldUser ++ ;
$oldTotalPrice = bcadd (( string ) $uid [ 'pay_price' ], ( string ) $oldTotalPrice , 2 );
}
}
$newTotalPrice = bcsub (( string ) $totalPrice , ( string ) $oldTotalPrice , 2 );
$newUser = $user - $oldUser ;
return compact ( 'newTotalPrice' , 'newUser' , 'oldTotalPrice' , 'oldUser' , 'totalPrice' , 'user' );
}, 2000 + random_int ( 600 , 1200 ));
$result = [ 'data' => $res ];
return table_assign ( 0 , '' , $result );
}
/**
* @ param 商品支付排行
* @ return mixed
* @ author xaboy
* @ day 2020 / 6 / 25
*/
public function product ()
{
$date = get_params ( 'date' ) ? ? 'today' ;
$merId = get_params ( 'mer_id' ) ? ? '' ;
$res = Cache :: store ( 'file' ) -> remember ( self :: class . '@product' . $merId . $date , function () use ( $merId , $date ) {
return Db :: connect ( 'shop' ) -> table ( 'eb_store_order_product' ) -> alias ( 'A' ) -> Join ( 'eb_store_order B' , 'A.order_id = B.order_id' )
-> field ( Db :: raw ( 'sum(A.product_num) as total,A.product_id,cart_info' ))
-> withAttr ( 'cart_info' , function ( $val ) {
return json_decode ( $val , true );
})
-> withAttr ( 'store_name' , function ( $val , $date ) {
return $date [ 'cart_info' ][ 'product' ][ 'store_name' ];
})
-> withAttr ( 'image' , function ( $val , $date ) {
return $date [ 'cart_info' ][ 'product' ][ 'image' ];
})
-> when ( $date , function ( $query , $date ) {
getModelTime ( $query , $date , 'B.pay_time' );
}) -> when ( $merId , function ( $query , $merId ) {
$query -> where ( 'B.mer_id' , $merId );
}) -> where ( 'B.paid' , 1 ) -> group ( 'A.product_id' ) -> limit ( 10 ) -> order ( 'total DESC' ) -> select ();
}, 2000 + random_int ( 600 , 1200 ));
$result = [ 'data' => $res ];
return table_assign ( 0 , '' , $result );
}
//商品访客排行
public function productVisit ()
{
$date = get_params ( 'date' ) ? ? 'today' ;
$merId = get_params ( 'mer_id' ) ? ? '' ;
$res = Cache :: store ( 'file' ) -> remember ( self :: class . '@productVisit' . $merId . $date , function () use ( $merId , $date ) {
return Db :: connect ( 'shop' ) -> table ( 'eb_user_visit' ) -> alias ( 'A' ) -> join ( 'eb_store_product B' , 'A.type_id = B.product_id' )
-> join ( 'eb_merchant C' , 'C.mer_id = B.mer_id' )
-> field ( Db :: raw ( 'count(A.type_id) as total,B.image,B.store_name' ))
-> when ( $date , function ( $query , $date ) {
getModelTime ( $query , $date , 'A.create_time' );
}) -> where ( 'A.type' , 'product' ) -> where ( 'B.mer_id' , $merId ) -> group ( 'A.type_id' ) -> order ( 'total DESC' )
-> limit ( 10 ) -> select ();
}, 2000 + random_int ( 600 , 1200 ));
$result = [ 'data' => $res ];
return table_assign ( 0 , '' , $result );
}
/**
* @ param 商品加购排行
* @ return mixed
* @ author xaboy
* @ day 2020 / 6 / 25
*/
public function productCart ()
{
$date = get_params ( 'date' ) ? ? 'today' ;
$merId = get_params ( 'mer_id' ) ? ? '' ;
$res = Cache :: store ( 'file' ) -> remember ( self :: class . '@productCart' . $merId . $date , function () use ( $merId , $date ) {
return Db :: connect ( 'shop' ) -> table ( 'eb_store_product' ) -> alias ( 'A' ) -> Join ( 'eb_store_cart B' , 'A.product_id = B.product_id' )
-> field ( Db :: raw ( 'sum(B.cart_num) as total,A.product_id,A.store_name,A.image' ))
-> when ( $date , function ( $query , $date ) {
getModelTime ( $query , $date , 'B.create_time' );
}) -> when ( $merId , function ( $query , $merId ) {
$query -> where ( 'A.mer_id' , $merId );
}) -> where ( 'B.product_type' , 0 ) -> where ( 'B.is_pay' , 0 ) -> where ( 'B.is_del' , 0 )
-> where ( 'B.is_new' , 0 ) -> where ( 'B.is_fail' , 0 ) -> group ( 'A.product_id' ) -> limit ( 10 ) -> order ( 'total DESC' ) -> select ();
}, 2000 + random_int ( 600 , 1200 ));
$result = [ 'data' => $res ];
return table_assign ( 0 , '' , $result );
}
// 接收微信支付状态的通知
public function notify ()
{
$app = $this -> payment ();
// 用 easywechat 封装的方法接收微信的信息, 根据 $message 的内容进行处理, 之后要告知微信服务器处理好了, 否则微信会一直请求这个 url, 发送信息
$response = $app -> handlePaidNotify ( function ( $message , $fail ){
// 首先查看 order 表, 如果 order 表有记录, 表示已经支付过了
$order = Order :: where ( 'order_sn' , $message [ 'out_trade_no' ]) -> first ();
if ( $order ) {
return true ; // 如果已经生成订单, 表示已经处理完了, 告诉微信不用再通知了
}
// 查看支付日志
$payLog = PayLog :: where ( 'out_trade_no' , $message [ 'out_trade_no' ]) -> first ();
if ( ! $payLog || $payLog -> paid_at ) { // 如果订单不存在 或者 订单已经支付过了
return true ; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
}
// return_code 表示通信状态,不代表支付状态
if ( $message [ 'return_code' ] === 'SUCCESS' ) {
// 用户是否支付成功
if ( $message [ 'result_code' ] === 'SUCCESS' ) {
// 更新支付时间为当前时间
$payLog -> paid_at = now ();
$post_id = $payLog -> post_id ;
// 联表查询 post 的相关信息
$post_title = $payLog -> post -> title ;
$post_price = $payLog -> post -> price ;
$post_original_price = $payLog -> post -> original_price ;
$post_cover = $payLog -> post -> post_cover ;
$post_description = $payLog -> post -> description ;
$user_id = $payLog -> post -> user_id ;
// 创建订单记录
Order :: create ([
'order_sn' => $message [ 'out_trade_no' ],
'total_fee' => $message [ 'total_fee' ],
'pay_log_id' => $payLog -> id ,
'status' => 1 ,
'user_id' => $user_id ,
'paid_at' => $payLog -> paid_at ,
'post_id' => $post_id ,
'post_title' => $post_title ,
'post_price' => $post_price ,
'post_original_price' => $post_original_price ,
'post_cover' => $post_cover ,
'post_description' => $post_description ,
]);
// 更新 PayLog, 这里的字段都是根据微信支付结果通知的字段设置的(https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_7&index=8)
PayLog :: where ( 'out_trade_no' , $message [ 'out_trade_no' ]) -> update ([
'appid' => $message [ 'appid' ],
'bank_type' => $message [ 'bank_type' ],
'total_fee' => $message [ 'total_fee' ],
'trade_type' => $message [ 'trade_type' ],
'is_subscribe' => $message [ 'is_subscribe' ],
'mch_id' => $message [ 'mch_id' ],
'nonce_str' => $message [ 'nonce_str' ],
'openid' => $message [ 'openid' ],
'sign' => $message [ 'sign' ],
'cash_fee' => $message [ 'cash_fee' ],
'fee_type' => $message [ 'fee_type' ],
'transaction_id' => $message [ 'transaction_id' ],
'time_end' => $payLog -> paid_at ,
'result_code' => $message [ 'result_code' ],
'return_code' => $message [ 'return_code' ],
]);
}
} else {
// 如果支付失败, 也更新 PayLog, 跟上面一样, 就是多了 error 信息
PayLog :: where ( 'out_trade_no' , $message [ 'out_trade_no' ]) -> update ([
'appid' => $message [ 'appid' ],
'bank_type' => $message [ 'bank_type' ],
'total_fee' => $message [ 'total_fee' ],
'trade_type' => $message [ 'trade_type' ],
'is_subscribe' => $message [ 'is_subscribe' ],
'mch_id' => $message [ 'mch_id' ],
'nonce_str' => $message [ 'nonce_str' ],
'openid' => $message [ 'openid' ],
'sign' => $message [ 'sign' ],
'cash_fee' => $message [ 'cash_fee' ],
'fee_type' => $message [ 'fee_type' ],
'transaction_id' => $message [ 'transaction_id' ],
'time_end' => $payLog -> paid_at ,
'result_code' => $message [ 'result_code' ],
'return_code' => $message [ 'return_code' ],
'err_code' => $message [ 'err_code' ],
'err_code_des' => $message [ 'err_code_des' ],
]);
return $fail ( '通信失败,请稍后再通知我' );
}
return true ; // 返回处理完成
});
// 这里是必须这样返回的, 会发送给微信服务器处理结果
return $response ;
}
2023-02-03 11:21:28 +08:00
2021-01-30 20:59:12 +08:00
}