No Description

TemplateController.php 2.7KB

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