No Description

TemplateController.php 2.9KB

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