UserBillController.php 22 KB

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