UserBillController.php 20 KB

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