UserBillController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. $user = User::select();
  75. $uid = get_downlines($user, $uid);
  76. $data['totalLevel'] = User::where('uid', 'in', $uid)->count();
  77. $data['order_count'] = StoreOrder::where('uid', 'in', $uid)->where('paid', 1)->sum('pay_price');// 团队总业界
  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', 'now_money'],
  91. ['type', 'brokerage'],
  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. foreach ($routineSpreadBanner as $key => &$item) {
  196. $posterInfo = '海报生成失败:(';
  197. $config = array(
  198. 'image' => array(
  199. array(
  200. 'url' => $urlCode, //二维码资源
  201. 'stream' => 0,
  202. 'left' => 225,
  203. 'top' => 913,
  204. 'right' => 0,
  205. 'bottom' => 0,
  206. 'width' => 300,
  207. 'height' => 300,
  208. 'opacity' => 100
  209. )
  210. ),
  211. 'text' => array(
  212. array(
  213. 'text' => $user['nickname'],
  214. 'left' => 250,
  215. 'top' => 840,
  216. 'fontPath' => $rootPath . 'public' . DS . $filelink['Bold'], //字体文件
  217. 'fontSize' => 16, //字号
  218. 'fontColor' => '40,40,40', //字体颜色
  219. 'angle' => 0,
  220. ),
  221. array(
  222. 'text' => '邀请您加入' . sys_config('site_name'),
  223. 'left' => 250,
  224. 'top' => 880,
  225. 'fontPath' => $rootPath . 'public' . DS . $filelink['Normal'], //字体文件
  226. 'fontSize' => 16, //字号
  227. 'fontColor' => '40,40,40', //字体颜色
  228. 'angle' => 0,
  229. )
  230. ),
  231. 'background' => $item['pic']
  232. );
  233. $resRoutine = $resRoutine && $posterInfo = UtilService::setSharePoster($config, 'routine/spread/poster');
  234. if (!is_array($posterInfo)) return app('json')->fail($posterInfo);
  235. SystemAttachment::attachmentAdd($posterInfo['name'], $posterInfo['size'], $posterInfo['type'], $posterInfo['dir'], $posterInfo['thumb_path'], 1, $posterInfo['image_type'], $posterInfo['time'], 2);
  236. if ($resRoutine) {
  237. if ($posterInfo['image_type'] == 1)
  238. $item['poster'] = $siteUrlHttps . $posterInfo['dir'];
  239. else
  240. $item['poster'] = set_http_type($posterInfo['dir'], 0);
  241. $item['poster'] = str_replace('\\', '/', $item['poster']);
  242. }
  243. }
  244. } else if ($type == 2) {
  245. //公众号
  246. $name = $user['uid'] . '_' . $user['is_promoter'] . '_user_wap.jpg';
  247. $imageInfo = SystemAttachment::getInfo($name, 'name');
  248. //检测远程文件是否存在
  249. if (isset($imageInfo['att_dir']) && strstr($imageInfo['att_dir'], 'http') !== false && curl_file_exist($imageInfo['att_dir']) === false) {
  250. $imageInfo = null;
  251. SystemAttachment::where(['name' => $name])->delete();
  252. }
  253. if (!$imageInfo) {
  254. // $qr_code = QrcodeService::getForeverQrcode('spread', $user['uid']);
  255. // if (isset($qr_code['url'])) {
  256. // $urlCode = $qr_code['url'];
  257. // } else {
  258. $codeUrl = set_http_type($siteUrl . '/register?spread=' . $user['uid'], 1);//二维码链接
  259. $imageInfo = UtilService::getQRCodePath($codeUrl, $name);
  260. if (is_string($imageInfo)) return app('json')->fail('二维码生成失败', ['error' => $imageInfo]);
  261. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  262. $urlCode = $imageInfo['dir'];
  263. // }
  264. } else $urlCode = $imageInfo['att_dir'];
  265. if ($imageInfo['image_type'] == 1) $urlCode = $siteUrl . $urlCode;
  266. $siteUrl = set_http_type($siteUrl, 1);
  267. $filelink = [
  268. 'Bold' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  269. 'Normal' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  270. ];
  271. if (!file_exists($filelink['Bold'])) return app('json')->fail('缺少字体文件Bold');
  272. if (!file_exists($filelink['Normal'])) return app('json')->fail('缺少字体文件Normal');
  273. foreach ($routineSpreadBanner as $key => &$item) {
  274. $posterInfo = '海报生成失败:(';
  275. $config = array(
  276. 'image' => array(
  277. array(
  278. 'url' => $urlCode, //二维码资源
  279. 'stream' => 0,
  280. 'left' => 225,
  281. 'top' => 913,
  282. 'right' => 0,
  283. 'bottom' => 0,
  284. 'width' => 300,
  285. 'height' => 300,
  286. 'opacity' => 100
  287. )
  288. ),
  289. 'text' => array(
  290. array(
  291. 'text' => $user['nickname'],
  292. 'left' => 250,
  293. 'top' => 840,
  294. 'fontPath' => $rootPath . 'public' . DS . $filelink['Bold'], //字体文件
  295. 'fontSize' => 16, //字号
  296. 'fontColor' => '40,40,40', //字体颜色
  297. 'angle' => 0,
  298. ),
  299. array(
  300. 'text' => '邀请您加入' . sys_config('site_name'),
  301. 'left' => 250,
  302. 'top' => 880,
  303. 'fontPath' => $rootPath . 'public' . DS . $filelink['Normal'], //字体文件
  304. 'fontSize' => 16, //字号
  305. 'fontColor' => '40,40,40', //字体颜色
  306. 'angle' => 0,
  307. )
  308. ),
  309. 'background' => $item['pic']
  310. );
  311. $resWap = $resWap && $posterInfo = UtilService::setSharePoster($config, 'wap/spread/poster');
  312. if (!is_array($posterInfo)) return app('json')->fail($posterInfo);
  313. SystemAttachment::attachmentAdd($posterInfo['name'], $posterInfo['size'], $posterInfo['type'], $posterInfo['dir'], $posterInfo['thumb_path'], 1, $posterInfo['image_type'], $posterInfo['time'], 2);
  314. if ($resWap) {
  315. if ($posterInfo['image_type'] == 1)
  316. $item['wap_poster'] = $siteUrl . $posterInfo['thumb_path'];
  317. else
  318. $item['wap_poster'] = UtilService::setHttpType($posterInfo['thumb_path'], 1);
  319. }
  320. }
  321. }
  322. if ($resRoutine && $resWap) return app('json')->successful($routineSpreadBanner);
  323. else return app('json')->fail('生成图片失败');
  324. } catch (\Exception $e) {
  325. return app('json')->fail('生成图片时,系统错误', ['line' => $e->getLine(), 'message' => $e->getMessage(), 'file' => $e->getFile()]);
  326. }
  327. }
  328. /**
  329. * 积分记录
  330. * @param Request $request
  331. * @return mixed
  332. * @throws \think\db\exception\DataNotFoundException
  333. * @throws \think\db\exception\ModelNotFoundException
  334. * @throws \think\exception\DbException
  335. */
  336. public function integral_list(Request $request)
  337. {
  338. list($page, $limit, $status) = UtilService::getMore([
  339. ['page', 0], ['limit', 0], ['pm', 0]
  340. ], $request, true);
  341. return app('json')->successful(UserBill::userBillList($request->uid(), $page, $limit, 'integral', ['pm' => $status]));
  342. }
  343. /**
  344. * 动态积分记录
  345. * @param Request $request
  346. * @return mixed
  347. * @throws \think\db\exception\DataNotFoundException
  348. * @throws \think\db\exception\ModelNotFoundException
  349. * @throws \think\exception\DbException
  350. */
  351. public function dynamic_integral_list(Request $request)
  352. {
  353. list($page, $limit, $status) = UtilService::getMore([
  354. ['page', 0], ['limit', 0], ['pm', 0]
  355. ], $request, true);
  356. return app('json')->successful(UserBill::userBillList($request->uid(), $page, $limit, 'dynamic_integral', ['pm' => $status]));
  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 consumption_list(Request $request)
  367. {
  368. list($page, $limit, $status) = UtilService::getMore([
  369. ['page', 0], ['limit', 0], ['pm', 0]
  370. ], $request, true);
  371. return app('json')->successful(UserBill::userBillList($request->uid(), $page, $limit, 'consumption', ['pm' => $status]));
  372. }
  373. /**
  374. * 消费券记录
  375. * @param Request $request
  376. * @return mixed
  377. * @throws \think\db\exception\DataNotFoundException
  378. * @throws \think\db\exception\ModelNotFoundException
  379. * @throws \think\exception\DbException
  380. */
  381. public function contribution_list(Request $request)
  382. {
  383. list($page, $limit, $status) = UtilService::getMore([
  384. ['page', 0], ['limit', 0], ['pm', 0]
  385. ], $request, true);
  386. return app('json')->successful(UserBill::userBillList($request->uid(), $page, $limit, 'contribution', ['pm' => $status]));
  387. }
  388. }