UserBillController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\api\v1\user;
  12. use app\Request;
  13. use app\services\other\QrcodeServices;
  14. use app\services\system\attachment\SystemAttachmentServices;
  15. use app\services\system\config\SystemConfigServices;
  16. use app\services\user\UserBillServices;
  17. use crmeb\services\UploadService;
  18. use crmeb\services\wechat\MiniProgram;
  19. /**
  20. * 账单类
  21. * Class UserBillController
  22. * @package app\controller\api\user
  23. */
  24. class UserBillController
  25. {
  26. protected $services = NUll;
  27. /**
  28. * UserBillController constructor.
  29. * @param UserBillServices $services
  30. */
  31. public function __construct(UserBillServices $services)
  32. {
  33. $this->services = $services;
  34. }
  35. /**
  36. * 推广佣金明细
  37. * @param Request $request
  38. * @param $type 0 全部 1 消费 2 充值 3 返佣 4 提现
  39. * @return mixed
  40. */
  41. public function spread_commission(Request $request, $type)
  42. {
  43. $uid = (int)$request->uid();
  44. return app('json')->successful($this->services->getUserBillList($uid, $type));
  45. }
  46. /**
  47. * 获取小程序二维码
  48. * @param Request $request
  49. * @return mixed
  50. * @throws \think\Exception
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\DbException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. */
  55. public function getRoutineCode(Request $request)
  56. {
  57. $user = $request->user();
  58. /** @var SystemAttachmentServices $systemAttachment */
  59. $systemAttachment = app()->make(SystemAttachmentServices::class);
  60. //小程序
  61. $name = $user['uid'] . '_' . $user['is_promoter'] . '_user_routine.jpg';
  62. $imageInfo = $systemAttachment->getInfo(['name' => $name]);
  63. //检测远程文件是否存在
  64. if (isset($imageInfo['att_dir']) && strstr($imageInfo['att_dir'], 'http') !== false && curl_file_exist($imageInfo['att_dir']) === false) {
  65. $imageInfo = null;
  66. $systemAttachment->delete(['name' => $name]);
  67. }
  68. $siteUrl = sys_config('site_url');
  69. /** @var QrcodeServices $qrCode */
  70. $qrCode = app()->make(QrcodeServices::class);
  71. $res1 = $qrCode->qrCodeExist($user['uid'], 'spread');
  72. //图片在,数据库没有数据,删除图片
  73. if ($imageInfo && !$res1) {
  74. try {
  75. $uploadRes = UploadService::init($imageInfo->image_type);
  76. if ($imageInfo['image_type'] == 1) {
  77. if (strpos($imageInfo['att_dir'], '/') == 0) {
  78. $imageInfo['att_dir'] = substr($imageInfo['att_dir'], 1);
  79. }
  80. if ($imageInfo['att_dir']) $uploadRes->delete(public_path() . $imageInfo['att_dir']);
  81. } else {
  82. if ($imageInfo['name']) $uploadRes->delete($imageInfo['name']);
  83. }
  84. } catch (\Throwable $e) {
  85. }
  86. }
  87. if (!$imageInfo || !$res1) {
  88. $resForever = $qrCode->qrCodeForever($user['uid'], 'spread', '', '');
  89. $data = 'id=' . $resForever->id . '&spid=' . $user['uid'];
  90. $resCode = MiniProgram::appCodeUnlimit($data, '', 280);
  91. if ($resCode) {
  92. $res = ['res' => $resCode, 'id' => $resForever->id];
  93. } else {
  94. $res = false;
  95. }
  96. if (!$res) return app('json')->fail('二维码生成失败');
  97. $uploadType = (int)sys_config('upload_type', 1);
  98. $upload = UploadService::init($uploadType);
  99. $uploadRes = $upload->to('routine/spread/code')->validate()->stream((string)$res['res'], $name);
  100. if ($uploadRes === false) {
  101. return app('json')->fail($upload->getError());
  102. }
  103. $imageInfo = $upload->getUploadInfo();
  104. $imageInfo['image_type'] = $uploadType;
  105. $systemAttachment->attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  106. $qrCode->setQrcodeFind($res['id'], ['status' => 1, 'url_time' => time(), 'qrcode_url' => $imageInfo['dir']]);
  107. $urlCode = $imageInfo['dir'];
  108. } else $urlCode = $imageInfo['att_dir'];
  109. if ($imageInfo['image_type'] == 1) $urlCode = $siteUrl . $urlCode;
  110. return app('json')->success(['url' => $urlCode]);
  111. }
  112. /**
  113. * 获取海报详细信息
  114. * @return mixed
  115. */
  116. public function getSpreadInfo(Request $request)
  117. {
  118. /** @var SystemConfigServices $systemConfigServices */
  119. $systemConfigServices = app()->make(SystemConfigServices::class);
  120. $spreadBanner = $systemConfigServices->getSpreadBanner() ?? [];
  121. $bannerCount = count($spreadBanner);
  122. $routineSpreadBanner = [];
  123. if ($bannerCount) {
  124. foreach ($spreadBanner as $item) {
  125. $routineSpreadBanner[] = ['pic' => $item];
  126. }
  127. }
  128. if (sys_config('share_qrcode', 0) && request()->isWechat()) {
  129. /** @var QrcodeServices $qrcodeService */
  130. $qrcodeService = app()->make(QrcodeServices::class);
  131. if (sys_config('spread_share_forever', 0)) {
  132. $qrcode = $qrcodeService->getForeverQrcode('spread', $request->uid())->url;
  133. } else {
  134. $qrcode = $qrcodeService->getTemporaryQrcode('spread', $request->uid())->url;
  135. }
  136. } else {
  137. $qrcode = '';
  138. }
  139. return app('json')->success([
  140. 'spread' => $routineSpreadBanner,
  141. 'qrcode' => $qrcode,
  142. 'nickname' => $request->user('nickname'),
  143. 'site_name' => sys_config('site_name')
  144. ]);
  145. }
  146. /**
  147. * 积分记录
  148. * @param Request $request
  149. * @return mixed
  150. * @throws \think\db\exception\DataNotFoundException
  151. * @throws \think\db\exception\ModelNotFoundException
  152. * @throws \think\exception\DbException
  153. */
  154. public function integral_list(Request $request)
  155. {
  156. $uid = (int)$request->uid();
  157. $data = $this->services->getIntegralList($uid);
  158. return app('json')->successful($data['list'] ?? []);
  159. }
  160. }