UserBillController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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\UtilService;
  12. use crmeb\services\upload\Upload;
  13. /**
  14. * 账单类
  15. * Class UserBillController
  16. * @package app\api\controller\user
  17. */
  18. class UserBillController
  19. {
  20. /**
  21. * @api {get} /commission 推广数据 昨天的佣金 累计提现金额 当前佣金
  22. * @apiName GetCommission
  23. * @apiGroup User.Bill
  24. *
  25. */
  26. public function commission(Request $request)
  27. {
  28. $uid = $request->uid();
  29. $lastDayCount = UserBill::yesterdayCommissionSum($uid); //昨天的佣金
  30. $extractCount = UserExtract::extractSum($uid); //累计提现金额
  31. $commissionCount = UserBill::getBrokerage($uid); //获取总佣金
  32. if ($commissionCount > 0) {
  33. $rechargeCount = UserBill::getRecharge($uid); //累计充值
  34. $orderYuePrice = StoreOrder::getOrderStatusYueSum($uid); //余额累计消费
  35. $systemAdd = UserBill::getSystemAdd($uid); //后台添加余额
  36. $yueCount = bcadd($rechargeCount, $systemAdd, 2); // 后台添加余额 + 累计充值 = 非佣金的总金额
  37. $orderYuePrice = $yueCount > $orderYuePrice ? 0 : bcsub($orderYuePrice, $yueCount, 2); // 余额累计消费(使用佣金消费的金额)
  38. $commissionCount = bcsub($commissionCount, $extractCount, 2); //减去已提现金额
  39. $extractPriceCount = UserExtract::userExtractTotalPrice($uid, 0);
  40. $commissionCount = $extractPriceCount < $commissionCount ? bcsub($commissionCount, $extractPriceCount, 2) : 0; //减去审核中的提现金额
  41. $commissionCount = $commissionCount > $orderYuePrice ? bcsub($commissionCount, $orderYuePrice, 2) : 0; //减掉余额支付
  42. }
  43. $data['lastDayCount'] = $lastDayCount;
  44. $data['extractCount'] = $extractCount;
  45. $data['commissionCount'] = $commissionCount;
  46. return app('json')->successful($data);
  47. }
  48. /**
  49. * @api {post} /spread/people 推荐用户
  50. * @apiName PostSpreadPeople
  51. * @apiGroup User.Bill
  52. *
  53. */
  54. public function spread_people(Request $request)
  55. {
  56. /*
  57. * grade == 0 获取一级推荐人
  58. * grade == 1 获取二级推荐人
  59. *
  60. * keyword 会员名称查询
  61. *
  62. * sort childCount ASC/DESC 团队排序 numberCount ASC/DESC 金额排序 orderCount ASC/DESC 订单排序
  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. * @api {post} /spread/order 推广订单
  79. * @apiName PostSpreadOrder
  80. * @apiGroup User.Bill
  81. *
  82. */
  83. public function spread_order(Request $request)
  84. {
  85. $orderInfo = UtilService::postMore([
  86. ['page', 1],
  87. ['limit', 20],
  88. ['category', 'now_money'],
  89. ['type', 'brokerage'],
  90. ], $request);
  91. $data['list'] = [];
  92. $data['count'] = 0;
  93. $uid = $request->uid();
  94. $data['list'] = UserBill::getRecordList($uid, $orderInfo['page'], $orderInfo['limit'], $orderInfo['category'], $orderInfo['type']);
  95. $count = UserBill::getRecordOrderCount($uid, $orderInfo['category'], $orderInfo['type']);
  96. $data['count'] = $count ? $count : 0;
  97. if (!count($data['list'])) return app('json')->successful($data);
  98. foreach ($data['list'] as $key => &$value) {
  99. $value['child'] = UserBill::getRecordOrderListDraw($uid, $value['time'], $orderInfo['category'], $orderInfo['type']);
  100. $value['count'] = count($value['child']);
  101. }
  102. return app('json')->successful($data);
  103. }
  104. /**
  105. * @api {get} /spread/commission/:type 推广佣金明细
  106. * @apiName GetSpreadCommission
  107. * @apiGroup User.Bill
  108. *
  109. */
  110. public function spread_commission(Request $request, $type)
  111. {
  112. // $type 0 全部 1 消费 2 充值 3 返佣 4 提现
  113. list($page, $limit) = UtilService::getMore([
  114. ['page', 0],
  115. ['limit', 0],
  116. ], $request, true);
  117. return app('json')->successful(UserBill::getUserBillList($request->uid(), $page, $limit, $type));
  118. }
  119. /**
  120. * @api {get} /spread/count/:type 推广 佣金/提现 总和
  121. * @apiName GetSpreadCount
  122. * @apiGroup User.Bill
  123. *
  124. */
  125. public function spread_count(Request $request, $type)
  126. {
  127. // $type 3 佣金 4 提现
  128. $count = 0;
  129. if ($type == BILL_BROKERAGE) {
  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 == BILL_EXTRACT) {
  134. $count = UserExtract::userExtractTotalPrice($request->uid()); //累计提现
  135. }
  136. $count = $count ? $count : 0;
  137. return app('json')->successful(['count' => $count]);
  138. }
  139. /**
  140. * @api {get} /spread/banner 分销二维码海报生成
  141. * @apiName GetSpreadBanner
  142. * @apiGroup User.Bill
  143. *
  144. */
  145. public function spread_banner(Request $request)
  146. {
  147. list($type) = UtilService::getMore([
  148. ['type', 2],
  149. ], $request, true);
  150. $user = $request->user();
  151. $rootPath = app()->getRootPath();
  152. $qiniu_path = 'user_share_qrcode';
  153. try {
  154. $resRoutine = true; //小程序
  155. $resWap = true; //公众号
  156. $siteUrl = sys_config('site_url');
  157. // 配置的海报背景模板
  158. $routineSpreadBanner = sys_data('routine_spread_banner');
  159. if (!count($routineSpreadBanner)) {
  160. return app('json')->fail('暂无海报');
  161. }
  162. if ($type == 1) {
  163. // 直接获取用户海报
  164. foreach ($routineSpreadBanner as &$item) {
  165. $item['namep'] = $user['uid'] . '_' . $user['is_promoter'] . '_' . $item['id'] . '_user_routine_poster.jpg';
  166. }
  167. $posters = SystemAttachment::whereIn('name', array_column($routineSpreadBanner, 'namep'))->field('name, att_dir')->select()->toArray();
  168. if (count($posters)) {
  169. foreach ($routineSpreadBanner as $key => &$item) {
  170. foreach ($posters as $poster) {
  171. if ($item['namep'] == $poster['name']) {
  172. $item['poster'] = $poster['att_dir'];
  173. }
  174. }
  175. unset($item['namep']);
  176. if (!isset($item['poster'])) {
  177. unset($routineSpreadBanner[$key]);
  178. }
  179. }
  180. $routineSpreadBanner = array_values($routineSpreadBanner);
  181. if (count($routineSpreadBanner) > 0) {
  182. return app('json')->successful($routineSpreadBanner);
  183. }
  184. }
  185. //小程序个人分享二维码文件名
  186. $name = $user['uid'] . '_' . $user['is_promoter'] . '_user_routine.jpg';
  187. $imageInfo = SystemAttachment::getInfo($name, 'name');
  188. //检测远程文件是否存在
  189. // 20210722 注释掉,太卡
  190. if (
  191. isset($imageInfo['att_dir']) &&
  192. strstr($imageInfo['att_dir'], 'http') !== false &&
  193. curl_file_exist($imageInfo['att_dir']) === false
  194. ) {
  195. $imageInfo = null;
  196. SystemAttachment::where(['name' => $name])->delete();
  197. }
  198. if (!$imageInfo) {
  199. $res = RoutineCode::getShareCode($user['uid'], 'spread', '', '');
  200. if (!$res) return app('json')->fail('二维码生成失败');
  201. $uploadType = (int)sys_config('upload_type', 1);
  202. $upload = new Upload($uploadType, [
  203. 'accessKey' => sys_config('accessKey'),
  204. 'secretKey' => sys_config('secretKey'),
  205. 'uploadUrl' => sys_config('uploadUrl'),
  206. 'storageName' => sys_config('storage_name'),
  207. 'storageRegion' => sys_config('storage_region'),
  208. ]);
  209. // 上传分享二维码 [ qiniu 添加一个子路径]
  210. $remoteName = $uploadType == 2 ? $qiniu_path . '/' . $name : $name;
  211. $uploadRes = $upload->to('routine/spread/code')->validate()->stream($res['res'], $remoteName);
  212. if ($uploadRes === false) {
  213. return app('json')->fail($upload->getError());
  214. }
  215. $upload->delete($name);
  216. $imageInfo = $upload->getUploadInfo();
  217. // 20210919 为了能小程序下载,这个函数上传的图片全部使用 https
  218. $httpsUrl = set_http_type($imageInfo['dir']);
  219. $imageInfo['image_type'] = $uploadType;
  220. SystemAttachment::attachmentAdd(basename($imageInfo['name']), $imageInfo['size'], $imageInfo['type'], $httpsUrl, $httpsUrl, 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  221. RoutineQrcode::setRoutineQrcodeFind($res['id'], ['status' => 1, 'url_time' => time(), 'qrcode_url' => $imageInfo['dir']]);
  222. $urlCode = $imageInfo['dir'];
  223. } else {
  224. $urlCode = $imageInfo['att_dir'];
  225. }
  226. if ($imageInfo['image_type'] == 1) {
  227. $urlCode = $siteUrl . $urlCode;
  228. }
  229. $siteUrlHttps = set_http_type($siteUrl, $request->isSsl() ? 0 : 1);
  230. $filelink = [
  231. 'Bold' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  232. 'Normal' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  233. ];
  234. if (!file_exists($filelink['Bold'])) {
  235. return app('json')->fail('缺少字体文件Bold');
  236. }
  237. if (!file_exists($filelink['Normal'])) {
  238. return app('json')->fail('缺少字体文件Normal');
  239. }
  240. foreach ($routineSpreadBanner as $key => &$item) {
  241. $posterInfo = '海报生成失败';
  242. $config = array(
  243. 'image' => array(
  244. array(
  245. 'url' => $urlCode, //二维码资源
  246. 'stream' => 0,
  247. 'left' => 114,
  248. 'top' => 790,
  249. 'right' => 0,
  250. 'bottom' => 0,
  251. 'width' => 120,
  252. 'height' => 120,
  253. 'opacity' => 100
  254. )
  255. ),
  256. 'text' => array(
  257. array(
  258. 'text' => $user['nickname'],
  259. 'left' => 250,
  260. 'top' => 840,
  261. 'fontPath' => $rootPath . 'public' . DS . $filelink['Bold'], //字体文件
  262. 'fontSize' => 16, //字号
  263. 'fontColor' => '40,40,40', //字体颜色
  264. 'angle' => 0,
  265. ),
  266. array(
  267. 'text' => '邀请您加入' . sys_config('site_name'),
  268. 'left' => 250,
  269. 'top' => 880,
  270. 'fontPath' => $rootPath . 'public' . DS . $filelink['Normal'], //字体文件
  271. 'fontSize' => 16, //字号
  272. 'fontColor' => '40,40,40', //字体颜色
  273. 'angle' => 0,
  274. )
  275. ),
  276. 'background' => $item['pic'],
  277. 'name' => $qiniu_path . '/' . $user['uid'] . '_' . $user['is_promoter'] . '_' . $item['id'] . '_user_routine_poster.jpg',
  278. );
  279. $resRoutine = $resRoutine && $posterInfo = UtilService::setSharePoster($config, 'routine/spread/poster');
  280. if (!is_array($posterInfo)) {
  281. return app('json')->fail($posterInfo);
  282. }
  283. // 20210919 为了能小程序下载,这个函数上传的图片全部使用 https
  284. $httpsUrl = set_http_type($posterInfo['dir']);
  285. SystemAttachment::attachmentAdd(basename($posterInfo['name']), $posterInfo['size'], $posterInfo['type'], $httpsUrl, $httpsUrl, 1, $posterInfo['image_type'], $posterInfo['time'], 2);
  286. if ($resRoutine) {
  287. if ($posterInfo['image_type'] == 1)
  288. $item['poster'] = $siteUrlHttps . $posterInfo['dir'];
  289. else
  290. $item['poster'] = set_http_type($posterInfo['dir'], $request->isSsl() ? 0 : 1);
  291. $item['poster'] = str_replace('\\', '/', $item['poster']);
  292. }
  293. }
  294. } else if ($type == 2) {
  295. //公众号
  296. $name = $user['uid'] . '_' . $user['is_promoter'] . '_user_wap.jpg';
  297. $imageInfo = SystemAttachment::getInfo($name, 'name');
  298. //检测远程文件是否存在
  299. if (isset($imageInfo['att_dir']) && strstr($imageInfo['att_dir'], 'http') !== false && curl_file_exist($imageInfo['att_dir']) === false) {
  300. $imageInfo = null;
  301. SystemAttachment::where(['name' => $name])->delete();
  302. }
  303. if (!$imageInfo) {
  304. $codeUrl = set_http_type($siteUrl . '?spread=' . $user['uid'], $request->isSsl() ? 0 : 1); //二维码链接
  305. $imageInfo = UtilService::getQRCodePath($codeUrl, $name);
  306. if (is_string($imageInfo)) return app('json')->fail('二维码生成失败', ['error' => $imageInfo]);
  307. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  308. $urlCode = $imageInfo['dir'];
  309. } else $urlCode = $imageInfo['att_dir'];
  310. if ($imageInfo['image_type'] == 1) $urlCode = $siteUrl . $urlCode;
  311. $siteUrl = set_http_type($siteUrl, $request->isSsl() ? 0 : 1);
  312. $filelink = [
  313. 'Bold' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  314. 'Normal' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  315. ];
  316. if (!file_exists($filelink['Bold'])) return app('json')->fail('缺少字体文件Bold');
  317. if (!file_exists($filelink['Normal'])) return app('json')->fail('缺少字体文件Normal');
  318. foreach ($routineSpreadBanner as $key => &$item) {
  319. $posterInfo = '海报生成失败:(';
  320. $config = array(
  321. 'image' => array(
  322. array(
  323. 'url' => $urlCode, //二维码资源
  324. 'stream' => 0,
  325. 'left' => 114,
  326. 'top' => 790,
  327. 'right' => 0,
  328. 'bottom' => 0,
  329. 'width' => 120,
  330. 'height' => 120,
  331. 'opacity' => 100
  332. )
  333. ),
  334. 'text' => array(
  335. array(
  336. 'text' => $user['nickname'],
  337. 'left' => 250,
  338. 'top' => 840,
  339. 'fontPath' => $rootPath . 'public' . DS . $filelink['Bold'], //字体文件
  340. 'fontSize' => 16, //字号
  341. 'fontColor' => '40,40,40', //字体颜色
  342. 'angle' => 0,
  343. ),
  344. array(
  345. 'text' => '邀请您加入' . sys_config('site_name'),
  346. 'left' => 250,
  347. 'top' => 880,
  348. 'fontPath' => $rootPath . 'public' . DS . $filelink['Normal'], //字体文件
  349. 'fontSize' => 16, //字号
  350. 'fontColor' => '40,40,40', //字体颜色
  351. 'angle' => 0,
  352. )
  353. ),
  354. 'background' => $item['pic']
  355. );
  356. $resWap = $resWap && $posterInfo = UtilService::setSharePoster($config, 'wap/spread/poster');
  357. if (!is_array($posterInfo)) return app('json')->fail($posterInfo);
  358. SystemAttachment::attachmentAdd($posterInfo['name'], $posterInfo['size'], $posterInfo['type'], $posterInfo['dir'], $posterInfo['thumb_path'], 1, $posterInfo['image_type'], $posterInfo['time'], 2);
  359. if ($resWap) {
  360. if ($posterInfo['image_type'] == 1)
  361. $item['wap_poster'] = $siteUrl . $posterInfo['thumb_path'];
  362. else
  363. $item['wap_poster'] = set_http_type($posterInfo['thumb_path'], 1);
  364. }
  365. }
  366. }
  367. if ($resRoutine && $resWap) return app('json')->successful($routineSpreadBanner);
  368. else return app('json')->fail('生成图片失败');
  369. } catch (\Exception $e) {
  370. errlog('生成图片时,系统错误:' . $e->getMessage());
  371. return app('json')->fail('生成图片时,系统错误', ['line' => $e->getLine(), 'message' => $e->getMessage(), 'file' => $e->getFile()]);
  372. }
  373. }
  374. /**
  375. * @api {get} /integral/list 积分记录
  376. * @apiName GetIntegralList
  377. * @apiGroup User.Bill
  378. *
  379. */
  380. public function integral_list(Request $request)
  381. {
  382. list($page, $limit) = UtilService::getMore([
  383. [['page', 'd'], 0], [['limit', 'd'], 0]
  384. ], $request, true);
  385. return app('json')->successful(UserBill::userBillList($request->uid(), $page, $limit));
  386. }
  387. }