UserBillController.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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\QrcodeService;
  13. use crmeb\services\SystemConfigService;
  14. use crmeb\services\UtilService;
  15. use crmeb\services\upload\Upload;
  16. /**
  17. * 账单类
  18. * Class UserBillController
  19. * @package app\api\controller\user
  20. */
  21. class UserBillController
  22. {
  23. /**
  24. * 推广数据 昨天的佣金 累计提现金额 当前佣金
  25. * @param Request $request
  26. * @return mixed
  27. */
  28. public function commission(Request $request)
  29. {
  30. $uid = $request->uid();
  31. $lastDayCount = UserBill::yesterdayCommissionSum($uid);//昨天的佣金
  32. $extractCount = UserExtract::extractSum($uid);//累计提现金额
  33. $commissionCount = UserBill::getBrokerage($uid);//获取总佣金
  34. if ($commissionCount > 0) {
  35. $rechargeCount = UserBill::getRecharge($uid);//累计充值
  36. $orderYuePrice = StoreOrder::getOrderStatusYueSum($uid);//余额累计消费
  37. $systemAdd = UserBill::getSystemAdd($uid);//后台添加余额
  38. $yueCount = bcadd($rechargeCount, $systemAdd, 2);// 后台添加余额 + 累计充值 = 非佣金的总金额
  39. $orderYuePrice = $yueCount > $orderYuePrice ? 0 : bcsub($orderYuePrice, $yueCount, 2);// 余额累计消费(使用佣金消费的金额)
  40. $commissionCount = bcsub($commissionCount, $extractCount, 2);//减去已提现金额
  41. $extractPriceCount = UserExtract::userExtractTotalPrice($uid, 0);
  42. $commissionCount = $extractPriceCount < $commissionCount ? bcsub($commissionCount, $extractPriceCount, 2) : 0;//减去审核中的提现金额
  43. $commissionCount = $commissionCount > $orderYuePrice ? bcsub($commissionCount, $orderYuePrice, 2) : 0;//减掉余额支付
  44. }
  45. $data['lastDayCount'] = $lastDayCount;
  46. $data['extractCount'] = $extractCount;
  47. $data['commissionCount'] = $commissionCount;
  48. return app('json')->successful($data);
  49. }
  50. /**
  51. * 推荐用户
  52. * @param Request $request
  53. * @return mixed
  54. *
  55. * grade == 0 获取一级推荐人
  56. * grade == 1 获取二级推荐人
  57. *
  58. * keyword 会员名称查询
  59. *
  60. * sort childCount ASC/DESC 团队排序 numberCount ASC/DESC 金额排序 orderCount ASC/DESC 订单排序
  61. */
  62. public function spread_people(Request $request)
  63. {
  64. $spreadInfo = UtilService::postMore([
  65. ['page', 1],
  66. ['limit', 20],
  67. ['grade', 0],
  68. ['keyword', ''],
  69. ['sort', ''],
  70. ], $request);
  71. $uid = $request->uid();
  72. $data['list'] = User::getUserSpreadGrade($uid, $spreadInfo['grade'], $spreadInfo['sort'], $spreadInfo['keyword'], $spreadInfo['page'], $spreadInfo['limit']);
  73. $data['total'] = User::getSpreadCount($uid);
  74. $data['totalLevel'] = User::getSpreadLevelCount($uid);
  75. return app('json')->successful($data);
  76. }
  77. /**
  78. * 推广订单
  79. * @param Request $request
  80. * @return mixed
  81. */
  82. public function spread_order(Request $request)
  83. {
  84. $orderInfo = UtilService::postMore([
  85. ['page', 1],
  86. ['limit', 20],
  87. ['category', 'brokerage_price'],
  88. ['type', 'spread_award'],
  89. ], $request);
  90. $data['list'] = [];
  91. $data['count'] = 0;
  92. $uid = $request->uid();
  93. $data['list'] = UserBill::getRecordList($uid, $orderInfo['page'], $orderInfo['limit'], $orderInfo['category'], $orderInfo['type']);
  94. $count = UserBill::getRecordOrderCount($uid, $orderInfo['category'], $orderInfo['type']);
  95. $data['count'] = $count ? $count : 0;
  96. if (!count($data['list'])) return app('json')->successful($data);
  97. foreach ($data['list'] as $key => &$value) {
  98. $value['child'] = UserBill::getRecordOrderListDraw($uid, $value['time'], $orderInfo['category'], $orderInfo['type']);
  99. $value['count'] = count($value['child']);
  100. }
  101. return app('json')->successful($data);
  102. }
  103. /**
  104. * 推广佣金明细
  105. * @param Request $request
  106. * @param $type 0 全部 1 消费 2 充值 3 返佣 4 提现
  107. * @return mixed
  108. */
  109. public function spread_commission(Request $request, $type)
  110. {
  111. list($page, $limit) = UtilService::getMore([
  112. ['page', 0],
  113. ['limit', 0],
  114. ], $request, true);
  115. return app('json')->successful(UserBill::getUserBillList($request->uid(), $page, $limit, $type));
  116. }
  117. /**
  118. * 推广 佣金/提现 总和
  119. * @param Request $request
  120. * @param $type 3 佣金 4 提现
  121. * @return mixed
  122. */
  123. public function spread_count(Request $request, $type)
  124. {
  125. $count = 0;
  126. if ($type == 3) {
  127. $count1 = UserBill::getRecordCount($request->uid(), 'now_money', 'brokerage');
  128. $count2 = UserBill::getRecordCount($request->uid(), 'now_money', 'brokerage', '', true);
  129. $count = $count1 - $count2;
  130. } else if ($type == 4) {
  131. $count = UserExtract::userExtractTotalPrice($request->uid());//累计提现
  132. }
  133. $count = $count ? $count : 0;
  134. return app('json')->successful(['count' => $count]);
  135. }
  136. /**
  137. * 分销二维码海报生成
  138. * @param Request $request
  139. * @return mixed
  140. */
  141. // public function spread_banner(Request $request)
  142. // {
  143. // list($type) = UtilService::getMore([
  144. // ['type', 2],
  145. // ], $request, true);
  146. // $user = $request->user();
  147. // $rootPath = app()->getRootPath();
  148. // try {
  149. // $resRoutine = true;//小程序
  150. // $resWap = true;//公众号
  151. // $siteUrl = sys_config('site_url');
  152. // $routineSpreadBanner = sys_data('routine_spread_banner');
  153. // if (!count($routineSpreadBanner)) return app('json')->fail('暂无海报');
  154. // if ($type == 1) {
  155. // //小程序
  156. // $name = $user['uid'] . '_' . $user['is_promoter'] . '_user_routine.jpg';
  157. // $imageInfo = SystemAttachment::getInfo($name, 'name');
  158. // //检测远程文件是否存在
  159. // if (isset($imageInfo['att_dir']) && strstr($imageInfo['att_dir'], 'http') !== false && curl_file_exist($imageInfo['att_dir']) === false) {
  160. // $imageInfo = null;
  161. // SystemAttachment::where(['name' => $name])->delete();
  162. // }
  163. // if (!$imageInfo) {
  164. // $res = RoutineCode::getShareCode($user['uid'], 'spread', '', '');
  165. // if (!$res) return app('json')->fail('二维码生成失败');
  166. // $uploadType = (int)sys_config('upload_type', 1);
  167. // $upload = new Upload($uploadType, [
  168. // 'accessKey' => sys_config('accessKey'),
  169. // 'secretKey' => sys_config('secretKey'),
  170. // 'uploadUrl' => sys_config('uploadUrl'),
  171. // 'storageName' => sys_config('storage_name'),
  172. // 'storageRegion' => sys_config('storage_region'),
  173. // ]);
  174. // $uploadRes = $upload->to('routine/spread/code')->validate()->stream($res['res'], $name);
  175. // if ($uploadRes === false) {
  176. // return app('json')->fail($upload->getError());
  177. // }
  178. // $imageInfo = $upload->getUploadInfo();
  179. // $imageInfo['image_type'] = $uploadType;
  180. // SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  181. // RoutineQrcode::setRoutineQrcodeFind($res['id'], ['status' => 1, 'url_time' => time(), 'qrcode_url' => $imageInfo['dir']]);
  182. // $urlCode = $imageInfo['dir'];
  183. // } else $urlCode = $imageInfo['att_dir'];
  184. // if ($imageInfo['image_type'] == 1) $urlCode = $siteUrl . $urlCode;
  185. // $siteUrlHttps = set_http_type($siteUrl, 0);
  186. // $filelink = [
  187. // 'Bold' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  188. // 'Normal' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  189. // ];
  190. // if (!file_exists($filelink['Bold'])) return app('json')->fail('缺少字体文件Bold');
  191. // if (!file_exists($filelink['Normal'])) return app('json')->fail('缺少字体文件Normal');
  192. // $avatar = $user['avatar'] ?? '';
  193. // if (empty($avatar)) { // 可选:返回错误或用默认头像
  194. // return app('json')->fail('用户头像不存在');
  195. // }
  196. // $avatarUrl = $avatar;
  197. // if (strpos($avatarUrl, 'http') !== 0) {
  198. // $avatarUrl = rtrim($siteUrl, '/') . '/' . ltrim($avatarUrl, '/');
  199. // }
  200. // $posterWidth = 750; // 公众号海报标准宽,实际调整
  201. // $fontBoldPath = $rootPath . 'public' . DS . $filelink['Bold']; // 注意:原代码两个text都Bold
  202. // // 昵称宽高 (Bold, 50pt)
  203. // $nickname = $user['nickname'];
  204. // $nicknameBox = imagettfbbox(50, 0, $fontBoldPath, $nickname);
  205. // $nicknameWidth = abs($nicknameBox[4] - $nicknameBox[6]);
  206. // $nicknameLeft = ($posterWidth - $nicknameWidth) / 2;
  207. // foreach ($routineSpreadBanner as $key => &$item) {
  208. // $posterInfo = '海报生成失败:(';
  209. // $config = array(
  210. // 'image' => array(
  211. //// array( // 圆形头像
  212. //// 'url' => $avatarUrl,
  213. //// 'stream' => 0,
  214. //// 'left' => 300, // 居中位置:(750-150)/2 = 300
  215. //// 'top' => 400, // 上移:从640改为500
  216. //// 'right' => 0,
  217. //// 'bottom' => 0,
  218. //// 'width' => 150,
  219. //// 'height' => 150,
  220. //// 'opacity' => 100,
  221. //// 'circle' => true // 圆形蒙版
  222. //// ),
  223. // array(
  224. // 'url' => $urlCode, //二维码资源
  225. // 'stream' => 0,
  226. // 'left' => 195,
  227. // 'top' => 810,
  228. // 'right' => 0,
  229. // 'bottom' => 0,
  230. // 'width' => 360,
  231. // 'height' => 360,
  232. // 'opacity' => 100
  233. // ),
  234. // ),
  235. // 'text' => array(
  236. // array(
  237. // 'text' => $nickname,
  238. // 'left' => 300, // 与头像左边对齐
  239. // 'top' => 640, // 头像上方20px
  240. // 'fontPath' => $fontBoldPath,
  241. // 'fontSize' => 50,
  242. // 'fontColor' => '40,40,40',
  243. // 'angle' => 0,
  244. // ),
  245. // array(
  246. // 'text' => '邀请您加入' . sys_config('site_name'),
  247. // 'left' => 120,
  248. // 'top' => 720,
  249. // 'fontPath' => $rootPath . 'public' . DS . $filelink['Bold'],
  250. // 'fontSize' => 40,
  251. // 'fontColor' => '40,40,40',
  252. // 'angle' => 0,
  253. // )
  254. // ),
  255. // 'background' => $item['pic']
  256. // );
  257. // $resRoutine = $resRoutine && $posterInfo = UtilService::setSharePoster($config, 'routine/spread/poster');
  258. // if (!is_array($posterInfo)) return app('json')->fail($posterInfo);
  259. // SystemAttachment::attachmentAdd($posterInfo['name'], $posterInfo['size'], $posterInfo['type'], $posterInfo['dir'], $posterInfo['thumb_path'], 1, $posterInfo['image_type'], $posterInfo['time'], 2);
  260. // if ($resRoutine) {
  261. // if ($posterInfo['image_type'] == 1)
  262. // $item['poster'] = $siteUrlHttps . $posterInfo['dir'];
  263. // else
  264. // $item['poster'] = set_http_type($posterInfo['dir'], 0);
  265. // $item['poster'] = str_replace('\\', '/', $item['poster']);
  266. // }
  267. // }
  268. // } else if ($type == 2) {
  269. // //公众号
  270. // $name = $user['uid'] . '_' . $user['is_promoter'] . '_user_wap.jpg';
  271. // $imageInfo = SystemAttachment::getInfo($name, 'name');
  272. // //检测远程文件是否存在
  273. // if (isset($imageInfo['att_dir']) && strstr($imageInfo['att_dir'], 'http') !== false && curl_file_exist($imageInfo['att_dir']) === false) {
  274. // $imageInfo = null;
  275. // SystemAttachment::where(['name' => $name])->delete();
  276. // }
  277. // if (!$imageInfo) {
  278. // try {
  279. // $qr_code = QrcodeService::getForeverQrcode('spread', $user['uid']);
  280. // if (isset($qr_code['url'])) {
  281. // $urlCode = $qr_code['url'];
  282. // } else {
  283. // $codeUrl = set_http_type($siteUrl . '?spread=' . $user['uid'], 1);//二维码链接
  284. // $imageInfo = UtilService::getQRCodePath($codeUrl, $name);
  285. // if (is_string($imageInfo)) return app('json')->fail('二维码生成失败', ['error' => $imageInfo]);
  286. // SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  287. // $urlCode = $imageInfo['dir'];
  288. // }
  289. // } catch (\Exception $e) {
  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. // } else $urlCode = $imageInfo['att_dir'];
  297. // if ($imageInfo['image_type'] == 1) $urlCode = $siteUrl . $urlCode;
  298. // $siteUrl = set_http_type($siteUrl, 1);
  299. // $filelink = [
  300. // 'Bold' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  301. // 'Normal' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  302. // ];
  303. // if (!file_exists($filelink['Bold'])) return app('json')->fail('缺少字体文件Bold');
  304. // if (!file_exists($filelink['Normal'])) return app('json')->fail('缺少字体文件Normal');
  305. // $avatar = $user['avatar'] ?? '';
  306. // if (empty($avatar)) {
  307. // return app('json')->fail('用户头像不存在');
  308. // }
  309. // $avatarUrl = $avatar;
  310. // if (strpos($avatarUrl, 'http') !== 0) {
  311. // $avatarUrl = rtrim($siteUrl, '/', '\\') . '/' . ltrim($avatarUrl, '/');
  312. // }
  313. // $posterWidth = 783; // 公众号海报标准宽,实际调整
  314. // $fontBoldPath = $rootPath . 'public' . DS . $filelink['Bold']; // 注意:原代码两个text都Bold
  315. // // 昵称宽高 (Bold, 50pt)
  316. // $nickname = $user['nickname'];
  317. // $nicknameBox = imagettfbbox(50, 0, $fontBoldPath, $nickname);
  318. // $nicknameWidth = abs($nicknameBox[4] - $nicknameBox[6]);
  319. // $nicknameLeft = ($posterWidth - $nicknameWidth) / 2;
  320. // foreach ($routineSpreadBanner as $key => &$item) {
  321. // $posterInfo = '海报生成失败:(';
  322. // $config = array(
  323. // 'image' => array(
  324. //// array( // 圆形头像
  325. //// 'url' => $avatarUrl,
  326. //// 'stream' => 0,
  327. //// 'left' => 300, // 居中位置:(750-150)/2 = 300
  328. //// 'top' => 400, // 上移:从640改为500
  329. //// 'right' => 0,
  330. //// 'bottom' => 0,
  331. //// 'width' => 150,
  332. //// 'height' => 150,
  333. //// 'opacity' => 100,
  334. //// 'circle' => true // 圆形蒙版
  335. //// ),
  336. // array(
  337. // 'url' => $urlCode, //二维码资源
  338. // 'stream' => 0,
  339. // 'left' => 195,
  340. // 'top' => 810,
  341. // 'right' => 0,
  342. // 'bottom' => 0,
  343. // 'width' => 360,
  344. // 'height' => 360,
  345. // 'opacity' => 100
  346. // ),
  347. // ),
  348. // 'text' => array(
  349. // array(
  350. // 'text' => $nickname,
  351. // 'left' => 300, // 与头像左边对齐
  352. // 'top' => 640, // 头像上方20px
  353. // 'fontPath' => $fontBoldPath,
  354. // 'fontSize' => 50,
  355. // 'fontColor' => '40,40,40',
  356. // 'angle' => 0,
  357. // ),
  358. // array(
  359. // 'text' => '邀请您加入' . sys_config('site_name'),
  360. // 'left' => 120,
  361. // 'top' => 720,
  362. // 'fontPath' => $rootPath . 'public' . DS . $filelink['Bold'],
  363. // 'fontSize' => 40,
  364. // 'fontColor' => '40,40,40',
  365. // 'angle' => 0,
  366. // )
  367. // ),
  368. // 'background' => $item['pic']
  369. // );
  370. // $resWap = $resWap && $posterInfo = UtilService::setSharePoster($config, 'wap/spread/poster');
  371. // if (!is_array($posterInfo)) return app('json')->fail($posterInfo);
  372. // SystemAttachment::attachmentAdd($posterInfo['name'], $posterInfo['size'], $posterInfo['type'], $posterInfo['dir'], $posterInfo['thumb_path'], 1, $posterInfo['image_type'], $posterInfo['time'], 2);
  373. // if ($resWap) {
  374. // if ($posterInfo['image_type'] == 1)
  375. // $item['wap_poster'] = $siteUrl . $posterInfo['thumb_path'];
  376. // else
  377. // $item['wap_poster'] = UtilService::setHttpType($posterInfo['thumb_path'], 1);
  378. // }
  379. // }
  380. // }
  381. // if ($resRoutine && $resWap) return app('json')->successful($routineSpreadBanner);
  382. // else return app('json')->fail('生成图片失败');
  383. // } catch (\Exception $e) {
  384. // @file_put_contents('quanju.txt', json_encode(['line' => $e->getLine(), 'message' => $e->getMessage(), 'file' => $e->getFile()]) . "-报错内容\r\n", 8);
  385. // return app('json')->fail('生成图片时,系统错误', ['line' => $e->getLine(), 'message' => $e->getMessage(), 'file' => $e->getFile()]);
  386. // }
  387. // }
  388. public function spread_banner(Request $request)
  389. {
  390. list($type) = UtilService::getMore([
  391. ['type', 2],
  392. ], $request, true);
  393. $user = $request->user();
  394. $rootPath = app()->getRootPath();
  395. try {
  396. $resRoutine = true;//小程序
  397. $resWap = true;//公众号
  398. $siteUrl = sys_config('site_url');
  399. $routineSpreadBanner = sys_data('routine_spread_banner');
  400. if (!count($routineSpreadBanner)) return app('json')->fail('暂无海报');
  401. if ($type == 1) {
  402. //小程序
  403. $name = $user['uid'] . '_' . $user['is_promoter'] . '_user_routine.jpg';
  404. $imageInfo = SystemAttachment::getInfo($name, 'name');
  405. //检测远程文件是否存在
  406. if (isset($imageInfo['att_dir']) && strstr($imageInfo['att_dir'], 'http') !== false && curl_file_exist($imageInfo['att_dir']) === false) {
  407. $imageInfo = null;
  408. SystemAttachment::where(['name' => $name])->delete();
  409. }
  410. if (!$imageInfo) {
  411. $res = RoutineCode::getShareCode($user['uid'], 'spread', '', '');
  412. if (!$res) return app('json')->fail('二维码生成失败');
  413. $uploadType = (int)sys_config('upload_type', 1);
  414. $upload = new Upload($uploadType, [
  415. 'accessKey' => sys_config('accessKey'),
  416. 'secretKey' => sys_config('secretKey'),
  417. 'uploadUrl' => sys_config('uploadUrl'),
  418. 'storageName' => sys_config('storage_name'),
  419. 'storageRegion' => sys_config('storage_region'),
  420. ]);
  421. $uploadRes = $upload->to('routine/spread/code')->validate()->stream($res['res'], $name);
  422. if ($uploadRes === false) {
  423. return app('json')->fail($upload->getError());
  424. }
  425. $imageInfo = $upload->getUploadInfo();
  426. $imageInfo['image_type'] = $uploadType;
  427. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  428. RoutineQrcode::setRoutineQrcodeFind($res['id'], ['status' => 1, 'url_time' => time(), 'qrcode_url' => $imageInfo['dir']]);
  429. $urlCode = $imageInfo['dir'];
  430. } else $urlCode = $imageInfo['att_dir'];
  431. if ($imageInfo['image_type'] == 1) $urlCode = $siteUrl . $urlCode;
  432. $siteUrlHttps = set_http_type($siteUrl, 0);
  433. $filelink = [
  434. 'Bold' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  435. 'Normal' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  436. ];
  437. if (!file_exists($filelink['Bold'])) return app('json')->fail('缺少字体文件Bold');
  438. if (!file_exists($filelink['Normal'])) return app('json')->fail('缺少字体文件Normal');
  439. $avatar = $user['avatar'] ?? '';
  440. if (empty($avatar)) {
  441. return app('json')->fail('用户头像不存在');
  442. }
  443. $avatarUrl = $avatar;
  444. if (strpos($avatarUrl, 'http') !== 0) {
  445. $avatarUrl = rtrim($siteUrl, '/') . '/' . ltrim($avatarUrl, '/');
  446. }
  447. $posterWidth = 750; // 公众号海报标准宽,实际调整
  448. $fontBoldPath = $rootPath . 'public' . DS . $filelink['Bold'];
  449. $nickname = $user['nickname'];
  450. foreach ($routineSpreadBanner as $key => &$item) {
  451. $posterInfo = '海报生成失败:(';
  452. // 计算昵称文字的宽度和居中位置
  453. $nicknameBox = imagettfbbox(50, 0, $fontBoldPath, $nickname);
  454. $nicknameWidth = abs($nicknameBox[4] - $nicknameBox[6]);
  455. $nicknameLeft = ($posterWidth - $nicknameWidth) / 2;
  456. $config = array(
  457. 'image' => array(
  458. // array( // 圆形头像
  459. // 'url' => $avatarUrl,
  460. // 'stream' => 0,
  461. // 'left' => 300, // 居中位置:(750-150)/2 = 300
  462. // 'top' => 400, // 上移:从640改为500
  463. // 'right' => 0,
  464. // 'bottom' => 0,
  465. // 'width' => 150,
  466. // 'height' => 150,
  467. // 'opacity' => 100,
  468. // 'circle' => true // 圆形蒙版
  469. // ),
  470. array(
  471. 'url' => $urlCode, //二维码资源
  472. 'stream' => 0,
  473. 'left' => 195,
  474. 'top' => 810,
  475. 'right' => 0,
  476. 'bottom' => 0,
  477. 'width' => 360,
  478. 'height' => 360,
  479. 'opacity' => 100
  480. ),
  481. ),
  482. 'text' => array(
  483. array(
  484. 'text' => $nickname,
  485. 'left' => $nicknameLeft, // 使用计算出的居中位置
  486. 'top' => 640,
  487. 'fontPath' => $fontBoldPath,
  488. 'fontSize' => 50,
  489. 'fontColor' => '40,40,40',
  490. 'angle' => 0,
  491. ),
  492. array(
  493. 'text' => '邀请您加入' . sys_config('site_name'),
  494. 'left' => 120,
  495. 'top' => 720,
  496. 'fontPath' => $rootPath . 'public' . DS . $filelink['Bold'],
  497. 'fontSize' => 40,
  498. 'fontColor' => '40,40,40',
  499. 'angle' => 0,
  500. )
  501. ),
  502. 'background' => $item['pic']
  503. );
  504. $resRoutine = $resRoutine && $posterInfo = UtilService::setSharePoster($config, 'routine/spread/poster');
  505. if (!is_array($posterInfo)) return app('json')->fail($posterInfo);
  506. SystemAttachment::attachmentAdd($posterInfo['name'], $posterInfo['size'], $posterInfo['type'], $posterInfo['dir'], $posterInfo['thumb_path'], 1, $posterInfo['image_type'], $posterInfo['time'], 2);
  507. if ($resRoutine) {
  508. if ($posterInfo['image_type'] == 1)
  509. $item['poster'] = $siteUrlHttps . $posterInfo['dir'];
  510. else
  511. $item['poster'] = set_http_type($posterInfo['dir'], 0);
  512. $item['poster'] = str_replace('\\', '/', $item['poster']);
  513. }
  514. }
  515. } else if ($type == 2) {
  516. //公众号
  517. $name = $user['uid'] . '_' . $user['is_promoter'] . '_user_wap.jpg';
  518. $imageInfo = SystemAttachment::getInfo($name, 'name');
  519. //检测远程文件是否存在
  520. if (isset($imageInfo['att_dir']) && strstr($imageInfo['att_dir'], 'http') !== false && curl_file_exist($imageInfo['att_dir']) === false) {
  521. $imageInfo = null;
  522. SystemAttachment::where(['name' => $name])->delete();
  523. }
  524. if (!$imageInfo) {
  525. try {
  526. $qr_code = QrcodeService::getForeverQrcode('spread', $user['uid']);
  527. if (isset($qr_code['url'])) {
  528. $urlCode = $qr_code['url'];
  529. } else {
  530. $codeUrl = set_http_type($siteUrl . '?spread=' . $user['uid'], 1);//二维码链接
  531. $imageInfo = UtilService::getQRCodePath($codeUrl, $name);
  532. if (is_string($imageInfo)) return app('json')->fail('二维码生成失败', ['error' => $imageInfo]);
  533. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  534. $urlCode = $imageInfo['dir'];
  535. }
  536. } catch (\Exception $e) {
  537. $codeUrl = set_http_type($siteUrl . '?spread=' . $user['uid'], 1);//二维码链接
  538. $imageInfo = UtilService::getQRCodePath($codeUrl, $name);
  539. if (is_string($imageInfo)) return app('json')->fail('二维码生成失败', ['error' => $imageInfo]);
  540. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  541. $urlCode = $imageInfo['dir'];
  542. }
  543. } else $urlCode = $imageInfo['att_dir'];
  544. if ($imageInfo['image_type'] == 1) $urlCode = $siteUrl . $urlCode;
  545. $siteUrl = set_http_type($siteUrl, 1);
  546. $filelink = [
  547. 'Bold' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  548. 'Normal' => 'static' . DS . 'font' . DS . 'Alibaba-PuHuiTi-Regular.otf',
  549. ];
  550. if (!file_exists($filelink['Bold'])) return app('json')->fail('缺少字体文件Bold');
  551. if (!file_exists($filelink['Normal'])) return app('json')->fail('缺少字体文件Normal');
  552. $avatar = $user['avatar'] ?? '';
  553. if (empty($avatar)) {
  554. return app('json')->fail('用户头像不存在');
  555. }
  556. $avatarUrl = $avatar;
  557. if (strpos($avatarUrl, 'http') !== 0) {
  558. $avatarUrl = rtrim($siteUrl, '/', '\\') . '/' . ltrim($avatarUrl, '/');
  559. }
  560. $posterWidth = 783; // 公众号海报标准宽,实际调整
  561. $fontBoldPath = $rootPath . 'public' . DS . $filelink['Bold'];
  562. $nickname = $user['nickname'];
  563. foreach ($routineSpreadBanner as $key => &$item) {
  564. $posterInfo = '海报生成失败:(';
  565. // 计算昵称文字的宽度和居中位置
  566. $nicknameBox = imagettfbbox(50, 0, $fontBoldPath, $nickname);
  567. $nicknameWidth = abs($nicknameBox[4] - $nicknameBox[6]);
  568. $nicknameLeft = ($posterWidth - $nicknameWidth) / 2;
  569. $config = array(
  570. 'image' => array(
  571. // array( // 圆形头像
  572. // 'url' => $avatarUrl,
  573. // 'stream' => 0,
  574. // 'left' => 300, // 居中位置:(750-150)/2 = 300
  575. // 'top' => 400, // 上移:从640改为500
  576. // 'right' => 0,
  577. // 'bottom' => 0,
  578. // 'width' => 150,
  579. // 'height' => 150,
  580. // 'opacity' => 100,
  581. // 'circle' => true // 圆形蒙版
  582. // ),
  583. array(
  584. 'url' => $urlCode, //二维码资源
  585. 'stream' => 0,
  586. 'left' => 195,
  587. 'top' => 810,
  588. 'right' => 0,
  589. 'bottom' => 0,
  590. 'width' => 360,
  591. 'height' => 360,
  592. 'opacity' => 100
  593. ),
  594. ),
  595. 'text' => array(
  596. array(
  597. 'text' => $nickname,
  598. 'left' => $nicknameLeft, // 使用计算出的居中位置
  599. 'top' => 640,
  600. 'fontPath' => $fontBoldPath,
  601. 'fontSize' => 50,
  602. 'fontColor' => '40,40,40',
  603. 'angle' => 0,
  604. ),
  605. array(
  606. 'text' => '邀请您加入' . sys_config('site_name'),
  607. 'left' => 120,
  608. 'top' => 720,
  609. 'fontPath' => $rootPath . 'public' . DS . $filelink['Bold'],
  610. 'fontSize' => 40,
  611. 'fontColor' => '40,40,40',
  612. 'angle' => 0,
  613. )
  614. ),
  615. 'background' => $item['pic']
  616. );
  617. $resWap = $resWap && $posterInfo = UtilService::setSharePoster($config, 'wap/spread/poster');
  618. if (!is_array($posterInfo)) return app('json')->fail($posterInfo);
  619. SystemAttachment::attachmentAdd($posterInfo['name'], $posterInfo['size'], $posterInfo['type'], $posterInfo['dir'], $posterInfo['thumb_path'], 1, $posterInfo['image_type'], $posterInfo['time'], 2);
  620. if ($resWap) {
  621. if ($posterInfo['image_type'] == 1)
  622. $item['wap_poster'] = $siteUrl . $posterInfo['thumb_path'];
  623. else
  624. $item['wap_poster'] = UtilService::setHttpType($posterInfo['thumb_path'], 1);
  625. }
  626. }
  627. }
  628. if ($resRoutine && $resWap) return app('json')->successful($routineSpreadBanner);
  629. else return app('json')->fail('生成图片失败');
  630. } catch (\Exception $e) {
  631. @file_put_contents('quanju.txt', json_encode(['line' => $e->getLine(), 'message' => $e->getMessage(), 'file' => $e->getFile()]) . "-报错内容\r\n", 8);
  632. return app('json')->fail('生成图片时,系统错误', ['line' => $e->getLine(), 'message' => $e->getMessage(), 'file' => $e->getFile()]);
  633. }
  634. }
  635. /**
  636. * 增强版的文字居中工具函数
  637. * @param string $text 文字内容
  638. * @param string $fontPath 字体文件路径
  639. * @param int $fontSize 字体大小
  640. * @param int $containerWidth 容器宽度
  641. * @return array 包含left位置和文字宽度的数组
  642. */
  643. function getTextCenterPosition($text, $fontPath, $fontSize, $containerWidth) {
  644. // 获取文字边界框
  645. $box = imagettfbbox($fontSize, 0, $fontPath, $text);
  646. // 计算文字宽度
  647. $textWidth = abs($box[4] - $box[6]);
  648. // 计算居中位置
  649. $centerLeft = ($containerWidth - $textWidth) / 2;
  650. return [
  651. 'left' => $centerLeft,
  652. 'width' => $textWidth
  653. ];
  654. }
  655. /**
  656. * 积分记录
  657. * @param Request $request
  658. * @return mixed
  659. * @throws \think\db\exception\DataNotFoundException
  660. * @throws \think\db\exception\ModelNotFoundException
  661. * @throws \think\exception\DbException
  662. */
  663. public function integral_list(Request $request)
  664. {
  665. list($page, $limit, $status) = UtilService::getMore([
  666. ['page', 0], ['limit', 0], ['pm', 0]
  667. ], $request, true);
  668. return app('json')->successful(UserBill::userBillList($request->uid(), $page, $limit, 'integral', ['pm' => $status]));
  669. }
  670. }