Common.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020/6/24
  7. *
  8. *
  9. */
  10. namespace app\controller\admin;
  11. use app\common\model\system\merchant\MerchantCategory;
  12. use app\common\repositories\user\UserVisitDayRepository;
  13. use crmeb\basic\BaseController;
  14. use app\common\repositories\store\order\StoreOrderProductRepository;
  15. use app\common\repositories\store\order\StoreOrderRepository;
  16. use app\common\repositories\store\product\ProductRepository;
  17. use app\common\repositories\system\config\ConfigClassifyRepository;
  18. use app\common\repositories\system\config\ConfigRepository;
  19. use app\common\repositories\system\config\ConfigValueRepository;
  20. use app\common\repositories\system\merchant\MerchantCategoryRepository;
  21. use app\common\repositories\system\merchant\MerchantRepository;
  22. use app\common\repositories\user\UserRelationRepository;
  23. use app\common\repositories\user\UserRepository;
  24. use app\common\repositories\user\UserVisitRepository;
  25. use crmeb\services\HttpService;
  26. use think\App;
  27. use think\facade\Db;
  28. /**
  29. * Class Common
  30. * @package app\controller\admin
  31. * @author xaboy
  32. * @day 2020/6/25
  33. */
  34. class Common extends BaseController
  35. {
  36. /**
  37. * @return mixed
  38. * @author xaboy
  39. * @day 2020/6/25
  40. */
  41. public function main()
  42. {
  43. $today = $this->mainGroup('today');
  44. $yesterday = $this->mainGroup('yesterday');
  45. $lastWeek = $this->mainGroup(date('Y-m-d', strtotime('- 7day')));
  46. $lastWeekRate = [];
  47. foreach ($lastWeek as $k => $item) {
  48. $lastWeekRate[$k] = $this->getRate($item, $today[$k]);
  49. }
  50. if (date("Y-m-d") == "2023-06-30"){
  51. $time = (int)date("h");
  52. $a = [
  53. [4782,803,1531,1088,271],
  54. [5644,831,1682,1105,275],
  55. [6121,875,1711,1157,275],
  56. [6601,906,1789,1194,278],
  57. [7009,930,1824,1213,280],
  58. [7333,941,1888,1267,284],
  59. ];
  60. $today = [
  61. "payPrice" => $a[$time][0],
  62. "userNum" => $a[$time][1],
  63. "storeNum" => $a[$time][2],
  64. "visitUserNum" => $a[$time][3],
  65. "visitNum" => $a[$time][4],
  66. ];
  67. $yesterday = [
  68. "payPrice" => $a[5][0],
  69. "userNum"=> $a[5][1],
  70. "storeNum"=> $a[5][2],
  71. "visitUserNum"=> $a[5][3],
  72. "visitNum"=> $a[5][4],
  73. ];
  74. $lastWeekRate = [
  75. "payPrice"=> 828.8,
  76. "userNum"=> 93,
  77. "storeNum"=> 81,
  78. "visitUserNum"=> 6,
  79. "visitNum"=> 6
  80. ];
  81. }
  82. return app('json')->success(compact('today', 'yesterday', 'lastWeekRate'));
  83. }
  84. /**
  85. * @param $date
  86. * @return array
  87. * @author xaboy
  88. * @day 2020/6/25
  89. */
  90. protected function mainGroup($date)
  91. {
  92. $userRepository = app()->make(UserRepository::class);
  93. $storeOrderRepository = app()->make(StoreOrderRepository::class);
  94. // $merchantRepository = app()->make(MerchantRepository::class);
  95. $userVisitRepository = app()->make(UserVisitRepository::class);
  96. $userVisitDayRepository = app()->make(UserVisitDayRepository::class);
  97. $payPrice = $storeOrderRepository->dayOrderPrice($date);
  98. $userNum = $userRepository->newUserNum($date);
  99. // $userVkeNum = $userRepository->newUserNum($date, ['user_group' => 2]);
  100. // $storeNum = $merchantRepository->dateMerchantNum($date);
  101. $visitUserNum = $userVisitRepository->dateVisitUserNumAdmin($date);
  102. $visitNum = $userVisitDayRepository->dateVisitUserNum($date);
  103. //只返回当天的数据 创客数据
  104. $startTime = strtotime(date("Y-m-d 00:00:00"));
  105. $endTime = strtotime(date("Y-m-d 23:59:59"));
  106. $storeNum = Db::name('activation_code')->where('status', 3)->whereBetweenTime('ctime', $startTime, $endTime)->count();
  107. $where = [
  108. 'mer_id' => 496,
  109. 'paid' => 1,
  110. 'pay_price' => 365.00,
  111. ];
  112. $userVkeNum = Db::name('store_order')->where($where)->when($date, function ($query, $date) {
  113. getModelTime($query, $date, 'create_time');
  114. })->count('DISTINCT uid');
  115. return compact('payPrice', 'userNum', 'storeNum', 'visitUserNum', 'visitNum', 'userVkeNum');
  116. }
  117. /**
  118. * @param StoreOrderRepository $repository
  119. * @return mixed
  120. * @author xaboy
  121. * @day 2020/6/25
  122. */
  123. public function order(StoreOrderRepository $repository)
  124. {
  125. $today = $repository->dayOrderPriceGroup('today')->toArray();
  126. $yesterday = $repository->dayOrderPriceGroup('yesterday')->toArray();
  127. $today = array_combine(array_column($today, 'time'), array_column($today, 'price'));
  128. $yesterday = array_combine(array_column($yesterday, 'time'), array_column($yesterday, 'price'));
  129. $time = getTimes();
  130. $order = [];
  131. foreach ($time as $item) {
  132. $order[] = [
  133. 'time' => $item,
  134. 'today' => $today[$item] ?? 0,
  135. 'yesterday' => $yesterday[$item] ?? 0,
  136. ];
  137. }
  138. $todayPrice = $repository->dayOrderPrice('today');
  139. $yesterdayPrice = $repository->dayOrderPrice('yesterday');
  140. return app('json')->success(compact('order', 'todayPrice', 'yesterdayPrice'));
  141. }
  142. /**
  143. * @param StoreOrderRepository $repository
  144. * @return mixed
  145. * @author xaboy
  146. * @day 2020/6/25
  147. */
  148. public function orderNum(StoreOrderRepository $repository)
  149. {
  150. $orderNum = $repository->dayOrderNum('today');
  151. $yesterdayNum = $repository->dayOrderNum('yesterday');
  152. $today = $repository->dayOrderNumGroup('today')->toArray();
  153. $today = array_combine(array_column($today, 'time'), array_column($today, 'total'));
  154. $monthOrderNum = $repository->dayOrderNum(date('Y/m/d', strtotime('first day of')) . ' 00:00:00' . '-' . date('Y/m/d H:i:s'));
  155. $beforeOrderNum = $repository->dayOrderNum(gmdate('Y-m-01', strtotime('last Month')) . '-' . date('Y-m-d', strtotime('first day of')));
  156. $monthRate = $this->getRate($beforeOrderNum, $monthOrderNum);
  157. $orderRate = $this->getRate($yesterdayNum, $orderNum);
  158. $time = getTimes();
  159. $data = [];
  160. foreach ($time as $item) {
  161. $data[] = [
  162. 'total' => $today[$item] ?? 0,
  163. 'time' => $item
  164. ];
  165. }
  166. $today = $data;
  167. return app('json')->success(compact('orderNum', 'today', 'monthOrderNum', 'monthRate', 'orderRate'));
  168. }
  169. /**
  170. * @param StoreOrderRepository $repository
  171. * @return mixed
  172. * @author xaboy
  173. * @day 2020/6/25
  174. */
  175. public function orderUser(StoreOrderRepository $repository)
  176. {
  177. $orderNum = $repository->dayOrderUserNum('today');
  178. $yesterdayNum = $repository->dayOrderUserNum('yesterday');
  179. $today = $repository->dayOrderUserGroup('today')->toArray();
  180. $today = array_combine(array_column($today, 'time'), array_column($today, 'total'));
  181. $monthOrderNum = $repository->dayOrderUserNum(date('Y-m-d', strtotime('first day of')) . ' 00:00:00' . '-' . date('Y/m/d H:i:s'));
  182. $beforeOrderNum = $repository->dayOrderUserNum(gmdate('Y-m-01', strtotime('last Month')) . '-' . date('Y-m-d', strtotime('first day of')));
  183. $monthRate = $this->getRate($beforeOrderNum, $monthOrderNum);
  184. $orderRate = $this->getRate($yesterdayNum, $orderNum);
  185. $time = getTimes();
  186. $data = [];
  187. foreach ($time as $item) {
  188. $data[] = [
  189. 'total' => $today[$item] ?? 0,
  190. 'time' => $item
  191. ];
  192. }
  193. $today = $data;
  194. return app('json')->success(compact('orderNum', 'today', 'monthOrderNum', 'monthRate', 'orderRate'));
  195. }
  196. /**
  197. * @param StoreOrderProductRepository $repository
  198. * @return mixed
  199. * @author xaboy
  200. * @day 2020/6/25
  201. */
  202. public function merchantStock(StoreOrderProductRepository $repository)
  203. {
  204. $date = $this->request->param('date') ?: 'lately7';
  205. $total = $repository->dateMerchantNum($date);
  206. $list = $repository->orderMerchantGroup($date, 0, 8)->toArray();
  207. foreach ($list as &$item) {
  208. $item['rate'] = bcdiv($item['total'], $total, 2);
  209. }
  210. return app('json')->success(compact('list', 'total'));
  211. }
  212. public function productStock(StoreOrderProductRepository $repository)
  213. {
  214. // $date = $this->request->param('date') ?: 'lately7';
  215. $total = $repository->dateProductNum(null);
  216. $list = $repository->orderProductGroup(null, 0, 8)->toArray();
  217. foreach ($list as &$item) {
  218. $item['rate'] = bcdiv($item['total'], $total, 2);
  219. }
  220. return app('json')->success(compact('list', 'total'));
  221. }
  222. /**
  223. * @param UserVisitRepository $repository
  224. * @return mixed
  225. * @author xaboy
  226. * @day 2020/6/25
  227. */
  228. public function merchantVisit(UserVisitRepository $repository)
  229. {
  230. $date = $this->request->param('date') ?: 'lately7';
  231. $total = $repository->dateVisitMerchantTotal($date);
  232. $list = $repository->dateVisitMerchantNum($date)->toArray();
  233. foreach ($list as &$item) {
  234. $item['rate'] = bcdiv($item['total'], $total, 2);
  235. }
  236. return app('json')->success(compact('list', 'total'));
  237. }
  238. /**
  239. * @param StoreOrderRepository $repository
  240. * @param MerchantCategoryRepository $merchantCategoryRepository
  241. * @return mixed
  242. * @author xaboy
  243. * @day 2020/6/25
  244. */
  245. public function merchantRate(StoreOrderRepository $repository, MerchantCategoryRepository $merchantCategoryRepository)
  246. {
  247. $date = $this->request->param('date') ?: 'lately7';
  248. $total = $repository->dateOrderPrice($date);
  249. $list = $merchantCategoryRepository->dateMerchantPriceGroup($date)->toArray();
  250. $rate = 1;
  251. $pay_price = $total;
  252. foreach ($list as $key => $item) {
  253. $list[$key]['rate'] = $temp_rate = bcdiv($item['pay_price'], $total, 4);
  254. $rate = bcsub($rate, $temp_rate, 2);
  255. $pay_price = bcsub($pay_price, $item['pay_price'], 2);
  256. }
  257. if ($rate > 0 && count($list)) {
  258. $list[] = [
  259. 'pay_price' => $pay_price,
  260. 'category_name' => '其他类',
  261. 'rate' => $rate
  262. ];
  263. }
  264. if (empty($list)) {
  265. $list[] = [
  266. 'pay_price' => "0",
  267. 'category_name' => MerchantCategory::getDB()->limit(1)->value('category_name'),
  268. 'rate' => "1"
  269. ];
  270. }
  271. return app('json')->success(compact('list', 'total'));
  272. }
  273. public function userData(UserRepository $repository, UserVisitRepository $visitRepository)
  274. {
  275. $date = $this->request->param('date') ?: 'lately7';
  276. $last = "";
  277. if ($date == "today") {
  278. $newUserList = $repository->userNumGroupHour($date)->toArray();
  279. $newUserList = array_combine(array_column($newUserList, 'time'), array_column($newUserList, 'new'));
  280. $visitList = $visitRepository->dateVisitNumGroupHour($date)->toArray();
  281. $visitList = array_combine(array_column($visitList, 'time'), array_column($visitList, 'total'));
  282. $base = $repository->beforeUserNum(getStartModelTime($date));
  283. $before = $base;
  284. for ($i = 0; $i <= 23; $i++) {
  285. $time[] = sprintf("%02d", $i);
  286. }
  287. $last = "时";
  288. } elseif ($date == "year") {
  289. $newUserList = $repository->userNumGroupYear($date)->toArray();
  290. $newUserList = array_combine(array_column($newUserList, 'time'), array_column($newUserList, 'new'));
  291. $visitList = $visitRepository->dateVisitNumGroupYear($date)->toArray();
  292. $visitList = array_combine(array_column($visitList, 'time'), array_column($visitList, 'total'));
  293. $base = $repository->beforeUserNum(getStartModelTime($date));
  294. $before = $base;
  295. for ($i = 1; $i <= date("m"); $i++) {
  296. $time[] = sprintf("%02d", $i);
  297. }
  298. $last = "月";
  299. } else {
  300. $newUserList = $repository->userNumGroup($date)->toArray();
  301. $newUserList = array_combine(array_column($newUserList, 'time'), array_column($newUserList, 'new'));
  302. $visitList = $visitRepository->dateVisitNumGroup($date)->toArray();
  303. $visitList = array_combine(array_column($visitList, 'time'), array_column($visitList, 'total'));
  304. $base = $repository->beforeUserNum(getStartModelTime($date));
  305. $before = $base;
  306. $time = getDatesBetweenTwoDays(getStartModelTime($date), date('Y-m-d'));
  307. }
  308. $userList = [];
  309. foreach ($time as $item) {
  310. $new = $newUserList[$item] ?? 0;
  311. $before += $new;
  312. $temp = [
  313. 'total' => $before,
  314. 'new' => $new,
  315. 'visit' => $visitList[$item] ?? 0,
  316. 'day' => $item
  317. ];
  318. if (in_array($date, ['today', 'year'])) {
  319. $temp['day'] = intval($item).$last;
  320. }
  321. $userList[] = $temp;
  322. }
  323. return app('json')->success($userList);
  324. }
  325. /**
  326. * @param $last
  327. * @param $today
  328. * @return int|string|null
  329. * @author xaboy
  330. * @day 2020/6/25
  331. */
  332. protected function getRate($last, $today)
  333. {
  334. if ($last == $today)
  335. return 0;
  336. else if ($last == 0)
  337. return $today;
  338. else if ($today == 0)
  339. return -$last;
  340. else
  341. return bcdiv(bcsub($today, $last, 2), $last, 2);
  342. }
  343. /**
  344. * @return mixed
  345. */
  346. public function check_auth()
  347. {
  348. return $this->checkAuthDecrypt();
  349. }
  350. /**
  351. * @return mixed
  352. */
  353. public function auth()
  354. {
  355. return $this->getAuth();
  356. }
  357. /**
  358. * 申请授权
  359. * @return mixed
  360. */
  361. public function auth_apply()
  362. {
  363. $data = $this->request->params([
  364. ['company_name', ''],
  365. ['domain_name', ''],
  366. ['order_id', ''],
  367. ['phone', ''],
  368. ['label', 10],
  369. ['captcha', ''],
  370. ]);
  371. if (!$data['company_name']) {
  372. return app('json')->fail('请填写公司名称');
  373. }
  374. if (!$data['domain_name']) {
  375. return app('json')->fail('请填写授权域名');
  376. }
  377. if (!$data['phone']) {
  378. return app('json')->fail('请填写手机号码');
  379. }
  380. if (!$data['order_id']) {
  381. return app('json')->fail('请填写订单id');
  382. }
  383. if (!$data['captcha']) {
  384. return app('json')->fail('请填写验证码');
  385. }
  386. $res = HttpService::postRequest('http://authorize.crmeb.net/api/auth_apply', $data);
  387. if ($res === false) {
  388. return app('json')->fail('申请失败,服务器没有响应!');
  389. }
  390. $res = json_decode($res, true);
  391. if (isset($res['status'])) {
  392. if ($res['status'] == 400) {
  393. return app('json')->fail($res['msg'] ?? "申请失败");
  394. } else {
  395. return app('json')->success($res['msg'] ?? '申请成功', $res);
  396. }
  397. }
  398. return app('json')->fail("申请授权失败!");
  399. }
  400. public function uploadConfig(ConfigRepository $repository)
  401. {
  402. return app('json')->success(formToData($repository->uploadForm()));
  403. }
  404. public function saveUploadConfig(ConfigRepository $repository)
  405. {
  406. $formData = $this->request->post();
  407. if (!count($formData)) return app('json')->fail('保存失败');
  408. $repository->saveUpload($formData);
  409. return app('json')->success('保存成功');
  410. }
  411. public function loginConfig()
  412. {
  413. $login_logo = systemConfig('sys_login_logo');
  414. $menu_logo = systemConfig('sys_menu_logo');
  415. $menu_slogo = systemConfig('sys_menu_slogo');
  416. $login_title = systemConfig('sys_login_title');
  417. $login_banner = systemGroupData('sys_login_banner');
  418. return app('json')->success(compact('login_banner', 'login_logo', 'login_title', 'menu_slogo', 'menu_logo'));
  419. }
  420. //删除一小时之前的图片
  421. public function del_file_by_time()
  422. {
  423. $dir='./public/shareimg';
  424. $n=60;
  425. if(is_dir($dir)){
  426. if($dh=opendir($dir)){
  427. while (false !== ($file = readdir($dh))){
  428. if($file!="." && $file!=".."){
  429. $fullpath=$dir."/".$file;
  430. if(!is_dir($fullpath)){
  431. $filedate=filemtime($fullpath);
  432. $minutes=round((time()-$filedate)/60);
  433. if($minutes>$n){
  434. unlink($fullpath); //删除文件
  435. }
  436. }
  437. }
  438. }
  439. }else{
  440. return app('json')->fail('打开目录失败');
  441. }
  442. closedir($dh);
  443. return app('json')->success('删除成功');
  444. }else{
  445. return app('json')->fail('没有此目录');
  446. }
  447. }
  448. /**
  449. * @param StoreOrderRepository $repository
  450. * @return mixed
  451. * @author xaboy
  452. * @day 2020/6/25
  453. */
  454. public function userRate(StoreOrderRepository $repository)
  455. {
  456. $date = $this->request->param('date') ?: 'today';
  457. $uids = $repository->orderUserGroup($date, 1)->toArray();
  458. $oldUids = $repository->oldUserIds(array_column($uids, 'uid'));
  459. $user = count($uids);
  460. $oldUser = count($oldUids);
  461. $totalPrice = 0;
  462. $oldTotalPrice = 0;
  463. foreach ($uids as $uid) {
  464. $totalPrice = bcadd($uid['pay_price'], $totalPrice, 2);
  465. if (in_array($uid['uid'], $oldUids))
  466. $oldTotalPrice = bcadd($uid['pay_price'], $oldTotalPrice, 2);
  467. }
  468. $newTotalPrice = bcsub($totalPrice, $oldTotalPrice);
  469. $newUser = $user - $oldUser;
  470. return app('json')->success(compact('newTotalPrice', 'newUser', 'oldTotalPrice', 'oldUser', 'totalPrice', 'user'));
  471. }
  472. }