UserBillController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. /**
  16. * 账单类
  17. * Class UserBillController
  18. * @package app\api\controller\user
  19. */
  20. class UserBillController
  21. {
  22. /**
  23. * 推广数据 昨天的佣金 累计提现金额 当前佣金
  24. * @param Request $request
  25. * @return mixed
  26. */
  27. public function commission(Request $request)
  28. {
  29. $uid = $request->uid();
  30. $lastDayCount = UserBill::yesterdayCommissionSum($uid);//昨天的佣金
  31. $extractCount = UserExtract::extractSum($uid);//累计提现金额
  32. $commissionCount = UserBill::getBrokerage($uid);//获取总佣金
  33. if ($commissionCount > 0) {
  34. $rechargeCount = UserBill::getRecharge($uid);//累计充值
  35. $orderYuePrice = StoreOrder::getOrderStatusYueSum($uid);//余额累计消费
  36. $systemAdd = UserBill::getSystemAdd($uid);//后台添加余额
  37. $yueCount = bcadd($rechargeCount, $systemAdd, 2);// 后台添加余额 + 累计充值 = 非佣金的总金额
  38. $orderYuePrice = $yueCount > $orderYuePrice ? 0 : bcsub($orderYuePrice, $yueCount, 2);// 余额累计消费(使用佣金消费的金额)
  39. $commissionCount = bcsub($commissionCount, $extractCount, 2);//减去已提现金额
  40. $extractPriceCount = UserExtract::userExtractTotalPrice($uid, 0);
  41. $commissionCount = $extractPriceCount < $commissionCount ? bcsub($commissionCount, $extractPriceCount, 2) : 0;//减去审核中的提现金额
  42. $commissionCount = $commissionCount > $orderYuePrice ? bcsub($commissionCount, $orderYuePrice, 2) : 0;//减掉余额支付
  43. }
  44. $data['lastDayCount'] = $lastDayCount;
  45. $data['extractCount'] = $extractCount;
  46. // $data['commissionCount'] = $commissionCount;
  47. $user = $request->user();
  48. $data['commissionCount'] = $user['brokerage_price'];
  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'], 'u.uid desc', $spreadInfo['keyword'], $spreadInfo['page'], $spreadInfo['limit']);
  74. $data['total'] = User::getSpreadCount($uid);
  75. $data['totalLevel'] = User::getSpreadLevelCount($uid);
  76. return app('json')->successful($data);
  77. }
  78. /**
  79. * 推广订单
  80. * @param Request $request
  81. * @return mixed
  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. * 推广佣金明细
  106. * @param Request $request
  107. * @param $type 0 全部 1 消费 2 充值 3 返佣 4 提现
  108. * @return mixed
  109. */
  110. public function spread_commission(Request $request, $type)
  111. {
  112. list($page, $limit) = UtilService::getMore([
  113. ['page', 0],
  114. ['limit', 0],
  115. ], $request, true);
  116. return app('json')->successful(UserBill::getUserBillList($request->uid(), $page, $limit, $type));
  117. }
  118. /**
  119. * 推广 佣金/提现 总和
  120. * @param Request $request
  121. * @param $type 3 佣金 4 提现
  122. * @return mixed
  123. */
  124. public function spread_count(Request $request, $type)
  125. {
  126. $count = 0;
  127. if ($type == 3) $count = UserBill::getRecordCount($request->uid(), 'now_money', 'brokerage');
  128. else if ($type == 4) $count = UserExtract::userExtractTotalPrice($request->uid());//累计提现
  129. $count = $count ? $count : 0;
  130. return app('json')->successful(['count' => $count]);
  131. }
  132. /**
  133. * 分销二维码海报生成
  134. * @param Request $request
  135. * @return mixed
  136. */
  137. public function spread_banner(Request $request)
  138. {
  139. list($type) = UtilService::getMore([
  140. ['type', 2],
  141. ], $request, true);
  142. $user = $request->user();
  143. $rootPath = app()->getRootPath();
  144. try {
  145. $resRoutine = true;//小程序
  146. $resWap = true;//公众号
  147. $siteUrl = sys_config('site_url');
  148. $routineSpreadBanner = sys_data('routine_spread_banner');
  149. if (!count($routineSpreadBanner)) return app('json')->fail('暂无海报');
  150. if ($type == 1) {
  151. //小程序
  152. $name = $user['uid'] . '_' . $user['is_promoter'] . '_user_routine.jpg';
  153. $imageInfo = SystemAttachment::getInfo($name, 'name');
  154. //检测远程文件是否存在
  155. if (isset($imageInfo['att_dir']) && strstr($imageInfo['att_dir'], 'http') !== false && UtilService::CurlFileExist($imageInfo['att_dir']) === false) {
  156. $imageInfo = null;
  157. SystemAttachment::where(['name' => $name])->delete();
  158. }
  159. if (!$imageInfo) {
  160. $res = RoutineCode::getShareCode($user['uid'], 'spread', '', '');
  161. if (!$res) return app('json')->fail('二维码生成失败');
  162. $uploadType = (int)sys_config('upload_type', 1);
  163. $upload = new Upload($uploadType, [
  164. 'accessKey' => sys_config('accessKey'),
  165. 'secretKey' => sys_config('secretKey'),
  166. 'uploadUrl' => sys_config('uploadUrl'),
  167. 'storageName' => sys_config('storage_name'),
  168. 'storageRegion' => sys_config('storage_region'),
  169. ]);
  170. $res2 = $upload->to('routine/spread/code')->validate()->stream($res['res'], $name);
  171. if ($res2 === false) {
  172. return app('json')->fail($upload->getError());
  173. }
  174. $imageInfo = $upload->getUploadInfo();
  175. $imageInfo['image_type'] = $uploadType;
  176. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  177. RoutineQrcode::setRoutineQrcodeFind($res['id'], ['status' => 1, 'time' => time(), 'qrcode_url' => $imageInfo['dir']]);
  178. $urlCode = $imageInfo['dir'];
  179. } else $urlCode = $imageInfo['att_dir'];
  180. if ($imageInfo['image_type'] == 1) $urlCode = $siteUrl . $urlCode;
  181. $siteUrlHttps = set_http_type($siteUrl, 0);
  182. $filelink = [
  183. 'Bold' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  184. 'Normal' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  185. ];
  186. if (!file_exists($filelink['Bold'])) return app('json')->fail('缺少字体文件Bold');
  187. if (!file_exists($filelink['Normal'])) return app('json')->fail('缺少字体文件Normal');
  188. foreach ($routineSpreadBanner as $key => &$item) {
  189. $posterInfo = '海报生成失败:(';
  190. $config = array(
  191. 'image' => array(
  192. array(
  193. 'url' => $urlCode, //二维码资源
  194. 'stream' => 0,
  195. 'left' => 114,
  196. 'top' => 790,
  197. 'right' => 0,
  198. 'bottom' => 0,
  199. 'width' => 120,
  200. 'height' => 120,
  201. 'opacity' => 100
  202. )
  203. ),
  204. 'text' => array(
  205. array(
  206. 'text' => $user['nickname'],
  207. 'left' => 250,
  208. 'top' => 840,
  209. 'fontPath' => $rootPath . 'public' . DS . $filelink['Bold'], //字体文件
  210. 'fontSize' => 16, //字号
  211. 'fontColor' => '40,40,40', //字体颜色
  212. 'angle' => 0,
  213. ),
  214. array(
  215. 'text' => '邀请您加入' . sys_config('site_name'),
  216. 'left' => 250,
  217. 'top' => 880,
  218. 'fontPath' => $rootPath . 'public' . DS . $filelink['Normal'], //字体文件
  219. 'fontSize' => 16, //字号
  220. 'fontColor' => '40,40,40', //字体颜色
  221. 'angle' => 0,
  222. )
  223. ),
  224. 'background' => $item['pic']
  225. );
  226. $resRoutine = $resRoutine && $posterInfo = UtilService::setSharePoster($config, 'routine/spread/poster');
  227. if (!is_array($posterInfo)) return app('json')->fail($posterInfo);
  228. SystemAttachment::attachmentAdd($posterInfo['name'], $posterInfo['size'], $posterInfo['type'], $posterInfo['dir'], $posterInfo['thumb_path'], 1, $posterInfo['image_type'], $posterInfo['time'], 2);
  229. if ($resRoutine) {
  230. if ($posterInfo['image_type'] == 1)
  231. $item['poster'] = $siteUrlHttps . $posterInfo['dir'];
  232. else {
  233. $item['poster'] = set_http_type($posterInfo['dir'], 0);
  234. }
  235. $item['poster'] = str_replace('\\', '/', $item['poster']);
  236. }
  237. $urlCode = set_http_type($urlCode, 0);//小程序推广海报
  238. $item['qrcode'] = $urlCode;
  239. }
  240. } else if ($type == 2) {
  241. //公众号
  242. $name = $user['uid'] . '_' . $user['is_promoter'] . '_user_wap.jpg';
  243. $imageInfo = SystemAttachment::getInfo($name, 'name');
  244. //检测远程文件是否存在
  245. if (isset($imageInfo['att_dir']) && strstr($imageInfo['att_dir'], 'http') !== false && curl_file_exist($imageInfo['att_dir']) === false ) {
  246. $imageInfo = null;
  247. SystemAttachment::where(['name' => $name])->delete();
  248. }
  249. if (!$imageInfo) {
  250. $codeUrl = set_http_type($siteUrl . '?spread=' . $user['uid'], 0);//二维码链接
  251. $imageInfo = UtilService::getQRCodePath($codeUrl, $name);
  252. if (is_string($imageInfo)) return app('json')->fail('二维码生成失败', ['error' => $imageInfo]);
  253. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  254. $urlCode = $imageInfo['dir'];
  255. } else $urlCode = $imageInfo['att_dir'];
  256. if ($imageInfo['image_type'] == 1) $urlCode = $siteUrl . $urlCode;
  257. $siteUrl = set_http_type($siteUrl, 0);
  258. $filelink = [
  259. 'Bold' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  260. 'Normal' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  261. ];
  262. if (!file_exists($filelink['Bold'])) return app('json')->fail('缺少字体文件Bold');
  263. if (!file_exists($filelink['Normal'])) return app('json')->fail('缺少字体文件Normal');
  264. foreach ($routineSpreadBanner as $key => &$item) {
  265. $posterInfo = '海报生成失败:(';
  266. $config = array(
  267. 'image' => array(
  268. array(
  269. 'url' => $urlCode, //二维码资源
  270. 'stream' => 0,
  271. 'left' => 114,
  272. 'top' => 790,
  273. 'right' => 0,
  274. 'bottom' => 0,
  275. 'width' => 120,
  276. 'height' => 120,
  277. 'opacity' => 100
  278. )
  279. ),
  280. 'text' => array(
  281. array(
  282. 'text' => $user['nickname'],
  283. 'left' => 250,
  284. 'top' => 840,
  285. 'fontPath' => $rootPath . 'public' . DS . $filelink['Bold'], //字体文件
  286. 'fontSize' => 16, //字号
  287. 'fontColor' => '40,40,40', //字体颜色
  288. 'angle' => 0,
  289. ),
  290. array(
  291. 'text' => '邀请您加入' . sys_config('site_name'),
  292. 'left' => 250,
  293. 'top' => 880,
  294. 'fontPath' => $rootPath . 'public' . DS . $filelink['Normal'], //字体文件
  295. 'fontSize' => 16, //字号
  296. 'fontColor' => '40,40,40', //字体颜色
  297. 'angle' => 0,
  298. )
  299. ),
  300. 'background' => $item['pic']
  301. );
  302. $resWap = $resWap && $posterInfo = UtilService::setSharePoster($config, 'wap/spread/poster');
  303. if (!is_array($posterInfo)) return app('json')->fail($posterInfo);
  304. SystemAttachment::attachmentAdd($posterInfo['name'], $posterInfo['size'], $posterInfo['type'], $posterInfo['dir'], $posterInfo['thumb_path'], 1, $posterInfo['image_type'], $posterInfo['time'], 2);
  305. if ($resWap) {
  306. if ($posterInfo['image_type'] == 1)
  307. $item['wap_poster'] = $siteUrl . $posterInfo['thumb_path'];
  308. else
  309. $item['wap_poster'] = UtilService::setHttpType($posterInfo['thumb_path'], 1);
  310. }
  311. $item['qrcode'] = $urlCode;
  312. }
  313. }
  314. if ($resRoutine && $resWap) return app('json')->successful($routineSpreadBanner);
  315. else return app('json')->fail('生成图片失败');
  316. } catch (\Exception $e) {
  317. return app('json')->fail('生成图片时,系统错误', ['line' => $e->getLine(), 'message' => $e->getMessage(), 'file' => $e->getFile()]);
  318. }
  319. }
  320. /**
  321. * 积分记录
  322. * @param Request $request
  323. * @return mixed
  324. * @throws \think\db\exception\DataNotFoundException
  325. * @throws \think\db\exception\ModelNotFoundException
  326. * @throws \think\exception\DbException
  327. */
  328. public function integral_list(Request $request)
  329. {
  330. list($page, $limit,$type) = UtilService::getMore([
  331. ['page', 0], ['limit', 0],['type',0]
  332. ], $request, true);
  333. return app('json')->successful(UserBill::userBillList($request->uid(), $page, $limit,'integral',$type));
  334. }
  335. /**
  336. * 积分记录
  337. * @param Request $request
  338. * @return mixed
  339. * @throws \think\db\exception\DataNotFoundException
  340. * @throws \think\db\exception\ModelNotFoundException
  341. * @throws \think\exception\DbException
  342. */
  343. public function wdc_list(Request $request)
  344. {
  345. list($page, $limit,$type) = UtilService::getMore([
  346. ['page', 0], ['limit', 0],['type',0]
  347. ], $request, true);
  348. return app('json')->successful(UserBill::userBillList($request->uid(), $page, $limit,'wdc',$type));
  349. }
  350. /**
  351. * 贡献值记录
  352. * @param Request $request
  353. * @return mixed
  354. * @throws \think\db\exception\DataNotFoundException
  355. * @throws \think\db\exception\ModelNotFoundException
  356. * @throws \think\exception\DbException
  357. */
  358. public function contribution_list(Request $request)
  359. {
  360. list($page, $limit,$type) = UtilService::getMore([
  361. ['page', 0], ['limit', 0],['type',0]
  362. ], $request, true);
  363. return app('json')->successful(UserBill::userBillList($request->uid(), $page, $limit,'contribution',$type));
  364. }
  365. }