12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- namespace App\Http\Controllers\Api;
- use App\Http\Controllers\Controller;
- use Illuminate\Http\Request;
- use App\Log;
- use App\Templates;
- use App\TemplatesSalers;
- use App\Admin;
- use App\TemplatesLog;
- use Illuminate\Support\Facades\Hash;
- use Illuminate\Support\Facades\DB;
- class TemplateController extends Controller {
-
- public function salerQrcode(Request $request){
- $t_id = (int)$request->input('t_id');
- $result = TemplatesSalers::where('t_id', $t_id)->where('is_del', 0)->where('weight','>',0)->orderBy('weight', 'asc')->lists('weight', 'admin_id');
- $saler_id = null;
- $rand = 0;
-
- $max = array_sum($result) * 10;
-
- $rand_re = mt_rand(1, $max);
- foreach($result as $k=>$weight){
- $rand += $weight * 10;
- if($rand_re <= $rand){
- $saler_id = $k;
- break;
- }
- }
- $ip = $this->getRealIp();
-
- $log = array();
- $log['t_id'] = $t_id;
- $log['t_url'] = Templates::where('id', $t_id)->pluck('url');
- $log['admin_id'] = $saler_id;
- $log['ip'] = $ip;
- TemplatesLog::insert($log);
- $qrcode = Admin::where('id', $saler_id)->pluck('qrcode');
- return self::returnValue(['qrcode'=>$qrcode, 'ip'=>$ip, 'saler_id'=>$saler_id]);
- }
-
- public function getRealIp()
- {
- $ip=false;
- if(!empty($_SERVER["HTTP_CLIENT_IP"])){
- $ip = $_SERVER["HTTP_CLIENT_IP"];
- }
- if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
- $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
- if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
- for ($i = 0; $i < count($ips); $i++) {
- if (!preg_match ("/^(10│172.16│192.168)./", $ips[$i])) {
- $ip = $ips[$i];
- break;
- }
- }
- }
- return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
- }
-
- public function addLongLog(Request $request){
- $saler_id = $request->input('saler_id');
- $t_id = $request->input('t_id');
- $ip = $this->getRealIp();
-
- $log = array();
- $log['t_id'] = $t_id;
- $log['t_url'] = Templates::where('id', $t_id)->pluck('url');
- $log['admin_id'] = $saler_id;
- $log['ip'] = $ip;
- $log['type'] = 2;
- TemplatesLog::insert($log);
- return self::returnValue([]);
- }
- }
|