Няма описание

TemplateController.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Apiistrator
  5. * Date: 2018-02-22
  6. * Time: 10:32
  7. */
  8. namespace App\Http\Controllers\Api;
  9. use App\Http\Controllers\Controller;
  10. use Illuminate\Http\Request;
  11. use App\Log;
  12. use App\Templates;
  13. use App\TemplatesSource;
  14. use App\TemplatesSalers;
  15. use App\TemplatesSourceSalers;
  16. use App\Admin;
  17. use App\TemplatesLog;
  18. use Illuminate\Support\Facades\Hash;
  19. use Illuminate\Support\Facades\DB;
  20. class TemplateController extends Controller {
  21. /**
  22. * 模板匹配销售qrcode
  23. */
  24. public function salerQrcode(Request $request){
  25. $t_id = (int)$request->input('t_id');
  26. if(!$t_id) $t_id = 1;
  27. //获取背景图
  28. $back_img = TemplatesSource::where('id', $t_id)->pluck('back_img');
  29. $result = TemplatesSourceSalers::where('s_id', $t_id)->where('is_del', 0)->where('weight','>',0)->orderBy('weight', 'asc')->lists('weight', 'admin_id');
  30. if(empty($result)){
  31. return self::returnValue([],1001);
  32. }
  33. $saler_id = null;
  34. $rand = 0;
  35. //获取最大随机值
  36. $max = array_sum($result) * 10;
  37. //随机一个值
  38. $rand_re = mt_rand(1, $max);
  39. foreach($result as $k=>$weight){
  40. $rand += $weight * 10;
  41. if($rand_re <= $rand){
  42. $saler_id = $k;
  43. break;
  44. }
  45. }
  46. $saler_ids = array_keys($result);
  47. $ip = $this->getRealIp();
  48. if($saler_id){
  49. //记录行为
  50. $log = array();
  51. $log['t_id'] = $t_id;
  52. $log['t_url'] = TemplatesSource::where('id', $t_id)->pluck('url');
  53. $log['admin_id'] = $saler_id;
  54. $log['ip'] = $ip;
  55. TemplatesLog::insert($log);
  56. }
  57. $qrcode = Admin::where('id', $saler_id)->pluck('qrcode');
  58. $qrcodes = Admin::whereIn('id', $saler_ids)->lists('qrcode');
  59. return self::returnValue(['qrcode'=>$qrcode, 'ip'=>$ip, 'saler_id'=>$saler_id, 'qrcodes'=>$qrcodes, 'back_img'=>$back_img]);
  60. }
  61. /**
  62. * 获取真实ip
  63. */
  64. public function getRealIp()
  65. {
  66. $ip=false;
  67. if(!empty($_SERVER["HTTP_CLIENT_IP"])){
  68. $ip = $_SERVER["HTTP_CLIENT_IP"];
  69. }
  70. if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  71. $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
  72. if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
  73. for ($i = 0; $i < count($ips); $i++) {
  74. if (!preg_match ("/^(10│172.16│192.168)./", $ips[$i])) {
  75. $ip = $ips[$i];
  76. break;
  77. }
  78. }
  79. }
  80. return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
  81. }
  82. /**
  83. * 长按用户存日志
  84. */
  85. public function addLongLog(Request $request){
  86. $saler_id = $request->input('saler_id');
  87. $t_id = $request->input('t_id');
  88. $ip = $this->getRealIp();
  89. if($saler_id && $t_id){
  90. //记录行为
  91. $log = array();
  92. $log['t_id'] = $t_id;
  93. $log['t_url'] = TemplatesSource::where('id', $t_id)->pluck('url');
  94. $log['admin_id'] = $saler_id;
  95. $log['ip'] = $ip;
  96. $log['type'] = 2; //长按
  97. TemplatesLog::insert($log);
  98. }
  99. return self::returnValue([]);
  100. }
  101. }