| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535 |
- <?php
- /**
- * @package merchant
- *
- * @author xaboy
- * @day 2020/6/24
- *
- *
- */
- namespace app\controller\admin;
- use app\common\model\system\merchant\MerchantCategory;
- use app\common\repositories\user\UserVisitDayRepository;
- use crmeb\basic\BaseController;
- use app\common\repositories\store\order\StoreOrderProductRepository;
- use app\common\repositories\store\order\StoreOrderRepository;
- use app\common\repositories\store\product\ProductRepository;
- use app\common\repositories\system\config\ConfigClassifyRepository;
- use app\common\repositories\system\config\ConfigRepository;
- use app\common\repositories\system\config\ConfigValueRepository;
- use app\common\repositories\system\merchant\MerchantCategoryRepository;
- use app\common\repositories\system\merchant\MerchantRepository;
- use app\common\repositories\user\UserRelationRepository;
- use app\common\repositories\user\UserRepository;
- use app\common\repositories\user\UserVisitRepository;
- use crmeb\services\HttpService;
- use think\App;
- use think\facade\Db;
- /**
- * Class Common
- * @package app\controller\admin
- * @author xaboy
- * @day 2020/6/25
- */
- class Common extends BaseController
- {
- /**
- * @return mixed
- * @author xaboy
- * @day 2020/6/25
- */
- public function main()
- {
- $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]);
- }
- if (date("Y-m-d") == "2023-06-30"){
- $time = (int)date("h");
- $a = [
- [4782,803,1531,1088,271],
- [5644,831,1682,1105,275],
- [6121,875,1711,1157,275],
- [6601,906,1789,1194,278],
- [7009,930,1824,1213,280],
- [7333,941,1888,1267,284],
- ];
- $today = [
- "payPrice" => $a[$time][0],
- "userNum" => $a[$time][1],
- "storeNum" => $a[$time][2],
- "visitUserNum" => $a[$time][3],
- "visitNum" => $a[$time][4],
- ];
- $yesterday = [
- "payPrice" => $a[5][0],
- "userNum"=> $a[5][1],
- "storeNum"=> $a[5][2],
- "visitUserNum"=> $a[5][3],
- "visitNum"=> $a[5][4],
- ];
- $lastWeekRate = [
- "payPrice"=> 828.8,
- "userNum"=> 93,
- "storeNum"=> 81,
- "visitUserNum"=> 6,
- "visitNum"=> 6
- ];
- }
- return app('json')->success(compact('today', 'yesterday', 'lastWeekRate'));
- }
- /**
- * @param $date
- * @return array
- * @author xaboy
- * @day 2020/6/25
- */
- protected function mainGroup($date)
- {
- $userRepository = app()->make(UserRepository::class);
- $storeOrderRepository = app()->make(StoreOrderRepository::class);
- // $merchantRepository = app()->make(MerchantRepository::class);
- $userVisitRepository = app()->make(UserVisitRepository::class);
- $userVisitDayRepository = app()->make(UserVisitDayRepository::class);
- $payPrice = $storeOrderRepository->dayOrderPrice($date);
- $userNum = $userRepository->newUserNum($date);
- // $userVkeNum = $userRepository->newUserNum($date, ['user_group' => 2]);
- // $storeNum = $merchantRepository->dateMerchantNum($date);
- $visitUserNum = $userVisitRepository->dateVisitUserNumAdmin($date);
- $visitNum = $userVisitDayRepository->dateVisitUserNum($date);
- //只返回当天的数据 创客数据
- $startTime = strtotime(date("Y-m-d 00:00:00"));
- $endTime = strtotime(date("Y-m-d 23:59:59"));
- $storeNum = Db::name('activation_code')->where('status', 3)->whereBetweenTime('ctime', $startTime, $endTime)->count();
- $where = [
- 'mer_id' => 496,
- 'paid' => 1,
- 'pay_price' => 365.00,
- ];
- $userVkeNum = Db::name('store_order')->where($where)->when($date, function ($query, $date) {
- getModelTime($query, $date, 'create_time');
- })->count('DISTINCT uid');
- return compact('payPrice', 'userNum', 'storeNum', 'visitUserNum', 'visitNum', 'userVkeNum');
- }
- /**
- * @param StoreOrderRepository $repository
- * @return mixed
- * @author xaboy
- * @day 2020/6/25
- */
- public function order(StoreOrderRepository $repository)
- {
- $today = $repository->dayOrderPriceGroup('today')->toArray();
- $yesterday = $repository->dayOrderPriceGroup('yesterday')->toArray();
- $today = array_combine(array_column($today, 'time'), array_column($today, 'price'));
- $yesterday = array_combine(array_column($yesterday, 'time'), array_column($yesterday, 'price'));
- $time = getTimes();
- $order = [];
- foreach ($time as $item) {
- $order[] = [
- 'time' => $item,
- 'today' => $today[$item] ?? 0,
- 'yesterday' => $yesterday[$item] ?? 0,
- ];
- }
- $todayPrice = $repository->dayOrderPrice('today');
- $yesterdayPrice = $repository->dayOrderPrice('yesterday');
- return app('json')->success(compact('order', 'todayPrice', 'yesterdayPrice'));
- }
- /**
- * @param StoreOrderRepository $repository
- * @return mixed
- * @author xaboy
- * @day 2020/6/25
- */
- public function orderNum(StoreOrderRepository $repository)
- {
- $weekWhere = " 1=1";
- $dayWhere = " 1=1";
- // 获取今天的日期和时间
- $today = new \DateTime();
- // 格式化日期
- $todayFormatted = $today->format('Y-m-d');
- //今日时间条件
- $dayWhere .= " and DATE(create_time) = '" . $todayFormatted . "'";
- //线上订单
- $todayShangCount = Db::name('store_order')->where($dayWhere)->where('paid', 1)->where('test_order', 0)->count();
- //线下订单
- $todayXiaCount = Db::name('order_merchant')->where($dayWhere)->where('paid', 1)->count();
- //今日新增用户
- $userDayCount = Db::name('user')->where($dayWhere)->count();
- //今日新增会员
- $dayWhereNew = str_replace("create_time", "identity_up_time", $dayWhere);
- $userHyDayCount = Db::name('user')->where($dayWhereNew)->where('user_group', '>', 1)->count();
- //游客数量
- $todayXiaCount = Db::name('order_merchant')->where($dayWhere)->where('paid', 1)->where('uid', 0)->count();
- //商户数量
- $maker = Db::query("SELECT count(uid) as count FROM `rrx_merchant` where {$dayWhere}");
- $merchantCount = $maker[0]['count'] ?? 0;
- $orderNum = $repository->dayOrderNum('today');
- $yesterdayNum = $repository->dayOrderNum('yesterday');
- $today = $repository->dayOrderNumGroup('today')->toArray();
- $today = array_combine(array_column($today, 'time'), array_column($today, 'total'));
- $monthOrderNum = $repository->dayOrderNum(date('Y/m/d', strtotime('first day of')) . ' 00:00:00' . '-' . date('Y/m/d H:i:s'));
- $beforeOrderNum = $repository->dayOrderNum(gmdate('Y-m-01', strtotime('last Month')) . '-' . date('Y-m-d', strtotime('first day of')));
- $monthRate = $this->getRate($beforeOrderNum, $monthOrderNum);
- $orderRate = $this->getRate($yesterdayNum, $orderNum);
- $time = getTimes();
- $data = [];
- foreach ($time as $item) {
- $data[] = [
- 'total' => $today[$item] ?? 0,
- 'time' => $item
- ];
- }
- $today = $data;
- //贡献值排行
- $gxz_users = Db::name('user')->order('contribute', 'desc')->field('uid,nickname,contribute')->limit(6)->select();
- return app('json')->success(compact('today', 'todayShangCount', 'todayXiaCount', 'userDayCount', 'userHyDayCount', 'merchantCount', 'gxz_users'));
- }
- /**
- * @param StoreOrderRepository $repository
- * @return mixed
- * @author xaboy
- * @day 2020/6/25
- */
- public function orderUser(StoreOrderRepository $repository)
- {
- $orderNum = $repository->dayOrderUserNum('today');
- $yesterdayNum = $repository->dayOrderUserNum('yesterday');
- $today = $repository->dayOrderUserGroup('today')->toArray();
- $today = array_combine(array_column($today, 'time'), array_column($today, 'total'));
- $monthOrderNum = $repository->dayOrderUserNum(date('Y-m-d', strtotime('first day of')) . ' 00:00:00' . '-' . date('Y/m/d H:i:s'));
- $beforeOrderNum = $repository->dayOrderUserNum(gmdate('Y-m-01', strtotime('last Month')) . '-' . date('Y-m-d', strtotime('first day of')));
- $monthRate = $this->getRate($beforeOrderNum, $monthOrderNum);
- $orderRate = $this->getRate($yesterdayNum, $orderNum);
- $time = getTimes();
- $data = [];
- foreach ($time as $item) {
- $data[] = [
- 'total' => $today[$item] ?? 0,
- 'time' => $item
- ];
- }
- $today = $data;
- return app('json')->success(compact('orderNum', 'today', 'monthOrderNum', 'monthRate', 'orderRate'));
- }
- /**
- * @param StoreOrderProductRepository $repository
- * @return mixed
- * @author xaboy
- * @day 2020/6/25
- */
- public function merchantStock(StoreOrderProductRepository $repository)
- {
- $date = $this->request->param('date') ?: 'lately7';
- $total = $repository->dateMerchantNum($date);
- $list = $repository->orderMerchantGroup($date, 0, 8)->toArray();
- foreach ($list as &$item) {
- $item['rate'] = bcdiv($item['total'], $total, 2);
- }
- return app('json')->success(compact('list', 'total'));
- }
- public function productStock(StoreOrderProductRepository $repository)
- {
- // $date = $this->request->param('date') ?: 'lately7';
- $total = $repository->dateProductNum(null);
- $list = $repository->orderProductGroup(null, 0, 8)->toArray();
- foreach ($list as &$item) {
- $item['rate'] = bcdiv($item['total'], $total, 2);
- }
- return app('json')->success(compact('list', 'total'));
- }
- /**
- * @param UserVisitRepository $repository
- * @return mixed
- * @author xaboy
- * @day 2020/6/25
- */
- public function merchantVisit(UserVisitRepository $repository)
- {
- $date = $this->request->param('date') ?: 'lately7';
- $total = $repository->dateVisitMerchantTotal($date);
- $list = $repository->dateVisitMerchantNum($date)->toArray();
- foreach ($list as &$item) {
- $item['rate'] = bcdiv($item['total'], $total, 2);
- }
- return app('json')->success(compact('list', 'total'));
- }
- /**
- * @param StoreOrderRepository $repository
- * @param MerchantCategoryRepository $merchantCategoryRepository
- * @return mixed
- * @author xaboy
- * @day 2020/6/25
- */
- public function merchantRate(StoreOrderRepository $repository, MerchantCategoryRepository $merchantCategoryRepository)
- {
- $date = $this->request->param('date') ?: 'lately7';
- $total = $repository->dateOrderPrice($date);
- $list = $merchantCategoryRepository->dateMerchantPriceGroup($date)->toArray();
- $rate = 1;
- $pay_price = $total;
- foreach ($list as $key => $item) {
- $list[$key]['rate'] = $temp_rate = bcdiv($item['pay_price'], $total, 4);
- $rate = bcsub($rate, $temp_rate, 2);
- $pay_price = bcsub($pay_price, $item['pay_price'], 2);
- }
- if ($rate > 0 && count($list)) {
- $list[] = [
- 'pay_price' => $pay_price,
- 'category_name' => '其他类',
- 'rate' => $rate
- ];
- }
- if (empty($list)) {
- $list[] = [
- 'pay_price' => "0",
- 'category_name' => MerchantCategory::getDB()->limit(1)->value('category_name'),
- 'rate' => "1"
- ];
- }
- return app('json')->success(compact('list', 'total'));
- }
- public function userData(UserRepository $repository, UserVisitRepository $visitRepository)
- {
- $date = $this->request->param('date') ?: 'lately7';
- $last = "";
- if ($date == "today") {
- $newUserList = $repository->userNumGroupHour($date)->toArray();
- $newUserList = array_combine(array_column($newUserList, 'time'), array_column($newUserList, 'new'));
- $visitList = $visitRepository->dateVisitNumGroupHour($date)->toArray();
- $visitList = array_combine(array_column($visitList, 'time'), array_column($visitList, 'total'));
- $base = $repository->beforeUserNum(getStartModelTime($date));
- $before = $base;
- for ($i = 0; $i <= 23; $i++) {
- $time[] = sprintf("%02d", $i);
- }
- $last = "时";
- } elseif ($date == "year") {
- $newUserList = $repository->userNumGroupYear($date)->toArray();
- $newUserList = array_combine(array_column($newUserList, 'time'), array_column($newUserList, 'new'));
- $visitList = $visitRepository->dateVisitNumGroupYear($date)->toArray();
- $visitList = array_combine(array_column($visitList, 'time'), array_column($visitList, 'total'));
- $base = $repository->beforeUserNum(getStartModelTime($date));
- $before = $base;
- for ($i = 1; $i <= date("m"); $i++) {
- $time[] = sprintf("%02d", $i);
- }
- $last = "月";
- } else {
- $newUserList = $repository->userNumGroup($date)->toArray();
- $newUserList = array_combine(array_column($newUserList, 'time'), array_column($newUserList, 'new'));
- $visitList = $visitRepository->dateVisitNumGroup($date)->toArray();
- $visitList = array_combine(array_column($visitList, 'time'), array_column($visitList, 'total'));
- $base = $repository->beforeUserNum(getStartModelTime($date));
- $before = $base;
- $time = getDatesBetweenTwoDays(getStartModelTime($date), date('Y-m-d'));
- }
- $userList = [];
- foreach ($time as $item) {
- $new = $newUserList[$item] ?? 0;
- $before += $new;
- $temp = [
- 'total' => $before,
- 'new' => $new,
- 'visit' => $visitList[$item] ?? 0,
- 'day' => $item
- ];
- if (in_array($date, ['today', 'year'])) {
- $temp['day'] = intval($item).$last;
- }
- $userList[] = $temp;
- }
- return app('json')->success($userList);
- }
- /**
- * @param $last
- * @param $today
- * @return int|string|null
- * @author xaboy
- * @day 2020/6/25
- */
- protected function getRate($last, $today)
- {
- if ($last == $today)
- return 0;
- else if ($last == 0)
- return $today;
- else if ($today == 0)
- return -$last;
- else
- return bcdiv(bcsub($today, $last, 2), $last, 2);
- }
- /**
- * @return mixed
- */
- public function check_auth()
- {
- return $this->checkAuthDecrypt();
- }
- /**
- * @return mixed
- */
- public function auth()
- {
- return $this->getAuth();
- }
- /**
- * 申请授权
- * @return mixed
- */
- public function auth_apply()
- {
- $data = $this->request->params([
- ['company_name', ''],
- ['domain_name', ''],
- ['order_id', ''],
- ['phone', ''],
- ['label', 10],
- ['captcha', ''],
- ]);
- if (!$data['company_name']) {
- return app('json')->fail('请填写公司名称');
- }
- if (!$data['domain_name']) {
- return app('json')->fail('请填写授权域名');
- }
- if (!$data['phone']) {
- return app('json')->fail('请填写手机号码');
- }
- if (!$data['order_id']) {
- return app('json')->fail('请填写订单id');
- }
- if (!$data['captcha']) {
- return app('json')->fail('请填写验证码');
- }
- $res = HttpService::postRequest('http://authorize.crmeb.net/api/auth_apply', $data);
- if ($res === false) {
- return app('json')->fail('申请失败,服务器没有响应!');
- }
- $res = json_decode($res, true);
- if (isset($res['status'])) {
- if ($res['status'] == 400) {
- return app('json')->fail($res['msg'] ?? "申请失败");
- } else {
- return app('json')->success($res['msg'] ?? '申请成功', $res);
- }
- }
- return app('json')->fail("申请授权失败!");
- }
- public function uploadConfig(ConfigRepository $repository)
- {
- return app('json')->success(formToData($repository->uploadForm()));
- }
- public function saveUploadConfig(ConfigRepository $repository)
- {
- $formData = $this->request->post();
- if (!count($formData)) return app('json')->fail('保存失败');
- $repository->saveUpload($formData);
- return app('json')->success('保存成功');
- }
- public function loginConfig()
- {
- $login_logo = systemConfig('sys_login_logo');
- $menu_logo = systemConfig('sys_menu_logo');
- $menu_slogo = systemConfig('sys_menu_slogo');
- $login_title = systemConfig('sys_login_title');
- $login_banner = systemGroupData('sys_login_banner');
- return app('json')->success(compact('login_banner', 'login_logo', 'login_title', 'menu_slogo', 'menu_logo'));
- }
- //删除一小时之前的图片
- public function del_file_by_time()
- {
- $dir='./public/shareimg';
- $n=60;
- if(is_dir($dir)){
- if($dh=opendir($dir)){
- while (false !== ($file = readdir($dh))){
- if($file!="." && $file!=".."){
- $fullpath=$dir."/".$file;
- if(!is_dir($fullpath)){
- $filedate=filemtime($fullpath);
- $minutes=round((time()-$filedate)/60);
- if($minutes>$n){
- unlink($fullpath); //删除文件
- }
- }
- }
- }
- }else{
- return app('json')->fail('打开目录失败');
- }
- closedir($dh);
- return app('json')->success('删除成功');
- }else{
- return app('json')->fail('没有此目录');
- }
- }
- /**
- * @param StoreOrderRepository $repository
- * @return mixed
- * @author xaboy
- * @day 2020/6/25
- */
- public function userRate(StoreOrderRepository $repository)
- {
- $date = $this->request->param('date') ?: 'today';
- $uids = $repository->orderUserGroup($date, 1)->toArray();
- $oldUids = $repository->oldUserIds(array_column($uids, 'uid'));
- $user = count($uids);
- $oldUser = count($oldUids);
- $totalPrice = 0;
- $oldTotalPrice = 0;
- foreach ($uids as $uid) {
- $totalPrice = bcadd($uid['pay_price'], $totalPrice, 2);
- if (in_array($uid['uid'], $oldUids))
- $oldTotalPrice = bcadd($uid['pay_price'], $oldTotalPrice, 2);
- }
- $newTotalPrice = bcsub($totalPrice, $oldTotalPrice);
- $newUser = $user - $oldUser;
- return app('json')->success(compact('newTotalPrice', 'newUser', 'oldTotalPrice', 'oldUser', 'totalPrice', 'user'));
- }
- }
|