UserBillController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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\SystemConfigService;
  13. use crmeb\services\UtilService;
  14. use crmeb\services\upload\Upload;
  15. use think\facade\Log;
  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. $qiniu_path = 'user_share_qrcode';
  149. try {
  150. $resRoutine = true;//小程序
  151. $resWap = true;//公众号
  152. $siteUrl = sys_config('site_url');
  153. // 配置的海报背景模板
  154. $routineSpreadBanner = sys_data('routine_spread_banner');
  155. if (!count($routineSpreadBanner)) {
  156. return app('json')->fail('暂无海报');
  157. }
  158. if ($type == 1) {
  159. // 直接获取用户海报
  160. foreach ($routineSpreadBanner as &$item) {
  161. $item['namep'] = $user['uid'] . '_' . $user['is_promoter'] . '_' . $item['id'] . '_user_routine_poster.jpg';
  162. }
  163. $posters = SystemAttachment::whereIn('name', array_column($routineSpreadBanner, 'namep'))->field('name, att_dir')->select()->toArray();
  164. if (count($posters)) {
  165. foreach ($routineSpreadBanner as $key => &$item) {
  166. foreach ($posters as $poster) {
  167. if ($item['namep'] == $poster['name']) {
  168. $item['poster'] = $poster['att_dir'];
  169. }
  170. }
  171. unset($item['namep']);
  172. if (!isset($item['poster'])) {
  173. unset($routineSpreadBanner[$key]);
  174. }
  175. }
  176. $routineSpreadBanner = array_values($routineSpreadBanner);
  177. if (count($routineSpreadBanner) > 0) {
  178. return app('json')->successful($routineSpreadBanner);
  179. }
  180. }
  181. //小程序个人分享二维码文件名
  182. $name = $user['uid'] . '_' . $user['is_promoter'] . '_user_routine.jpg';
  183. $imageInfo = SystemAttachment::getInfo($name, 'name');
  184. //检测远程文件是否存在
  185. // 20210722 注释掉,太卡
  186. if (isset($imageInfo['att_dir']) &&
  187. strstr($imageInfo['att_dir'], 'http') !== false &&
  188. curl_file_exist($imageInfo['att_dir']) === false) {
  189. $imageInfo = null;
  190. SystemAttachment::where(['name' => $name])->delete();
  191. }
  192. if (!$imageInfo) {
  193. $res = RoutineCode::getShareCode($user['uid'], 'spread', '', '');
  194. if (!$res) return app('json')->fail('二维码生成失败');
  195. $uploadType = (int)sys_config('upload_type', 1);
  196. $upload = new Upload($uploadType, [
  197. 'accessKey' => sys_config('accessKey'),
  198. 'secretKey' => sys_config('secretKey'),
  199. 'uploadUrl' => sys_config('uploadUrl'),
  200. 'storageName' => sys_config('storage_name'),
  201. 'storageRegion' => sys_config('storage_region'),
  202. ]);
  203. // 上传分享二维码 [ qiniu 添加一个子路径]
  204. $remoteName = $uploadType == 2 ? $qiniu_path. '/' . $name : $name;
  205. $uploadRes = $upload->to('routine/spread/code')->validate()->stream($res['res'], $remoteName);
  206. if ($uploadRes === false) {
  207. return app('json')->fail($upload->getError());
  208. }
  209. $upload->delete($name);
  210. $imageInfo = $upload->getUploadInfo();
  211. // 20210919 为了能小程序下载,这个函数上传的图片全部使用 https
  212. $httpsUrl = set_http_type($imageInfo['dir']);
  213. $imageInfo['image_type'] = $uploadType;
  214. SystemAttachment::attachmentAdd(basename($imageInfo['name']), $imageInfo['size'], $imageInfo['type'], $httpsUrl, $httpsUrl, 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  215. RoutineQrcode::setRoutineQrcodeFind($res['id'], ['status' => 1, 'url_time' => time(), 'qrcode_url' => $imageInfo['dir']]);
  216. $urlCode = $imageInfo['dir'];
  217. } else {
  218. $urlCode = $imageInfo['att_dir'];
  219. }
  220. if ($imageInfo['image_type'] == 1) {
  221. $urlCode = $siteUrl . $urlCode;
  222. }
  223. $siteUrlHttps = set_http_type($siteUrl, $request->isSsl() ? 0 : 1);
  224. $filelink = [
  225. 'Bold' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  226. 'Normal' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  227. ];
  228. if (!file_exists($filelink['Bold'])) {
  229. return app('json')->fail('缺少字体文件Bold');
  230. }
  231. if (!file_exists($filelink['Normal'])) {
  232. return app('json')->fail('缺少字体文件Normal');
  233. }
  234. foreach ($routineSpreadBanner as $key => &$item) {
  235. $posterInfo = '海报生成失败';
  236. $config = array(
  237. 'image' => array(
  238. array(
  239. 'url' => $urlCode, //二维码资源
  240. 'stream' => 0,
  241. 'left' => 114,
  242. 'top' => 790,
  243. 'right' => 0,
  244. 'bottom' => 0,
  245. 'width' => 120,
  246. 'height' => 120,
  247. 'opacity' => 100
  248. )
  249. ),
  250. 'text' => array(
  251. array(
  252. 'text' => $user['nickname'],
  253. 'left' => 250,
  254. 'top' => 840,
  255. 'fontPath' => $rootPath . 'public' . DS . $filelink['Bold'], //字体文件
  256. 'fontSize' => 16, //字号
  257. 'fontColor' => '40,40,40', //字体颜色
  258. 'angle' => 0,
  259. ),
  260. array(
  261. 'text' => '邀请您加入' . sys_config('site_name'),
  262. 'left' => 250,
  263. 'top' => 880,
  264. 'fontPath' => $rootPath . 'public' . DS . $filelink['Normal'], //字体文件
  265. 'fontSize' => 16, //字号
  266. 'fontColor' => '40,40,40', //字体颜色
  267. 'angle' => 0,
  268. )
  269. ),
  270. 'background' => $item['pic'],
  271. 'name' => $qiniu_path. '/' . $user['uid'] . '_' . $user['is_promoter'] . '_' . $item['id'] . '_user_routine_poster.jpg',
  272. );
  273. $resRoutine = $resRoutine && $posterInfo = UtilService::setSharePoster($config, 'routine/spread/poster');
  274. if (!is_array($posterInfo)) {
  275. return app('json')->fail($posterInfo);
  276. }
  277. // 20210919 为了能小程序下载,这个函数上传的图片全部使用 https
  278. $httpsUrl = set_http_type($posterInfo['dir']);
  279. SystemAttachment::attachmentAdd(basename($posterInfo['name']), $posterInfo['size'], $posterInfo['type'], $httpsUrl, $httpsUrl, 1, $posterInfo['image_type'], $posterInfo['time'], 2);
  280. if ($resRoutine) {
  281. if ($posterInfo['image_type'] == 1)
  282. $item['poster'] = $siteUrlHttps . $posterInfo['dir'];
  283. else
  284. $item['poster'] = set_http_type($posterInfo['dir'], $request->isSsl() ? 0 : 1);
  285. $item['poster'] = str_replace('\\', '/', $item['poster']);
  286. }
  287. }
  288. } else if ($type == 2) {
  289. //公众号
  290. $name = $user['uid'] . '_' . $user['is_promoter'] . '_user_wap.jpg';
  291. $imageInfo = SystemAttachment::getInfo($name, 'name');
  292. //检测远程文件是否存在
  293. if (isset($imageInfo['att_dir']) && strstr($imageInfo['att_dir'], 'http') !== false && curl_file_exist($imageInfo['att_dir']) === false) {
  294. $imageInfo = null;
  295. SystemAttachment::where(['name' => $name])->delete();
  296. }
  297. if (!$imageInfo) {
  298. $codeUrl = set_http_type($siteUrl . '?spread=' . $user['uid'], $request->isSsl() ? 0 : 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. } else $urlCode = $imageInfo['att_dir'];
  304. if ($imageInfo['image_type'] == 1) $urlCode = $siteUrl . $urlCode;
  305. $siteUrl = set_http_type($siteUrl, $request->isSsl() ? 0 : 1);
  306. $filelink = [
  307. 'Bold' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  308. 'Normal' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  309. ];
  310. if (!file_exists($filelink['Bold'])) return app('json')->fail('缺少字体文件Bold');
  311. if (!file_exists($filelink['Normal'])) return app('json')->fail('缺少字体文件Normal');
  312. foreach ($routineSpreadBanner as $key => &$item) {
  313. $posterInfo = '海报生成失败:(';
  314. $config = array(
  315. 'image' => array(
  316. array(
  317. 'url' => $urlCode, //二维码资源
  318. 'stream' => 0,
  319. 'left' => 114,
  320. 'top' => 790,
  321. 'right' => 0,
  322. 'bottom' => 0,
  323. 'width' => 120,
  324. 'height' => 120,
  325. 'opacity' => 100
  326. )
  327. ),
  328. 'text' => array(
  329. array(
  330. 'text' => $user['nickname'],
  331. 'left' => 250,
  332. 'top' => 840,
  333. 'fontPath' => $rootPath . 'public' . DS . $filelink['Bold'], //字体文件
  334. 'fontSize' => 16, //字号
  335. 'fontColor' => '40,40,40', //字体颜色
  336. 'angle' => 0,
  337. ),
  338. array(
  339. 'text' => '邀请您加入' . sys_config('site_name'),
  340. 'left' => 250,
  341. 'top' => 880,
  342. 'fontPath' => $rootPath . 'public' . DS . $filelink['Normal'], //字体文件
  343. 'fontSize' => 16, //字号
  344. 'fontColor' => '40,40,40', //字体颜色
  345. 'angle' => 0,
  346. )
  347. ),
  348. 'background' => $item['pic']
  349. );
  350. $resWap = $resWap && $posterInfo = UtilService::setSharePoster($config, 'wap/spread/poster');
  351. if (!is_array($posterInfo)) return app('json')->fail($posterInfo);
  352. SystemAttachment::attachmentAdd($posterInfo['name'], $posterInfo['size'], $posterInfo['type'], $posterInfo['dir'], $posterInfo['thumb_path'], 1, $posterInfo['image_type'], $posterInfo['time'], 2);
  353. if ($resWap) {
  354. if ($posterInfo['image_type'] == 1)
  355. $item['wap_poster'] = $siteUrl . $posterInfo['thumb_path'];
  356. else
  357. $item['wap_poster'] = set_http_type($posterInfo['thumb_path'], 1);
  358. }
  359. }
  360. }
  361. if ($resRoutine && $resWap) return app('json')->successful($routineSpreadBanner);
  362. else return app('json')->fail('生成图片失败');
  363. } catch (\Exception $e) {
  364. Log::error('生成图片时,系统错误:' . $e->getMessage());
  365. return app('json')->fail('生成图片时,系统错误', ['line' => $e->getLine(), 'message' => $e->getMessage(), 'file' => $e->getFile()]);
  366. }
  367. }
  368. /**
  369. * 积分记录
  370. * @param Request $request
  371. * @return mixed
  372. * @throws \think\db\exception\DataNotFoundException
  373. * @throws \think\db\exception\ModelNotFoundException
  374. * @throws \think\exception\DbException
  375. */
  376. public function integral_list(Request $request)
  377. {
  378. list($page, $limit) = UtilService::getMore([
  379. [['page', 'd'], 0], [['limit', 'd'], 0]
  380. ], $request, true);
  381. return app('json')->successful(UserBill::userBillList($request->uid(), $page, $limit));
  382. }
  383. }