123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Apiistrator
- * Date: 2018-02-22
- * Time: 10:32
- */
- 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 {
- /**
- * 模板匹配销售qrcode
- */
- 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;
- }
- }
- $saler_ids = array_keys($result);
-
- $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');
- $qrcodes = Admin::whereIn('id', $saler_ids)->lists('qrcode');
- return self::returnValue(['qrcode'=>$qrcode, 'ip'=>$ip, 'saler_id'=>$saler_id, 'qrcodes'=>$qrcodes]);
- }
- /**
- * 获取真实ip
- */
- 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([]);
- }
- }
|