UserBillController.php 20 KB

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