Common.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\admin;
  12. use crmeb\basic\BaseController;
  13. use app\common\repositories\store\order\StoreOrderProductRepository;
  14. use app\common\repositories\store\order\StoreOrderRepository;
  15. use app\common\repositories\store\product\ProductRepository;
  16. use app\common\repositories\system\config\ConfigClassifyRepository;
  17. use app\common\repositories\system\config\ConfigRepository;
  18. use app\common\repositories\system\config\ConfigValueRepository;
  19. use app\common\repositories\system\merchant\MerchantCategoryRepository;
  20. use app\common\repositories\system\merchant\MerchantRepository;
  21. use app\common\repositories\user\UserRelationRepository;
  22. use app\common\repositories\user\UserRepository;
  23. use app\common\repositories\user\UserVisitRepository;
  24. use crmeb\services\HttpService;
  25. use think\App;
  26. use think\facade\Db;
  27. /**
  28. * Class Common
  29. * @package app\controller\admin
  30. * @author xaboy
  31. * @day 2020/6/25
  32. */
  33. class Common extends BaseController
  34. {
  35. /**
  36. * @return mixed
  37. * @author xaboy
  38. * @day 2020/6/25
  39. */
  40. public function main()
  41. {
  42. $today = $this->mainGroup('today');
  43. $yesterday = $this->mainGroup('yesterday');
  44. $lastWeek = $this->mainGroup(date('Y-m-d', strtotime('- 7day')));
  45. $lastWeekRate = [];
  46. foreach ($lastWeek as $k => $item) {
  47. $lastWeekRate[$k] = $this->getRate($item, $today[$k], 4);
  48. }
  49. return app('json')->success(compact('today', 'yesterday', 'lastWeekRate'));
  50. }
  51. /**
  52. * @param $date
  53. * @return array
  54. * @author xaboy
  55. * @day 2020/6/25
  56. */
  57. protected function mainGroup($date)
  58. {
  59. $userRepository = app()->make(UserRepository::class);
  60. $storeOrderRepository = app()->make(StoreOrderRepository::class);
  61. $merchantRepository = app()->make(MerchantRepository::class);
  62. $userVisitRepository = app()->make(UserVisitRepository::class);
  63. $payPrice = (float)$storeOrderRepository->dayOrderPrice($date);
  64. $userNum = (float)$userRepository->newUserNum($date);
  65. $storeNum = (float)$merchantRepository->dateMerchantNum($date);
  66. $visitUserNum = (float)$userVisitRepository->dateVisitUserNum($date);
  67. $visitNum = (float)$userVisitRepository->dateVisitNum($date);
  68. return compact('payPrice', 'userNum', 'storeNum', 'visitUserNum', 'visitNum');
  69. }
  70. /**
  71. * @param StoreOrderRepository $repository
  72. * @return mixed
  73. * @author xaboy
  74. * @day 2020/6/25
  75. */
  76. public function order(StoreOrderRepository $repository)
  77. {
  78. $today = $repository->dayOrderPriceGroup('today')->toArray();
  79. $yesterday = $repository->dayOrderPriceGroup('yesterday')->toArray();
  80. $today = array_combine(array_column($today, 'time'), array_column($today, 'price'));
  81. $yesterday = array_combine(array_column($yesterday, 'time'), array_column($yesterday, 'price'));
  82. $time = getTimes();
  83. $order = [];
  84. foreach ($time as $item) {
  85. $order[] = [
  86. 'time' => $item,
  87. 'today' => $today[$item] ?? 0,
  88. 'yesterday' => $yesterday[$item] ?? 0,
  89. ];
  90. }
  91. $todayPrice = $repository->dayOrderPrice('today');
  92. $yesterdayPrice = $repository->dayOrderPrice('yesterday');
  93. return app('json')->success(compact('order', 'todayPrice', 'yesterdayPrice'));
  94. }
  95. /**
  96. * @param StoreOrderRepository $repository
  97. * @return mixed
  98. * @author xaboy
  99. * @day 2020/6/25
  100. */
  101. public function orderNum(StoreOrderRepository $repository)
  102. {
  103. $orderNum = $repository->dayOrderNum('today');
  104. $yesterdayNum = $repository->dayOrderNum('yesterday');
  105. $today = $repository->dayOrderNumGroup('today')->toArray();
  106. $today = array_combine(array_column($today, 'time'), array_column($today, 'total'));
  107. $monthOrderNum = $repository->dayOrderNum(date('Y/m/d', strtotime('first day of')) . ' 00:00:00' . '-' . date('Y/m/d H:i:s'));
  108. $beforeOrderNum = $repository->dayOrderNum(date('Y/m/01 00:00:00', strtotime('last Month')) . '-' . date('Y/m/d 00:00:00', strtotime('-1 day', strtotime('first day of'))));
  109. $monthRate = $this->getRate($beforeOrderNum, $monthOrderNum);
  110. $orderRate = $this->getRate($yesterdayNum, $orderNum);
  111. $time = getTimes();
  112. $data = [];
  113. foreach ($time as $item) {
  114. $data[] = [
  115. 'total' => $today[$item] ?? 0,
  116. 'time' => $item
  117. ];
  118. }
  119. $today = $data;
  120. return app('json')->success(compact('orderNum', 'today', 'monthOrderNum', 'monthRate', 'orderRate'));
  121. }
  122. /**
  123. * @param StoreOrderRepository $repository
  124. * @return mixed
  125. * @author xaboy
  126. * @day 2020/6/25
  127. */
  128. public function orderUser(StoreOrderRepository $repository)
  129. {
  130. $orderNum = $repository->dayOrderUserNum('today');
  131. $yesterdayNum = $repository->dayOrderUserNum('yesterday');
  132. $today = $repository->dayOrderUserGroup('today')->toArray();
  133. $today = array_combine(array_column($today, 'time'), array_column($today, 'total'));
  134. $monthOrderNum = $repository->dayOrderUserNum(date('Y/m/d', strtotime('first day of')) . ' 00:00:00' . '-' . date('Y/m/d H:i:s'));
  135. $beforeOrderNum = $repository->dayOrderUserNum(gmdate('Y/m/01 00:00:00', strtotime('last Month')) . '-' . date('Y/m/d 00:00:00', strtotime('-1 day', strtotime('first day of'))));
  136. $monthRate = $this->getRate($beforeOrderNum, $monthOrderNum);
  137. $orderRate = $this->getRate($yesterdayNum, $orderNum);
  138. $time = getTimes();
  139. $data = [];
  140. foreach ($time as $item) {
  141. $data[] = [
  142. 'total' => $today[$item] ?? 0,
  143. 'time' => $item
  144. ];
  145. }
  146. $today = $data;
  147. return app('json')->success(compact('orderNum', 'today', 'monthOrderNum', 'monthRate', 'orderRate'));
  148. }
  149. /**
  150. * @param StoreOrderProductRepository $repository
  151. * @return mixed
  152. * @author xaboy
  153. * @day 2020/6/25
  154. */
  155. public function merchantStock(StoreOrderProductRepository $repository)
  156. {
  157. $date = $this->request->param('date') ?: 'lately7';
  158. $total = $repository->dateProductNum($date);
  159. $list = $repository->orderProductGroup($date)->toArray();
  160. foreach ($list as &$item) {
  161. $item['rate'] = bcdiv($item['total'], $total, 2);
  162. }
  163. return app('json')->success(compact('list', 'total'));
  164. }
  165. /**
  166. * @param UserVisitRepository $repository
  167. * @return mixed
  168. * @author xaboy
  169. * @day 2020/6/25
  170. */
  171. public function merchantVisit(UserVisitRepository $repository)
  172. {
  173. $date = $this->request->param('date') ?: 'lately7';
  174. $total = $repository->dateVisitMerchantTotal($date);
  175. $list = $repository->dateVisitMerchantNum($date)->toArray();
  176. foreach ($list as &$item) {
  177. $item['rate'] = bcdiv($item['total'], $total, 2);
  178. }
  179. return app('json')->success(compact('list', 'total'));
  180. }
  181. /**
  182. * @param StoreOrderRepository $repository
  183. * @param MerchantCategoryRepository $merchantCategoryRepository
  184. * @return mixed
  185. * @author xaboy
  186. * @day 2020/6/25
  187. */
  188. public function merchantRate(StoreOrderRepository $repository, MerchantCategoryRepository $merchantCategoryRepository)
  189. {
  190. $date = $this->request->param('date') ?: 'lately7';
  191. $total = $repository->dateOrderPrice($date);
  192. $list = $merchantCategoryRepository->dateMerchantPriceGroup($date)->toArray();
  193. $rate = 1;
  194. $pay_price = $total;
  195. foreach ($list as &$item) {
  196. $item['rate'] = bcdiv($item['pay_price'], $total, 2);
  197. $rate = bcsub($rate, $item['rate'], 2);
  198. $pay_price = bcsub($pay_price, $item['pay_price'], 2);
  199. }
  200. if ($rate > 0 && count($list)) {
  201. $list[] = [
  202. 'pay_price' => $pay_price,
  203. 'category_name' => '其他类',
  204. 'rate' => $rate
  205. ];
  206. }
  207. return app('json')->success(compact('list', 'total'));
  208. }
  209. public function userData(UserRepository $repository, UserVisitRepository $visitRepository)
  210. {
  211. $date = $this->request->param('date') ?: 'lately7';
  212. $newUserList = $repository->userNumGroup($date)->toArray();
  213. $newUserList = array_combine(array_column($newUserList, 'time'), array_column($newUserList, 'new'));
  214. $visitList = $visitRepository->dateVisitNumGroup($date)->toArray();
  215. $visitList = array_combine(array_column($visitList, 'time'), array_column($visitList, 'total'));
  216. $base = $repository->beforeUserNum(getStartModelTime($date));
  217. $time = getDatesBetweenTwoDays(getStartModelTime($date), date('Y-m-d'));
  218. $userList = [];
  219. $before = $base;
  220. foreach ($time as $item) {
  221. $new = $newUserList[$item] ?? 0;
  222. $before += $new;
  223. $userList[] = [
  224. 'total' => $before,
  225. 'new' => $new,
  226. 'visit' => $visitList[$item] ?? 0,
  227. 'day' => $item
  228. ];
  229. }
  230. return app('json')->success($userList);
  231. }
  232. /**
  233. * @param $last
  234. * @param $today
  235. * @param int $scale
  236. * @return int|string|null
  237. * @author xaboy
  238. * @day 2020/6/25
  239. */
  240. protected function getRate($last, $today, $scale = 2)
  241. {
  242. if ($last == $today)
  243. return 0;
  244. else if ($last == 0)
  245. return $today;
  246. else if ($today == 0)
  247. return -$last;
  248. else
  249. return (float)bcdiv(bcsub($today, $last, $scale), $last, $scale);
  250. }
  251. /**
  252. * 申请授权
  253. * @return mixed
  254. */
  255. public function auth_apply()
  256. {
  257. $data = $this->request->params([
  258. ['company_name', ''],
  259. ['domain_name', ''],
  260. ['order_id', ''],
  261. ['phone', ''],
  262. ['label', 10],
  263. ['captcha', ''],
  264. ]);
  265. if (!$data['company_name']) {
  266. return app('json')->fail('请填写公司名称');
  267. }
  268. if (!$data['domain_name']) {
  269. return app('json')->fail('请填写授权域名');
  270. }
  271. if (!$data['phone']) {
  272. return app('json')->fail('请填写手机号码');
  273. }
  274. if (!$data['order_id']) {
  275. return app('json')->fail('请填写订单id');
  276. }
  277. if (!$data['captcha']) {
  278. return app('json')->fail('请填写验证码');
  279. }
  280. $res = HttpService::postRequest('http://authorize.crmeb.net/api/auth_apply', $data);
  281. if ($res === false) {
  282. return app('json')->fail('申请失败,服务器没有响应!');
  283. }
  284. $res = json_decode($res, true);
  285. if (isset($res['status'])) {
  286. if ($res['status'] == 400) {
  287. return app('json')->fail($res['msg'] ?? "申请失败");
  288. } else {
  289. return app('json')->success($res['msg'] ?? '申请成功', $res);
  290. }
  291. }
  292. return app('json')->fail("申请授权失败!");
  293. }
  294. public function uploadConfig(ConfigRepository $repository)
  295. {
  296. return app('json')->success(formToData($repository->uploadForm()));
  297. }
  298. public function saveUploadConfig(ConfigRepository $repository)
  299. {
  300. $formData = $this->request->post();
  301. if (!count($formData)) return app('json')->fail('保存失败');
  302. $repository->saveUpload($formData);
  303. return app('json')->success('保存成功');
  304. }
  305. public function loginConfig()
  306. {
  307. $login_logo = systemConfig('sys_login_logo');
  308. $menu_logo = systemConfig('sys_menu_logo');
  309. $menu_slogo = systemConfig('sys_menu_slogo');
  310. $login_title = systemConfig('sys_login_title');
  311. $login_banner = systemGroupData('sys_login_banner');
  312. return app('json')->success(compact('login_banner', 'login_logo', 'login_title', 'menu_slogo', 'menu_logo'));
  313. }
  314. public function version()
  315. {
  316. return app('json')->success(['version' => get_crmeb_version('未知')]);
  317. }
  318. }