Common.php 11 KB

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