UserBillController.php 19 KB

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