UserBillController.php 21 KB

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