No Description

TemplateController.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. $saler_id = null;
  31. $rand = 0;
  32. //获取最大随机值
  33. $max = array_sum($result) * 10;
  34. //随机一个值
  35. $rand_re = mt_rand(1, $max);
  36. foreach($result as $k=>$weight){
  37. $rand += $weight * 10;
  38. if($rand_re <= $rand){
  39. $saler_id = $k;
  40. break;
  41. }
  42. }
  43. $saler_ids = array_keys($result);
  44. $ip = $this->getRealIp();
  45. if($saler_id){
  46. //记录行为
  47. $log = array();
  48. $log['t_id'] = $t_id;
  49. $log['t_url'] = TemplatesSource::where('id', $t_id)->pluck('url');
  50. $log['admin_id'] = $saler_id;
  51. $log['ip'] = $ip;
  52. TemplatesLog::insert($log);
  53. }
  54. $qrcode = Admin::where('id', $saler_id)->pluck('qrcode');
  55. $qrcodes = Admin::whereIn('id', $saler_ids)->lists('qrcode');
  56. return self::returnValue(['qrcode'=>$qrcode, 'ip'=>$ip, 'saler_id'=>$saler_id, 'qrcodes'=>$qrcodes, 'back_img'=>$back_img]);
  57. }
  58. /**
  59. * 获取真实ip
  60. */
  61. public function getRealIp()
  62. {
  63. $ip=false;
  64. if(!empty($_SERVER["HTTP_CLIENT_IP"])){
  65. $ip = $_SERVER["HTTP_CLIENT_IP"];
  66. }
  67. if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  68. $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
  69. if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
  70. for ($i = 0; $i < count($ips); $i++) {
  71. if (!preg_match ("/^(10│172.16│192.168)./", $ips[$i])) {
  72. $ip = $ips[$i];
  73. break;
  74. }
  75. }
  76. }
  77. return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
  78. }
  79. /**
  80. * 长按用户存日志
  81. */
  82. public function addLongLog(Request $request){
  83. $saler_id = $request->input('saler_id');
  84. $t_id = $request->input('t_id');
  85. $ip = $this->getRealIp();
  86. if($saler_id && $t_id){
  87. //记录行为
  88. $log = array();
  89. $log['t_id'] = $t_id;
  90. $log['t_url'] = TemplatesSource::where('id', $t_id)->pluck('url');
  91. $log['admin_id'] = $saler_id;
  92. $log['ip'] = $ip;
  93. $log['type'] = 2; //长按
  94. TemplatesLog::insert($log);
  95. }
  96. return self::returnValue([]);
  97. }
  98. }