Common.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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\merchant;
  12. use app\common\repositories\system\CountRepository;
  13. use app\common\repositories\user\UserRepository;
  14. use crmeb\basic\BaseController;
  15. use app\common\repositories\system\merchant\MerchantRepository;
  16. use app\common\repositories\system\config\ConfigValueRepository;
  17. use app\common\repositories\store\order\StoreOrderProductRepository;
  18. use app\common\repositories\store\order\StoreOrderRepository;
  19. use app\common\repositories\store\product\ProductRepository;
  20. use app\common\repositories\user\UserRelationRepository;
  21. use app\common\repositories\user\UserVisitRepository;
  22. use crmeb\services\ImageWaterMarkService;
  23. use crmeb\services\UploadService;
  24. use Joypack\Tencent\Map\Bundle\Address;
  25. use Joypack\Tencent\Map\Bundle\AddressOption;
  26. use think\App;
  27. use think\facade\Cache;
  28. use think\facade\Db;
  29. /**
  30. * Class Common
  31. * @package app\controller\merchant
  32. * @author xaboy
  33. * @day 2020/6/25
  34. */
  35. class Common extends BaseController
  36. {
  37. /**
  38. * @var int|null
  39. */
  40. protected $merId;
  41. /**
  42. * Common constructor.
  43. * @param App $app
  44. */
  45. public function __construct(App $app)
  46. {
  47. parent::__construct($app);
  48. $this->merId = $this->request->merId() ?: null;
  49. }
  50. /**
  51. * @param null $merId
  52. * @return mixed
  53. * @author xaboy
  54. * @day 2020/6/25
  55. */
  56. public function main($merId = null)
  57. {
  58. $res = Cache::remember(self::class . '@mermain'.$this->merId, function () use($merId){
  59. $today = $this->mainGroup('today', $merId ?? $this->merId);
  60. $yesterday = $this->mainGroup('yesterday', $merId ?? $this->merId);
  61. $month = $this->mainGroup('month', $merId ?? $this->merId);
  62. $lastWeek = $this->mainGroup(date('Y-m-d', strtotime('- 7day')), $merId ?? $this->merId);
  63. $lastWeekRate = [];
  64. foreach ($lastWeek as $k => $item) {
  65. if ($item == $today[$k])
  66. $lastWeekRate[$k] = 0; else if ($item == 0)
  67. $lastWeekRate[$k] = $today[$k]; else if ($today[$k] == 0)
  68. $lastWeekRate[$k] = -$item; else
  69. $lastWeekRate[$k] = (float)bcdiv(bcsub($today[$k], $item, 4), $item, 4);
  70. }
  71. $day = date('Y-m-d');
  72. $res = compact('today', 'yesterday', 'lastWeekRate', 'day', 'month');
  73. return $res;
  74. }, 1800 + random_int(600, 1200));
  75. return $merId ? : app('json')->success($res);
  76. }
  77. /**
  78. * @param $date
  79. * @param $merId
  80. * @return array
  81. * @author xaboy
  82. * @day 2020/6/25
  83. */
  84. public function mainGroup($date, $merId)
  85. {
  86. $userVisitRepository = app()->make(UserVisitRepository::class);
  87. $repository = app()->make(StoreOrderRepository::class);
  88. $relationRepository = app()->make(UserRelationRepository::class);
  89. $orderNum = (float)$repository->dayOrderNum($date, $merId);
  90. $payPrice = (float)$repository->dayOrderPrice($date, $merId);
  91. $payUser = (float)$repository->dayOrderUserNum($date, $merId);
  92. $visitNum = (float)$userVisitRepository->dateVisitUserNum($date, $merId);
  93. $likeStore = (float)$relationRepository->dayLikeStore($date, $merId);
  94. return compact('orderNum', 'payPrice', 'payUser', 'visitNum', 'likeStore');
  95. }
  96. /**
  97. * @param StoreOrderRepository $repository
  98. * @return mixed
  99. * @author xaboy
  100. * @day 2020/6/25
  101. */
  102. public function order(StoreOrderRepository $repository)
  103. {
  104. $date = $this->request->param('date') ?: 'lately7';
  105. $res = Cache::remember(self::class . '@order' . $this->merId . $date, function () use ($repository, $date) {
  106. if ($date == 'year') {
  107. $m = date('m', time());
  108. $time[] = $m;
  109. do {
  110. $time[] = '0' . ($m - 1);
  111. $m--;
  112. } while ($m > 1);
  113. $time = array_reverse($time);
  114. } else {
  115. $time = getDatesBetweenTwoDays(getStartModelTime($date), date('Y-m-d'));
  116. }
  117. $list = $repository->orderGroupNum($date, $this->merId)->toArray();
  118. $list = array_combine(array_column($list, 'day'), $list);
  119. $data = [];
  120. foreach ($time as $item) {
  121. $data[] = [
  122. 'day' => $item,
  123. 'total' => $list[$item]['total'] ?? 0,
  124. 'user' => $list[$item]['user'] ?? 0,
  125. 'pay_price' => $list[$item]['pay_price'] ?? 0
  126. ];
  127. }
  128. return $data;
  129. }, 2000 + random_int(600, 1200));
  130. return app('json')->success($res);
  131. }
  132. /**
  133. * @param UserRelationRepository $repository
  134. * @param StoreOrderRepository $orderRepository
  135. * @param UserVisitRepository $userVisitRepository
  136. * @return \think\response\Json
  137. * @author xaboy
  138. * @day 2020/9/24
  139. */
  140. public function user(StoreOrderRepository $orderRepository, UserVisitRepository $userVisitRepository)
  141. {
  142. $date = $this->request->param('date', 'today') ?: 'today';
  143. $res = Cache::store('file')->remember(self::class . '@user' . $this->merId . $date, function () use ($orderRepository, $userVisitRepository, $date) {
  144. $visitUser = $userVisitRepository->dateVisitUserNum($date, $this->merId);
  145. $orderUser = $orderRepository->orderUserNum($date, null, $this->merId);
  146. $orderPrice = $orderRepository->orderPrice($date, null, $this->merId);
  147. $payOrderUser = $orderRepository->orderUserNum($date, 1, $this->merId);
  148. $payOrderPrice = $orderRepository->orderPrice($date, 1, $this->merId);
  149. $userRate = $payOrderUser ? bcdiv($payOrderPrice, $payOrderUser, 2) : 0;
  150. $orderRate = $visitUser ? bcdiv($orderUser, $visitUser, 2) : 0;
  151. $payOrderRate = $orderUser ? bcdiv($payOrderUser, $orderUser, 2) : 0;
  152. return compact('visitUser', 'orderUser', 'orderPrice', 'payOrderUser', 'payOrderPrice', 'payOrderRate', 'userRate', 'orderRate');
  153. }, 2000 + random_int(600, 1200));
  154. return app('json')->success($res);
  155. }
  156. /**
  157. * @param StoreOrderRepository $repository
  158. * @return mixed
  159. * @author xaboy
  160. * @day 2020/6/25
  161. */
  162. public function userRate(StoreOrderRepository $repository, UserRepository $userRepository)
  163. {
  164. $date = $this->request->param('date') ?: 'today';
  165. $res = Cache::store('file')->remember(self::class . '@userRate' . $this->merId . $date, function () use ($userRepository, $repository, $date) {
  166. $uids = $repository->orderUserGroup($date, 1, $this->merId)->toArray();
  167. $userPayCount = $userRepository->idsByPayCount(array_column($uids, 'uid'));
  168. $user = count($uids);
  169. $oldUser = 0;
  170. $totalPrice = 0;
  171. $oldTotalPrice = 0;
  172. foreach ($uids as $uid) {
  173. $totalPrice = bcadd($uid['pay_price'], $totalPrice, 2);
  174. if (($userPayCount[$uid['uid']] ?? 0) > $uid['total']) {
  175. $oldUser++;
  176. $oldTotalPrice = bcadd($uid['pay_price'], $oldTotalPrice, 2);
  177. }
  178. }
  179. $newTotalPrice = bcsub($totalPrice, $oldTotalPrice, 2);
  180. $newUser = $user - $oldUser;
  181. return compact('newTotalPrice', 'newUser', 'oldTotalPrice', 'oldUser', 'totalPrice', 'user');
  182. }, 2000 + random_int(600, 1200));
  183. return app('json')->success($res);
  184. }
  185. /**
  186. * @param StoreOrderProductRepository $repository
  187. * @return mixed
  188. * @author xaboy
  189. * @day 2020/6/25
  190. */
  191. public function product(StoreOrderProductRepository $repository)
  192. {
  193. $date = $this->request->param('date', 'today') ?: 'today';
  194. $res = Cache::store('file')->remember(self::class . '@product' . $this->merId . $date, function () use ($repository, $date) {
  195. return $repository->orderProductGroup($date, $this->merId)->toArray();
  196. }, 2000 + random_int(600, 1200));
  197. return app('json')->success($res);
  198. }
  199. public function productVisit(UserVisitRepository $repository)
  200. {
  201. $date = $this->request->param('date', 'today') ?: 'today';
  202. $res = Cache::store('file')->remember(self::class . '@productVisit' . $this->merId . $date, function () use ($repository, $date) {
  203. return $repository->dateVisitProductNum($date, $this->merId);
  204. }, 2000 + random_int(600, 1200));
  205. return app('json')->success($res);
  206. }
  207. /**
  208. * @param ProductRepository $repository
  209. * @return mixed
  210. * @author xaboy
  211. * @day 2020/6/25
  212. */
  213. public function productCart(ProductRepository $repository)
  214. {
  215. $date = $this->request->param('date', 'today') ?: 'today';
  216. $res = Cache::store('file')->remember(self::class . '@productCart' . $this->merId . $date, function () use ($repository, $date) {
  217. return $repository->cartProductGroup($date, $this->merId);
  218. }, 2000 + random_int(600, 1200));
  219. return app('json')->success($res);
  220. }
  221. public function uploadCertificate()
  222. {
  223. $file = $this->request->file('file');
  224. if (!$file)
  225. return app('json')->fail('请上传证书');
  226. validate(["file|图片" => [
  227. 'fileSize' => config('upload.filesize'),
  228. 'fileExt' => 'jpg,jpeg,png,bmp',
  229. 'fileMime' => 'image/jpeg,image/png',
  230. ]])->check(['file' => $file]);
  231. $upload = UploadService::create(1);
  232. $data = $upload->to('attach')->move('file', false, false);
  233. if ($data === false) {
  234. return app('json')->fail($upload->getError());
  235. }
  236. app()->make(ImageWaterMarkService::class)->run(public_path() . $upload->getFileInfo()->filePath);
  237. return app('json')->success(['src' => tidy_url($upload->getFileInfo()->filePath)]);
  238. }
  239. public function uploadVideo()
  240. {
  241. $file = $this->request->file('file');
  242. if (!$file)
  243. return app('json')->fail('请上传视频');
  244. validate(["file|视频" => [
  245. 'fileSize' => config('upload.filesize'),
  246. 'fileExt' => 'mp4,mov',
  247. 'fileMime' => 'video/mp4,video/quicktime',
  248. ]])->check(['file' => $file]);
  249. $upload = UploadService::create();
  250. $data = $upload->to('media')->validate([])->move('file');
  251. if ($data === false) {
  252. return app('json')->fail($upload->getError());
  253. }
  254. return app('json')->success(['src' => tidy_url($upload->getFileInfo()->filePath)]);
  255. }
  256. public function getMerchantCount()
  257. {
  258. return app('json')->success(app()->make(CountRepository::class)->getMerchantCount($this->request->merId()));
  259. }
  260. public function getMerchantTodo()
  261. {
  262. return app('json')->success(app()->make(CountRepository::class)->getMerchantTodo($this->request->merId()));
  263. }
  264. public function getProductSalesPriceTop()
  265. {
  266. $date = $this->request->param('date') ?: 'lately7';
  267. return app('json')->success(app()->make(CountRepository::class)->getMerchantProductSalesPriceTop($this->request->merId(), $date));
  268. }
  269. public function config()
  270. {
  271. $data = systemConfig(['tx_map_key', 'delivery_status', 'delivery_type', 'rmargin_emind_switch']);
  272. $mer = merchantConfig($this->request->merId(),[
  273. 'mer_dump_switch',
  274. 'mer_dump_type',
  275. ]);
  276. $reservation = merchantConfig($this->request->merId(),[ 'enable_assigned', 'enable_checkin', 'checkin_radius',
  277. 'enable_trace', 'trace_form_id', 'enable_tostore_assigned','checkin_take_photo']);
  278. $data['mer_id'] = $this->request->merId();
  279. $data['app_referer'] = 'sms,dump';
  280. $data = array_merge($data,$mer);
  281. $data['reservation'] = $reservation;
  282. return app('json')->success($data);
  283. }
  284. public function saveConfig($type)
  285. {
  286. switch ($type) {
  287. case 'reservation':
  288. $params = ['enable_assigned', 'enable_checkin', 'checkin_radius', 'enable_trace', 'trace_form_id','enable_tostore_assigned','checkin_take_photo'];
  289. break;
  290. default:
  291. $params = [];
  292. break;
  293. }
  294. if (!$params) {
  295. return app('json')->fail('缺少配置项');
  296. }
  297. $data = $this->request->params($params);
  298. $configValueRepository = app()->make(ConfigValueRepository::class);
  299. $configValueRepository->setFormData($data,$this->request->merId());
  300. return app('json')->success('添加成功');
  301. }
  302. }