UserBillController.php 20 KB

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