UserBillController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <?php
  2. namespace app\api\controller\user;
  3. use app\admin\model\system\SystemAttachment;
  4. use app\models\routine\RoutineCode;
  5. use app\models\routine\RoutineQrcode;
  6. use app\models\store\StoreOrder;
  7. use app\models\user\User;
  8. use app\models\user\UserBill;
  9. use app\models\user\UserExtract;
  10. use app\Request;
  11. use crmeb\services\GroupDataService;
  12. use crmeb\services\QrcodeService;
  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. return app('json')->successful($data);
  76. }
  77. /**
  78. * 推广订单
  79. * @param Request $request
  80. * @return mixed
  81. */
  82. public function spread_order(Request $request)
  83. {
  84. $orderInfo = UtilService::postMore([
  85. ['page', 1],
  86. ['limit', 20],
  87. ['category', 'now_money'],
  88. ['type', 'brokerage'],
  89. ], $request);
  90. $data['list'] = [];
  91. $data['count'] = 0;
  92. $uid = $request->uid();
  93. $data['list'] = UserBill::getRecordList($uid, $orderInfo['page'], $orderInfo['limit'], $orderInfo['category'], $orderInfo['type']);
  94. $count = UserBill::getRecordOrderCount($uid, $orderInfo['category'], $orderInfo['type']);
  95. $data['count'] = $count ? $count : 0;
  96. if (!count($data['list'])) return app('json')->successful($data);
  97. foreach ($data['list'] as $key => &$value) {
  98. $value['child'] = UserBill::getRecordOrderListDraw($uid, $value['time'], $orderInfo['category'], $orderInfo['type']);
  99. $value['count'] = count($value['child']);
  100. }
  101. return app('json')->successful($data);
  102. }
  103. /**
  104. * 推广佣金明细
  105. * @param Request $request
  106. * @param $type 0 全部 1 消费 2 充值 3 返佣 4 提现
  107. * @return mixed
  108. */
  109. public function spread_commission(Request $request, $type)
  110. {
  111. list($page, $limit) = UtilService::getMore([
  112. ['page', 0],
  113. ['limit', 0],
  114. ], $request, true);
  115. return app('json')->successful(UserBill::getUserBillList($request->uid(), $page, $limit, $type));
  116. }
  117. /**
  118. * 推广 佣金/提现 总和
  119. * @param Request $request
  120. * @param $type 3 佣金 4 提现
  121. * @return mixed
  122. */
  123. public function spread_count(Request $request, $type)
  124. {
  125. $count = 0;
  126. if ($type == 3) {
  127. $count1 = UserBill::getRecordCount($request->uid(), 'now_money', 'brokerage');
  128. $count2 = UserBill::getRecordCount($request->uid(), 'now_money', 'brokerage', '', true);
  129. $count = $count1 - $count2;
  130. } else if ($type == 4) {
  131. $count = UserExtract::userExtractTotalPrice($request->uid());//累计提现
  132. }
  133. $count = $count ? $count : 0;
  134. return app('json')->successful(['count' => $count]);
  135. }
  136. /**
  137. * 分销二维码海报生成
  138. * @param Request $request
  139. * @return mixed
  140. */
  141. public function spread_banner(Request $request)
  142. {
  143. list($type) = UtilService::getMore([
  144. ['type', 2],
  145. ], $request, true);
  146. $user = $request->user();
  147. $rootPath = app()->getRootPath();
  148. try {
  149. $resRoutine = true;//小程序
  150. $resWap = true;//公众号
  151. $siteUrl = sys_config('site_url');
  152. $routineSpreadBanner = sys_data('routine_spread_banner');
  153. if (!count($routineSpreadBanner)) return app('json')->fail('暂无海报');
  154. if ($type == 1) {
  155. //小程序
  156. $name = $user['uid'] . '_' . $user['is_promoter'] . '_user_routine.jpg';
  157. $imageInfo = SystemAttachment::getInfo($name, 'name');
  158. //检测远程文件是否存在
  159. if (isset($imageInfo['att_dir']) && strstr($imageInfo['att_dir'], 'http') !== false && curl_file_exist($imageInfo['att_dir']) === false) {
  160. $imageInfo = null;
  161. SystemAttachment::where(['name' => $name])->delete();
  162. }
  163. if (!$imageInfo) {
  164. $res = RoutineCode::getShareCode($user['uid'], 'spread', '', '');
  165. if (!$res) return app('json')->fail('二维码生成失败');
  166. $uploadType = (int)sys_config('upload_type', 1);
  167. $upload = new Upload($uploadType, [
  168. 'accessKey' => sys_config('accessKey'),
  169. 'secretKey' => sys_config('secretKey'),
  170. 'uploadUrl' => sys_config('uploadUrl'),
  171. 'storageName' => sys_config('storage_name'),
  172. 'storageRegion' => sys_config('storage_region'),
  173. ]);
  174. $uploadRes = $upload->to('routine/spread/code')->validate()->stream($res['res'], $name);
  175. if ($uploadRes === false) {
  176. return app('json')->fail($upload->getError());
  177. }
  178. $imageInfo = $upload->getUploadInfo();
  179. $imageInfo['image_type'] = $uploadType;
  180. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  181. RoutineQrcode::setRoutineQrcodeFind($res['id'], ['status' => 1, 'url_time' => time(), 'qrcode_url' => $imageInfo['dir']]);
  182. $urlCode = $imageInfo['dir'];
  183. } else $urlCode = $imageInfo['att_dir'];
  184. if ($imageInfo['image_type'] == 1) $urlCode = $siteUrl . $urlCode;
  185. $siteUrlHttps = set_http_type($siteUrl, 0);
  186. $filelink = [
  187. 'Bold' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  188. 'Normal' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  189. ];
  190. if (!file_exists($filelink['Bold'])) return app('json')->fail('缺少字体文件Bold');
  191. if (!file_exists($filelink['Normal'])) return app('json')->fail('缺少字体文件Normal');
  192. foreach ($routineSpreadBanner as $key => &$item) {
  193. $posterInfo = '海报生成失败:(';
  194. $config = array(
  195. 'image' => array(
  196. array(
  197. 'url' => $urlCode, //二维码资源
  198. 'stream' => 0,
  199. 'left' => 225,
  200. 'top' => 913,
  201. 'right' => 0,
  202. 'bottom' => 0,
  203. 'width' => 300,
  204. 'height' => 300,
  205. 'opacity' => 100
  206. )
  207. ),
  208. 'text' => array(
  209. array(
  210. 'text' => $user['nickname'],
  211. 'left' => 250,
  212. 'top' => 840,
  213. 'fontPath' => $rootPath . 'public' . DS . $filelink['Bold'], //字体文件
  214. 'fontSize' => 16, //字号
  215. 'fontColor' => '40,40,40', //字体颜色
  216. 'angle' => 0,
  217. ),
  218. array(
  219. 'text' => '邀请您加入' . sys_config('site_name'),
  220. 'left' => 250,
  221. 'top' => 880,
  222. 'fontPath' => $rootPath . 'public' . DS . $filelink['Normal'], //字体文件
  223. 'fontSize' => 16, //字号
  224. 'fontColor' => '40,40,40', //字体颜色
  225. 'angle' => 0,
  226. )
  227. ),
  228. 'background' => $item['pic']
  229. );
  230. $resRoutine = $resRoutine && $posterInfo = UtilService::setSharePoster($config, 'routine/spread/poster');
  231. if (!is_array($posterInfo)) return app('json')->fail($posterInfo);
  232. SystemAttachment::attachmentAdd($posterInfo['name'], $posterInfo['size'], $posterInfo['type'], $posterInfo['dir'], $posterInfo['thumb_path'], 1, $posterInfo['image_type'], $posterInfo['time'], 2);
  233. if ($resRoutine) {
  234. if ($posterInfo['image_type'] == 1)
  235. $item['poster'] = $siteUrlHttps . $posterInfo['dir'];
  236. else
  237. $item['poster'] = set_http_type($posterInfo['dir'], 0);
  238. $item['poster'] = str_replace('\\', '/', $item['poster']);
  239. }
  240. }
  241. } else if ($type == 2) {
  242. //公众号
  243. $name = $user['uid'] . '_' . $user['is_promoter'] . '_user_wap.jpg';
  244. $imageInfo = SystemAttachment::getInfo($name, 'name');
  245. //检测远程文件是否存在
  246. if (isset($imageInfo['att_dir']) && strstr($imageInfo['att_dir'], 'http') !== false && curl_file_exist($imageInfo['att_dir']) === false) {
  247. $imageInfo = null;
  248. SystemAttachment::where(['name' => $name])->delete();
  249. }
  250. if (!$imageInfo) {
  251. // $qr_code = QrcodeService::getForeverQrcode('spread', $user['uid']);
  252. // if (isset($qr_code['url'])) {
  253. // $urlCode = $qr_code['url'];
  254. // } else {
  255. $codeUrl = set_http_type($siteUrl . '/register?spread=' . $user['uid'], 1);//二维码链接
  256. $imageInfo = UtilService::getQRCodePath($codeUrl, $name);
  257. if (is_string($imageInfo)) return app('json')->fail('二维码生成失败', ['error' => $imageInfo]);
  258. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  259. $urlCode = $imageInfo['dir'];
  260. // }
  261. } else $urlCode = $imageInfo['att_dir'];
  262. if ($imageInfo['image_type'] == 1) $urlCode = $siteUrl . $urlCode;
  263. $siteUrl = set_http_type($siteUrl, 1);
  264. $filelink = [
  265. 'Bold' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  266. 'Normal' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  267. ];
  268. if (!file_exists($filelink['Bold'])) return app('json')->fail('缺少字体文件Bold');
  269. if (!file_exists($filelink['Normal'])) return app('json')->fail('缺少字体文件Normal');
  270. foreach ($routineSpreadBanner as $key => &$item) {
  271. $posterInfo = '海报生成失败:(';
  272. $config = array(
  273. 'image' => array(
  274. array(
  275. 'url' => $urlCode, //二维码资源
  276. 'stream' => 0,
  277. 'left' => 225,
  278. 'top' => 913,
  279. 'right' => 0,
  280. 'bottom' => 0,
  281. 'width' => 300,
  282. 'height' => 300,
  283. 'opacity' => 100
  284. )
  285. ),
  286. 'text' => array(
  287. array(
  288. 'text' => $user['nickname'],
  289. 'left' => 250,
  290. 'top' => 840,
  291. 'fontPath' => $rootPath . 'public' . DS . $filelink['Bold'], //字体文件
  292. 'fontSize' => 16, //字号
  293. 'fontColor' => '40,40,40', //字体颜色
  294. 'angle' => 0,
  295. ),
  296. array(
  297. 'text' => '邀请您加入' . sys_config('site_name'),
  298. 'left' => 250,
  299. 'top' => 880,
  300. 'fontPath' => $rootPath . 'public' . DS . $filelink['Normal'], //字体文件
  301. 'fontSize' => 16, //字号
  302. 'fontColor' => '40,40,40', //字体颜色
  303. 'angle' => 0,
  304. )
  305. ),
  306. 'background' => $item['pic']
  307. );
  308. $resWap = $resWap && $posterInfo = UtilService::setSharePoster($config, 'wap/spread/poster');
  309. if (!is_array($posterInfo)) return app('json')->fail($posterInfo);
  310. SystemAttachment::attachmentAdd($posterInfo['name'], $posterInfo['size'], $posterInfo['type'], $posterInfo['dir'], $posterInfo['thumb_path'], 1, $posterInfo['image_type'], $posterInfo['time'], 2);
  311. if ($resWap) {
  312. if ($posterInfo['image_type'] == 1)
  313. $item['wap_poster'] = $siteUrl . $posterInfo['thumb_path'];
  314. else
  315. $item['wap_poster'] = UtilService::setHttpType($posterInfo['thumb_path'], 1);
  316. }
  317. }
  318. }
  319. if ($resRoutine && $resWap) return app('json')->successful($routineSpreadBanner);
  320. else return app('json')->fail('生成图片失败');
  321. } catch (\Exception $e) {
  322. return app('json')->fail('生成图片时,系统错误', ['line' => $e->getLine(), 'message' => $e->getMessage(), 'file' => $e->getFile()]);
  323. }
  324. }
  325. /**
  326. * 积分记录
  327. * @param Request $request
  328. * @return mixed
  329. * @throws \think\db\exception\DataNotFoundException
  330. * @throws \think\db\exception\ModelNotFoundException
  331. * @throws \think\exception\DbException
  332. */
  333. public function integral_list(Request $request)
  334. {
  335. list($page, $limit, $status) = UtilService::getMore([
  336. ['page', 0], ['limit', 0], ['pm', 0]
  337. ], $request, true);
  338. return app('json')->successful(UserBill::userBillList($request->uid(), $page, $limit, 'integral', ['pm' => $status]));
  339. }
  340. /**
  341. * 白积分记录
  342. * @param Request $request
  343. * @return mixed
  344. * @throws \think\db\exception\DataNotFoundException
  345. * @throws \think\db\exception\ModelNotFoundException
  346. * @throws \think\exception\DbException
  347. */
  348. public function white_integral(Request $request)
  349. {
  350. list($page, $limit, $status) = UtilService::getMore([
  351. ['page', 0], ['limit', 0], ['pm', 0]
  352. ], $request, true);
  353. return app('json')->successful(UserBill::userBillList($request->uid(), $page, $limit, 'white_integral', ['pm' => $status]));
  354. }
  355. /**
  356. * 紫积分记录
  357. * @param Request $request
  358. * @return mixed
  359. * @throws \think\db\exception\DataNotFoundException
  360. * @throws \think\db\exception\ModelNotFoundException
  361. * @throws \think\exception\DbException
  362. */
  363. public function purple_integral(Request $request)
  364. {
  365. list($page, $limit, $status) = UtilService::getMore([
  366. ['page', 0], ['limit', 0], ['pm', 0]
  367. ], $request, true);
  368. return app('json')->successful(UserBill::userBillList($request->uid(), $page, $limit, 'purple_integral', ['pm' => $status]));
  369. }
  370. /**
  371. * 绿积分记录
  372. * @param Request $request
  373. * @return mixed
  374. * @throws \think\db\exception\DataNotFoundException
  375. * @throws \think\db\exception\ModelNotFoundException
  376. * @throws \think\exception\DbException
  377. */
  378. public function green_integral(Request $request)
  379. {
  380. list($page, $limit, $status) = UtilService::getMore([
  381. ['page', 0], ['limit', 0], ['pm', 0]
  382. ], $request, true);
  383. return app('json')->successful(UserBill::userBillList($request->uid(), $page, $limit, 'green_integral', ['pm' => $status]));
  384. }
  385. /**
  386. * 商家积分记录
  387. * @param Request $request
  388. * @return mixed
  389. * @throws \think\db\exception\DataNotFoundException
  390. * @throws \think\db\exception\ModelNotFoundException
  391. * @throws \think\exception\DbException
  392. */
  393. public function business_integral(Request $request)
  394. {
  395. list($page, $limit, $status) = UtilService::getMore([
  396. ['page', 0], ['limit', 0], ['pm', 0]
  397. ], $request, true);
  398. return app('json')->successful(UserBill::userBillList($request->uid(), $page, $limit, 'business_integral', ['pm' => $status]));
  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 paper_ticket(Request $request)
  409. {
  410. list($page, $limit, $status) = UtilService::getMore([
  411. ['page', 0], ['limit', 0], ['pm', 0]
  412. ], $request, true);
  413. return app('json')->successful(UserBill::userBillList($request->uid(), $page, $limit, 'paper_ticket', ['pm' => $status]));
  414. }
  415. }