UserBillController.php 19 KB

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