123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- <?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\TemplatesSource;
- use App\TemplatesSalers;
- use App\TemplatesSourceSalers;
- use App\Admin;
- use App\TemplatesLog;
- use App\CustTotal;
- use App\CustDetail;
- use App\Order;
- use App\Services\OssServices as oss;
- 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');
- $url = $request->server('HTTP_HOST');//获取请求地址域名 例如:seafood.726p.com
- if(!$t_id) $t_id = 1;
- //查询未离职销售
- $_open_ids = Admin::where('is_use', 1)->where('is_qrcode_use', 1)->lists('id');
- //获取背景图
- $source_info = TemplatesSource::where('id', $t_id)->first();
- $back_img = $source_info->back_img;
- $new_context = $source_info->new_context;
- #假如是自定义文章模板,获取html代码
- $is_html = $source_info->is_html;
- $article_html = null;
- if( $is_html == 1 ){
- $article_html = Templates::where('id', $source_info->t_id)->pluck('article_html');
- }
- $new_data = array();
- $title = '';
- if(!empty($new_context)){
- $new_context = json_decode($new_context, true);
- $title = $new_context['title'];
- $new_data[0]['text'] = $new_context['text1'];
- $new_data[0]['img'] = [$new_context['img1']];
- $new_data[1]['text'] = $new_context['text2'];
- $new_data[1]['img'] = [$new_context['img2_1'], $new_context['img2_2']];
- $new_data[2]['text'] = $new_context['text3'];
- $new_data[2]['img'] = [$new_context['img3']];
- }
- $result = TemplatesSourceSalers::where('s_id', $t_id)->where('is_del', 0)->where('weight','>',0)->whereIn('admin_id', $_open_ids)->orderBy('weight', 'asc')->lists('weight', 'admin_id');
- if(empty($result)){
- return self::returnValue([],1001);
- }
- $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();
- if($saler_id && $t_id !=17){
- //记录行为
- $log = array();
- $log['t_id'] = $t_id;
- $log['t_url'] = TemplatesSource::where('id', $t_id)->pluck('url');
- $log['admin_id'] = $saler_id;
- $log['ip'] = $ip;
- $log['idate'] = date('Y-m-d');
- $log['mdate'] = date('Y-m');
- $log['request_url'] = $url;
- 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, 'back_img'=>$back_img, 'title'=>$title, 'new_context'=>$new_data, 'is_html'=>$is_html, 'article_html'=>$article_html]);
- }
- /**
- * 获取真实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');
- $url = $request->server('HTTP_HOST');//获取请求地址域名 例如:seafood.726p.com
- $ip = $this->getRealIp();
- if($saler_id && $t_id && $t_id !=17){
- //记录行为
- $log = array();
- $log['t_id'] = $t_id;
- $log['t_url'] = TemplatesSource::where('id', $t_id)->pluck('url');
- $log['admin_id'] = $saler_id;
- $log['ip'] = $ip;
- $log['type'] = 2; //长按
- $log['idate'] = date('Y-m-d');
- $log['mdate'] = date('Y-m');
- $log['request_url'] = $url;
- TemplatesLog::insert($log);
- }
- return self::returnValue([]);
- }
- /**
- * 每日累计汇总数据
- */
- public function dayGrandTotal(Request $request){
- $days = (int)$request->input('days');
- if(!$days){
- $days = 7;
- }
- $stime = date('Y-m-d', strtotime('-'.$days.' day'));
- $data = array();
- $_start = '2019-09-04';
- //今日数据实时计算
- $idate = date('Y-m-d');
- $data['idate'] = $idate;
- //总投入
- $data['throw_cost'] = CustTotal::where('is_del',0)->where('dtime','>=',$_start)->sum('total_cost');
- //订单信息
- $order = Order::select(DB::raw('sum(cost) as goods_cost, sum(freight_cost) as freight_cost, sum(aftersale_fee) as aftersale_cost, count(1) as order_count, sum(receivedAmount) as order_amount, count(distinct(receiverMobile)) as cust_count, sum(refund_price) as refund_fee'))->where('createTime','>=',$_start)->where('is_del',0)->first();
- $data['goods_cost'] = $order->goods_cost;
- $data['freight_cost'] = $order->freight_cost;
- $data['aftersale_cost'] = $order->aftersale_cost;
- $data['refund_fee'] = $order->refund_fee;
- $data['order_count'] = $order->order_count;
- $data['order_amount'] = $order->order_amount;
- $data['cust_count'] = $order->cust_count;
- //加粉
- $data['fan_count'] = CustDetail::where('is_del',0)->where('dtime','>=',$_start)->sum('fan_add');
- //毛利 = 总销售额-总商品成本-总运费-总售后
- $data['profit'] = $order->order_amount - $order->goods_cost - $order->freight_cost - $order->aftersale_cost - $data['throw_cost'] + $order->refund_fee;
- $data['roi'] = $data['throw_cost']>0? round($order->order_amount / $data['throw_cost'], 4) : '';
- //总新粉单数
- $new_order = Order::select(DB::raw('count(1) as order_count'))->leftJoin('customers as cu','cu.phone', '=', 'order.receiverMobile')->whereRaw('left(order.createTime, 10) = cu.fanTime')->where('order.is_del', 0)->where('order.createTime','>=',$_start)->first();
- $data['new_order_count'] = $new_order->order_count;
- //总老粉单数
- $data['old_order_count'] = $data['order_count'] - $data['new_order_count'];
- //总复购订单
- $data['fugou_order_count'] = $data['order_count'] - $data['cust_count'];
- //总复购率
- $data['fugou_rate'] = $data['cust_count']>0 ? round($data['fugou_order_count'] / $data['cust_count'], 4) : '';
- //查询历史数据
- $result = DB::table('day_grand_total')->where('idate', '>', $stime)->orderBy('idate', 'desc')->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- $v['roi'] = $v['throw_cost']>0 ? round($v['order_amount'] / $v['throw_cost'], 4) : '';
- $v['fugou_rate'] = $v['cust_count']>0 ? round($v['fugou_order_count'] / $v['cust_count'], 4) : '';
- }
- $result = array_merge([$data], $result);
-
- exit(json_encode($result));
- }
- public function createArticleHtml(Request $request)
- {
- $article_html = $request->input('article_html');
- $note = $request->input('note');
- $host_str = $request->input('host_str');
- $host_info = parse_url($host_str);
- $host = $host_info['host'];
- $template = new Templates();
- $template->note = $note;
- $template->is_html = 1;
- $template->article_html = $article_html;
- $template->url = 'http://'.$host.'/seafood/articleTemplat/template1.html';
- $res = $template->save();
- return self::returnValue($res);
- }
- public function getArticleHtml(Request $request)
- {
- $id = (int)$request->input('id');
- $result = Templates::where('is_html', 1)->where(function($query) use($id){
- if($id) $query->where('id', $id);
- })->orderBy('id', 'desc')->get();
- return self::returnValue($result);
- }
- public function updateArticleHtml(Request $request)
- {
- $article_html = $request->input('article_html');
- $note = $request->input('note');
- $id = (int)$request->input('id');
- if( !$id ){
- return self::returnValue();
- }
- $template = Templates::where('id', $id)->first();
- $template->note = $note;
- $template->article_html = $article_html;
- $res = $template->save();
- return self::returnValue($res);
- }
- public function uploadHtmlPic(Request $request)
- {
- $imgBase64 = $request->input('img');
- if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $imgBase64, $res)) {
- //获取图片类型
- $type = $res[2];
- // 临时文件
- $tmpfname = tempnam("/image/", "FOO");
- //保存图片
- $handle = fopen($tmpfname, "w");
- if (fwrite($handle, base64_decode(str_replace($res[1], '', $imgBase64))))
- {
- $ossClient=new oss();
- // 上传阿里云
- $file = $ossClient->upload($type, $tmpfname, 'upload/seafood_template/'.date("Y-m-d",time()));
- $img=$file['oss-request-url'];
- #关闭缓存
- fclose($handle);
- #删除本地该图片
- unlink($tmpfname);
- return self::returnValue(['img'=>$img]);
- }
- }
- return self::returnValue();
- }
- }
|