Controller.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Models\ReferralLog;
  4. use App\Http\Models\SensitiveWords;
  5. use App\Http\Models\UserBalanceLog;
  6. use Exception;
  7. use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
  8. use Illuminate\Foundation\Bus\DispatchesJobs;
  9. use Illuminate\Foundation\Validation\ValidatesRequests;
  10. use Illuminate\Routing\Controller as BaseController;
  11. class Controller extends BaseController
  12. {
  13. use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
  14. // 生成随机密码
  15. public function makePasswd()
  16. {
  17. exit(makeRandStr());
  18. }
  19. // 生成VmessId
  20. public function makeVmessId()
  21. {
  22. exit(createGuid());
  23. }
  24. // 生成网站安全码
  25. public function makeSecurityCode()
  26. {
  27. exit(strtolower(makeRandStr(8)));
  28. }
  29. // 类似Linux中的tail命令
  30. public function tail($file, $n, $base = 5)
  31. {
  32. $fileLines = $this->countLine($file);
  33. if($fileLines < 15000){
  34. return FALSE;
  35. }
  36. $fp = fopen($file, "r+");
  37. assert($n > 0);
  38. $pos = $n+1;
  39. $lines = [];
  40. while(count($lines) <= $n){
  41. try{
  42. fseek($fp, -$pos, SEEK_END);
  43. } catch(Exception $e){
  44. fseek(0);
  45. break;
  46. }
  47. $pos *= $base;
  48. while(!feof($fp)){
  49. array_unshift($lines, fgets($fp));
  50. }
  51. }
  52. return array_slice($lines, 0, $n);
  53. }
  54. /**
  55. * 计算文件行数
  56. */
  57. public function countLine($file)
  58. {
  59. $fp = fopen($file, "r");
  60. $i = 0;
  61. while(!feof($fp)){
  62. //每次读取2M
  63. if($data = fread($fp, 1024*1024*2)){
  64. //计算读取到的行数
  65. $num = substr_count($data, "\n");
  66. $i += $num;
  67. }
  68. }
  69. fclose($fp);
  70. return $i;
  71. }
  72. /**
  73. * 记录余额操作日志
  74. *
  75. * @param int $userId 用户ID
  76. * @param string $oid 订单ID
  77. * @param int $before 记录前余额
  78. * @param int $after 记录后余额
  79. * @param int $amount 发生金额
  80. * @param string $desc 描述
  81. *
  82. * @return int
  83. */
  84. public function addUserBalanceLog($userId, $oid, $before, $after, $amount, $desc = '')
  85. {
  86. $log = new UserBalanceLog();
  87. $log->user_id = $userId;
  88. $log->order_id = $oid;
  89. $log->before = $before;
  90. $log->after = $after;
  91. $log->amount = $amount;
  92. $log->desc = $desc;
  93. $log->created_at = date('Y-m-d H:i:s');
  94. return $log->save();
  95. }
  96. /**
  97. * 添加返利日志
  98. *
  99. * @param int $userId 用户ID
  100. * @param int $refUserId 返利用户ID
  101. * @param int $oid 订单ID
  102. * @param int $amount 发生金额
  103. * @param int $refAmount 返利金额
  104. *
  105. * @return int
  106. */
  107. public function addReferralLog($userId, $refUserId, $oid, $amount, $refAmount)
  108. {
  109. $log = new ReferralLog();
  110. $log->user_id = $userId;
  111. $log->ref_user_id = $refUserId;
  112. $log->order_id = $oid;
  113. $log->amount = $amount;
  114. $log->ref_amount = $refAmount;
  115. $log->status = 0;
  116. return $log->save();
  117. }
  118. // 获取敏感词
  119. public function sensitiveWords($type)
  120. {
  121. return SensitiveWords::query()->where('type',$type)->get()->pluck('words')->toArray();
  122. }
  123. // 将Base64图片转换为本地图片并保存
  124. function base64ImageSaver($base64_image_content)
  125. {
  126. // 匹配出图片的格式
  127. if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
  128. $type = $result[2];
  129. $directory = date('Ymd');
  130. $path = '/assets/images/qrcode/'.$directory.'/';
  131. if(!file_exists(public_path($path))){ // 检查是否有该文件夹,如果没有就创建,并给予最高权限
  132. mkdir(public_path($path), 0755, TRUE);
  133. }
  134. $fileName = makeRandStr(18, TRUE).".{$type}";
  135. if(file_put_contents(public_path($path.$fileName), base64_decode(str_replace($result[1], '', $base64_image_content)))){
  136. chmod(public_path($path.$fileName), 0744);
  137. return $path.$fileName;
  138. }else{
  139. return '';
  140. }
  141. }else{
  142. return '';
  143. }
  144. }
  145. }