UserBillController.php 17 KB

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