UserBillController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. <?php
  2. namespace app\api\controller\user;
  3. use app\admin\model\system\SystemAttachment;
  4. use app\models\mining\MiningMachine;
  5. use app\models\mining\UserMiningMachine;
  6. use app\models\mining\UserMiningRatio;
  7. use app\models\mining\UserMiningService;
  8. use app\models\routine\RoutineCode;
  9. use app\models\routine\RoutineQrcode;
  10. use app\models\store\StoreOrder;
  11. use app\models\user\User;
  12. use app\models\user\UserBill;
  13. use app\models\user\UserExtract;
  14. use app\Request;
  15. use crmeb\services\GroupDataService;
  16. use crmeb\services\SystemConfigService;
  17. use crmeb\services\UtilService;
  18. use crmeb\services\upload\Upload;
  19. /**
  20. * 账单类
  21. * Class UserBillController
  22. * @package app\api\controller\user
  23. */
  24. class UserBillController
  25. {
  26. /**
  27. * 推广数据 昨天的佣金 累计提现金额 当前佣金
  28. * @param Request $request
  29. * @return mixed
  30. */
  31. public function commission(Request $request)
  32. {
  33. $uid = $request->uid();
  34. $lastDayCount = UserBill::yesterdayCommissionSum($uid);//昨天的佣金
  35. $extractCount = UserExtract::extractSum($uid);//累计提现金额
  36. $commissionCount = UserBill::getBrokerage($uid);//获取总佣金
  37. if ($commissionCount > 0) {
  38. $rechargeCount = UserBill::getRecharge($uid);//累计充值
  39. $orderYuePrice = StoreOrder::getOrderStatusYueSum($uid);//余额累计消费
  40. $systemAdd = UserBill::getSystemAdd($uid);//后台添加余额
  41. $yueCount = bcadd($rechargeCount, $systemAdd, 2);// 后台添加余额 + 累计充值 = 非佣金的总金额
  42. $orderYuePrice = $yueCount > $orderYuePrice ? 0 : bcsub($orderYuePrice, $yueCount, 2);// 余额累计消费(使用佣金消费的金额)
  43. $commissionCount = bcsub($commissionCount, $extractCount, 2);//减去已提现金额
  44. $extractPriceCount = UserExtract::userExtractTotalPrice($uid, 0);
  45. $commissionCount = $extractPriceCount < $commissionCount ? bcsub($commissionCount, $extractPriceCount, 2) : 0;//减去审核中的提现金额
  46. $commissionCount = $commissionCount > $orderYuePrice ? bcsub($commissionCount, $orderYuePrice, 2) : 0;//减掉余额支付
  47. }
  48. $data['lastDayCount'] = $lastDayCount;
  49. $data['extractCount'] = $extractCount;
  50. $data['commissionCount'] = $commissionCount;
  51. return app('json')->successful($data);
  52. }
  53. /**
  54. * 推荐用户
  55. * @param Request $request
  56. * @return mixed
  57. *
  58. * grade == 0 获取一级推荐人
  59. * grade == 1 获取二级推荐人
  60. *
  61. * keyword 会员名称查询
  62. *
  63. * sort childCount ASC/DESC 团队排序 numberCount ASC/DESC 金额排序 orderCount ASC/DESC 订单排序
  64. */
  65. public function spread_people(Request $request)
  66. {
  67. $spreadInfo = UtilService::postMore([
  68. ['page', 1],
  69. ['limit', 20],
  70. ['grade', 0],
  71. ['keyword', ''],
  72. ['sort', ''],
  73. ], $request);
  74. $uid = $request->uid();
  75. $data['list'] = User::getUserSpreadGrade($uid, $spreadInfo['grade'], $spreadInfo['sort'], $spreadInfo['keyword'], $spreadInfo['page'], $spreadInfo['limit']);
  76. $data['total'] = User::getSpreadCount($uid);
  77. $data['totalLevel'] = User::getSpreadLevelCount($uid);
  78. $recommend = User::where('spread_uid', $uid)->column('uid');
  79. $data['recommend_num'] = count($recommend);
  80. $data['recommend_achievement'] = UserMiningMachine::where('uid', 'in', $recommend)->where('status', 'in', [0, 1])->where('mining_start_time', '<=', time())
  81. ->where('mining_end_time', '>', time())->where('paid', 1)->sum('num');
  82. $sub_recommend = [];
  83. while ($recommend) {
  84. $sub_recommend = array_merge($sub_recommend, $recommend);
  85. $recommend = User::where('spread_uid', 'in', $recommend)->column('uid');
  86. }
  87. $data['group_num'] = count($sub_recommend);
  88. $data['group_achievenent'] = UserMiningMachine::where('uid', 'in', $sub_recommend)->where('status', 'in', [0, 1])->where('mining_start_time', '<=', time())
  89. ->where('mining_end_time', '>', time())->where('paid', 1)->sum('num');
  90. $max = 0;
  91. foreach ($sub_recommend as $v) {
  92. $re = UserMiningMachine::where('uid', $v)->where('status', 'in', [0, 1])->where('mining_start_time', '<=', time())
  93. ->where('mining_end_time', '>', time())->where('paid', 1)->sum('num');
  94. if ($re > $max) {
  95. $max = $re;
  96. }
  97. }
  98. $data['small_group_achievenent'] = $data['group_achievenent'] - $max;
  99. $data['today_achievement'] = UserMiningMachine::where('uid', 'in', $sub_recommend)->where('status', 'in', [0, 1])
  100. ->where('mining_end_time', '>', time())->whereTime('pay_time', 'today')->where('paid', 1)->sum('num');
  101. return app('json')->successful($data);
  102. }
  103. public function spread_people_all(Request $request)
  104. {
  105. $spreadInfo = UtilService::postMore([
  106. ['page', 1],
  107. ['limit', 20],
  108. ['keyword', ''],
  109. ['sort', ''],
  110. ['new', 0]
  111. ], $request);
  112. $uid = $request->uid();
  113. $data['list'] = User::getUserSpreadAll($uid, $spreadInfo['sort'], $spreadInfo['keyword'], $spreadInfo['page'], $spreadInfo['limit'], $spreadInfo['new']);
  114. if ($spreadInfo['new']) {
  115. $userStair = [];
  116. $users = [$uid];
  117. while ($users) {
  118. $users = User::where('spread_uid', 'in', $users)->column('uid');
  119. if ($users) $userStair = array_merge($userStair, $users);
  120. }
  121. $data['count'] = User::where('uid', 'in', $userStair)->whereTime('add_time', 'today')->count();
  122. }
  123. return app('json')->successful($data);
  124. }
  125. /**
  126. * 推广订单
  127. * @param Request $request
  128. * @return mixed
  129. */
  130. public function spread_order(Request $request)
  131. {
  132. $orderInfo = UtilService::postMore([
  133. ['page', 1],
  134. ['limit', 20],
  135. ['category', 'now_money'],
  136. ['type', 'brokerage'],
  137. ], $request);
  138. $data['list'] = [];
  139. $data['count'] = 0;
  140. $uid = $request->uid();
  141. $data['list'] = UserBill::getRecordList($uid, $orderInfo['page'], $orderInfo['limit'], $orderInfo['category'], $orderInfo['type']);
  142. $count = UserBill::getRecordOrderCount($uid, $orderInfo['category'], $orderInfo['type']);
  143. $data['count'] = $count ? $count : 0;
  144. if (!count($data['list'])) return app('json')->successful($data);
  145. foreach ($data['list'] as $key => &$value) {
  146. $value['child'] = UserBill::getRecordOrderListDraw($uid, $value['time'], $orderInfo['category'], $orderInfo['type']);
  147. $value['count'] = count($value['child']);
  148. }
  149. return app('json')->successful($data);
  150. }
  151. /**
  152. * 推广佣金明细
  153. * @param Request $request
  154. * @param $type 0 全部 1 消费 2 充值 3 返佣 4 提现
  155. * @return mixed
  156. */
  157. public function spread_commission(Request $request, $type)
  158. {
  159. list($page, $limit) = UtilService::getMore([
  160. ['page', 0],
  161. ['limit', 0],
  162. ], $request, true);
  163. return app('json')->successful(UserBill::getUserBillList($request->uid(), $page, $limit, $type));
  164. }
  165. /**
  166. * 推广 佣金/提现 总和
  167. * @param Request $request
  168. * @param $type 3 佣金 4 提现
  169. * @return mixed
  170. */
  171. public function spread_count(Request $request, $type)
  172. {
  173. $count = 0;
  174. if ($type == 3) {
  175. $count1 = UserBill::getRecordCount($request->uid(), 'now_money', 'brokerage');
  176. $count2 = UserBill::getRecordCount($request->uid(), 'now_money', 'brokerage', '', true);
  177. $count = $count1 - $count2;
  178. } else if ($type == 4) {
  179. $count = UserExtract::userExtractTotalPrice($request->uid());//累计提现
  180. }
  181. $count = $count ? $count : 0;
  182. return app('json')->successful(['count' => $count]);
  183. }
  184. /**
  185. * 分销二维码海报生成
  186. * @param Request $request
  187. * @return mixed
  188. */
  189. public function spread_banner(Request $request)
  190. {
  191. list($type) = UtilService::getMore([
  192. ['type', 2],
  193. ], $request, true);
  194. $user = $request->user();
  195. $rootPath = app()->getRootPath();
  196. try {
  197. $resRoutine = true;//小程序
  198. $resWap = true;//公众号
  199. $siteUrl = sys_config('site_url');
  200. $routineSpreadBanner = sys_data('routine_spread_banner');
  201. if (!count($routineSpreadBanner)) return app('json')->fail('暂无海报');
  202. if ($type == 1) {
  203. //小程序
  204. $name = $user['uid'] . '_' . $user['is_promoter'] . '_user_routine.jpg';
  205. $imageInfo = SystemAttachment::getInfo($name, 'name');
  206. //检测远程文件是否存在
  207. if (isset($imageInfo['att_dir']) && strstr($imageInfo['att_dir'], 'http') !== false && curl_file_exist($imageInfo['att_dir']) === false) {
  208. $imageInfo = null;
  209. SystemAttachment::where(['name' => $name])->delete();
  210. }
  211. if (!$imageInfo) {
  212. $res = RoutineCode::getShareCode($user['uid'], 'spread', '', '');
  213. if (!$res) return app('json')->fail('二维码生成失败');
  214. $uploadType = (int)sys_config('upload_type', 1);
  215. $upload = new Upload($uploadType, [
  216. 'accessKey' => sys_config('accessKey'),
  217. 'secretKey' => sys_config('secretKey'),
  218. 'uploadUrl' => sys_config('uploadUrl'),
  219. 'storageName' => sys_config('storage_name'),
  220. 'storageRegion' => sys_config('storage_region'),
  221. ]);
  222. $uploadRes = $upload->to('routine/spread/code')->validate()->stream($res['res'], $name);
  223. if ($uploadRes === false) {
  224. return app('json')->fail($upload->getError());
  225. }
  226. $imageInfo = $upload->getUploadInfo();
  227. $imageInfo['image_type'] = $uploadType;
  228. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  229. RoutineQrcode::setRoutineQrcodeFind($res['id'], ['status' => 1, 'url_time' => time(), 'qrcode_url' => $imageInfo['dir']]);
  230. $urlCode = $imageInfo['dir'];
  231. } else $urlCode = $imageInfo['att_dir'];
  232. if ($imageInfo['image_type'] == 1) $urlCode = $siteUrl . $urlCode;
  233. $siteUrlHttps = set_http_type($siteUrl, 0);
  234. $filelink = [
  235. 'Bold' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  236. 'Normal' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  237. ];
  238. if (!file_exists($filelink['Bold'])) return app('json')->fail('缺少字体文件Bold');
  239. if (!file_exists($filelink['Normal'])) return app('json')->fail('缺少字体文件Normal');
  240. foreach ($routineSpreadBanner as $key => &$item) {
  241. $posterInfo = '海报生成失败:(';
  242. $config = array(
  243. 'image' => array(
  244. array(
  245. 'url' => $urlCode, //二维码资源
  246. 'stream' => 0,
  247. 'left' => 114,
  248. 'top' => 790,
  249. 'right' => 0,
  250. 'bottom' => 0,
  251. 'width' => 120,
  252. 'height' => 120,
  253. 'opacity' => 100
  254. )
  255. ),
  256. 'text' => array(
  257. array(
  258. 'text' => $user['nickname'],
  259. 'left' => 250,
  260. 'top' => 840,
  261. 'fontPath' => $rootPath . 'public' . DS . $filelink['Bold'], //字体文件
  262. 'fontSize' => 16, //字号
  263. 'fontColor' => '40,40,40', //字体颜色
  264. 'angle' => 0,
  265. ),
  266. array(
  267. 'text' => '邀请您加入' . sys_config('site_name'),
  268. 'left' => 250,
  269. 'top' => 880,
  270. 'fontPath' => $rootPath . 'public' . DS . $filelink['Normal'], //字体文件
  271. 'fontSize' => 16, //字号
  272. 'fontColor' => '40,40,40', //字体颜色
  273. 'angle' => 0,
  274. )
  275. ),
  276. 'background' => $item['pic']
  277. );
  278. $resRoutine = $resRoutine && $posterInfo = UtilService::setSharePoster($config, 'routine/spread/poster');
  279. if (!is_array($posterInfo)) return app('json')->fail($posterInfo);
  280. SystemAttachment::attachmentAdd($posterInfo['name'], $posterInfo['size'], $posterInfo['type'], $posterInfo['dir'], $posterInfo['thumb_path'], 1, $posterInfo['image_type'], $posterInfo['time'], 2);
  281. if ($resRoutine) {
  282. if ($posterInfo['image_type'] == 1)
  283. $item['poster'] = $siteUrlHttps . $posterInfo['dir'];
  284. else
  285. $item['poster'] = set_http_type($posterInfo['dir'], 0);
  286. $item['poster'] = str_replace('\\', '/', $item['poster']);
  287. }
  288. }
  289. } else if ($type == 2) {
  290. //公众号
  291. $name = $user['uid'] . '_' . $user['is_promoter'] . '_user_wap.jpg';
  292. $imageInfo = SystemAttachment::getInfo($name, 'name');
  293. //检测远程文件是否存在
  294. if (isset($imageInfo['att_dir']) && strstr($imageInfo['att_dir'], 'http') !== false && curl_file_exist($imageInfo['att_dir']) === false) {
  295. $imageInfo = null;
  296. SystemAttachment::where(['name' => $name])->delete();
  297. }
  298. if (!$imageInfo) {
  299. $codeUrl = set_http_type($siteUrl . '?invite_code=' . $user['invite_code'], 1);//二维码链接
  300. $imageInfo = UtilService::getQRCodePath($codeUrl, $name);
  301. if (is_string($imageInfo)) return app('json')->fail('二维码生成失败', ['error' => $imageInfo]);
  302. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  303. $urlCode = $imageInfo['dir'];
  304. } else $urlCode = $imageInfo['att_dir'];
  305. if ($imageInfo['image_type'] == 1) $urlCode = $siteUrl . $urlCode;
  306. $siteUrl = set_http_type($siteUrl, 1);
  307. $filelink = [
  308. 'Bold' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  309. 'Normal' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  310. ];
  311. if (!file_exists($filelink['Bold'])) return app('json')->fail('缺少字体文件Bold');
  312. if (!file_exists($filelink['Normal'])) return app('json')->fail('缺少字体文件Normal');
  313. foreach ($routineSpreadBanner as $key => &$item) {
  314. $posterInfo = '海报生成失败:(';
  315. $config = array(
  316. 'image' => array(
  317. array(
  318. 'url' => $urlCode, //二维码资源
  319. 'stream' => 0,
  320. 'left' => 550,
  321. 'top' => 1063,
  322. 'right' => 0,
  323. 'bottom' => 0,
  324. 'width' => 130,
  325. 'height' => 120,
  326. 'opacity' => 100,
  327. )
  328. ),
  329. 'text' => array(
  330. array(
  331. 'text' => '邀请码:' . $user['invite_code'],
  332. 'left' => 516,
  333. 'top' => 1200,
  334. 'fontPath' => $rootPath . 'public' . DS . $filelink['Bold'], //字体文件
  335. 'fontSize' => 14, //字号
  336. 'fontColor' => '40,40,40', //字体颜色
  337. 'angle' => 0,
  338. ),
  339. ),
  340. 'background' => $item['pic']
  341. );
  342. $resWap = $resWap && $posterInfo = UtilService::setSharePoster($config, 'wap/spread/poster');
  343. if (!is_array($posterInfo)) return app('json')->fail($posterInfo);
  344. SystemAttachment::attachmentAdd($posterInfo['name'], $posterInfo['size'], $posterInfo['type'], $posterInfo['dir'], $posterInfo['thumb_path'], 1, $posterInfo['image_type'], $posterInfo['time'], 2);
  345. if ($resWap) {
  346. if ($posterInfo['image_type'] == 1)
  347. $item['wap_poster'] = $siteUrl . $posterInfo['thumb_path'];
  348. else
  349. $item['wap_poster'] = UtilService::setHttpType($posterInfo['thumb_path'], 1);
  350. }
  351. }
  352. }
  353. if ($resRoutine && $resWap) return app('json')->successful($routineSpreadBanner);
  354. else return app('json')->fail('生成图片失败');
  355. } catch (\Exception $e) {
  356. return app('json')->fail('生成图片时,系统错误', ['line' => $e->getLine(), 'message' => $e->getMessage(), 'file' => $e->getFile()]);
  357. }
  358. }
  359. /**
  360. * 积分记录
  361. * @param Request $request
  362. * @return mixed
  363. * @throws \think\db\exception\DataNotFoundException
  364. * @throws \think\db\exception\ModelNotFoundException
  365. * @throws \think\exception\DbException
  366. */
  367. public function integral_list(Request $request)
  368. {
  369. list($page, $limit) = UtilService::getMore([
  370. ['page', 0], ['limit', 0]
  371. ], $request, true);
  372. return app('json')->successful(UserBill::userBillList($request->uid(), $page, $limit));
  373. }
  374. public function setReturnUper($uid, Request $request)
  375. {
  376. $user = User::getUserInfo($uid);
  377. if (!$user || $user['spread_uid'] != $request->uid()) {
  378. return app('json')->fail('用户不合法');
  379. }
  380. $ratio = $request->post('ratio', 0);
  381. $mid = $request->post('mid', 0);
  382. if (!$mid || !MiningMachine::get($mid)) return app('json')->fail('矿机不合法');
  383. $self = UserMiningRatio::where('mid', $mid)->where('uid', $request->uid())->find();
  384. if (!isset($self['ratio']) || $ratio > $self['ratio']) {
  385. return app('json')->fail('佣金比例不能超过自己');
  386. }
  387. $uids = User::where('spread_uid', $user['uid'])->column('uid');
  388. $user2 = UserMiningRatio::where('mid', $mid)->where('uid', 'in', $uids)->max('ratio');
  389. if ($ratio < $user2) {
  390. return app('json')->fail('佣金比例不能低于目标下级');
  391. }
  392. if (UserMiningRatio::where('mid', $mid)->where('uid', $uid)->find()) {
  393. $res = UserMiningRatio::where('mid', $mid)->where('uid', $uid)
  394. ->update(['ratio' => $ratio, 'update_time' => time(), 'update_uid' => $request->uid()]);
  395. } else {
  396. $res = UserMiningRatio::create([
  397. 'uid' => $uid,
  398. 'mid' => $mid,
  399. 'ratio' => $ratio,
  400. 'add_time' => time(),
  401. 'update_time' => time(),
  402. 'update_uid' => $request->uid(),
  403. ]);
  404. }
  405. if ($res) {
  406. return app('json')->success('修改成功');
  407. } else {
  408. return app('json')->fail('修改失败或者您没有修改什么');
  409. }
  410. }
  411. public function setService($uid, Request $request)
  412. {
  413. $user = User::getUserInfo($uid);
  414. if (!$user || $user['spread_uid'] != $request->uid()) {
  415. return app('json')->fail('用户不合法');
  416. }
  417. $ratio = $request->post('ratio', 0);
  418. $mid = $request->post('mid', 0);
  419. if (!$mid || !MiningMachine::get($mid)) return app('json')->fail('矿机不合法');
  420. $self = UserMiningService::where('mid', $mid)->where('uid', $request->uid())->find();
  421. if (!isset($self['ratio']) || $ratio < $self['ratio']) {
  422. return app('json')->fail('服务费比例不能低于自己');
  423. }
  424. $uids = User::where('spread_uid', $user['uid'])->column('uid');
  425. $user2 = UserMiningService::where('mid', $mid)->where('uid', 'in', $uids)->min('ratio');
  426. if ($user2 > 0 && $ratio > $user2) {
  427. return app('json')->fail('服务费比例不能高于目标下级');
  428. }
  429. if ($ratio > MiningMachine::where('id', $mid)->value('service_ratio')) {
  430. return app('json')->fail('服务费比例不能高于系统设置');
  431. }
  432. if (UserMiningService::where('mid', $mid)->where('uid', $uid)->find()) {
  433. $res = UserMiningService::where('mid', $mid)->where('uid', $uid)
  434. ->update(['ratio' => $ratio, 'update_time' => time(), 'update_uid' => $request->uid()]);
  435. } else {
  436. $res = UserMiningService::create([
  437. 'uid' => $uid,
  438. 'mid' => $mid,
  439. 'ratio' => $ratio,
  440. 'add_time' => time(),
  441. 'update_time' => time(),
  442. 'update_uid' => $request->uid(),
  443. ]);
  444. }
  445. if ($res) {
  446. return app('json')->success('修改成功');
  447. } else {
  448. return app('json')->fail('修改失败或者您没有修改什么');
  449. }
  450. }
  451. }