UserBillController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. return app('json')->successful($data);
  48. }
  49. /**
  50. * 推荐用户
  51. * @param Request $request
  52. * @return mixed
  53. *
  54. * grade == 0 获取一级推荐人
  55. * grade == 1 获取二级推荐人
  56. *
  57. * keyword 会员名称查询
  58. *
  59. * sort childCount ASC/DESC 团队排序 numberCount ASC/DESC 金额排序 orderCount ASC/DESC 订单排序
  60. */
  61. public function spread_people(Request $request)
  62. {
  63. $spreadInfo = UtilService::postMore([
  64. ['page', 1],
  65. ['limit', 20],
  66. ['grade', 0],
  67. ['keyword', ''],
  68. ['sort', ''],
  69. ], $request);
  70. $uid = $request->uid();
  71. $data['list'] = User::getUserSpreadGrade($uid, $spreadInfo['grade'], $spreadInfo['sort'], $spreadInfo['keyword'], $spreadInfo['page'], $spreadInfo['limit']);
  72. $data['total'] = User::getSpreadCount($uid);
  73. $data['totalLevel'] = User::getSpreadLevelCount($uid);
  74. return app('json')->successful($data);
  75. }
  76. public function setReturnUper($uid, Request $request)
  77. {
  78. $user = User::getUserInfo($uid);
  79. if (!$user || $user['spread_uid'] != $request->uid()) {
  80. return app('json')->fail('用户不合法');
  81. }
  82. $ratio = $request->post('ratio', 0);
  83. if ($ratio > $request->user()['return_uper']) {
  84. return app('json')->fail('佣金比例不能超过自己');
  85. }
  86. $user2 = User::where('spread_uid', $user['uid'])->max('return_uper');
  87. if ($ratio < $user2) {
  88. return app('json')->fail('佣金比例不能低于目标下级');
  89. }
  90. $res = User::where('uid', $uid)->update(['return_uper' => $ratio]);
  91. if ($res) {
  92. return app('json')->success('修改成功');
  93. } else {
  94. return app('json')->fail('修改失败或者您没有修改什么');
  95. }
  96. }
  97. /**
  98. * 推广订单
  99. * @param Request $request
  100. * @return mixed
  101. */
  102. public function spread_order(Request $request)
  103. {
  104. $orderInfo = UtilService::postMore([
  105. ['page', 1],
  106. ['limit', 20],
  107. ['category', 'now_money'],
  108. ['type', 'brokerage'],
  109. ], $request);
  110. $data['list'] = [];
  111. $data['count'] = 0;
  112. $uid = $request->uid();
  113. $data['list'] = UserBill::getRecordList($uid, $orderInfo['page'], $orderInfo['limit'], $orderInfo['category'], $orderInfo['type']);
  114. $count = UserBill::getRecordOrderCount($uid, $orderInfo['category'], $orderInfo['type']);
  115. $data['count'] = $count ? $count : 0;
  116. if (!count($data['list'])) return app('json')->successful($data);
  117. foreach ($data['list'] as $key => &$value) {
  118. $value['child'] = UserBill::getRecordOrderListDraw($uid, $value['time'], $orderInfo['category'], $orderInfo['type']);
  119. $value['count'] = count($value['child']);
  120. }
  121. return app('json')->successful($data);
  122. }
  123. /**
  124. * 推广佣金明细
  125. * @param Request $request
  126. * @param $type 0 全部 1 消费 2 充值 3 返佣 4 提现
  127. * @return mixed
  128. */
  129. public function spread_commission(Request $request, $type)
  130. {
  131. list($page, $limit) = UtilService::getMore([
  132. ['page', 0],
  133. ['limit', 0],
  134. ], $request, true);
  135. // var_dump($type);
  136. // var_dump($request->uid());
  137. // var_dump($page);
  138. // var_dump($limit);
  139. return app('json')->successful(UserBill::getUserBillList($request->uid(), (int)$page, (int)$limit, $type));
  140. }
  141. /**
  142. * 推广 佣金/提现 总和
  143. * @param Request $request
  144. * @param $type 3 佣金 4 提现
  145. * @return mixed
  146. */
  147. public function spread_count(Request $request, $type)
  148. {
  149. $count = 0;
  150. if ($type == 3) {
  151. $count1 = UserBill::getRecordCount($request->uid(), 'now_money', 'brokerage');
  152. $count2 = UserBill::getRecordCount($request->uid(), 'now_money', 'brokerage', '', true);
  153. $count = $count1 - $count2;
  154. } else if ($type == 4) {
  155. $count = UserExtract::userExtractTotalPrice($request->uid());//累计提现
  156. }
  157. $count = $count ? $count : 0;
  158. return app('json')->successful(['count' => $count]);
  159. }
  160. /**
  161. * 分销二维码海报生成
  162. * @param Request $request
  163. * @return mixed
  164. */
  165. public function spread_banner(Request $request)
  166. {
  167. list($type) = UtilService::getMore([
  168. ['type', 2],
  169. ], $request, true);
  170. $user = $request->user();
  171. $rootPath = app()->getRootPath();
  172. try {
  173. $resRoutine = true;//小程序
  174. $resWap = true;//公众号
  175. $siteUrl = sys_config('site_url');
  176. $routineSpreadBanner = sys_data('routine_spread_banner');
  177. if (!count($routineSpreadBanner)) return app('json')->fail('暂无海报');
  178. if ($type == 1) {
  179. //小程序
  180. $name = $user['uid'] . '_' . $user['is_promoter'] . '_user_routine.jpg';
  181. $imageInfo = SystemAttachment::getInfo($name, 'name');
  182. //检测远程文件是否存在
  183. if (isset($imageInfo['att_dir']) && strstr($imageInfo['att_dir'], 'http') !== false && curl_file_exist($imageInfo['att_dir']) === false) {
  184. $imageInfo = null;
  185. SystemAttachment::where(['name' => $name])->delete();
  186. }
  187. if (!$imageInfo) {
  188. $res = RoutineCode::getShareCode($user['uid'], 'spread', '', '');
  189. if (!$res) return app('json')->fail('二维码生成失败');
  190. $uploadType = (int)sys_config('upload_type', 1);
  191. $upload = new Upload($uploadType, [
  192. 'accessKey' => sys_config('accessKey'),
  193. 'secretKey' => sys_config('secretKey'),
  194. 'uploadUrl' => sys_config('uploadUrl'),
  195. 'storageName' => sys_config('storage_name'),
  196. 'storageRegion' => sys_config('storage_region'),
  197. ]);
  198. $uploadRes = $upload->to('routine/spread/code')->validate()->stream($res['res'], $name);
  199. if ($uploadRes === false) {
  200. return app('json')->fail($upload->getError());
  201. }
  202. $imageInfo = $upload->getUploadInfo();
  203. $imageInfo['image_type'] = $uploadType;
  204. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  205. RoutineQrcode::setRoutineQrcodeFind($res['id'], ['status' => 1, 'url_time' => time(), 'qrcode_url' => $imageInfo['dir']]);
  206. $urlCode = $imageInfo['dir'];
  207. } else $urlCode = $imageInfo['att_dir'];
  208. if ($imageInfo['image_type'] == 1) $urlCode = $siteUrl . $urlCode;
  209. $siteUrlHttps = set_http_type($siteUrl, 0);
  210. $filelink = [
  211. 'Bold' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  212. 'Normal' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  213. ];
  214. if (!file_exists($filelink['Bold'])) return app('json')->fail('缺少字体文件Bold');
  215. if (!file_exists($filelink['Normal'])) return app('json')->fail('缺少字体文件Normal');
  216. foreach ($routineSpreadBanner as $key => &$item) {
  217. $posterInfo = '海报生成失败:(';
  218. $config = array(
  219. 'image' => array(
  220. array(
  221. 'url' => $urlCode, //二维码资源
  222. 'stream' => 0,
  223. 'left' => 114,
  224. 'top' => 790,
  225. 'right' => 0,
  226. 'bottom' => 0,
  227. 'width' => 120,
  228. 'height' => 120,
  229. 'opacity' => 100
  230. )
  231. ),
  232. 'text' => array(
  233. array(
  234. 'text' => $user['nickname'],
  235. 'left' => 250,
  236. 'top' => 840,
  237. 'fontPath' => $rootPath . 'public' . DS . $filelink['Bold'], //字体文件
  238. 'fontSize' => 16, //字号
  239. 'fontColor' => '40,40,40', //字体颜色
  240. 'angle' => 0,
  241. ),
  242. array(
  243. 'text' => '邀请您加入' . sys_config('site_name'),
  244. 'left' => 250,
  245. 'top' => 880,
  246. 'fontPath' => $rootPath . 'public' . DS . $filelink['Normal'], //字体文件
  247. 'fontSize' => 16, //字号
  248. 'fontColor' => '40,40,40', //字体颜色
  249. 'angle' => 0,
  250. )
  251. ),
  252. 'background' => $item['pic']
  253. );
  254. $resRoutine = $resRoutine && $posterInfo = UtilService::setSharePoster($config, 'routine/spread/poster');
  255. if (!is_array($posterInfo)) return app('json')->fail($posterInfo);
  256. SystemAttachment::attachmentAdd($posterInfo['name'], $posterInfo['size'], $posterInfo['type'], $posterInfo['dir'], $posterInfo['thumb_path'], 1, $posterInfo['image_type'], $posterInfo['time'], 2);
  257. if ($resRoutine) {
  258. if ($posterInfo['image_type'] == 1)
  259. $item['poster'] = $siteUrlHttps . $posterInfo['dir'];
  260. else
  261. $item['poster'] = set_http_type($posterInfo['dir'], 0);
  262. $item['poster'] = str_replace('\\', '/', $item['poster']);
  263. }
  264. }
  265. } else if ($type == 2) {
  266. //公众号
  267. $name = $user['uid'] . '_' . $user['is_promoter'] . '_user_wap.jpg';
  268. $imageInfo = SystemAttachment::getInfo($name, 'name');
  269. //检测远程文件是否存在
  270. if (isset($imageInfo['att_dir']) && strstr($imageInfo['att_dir'], 'http') !== false && curl_file_exist($imageInfo['att_dir']) === false) {
  271. $imageInfo = null;
  272. SystemAttachment::where(['name' => $name])->delete();
  273. }
  274. if (!$imageInfo) {
  275. $codeUrl = set_http_type($siteUrl . '?invite_code=' . $user['invite_code'], 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. } else $urlCode = $imageInfo['att_dir'];
  281. if ($imageInfo['image_type'] == 1) $urlCode = $siteUrl . $urlCode;
  282. $siteUrl = set_http_type($siteUrl, 1);
  283. $filelink = [
  284. 'Bold' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  285. 'Normal' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  286. ];
  287. if (!file_exists($filelink['Bold'])) return app('json')->fail('缺少字体文件Bold');
  288. if (!file_exists($filelink['Normal'])) return app('json')->fail('缺少字体文件Normal');
  289. foreach ($routineSpreadBanner as $key => &$item) {
  290. $posterInfo = '海报生成失败:(';
  291. $config = array(
  292. 'image' => array(
  293. array(
  294. 'url' => $urlCode, //二维码资源
  295. 'stream' => 0,
  296. 'left' => 250,
  297. 'top' => 765,
  298. 'right' => 0,
  299. 'bottom' => 0,
  300. 'width' => 250,
  301. 'height' => 250,
  302. 'opacity' => 100
  303. )
  304. ),
  305. // 'text' => array(
  306. // array(
  307. // 'text' => $user['nickname'],
  308. // 'left' => 250,
  309. // 'top' => 840,
  310. // 'fontPath' => $rootPath . 'public' . DS . $filelink['Bold'], //字体文件
  311. // 'fontSize' => 16, //字号
  312. // 'fontColor' => '40,40,40', //字体颜色
  313. // 'angle' => 0,
  314. // ),
  315. // array(
  316. // 'text' => '邀请您加入' . sys_config('site_name'),
  317. // 'left' => 250,
  318. // 'top' => 880,
  319. // 'fontPath' => $rootPath . 'public' . DS . $filelink['Normal'], //字体文件
  320. // 'fontSize' => 16, //字号
  321. // 'fontColor' => '40,40,40', //字体颜色
  322. // 'angle' => 0,
  323. // )
  324. // ),
  325. 'background' => $item['pic']
  326. );
  327. $resWap = $resWap && $posterInfo = UtilService::setSharePoster($config, 'wap/spread/poster');
  328. if (!is_array($posterInfo)) return app('json')->fail($posterInfo);
  329. SystemAttachment::attachmentAdd($posterInfo['name'], $posterInfo['size'], $posterInfo['type'], $posterInfo['dir'], $posterInfo['thumb_path'], 1, $posterInfo['image_type'], $posterInfo['time'], 2);
  330. if ($resWap) {
  331. if ($posterInfo['image_type'] == 1)
  332. $item['wap_poster'] = $siteUrl . $posterInfo['thumb_path'];
  333. else
  334. $item['wap_poster'] = UtilService::setHttpType($posterInfo['thumb_path'], 1);
  335. }
  336. }
  337. }
  338. if ($resRoutine && $resWap) return app('json')->successful($routineSpreadBanner);
  339. else return app('json')->fail('生成图片失败');
  340. } catch (\Exception $e) {
  341. return app('json')->fail('生成图片时,系统错误', ['line' => $e->getLine(), 'message' => $e->getMessage(), 'file' => $e->getFile()]);
  342. }
  343. }
  344. /**
  345. * 积分记录
  346. * @param Request $request
  347. * @return mixed
  348. * @throws \think\db\exception\DataNotFoundException
  349. * @throws \think\db\exception\ModelNotFoundException
  350. * @throws \think\exception\DbException
  351. */
  352. public function integral_list(Request $request)
  353. {
  354. list($page, $limit) = UtilService::getMore([
  355. ['page', 0], ['limit', 0]
  356. ], $request, true);
  357. return app('json')->successful(UserBill::userBillList($request->uid(), $page, $limit));
  358. }
  359. }