UserBillController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\api\controller\v1\user;
  12. use app\Request;
  13. use app\services\other\PosterServices;
  14. use app\services\other\QrcodeServices;
  15. use app\services\system\attachment\SystemAttachmentServices;
  16. use app\services\system\config\SystemConfigServices;
  17. use app\services\user\UserBillServices;
  18. use app\services\user\UserBrokerageServices;
  19. use app\services\user\UserMoneyServices;
  20. use crmeb\services\app\MiniProgramService;
  21. use app\services\other\UploadService;
  22. /**
  23. * 账单类
  24. * Class UserBillController
  25. * @package app\api\controller\user
  26. */
  27. class UserBillController
  28. {
  29. protected $services;
  30. /**
  31. * UserBillController constructor.
  32. * @param UserBillServices $services
  33. */
  34. public function __construct(UserBillServices $services)
  35. {
  36. $this->services = $services;
  37. }
  38. /**
  39. * 推广数据 昨天的佣金 累计提现金额 当前佣金
  40. * @param Request $request
  41. * @return mixed
  42. */
  43. public function commission(Request $request)
  44. {
  45. $uid = (int)$request->uid();
  46. return app('json')->success($this->services->commission($uid));
  47. }
  48. /**
  49. * 推广订单
  50. * @param Request $request
  51. * @return mixed
  52. */
  53. public function spread_order(Request $request)
  54. {
  55. $orderInfo = $request->postMore([
  56. ['page', 1],
  57. ['limit', 20],
  58. ['category', 'now_money'],
  59. ['type', 'brokerage'],
  60. ]);
  61. $uid = (int)$request->uid();
  62. return app('json')->success($this->services->spread_order($uid, $orderInfo));
  63. }
  64. /**
  65. * 推广佣金明细
  66. * @param Request $request
  67. * @param $type 0 全部 1 消费 2 充值 3 返佣 4 提现
  68. * @return mixed
  69. */
  70. public function spread_commission(Request $request, $type)
  71. {
  72. $uid = (int)$request->uid();
  73. $data = [];
  74. switch ($type) {
  75. case 0:
  76. case 1:
  77. case 2:
  78. /** @var UserMoneyServices $moneyService */
  79. $moneyService = app()->make(UserMoneyServices::class);
  80. $data = $moneyService->getMoneyList($uid, $type);
  81. break;
  82. case 3:
  83. case 4:
  84. /** @var UserBrokerageServices $brokerageService */
  85. $brokerageService = app()->make(UserBrokerageServices::class);
  86. $data = $brokerageService->getBrokerageList($uid, $type);
  87. break;
  88. }
  89. return app('json')->success($data);
  90. }
  91. /**
  92. * 推广 佣金/提现 总和
  93. * @param Request $request
  94. * @param $type 3 佣金 4 提现
  95. * @return mixed
  96. */
  97. public function spread_count(Request $request, $type)
  98. {
  99. $uid = (int)$request->uid();
  100. return app('json')->success(['count' => $this->services->spread_count($uid, $type)]);
  101. }
  102. /**
  103. * 分销二维码海报生成
  104. * @param Request $request
  105. * @return mixed
  106. */
  107. public function spread_banner(Request $request)
  108. {
  109. list($type) = $request->getMore([
  110. ['type', 2],
  111. ], true);
  112. $user = $request->user();
  113. $rootPath = app()->getRootPath();
  114. /** @var SystemConfigServices $systemConfigServices */
  115. $systemConfigServices = app()->make(SystemConfigServices::class);
  116. $spreadBanner = $systemConfigServices->getSpreadBanner() ?? [];
  117. $bannerCount = count($spreadBanner);
  118. if (!$bannerCount) return app('json')->fail(410166);
  119. $routineSpreadBanner = [];
  120. foreach ($spreadBanner as $item) {
  121. $routineSpreadBanner[] = ['pic' => $item];
  122. }
  123. if ($type == 1) {
  124. $poster = $user['uid'] . '_' . $user['is_promoter'] . '_user_routine_poster_';
  125. } else {
  126. $poster = $user['uid'] . '_' . $user['is_promoter'] . '_user_wap_poster_';
  127. }
  128. /** @var SystemAttachmentServices $systemAttachment */
  129. $systemAttachment = app()->make(SystemAttachmentServices::class);
  130. /** @var QrcodeServices $qrCode */
  131. $qrCode = app()->make(QrcodeServices::class);
  132. $count = $systemAttachment->getCount([['name', 'LIKE', "$poster%"]]);
  133. if ($count) {
  134. $SpreadBanner = $systemAttachment->getLikeNameList($poster);
  135. //发生变化 重新生成
  136. if ($bannerCount != count($SpreadBanner)) {
  137. $systemAttachment->delete([['name', 'like', "$poster%"]]);
  138. } else {
  139. $siteUrl = sys_config('site_url');
  140. $siteUrlHttps = set_http_type($siteUrl, $request->isSsl() ? 0 : 1);
  141. foreach ($SpreadBanner as &$item) {
  142. if ($type == 1) {
  143. if ($item['image_type'] == 1)
  144. $item['poster'] = $siteUrlHttps . $item['att_dir'];
  145. else
  146. $item['poster'] = set_http_type($item['att_dir'], $request->isSsl() ? 0 : 1);
  147. $item['poster'] = str_replace('\\', '/', $item['poster']);
  148. } else {
  149. if ($item['image_type'] == 1)
  150. $item['wap_poster'] = $siteUrl . $item['att_dir'];
  151. else
  152. $item['wap_poster'] = set_http_type($item['att_dir'], 1);
  153. }
  154. }
  155. return app('json')->success($SpreadBanner);
  156. }
  157. }
  158. try {
  159. $resRoutine = true;//小程序
  160. $resWap = true;//公众号
  161. $siteUrl = sys_config('site_url');
  162. if ($type == 1) {
  163. //小程序
  164. $name = $user['uid'] . '_' . $user['is_promoter'] . '_user_routine.jpg';
  165. $imageInfo = $systemAttachment->getInfo(['name' => $name]);
  166. //检测远程文件是否存在
  167. if (isset($imageInfo['att_dir']) && strstr($imageInfo['att_dir'], 'http') !== false && curl_file_exist($imageInfo['att_dir']) === false) {
  168. $imageInfo = null;
  169. $systemAttachment->delete(['name' => $name]);
  170. }
  171. if (!$imageInfo) {
  172. $resForever = $qrCode->qrCodeForever($user['uid'], 'spread', '', '');
  173. $resCode = MiniProgramService::appCodeUnlimitService($resForever->id, '', 280);
  174. if ($resCode) {
  175. $res = ['res' => $resCode, 'id' => $resForever->id];
  176. } else {
  177. $res = false;
  178. }
  179. if (!$res) return app('json')->fail(410167);
  180. $uploadType = (int)sys_config('upload_type', 1);
  181. $upload = UploadService::init();
  182. $uploadRes = $upload->to('routine/spread/code')->validate()->setAuthThumb(false)->stream($res['res'], $name);
  183. if ($uploadRes === false) {
  184. return app('json')->fail($upload->getError());
  185. }
  186. $imageInfo = $upload->getUploadInfo();
  187. $imageInfo['image_type'] = $uploadType;
  188. $systemAttachment->attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  189. $qrCode->setQrcodeFind($res['id'], ['status' => 1, 'url_time' => time(), 'qrcode_url' => $imageInfo['dir']]);
  190. $urlCode = $imageInfo['dir'];
  191. } else $urlCode = $imageInfo['att_dir'];
  192. if ($imageInfo['image_type'] == 1) $urlCode = $siteUrl . $urlCode;
  193. $siteUrlHttps = set_http_type($siteUrl, $request->isSsl() ? 0 : 1);
  194. $filelink = [
  195. 'Bold' => 'statics' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  196. 'Normal' => 'statics' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  197. ];
  198. if (!file_exists($filelink['Bold'])) return app('json')->fail(410168);
  199. if (!file_exists($filelink['Normal'])) return app('json')->fail(410169);
  200. foreach ($routineSpreadBanner as $key => &$item) {
  201. $posterInfo = '海报生成失败:(';
  202. $config = array(
  203. 'image' => array(
  204. array(
  205. 'url' => $urlCode, //二维码资源
  206. 'stream' => 0,
  207. 'left' => 114,
  208. 'top' => 790,
  209. 'right' => 0,
  210. 'bottom' => 0,
  211. 'width' => 120,
  212. 'height' => 120,
  213. 'opacity' => 100
  214. )
  215. ),
  216. 'text' => array(
  217. array(
  218. 'text' => $user['nickname'],
  219. 'left' => 250,
  220. 'top' => 840,
  221. 'fontPath' => $rootPath . 'public' . DS . $filelink['Bold'], //字体文件
  222. 'fontSize' => 16, //字号
  223. 'fontColor' => '40,40,40', //字体颜色
  224. 'angle' => 0,
  225. ),
  226. array(
  227. 'text' => '邀请您加入' . sys_config('site_name'),
  228. 'left' => 250,
  229. 'top' => 880,
  230. 'fontPath' => $rootPath . 'public' . DS . $filelink['Normal'], //字体文件
  231. 'fontSize' => 16, //字号
  232. 'fontColor' => '40,40,40', //字体颜色
  233. 'angle' => 0,
  234. )
  235. ),
  236. 'background' => $item['pic']
  237. );
  238. $resRoutine = $resRoutine && $posterInfo = PosterServices::setSharePoster($config, 'routine/spread/poster', $user['uid'] . '_' . $user['is_promoter'] . '_user_routine_poster_' . $key . '.jpg');
  239. if (!is_array($posterInfo)) return app('json')->fail($posterInfo);
  240. $systemAttachment->attachmentAdd($posterInfo['name'], $posterInfo['size'], $posterInfo['type'], $posterInfo['dir'], $posterInfo['thumb_path'], 1, $posterInfo['image_type'], $posterInfo['time'], 2);
  241. if ($resRoutine) {
  242. if ($posterInfo['image_type'] == 1)
  243. $item['poster'] = $siteUrlHttps . $posterInfo['dir'];
  244. else
  245. $item['poster'] = set_http_type($posterInfo['dir'], $request->isSsl() ? 0 : 1);
  246. $item['poster'] = str_replace('\\', '/', $item['poster']);
  247. }
  248. }
  249. } else if ($type == 2) {
  250. //公众号
  251. $name = $user['uid'] . '_' . $user['is_promoter'] . '_user_wap.jpg';
  252. $imageInfo = $systemAttachment->getInfo(['name' => $name]);
  253. //检测远程文件是否存在
  254. if (isset($imageInfo['att_dir']) && strstr($imageInfo['att_dir'], 'http') !== false && curl_file_exist($imageInfo['att_dir']) === false) {
  255. $imageInfo = null;
  256. $systemAttachment->delete(['name' => $name]);
  257. }
  258. if (!$imageInfo) {
  259. $codeUrl = set_http_type($siteUrl . '?spread=' . $user['uid'], $request->isSsl() ? 0 : 1);//二维码链接
  260. $imageInfo = PosterServices::getQRCodePath($codeUrl, $name);
  261. if (is_string($imageInfo)) return app('json')->fail(410167, ['error' => $imageInfo]);
  262. $systemAttachment->attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  263. $urlCode = $imageInfo['dir'];
  264. } else $urlCode = $imageInfo['att_dir'];
  265. if ($imageInfo['image_type'] == 1) $urlCode = $siteUrl . $urlCode;
  266. $siteUrl = set_http_type($siteUrl, $request->isSsl() ? 0 : 1);
  267. $filelink = [
  268. 'Bold' => 'statics' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  269. 'Normal' => 'statics' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  270. ];
  271. if (!file_exists($filelink['Bold'])) return app('json')->fail(410168);
  272. if (!file_exists($filelink['Normal'])) return app('json')->fail(410169);
  273. foreach ($routineSpreadBanner as $key => &$item) {
  274. $posterInfo = '海报生成失败:(';
  275. $config = array(
  276. 'image' => array(
  277. array(
  278. 'url' => $urlCode, //二维码资源
  279. 'stream' => 0,
  280. 'left' => 114,
  281. 'top' => 790,
  282. 'right' => 0,
  283. 'bottom' => 0,
  284. 'width' => 120,
  285. 'height' => 120,
  286. 'opacity' => 100
  287. )
  288. ),
  289. 'text' => array(
  290. array(
  291. 'text' => $user['nickname'],
  292. 'left' => 250,
  293. 'top' => 840,
  294. 'fontPath' => $rootPath . 'public' . DS . $filelink['Bold'], //字体文件
  295. 'fontSize' => 16, //字号
  296. 'fontColor' => '40,40,40', //字体颜色
  297. 'angle' => 0,
  298. ),
  299. array(
  300. 'text' => '邀请您加入' . sys_config('site_name'),
  301. 'left' => 250,
  302. 'top' => 880,
  303. 'fontPath' => $rootPath . 'public' . DS . $filelink['Normal'], //字体文件
  304. 'fontSize' => 16, //字号
  305. 'fontColor' => '40,40,40', //字体颜色
  306. 'angle' => 0,
  307. )
  308. ),
  309. 'background' => $item['pic']
  310. );
  311. $resWap = $resWap && $posterInfo = PosterServices::setSharePoster($config, 'wap/spread/poster', $user['uid'] . '_' . $user['is_promoter'] . '_user_wap_poster_' . $key . '.jpg');
  312. if (!is_array($posterInfo)) return app('json')->fail($posterInfo);
  313. $systemAttachment->attachmentAdd($posterInfo['name'], $posterInfo['size'], $posterInfo['type'], $posterInfo['dir'], $posterInfo['thumb_path'], 1, $posterInfo['image_type'], $posterInfo['time'], 2);
  314. if ($resWap) {
  315. if ($posterInfo['image_type'] == 1)
  316. $item['wap_poster'] = $siteUrl . $posterInfo['thumb_path'];
  317. else
  318. $item['wap_poster'] = set_http_type($posterInfo['thumb_path'], 1);
  319. }
  320. }
  321. }
  322. if ($resRoutine && $resWap) return app('json')->success($routineSpreadBanner);
  323. else return app('json')->fail(410170);
  324. } catch (\Exception $e) {
  325. return app('json')->fail(410171, ['line' => $e->getLine(), 'message' => $e->getMessage(), 'file' => $e->getFile()]);
  326. }
  327. }
  328. /**
  329. * 获取小程序二维码
  330. * @param Request $request
  331. * @return mixed
  332. * @throws \think\Exception
  333. * @throws \think\db\exception\DataNotFoundException
  334. * @throws \think\db\exception\DbException
  335. * @throws \think\db\exception\ModelNotFoundException
  336. */
  337. public function getRoutineCode(Request $request)
  338. {
  339. $user = $request->user();
  340. /** @var SystemAttachmentServices $systemAttachment */
  341. $systemAttachment = app()->make(SystemAttachmentServices::class);
  342. //小程序
  343. $name = $user['uid'] . '_' . $user['is_promoter'] . '_user_routine.jpg';
  344. $imageInfo = $systemAttachment->getInfo(['name' => $name]);
  345. //检测远程文件是否存在
  346. if (isset($imageInfo['att_dir']) && strstr($imageInfo['att_dir'], 'http') !== false && curl_file_exist($imageInfo['att_dir']) === false) {
  347. $imageInfo = null;
  348. $systemAttachment->delete(['name' => $name]);
  349. }
  350. $siteUrl = sys_config('site_url');
  351. /** @var QrcodeServices $qrCode */
  352. $qrCode = app()->make(QrcodeServices::class);
  353. if (!$imageInfo) {
  354. $resForever = $qrCode->qrCodeForever($user['uid'], 'spread', '', '');
  355. $resCode = MiniProgramService::appCodeUnlimitService($resForever->id, '', 280);
  356. if ($resCode) {
  357. $res = ['res' => $resCode, 'id' => $resForever->id];
  358. } else {
  359. $res = false;
  360. }
  361. if (!$res) return app('json')->fail(410167);
  362. $uploadType = (int)sys_config('upload_type', 1);
  363. $upload = UploadService::init();
  364. $uploadRes = $upload->to('routine/spread/code')->validate()->setAuthThumb(false)->stream($res['res'], $name);
  365. if ($uploadRes === false) {
  366. return app('json')->fail($upload->getError());
  367. }
  368. $imageInfo = $upload->getUploadInfo();
  369. $imageInfo['image_type'] = $uploadType;
  370. $systemAttachment->attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  371. $qrCode->setQrcodeFind($res['id'], ['status' => 1, 'url_time' => time(), 'qrcode_url' => $imageInfo['dir']]);
  372. $urlCode = $imageInfo['dir'];
  373. } else $urlCode = $imageInfo['att_dir'];
  374. if ($imageInfo['image_type'] == 1) $urlCode = $siteUrl . $urlCode;
  375. return app('json')->success(['url' => $urlCode]);
  376. }
  377. /**
  378. * 获取海报详细信息
  379. * @param Request $request
  380. * @return mixed
  381. * @throws \think\db\exception\DataNotFoundException
  382. * @throws \think\db\exception\DbException
  383. * @throws \think\db\exception\ModelNotFoundException
  384. */
  385. public function getSpreadInfo(Request $request)
  386. {
  387. /** @var SystemConfigServices $systemConfigServices */
  388. $systemConfigServices = app()->make(SystemConfigServices::class);
  389. $spreadBanner = $systemConfigServices->getSpreadBanner() ?? [];
  390. $bannerCount = count($spreadBanner);
  391. $routineSpreadBanner = [];
  392. if ($bannerCount) {
  393. foreach ($spreadBanner as $item) {
  394. $routineSpreadBanner[] = ['pic' => $item];
  395. }
  396. }
  397. if (sys_config('share_qrcode', 0) && request()->isWechat()) {
  398. /** @var QrcodeServices $qrcodeService */
  399. $qrcodeService = app()->make(QrcodeServices::class);
  400. $qrcode = $qrcodeService->getTemporaryQrcode('spread', $request->uid())->url;
  401. } else {
  402. $qrcode = '';
  403. }
  404. return app('json')->success([
  405. 'spread' => $routineSpreadBanner,
  406. 'qrcode' => $qrcode,
  407. 'nickname' => $request->user('nickname'),
  408. 'site_name' => sys_config('site_name')
  409. ]);
  410. }
  411. /**
  412. * 积分记录
  413. * @param Request $request
  414. * @return mixed
  415. * @throws \think\db\exception\DataNotFoundException
  416. * @throws \think\db\exception\DbException
  417. * @throws \think\db\exception\ModelNotFoundException
  418. */
  419. public function integral_list(Request $request)
  420. {
  421. $uid = (int)$request->uid();
  422. $data = $this->services->getIntegralList($uid);
  423. return app('json')->success($data['list'] ?? []);
  424. }
  425. /**
  426. * 佣金排行
  427. * @param Request $request
  428. * @return mixed
  429. * @throws \think\db\exception\DataNotFoundException
  430. * @throws \think\db\exception\DbException
  431. * @throws \think\db\exception\ModelNotFoundException
  432. */
  433. public function brokerage_rank(Request $request)
  434. {
  435. $data = $request->getMore([
  436. ['page', ''],
  437. ['limit'],
  438. ['type']
  439. ]);
  440. $uid = (int)$request->uid();
  441. return app('json')->success($this->services->brokerage_rank($uid, $data['type']));
  442. }
  443. /**
  444. * 事业部/代理商推广订单
  445. * @param Request $request
  446. * @return mixed
  447. */
  448. public function divisionOrder(Request $request)
  449. {
  450. $uid = (int)$request->uid();
  451. return app('json')->success($this->services->divisionOrder($uid));
  452. }
  453. }