UserBillController.php 20 KB

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