UserBillController.php 18 KB

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