Нет описания

TemplateController.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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\TemplatesSource;
  14. use App\TemplatesSalers;
  15. use App\TemplatesSourceSalers;
  16. use App\Admin;
  17. use App\TemplatesLog;
  18. use App\CustTotal;
  19. use App\CustDetail;
  20. use App\Order;
  21. use App\Services\OssServices as oss;
  22. use Illuminate\Support\Facades\Hash;
  23. use Illuminate\Support\Facades\DB;
  24. class TemplateController extends Controller {
  25. /**
  26. * 模板匹配销售qrcode
  27. */
  28. public function salerQrcode(Request $request){
  29. $t_id = (int)$request->input('t_id');
  30. $url = $request->server('HTTP_HOST');//获取请求地址域名 例如:seafood.726p.com
  31. if(!$t_id) $t_id = 1;
  32. //查询未离职销售
  33. $_open_ids = Admin::where('is_use', 1)->where('is_qrcode_use', 1)->lists('id');
  34. //获取背景图
  35. $source_info = TemplatesSource::where('id', $t_id)->first();
  36. $back_img = $source_info->back_img;
  37. $new_context = $source_info->new_context;
  38. #假如是自定义文章模板,获取html代码
  39. $is_html = $source_info->is_html;
  40. $article_html = null;
  41. if( $is_html == 1 ){
  42. $article_html = Templates::where('id', $source_info->t_id)->pluck('article_html');
  43. }
  44. $new_data = array();
  45. $title = '';
  46. if(!empty($new_context)){
  47. $new_context = json_decode($new_context, true);
  48. $title = $new_context['title'];
  49. $new_data[0]['text'] = $new_context['text1'];
  50. $new_data[0]['img'] = [$new_context['img1']];
  51. $new_data[1]['text'] = $new_context['text2'];
  52. $new_data[1]['img'] = [$new_context['img2_1'], $new_context['img2_2']];
  53. $new_data[2]['text'] = $new_context['text3'];
  54. $new_data[2]['img'] = [$new_context['img3']];
  55. }
  56. $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');
  57. if(empty($result)){
  58. return self::returnValue([],1001);
  59. }
  60. $saler_id = null;
  61. $rand = 0;
  62. //获取最大随机值
  63. $max = array_sum($result) * 10;
  64. //随机一个值
  65. $rand_re = mt_rand(1, $max);
  66. foreach($result as $k=>$weight){
  67. $rand += $weight * 10;
  68. if($rand_re <= $rand){
  69. $saler_id = $k;
  70. break;
  71. }
  72. }
  73. $saler_ids = array_keys($result);
  74. $ip = $this->getRealIp();
  75. if($saler_id && $t_id !=17){
  76. //记录行为
  77. $log = array();
  78. $log['t_id'] = $t_id;
  79. $log['t_url'] = TemplatesSource::where('id', $t_id)->pluck('url');
  80. $log['admin_id'] = $saler_id;
  81. $log['ip'] = $ip;
  82. $log['idate'] = date('Y-m-d');
  83. $log['mdate'] = date('Y-m');
  84. $log['request_url'] = $url;
  85. TemplatesLog::insert($log);
  86. }
  87. $qrcode = Admin::where('id', $saler_id)->pluck('qrcode');
  88. $qrcodes = Admin::whereIn('id', $saler_ids)->lists('qrcode');
  89. 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]);
  90. }
  91. /**
  92. * 获取真实ip
  93. */
  94. public function getRealIp()
  95. {
  96. $ip=false;
  97. if(!empty($_SERVER["HTTP_CLIENT_IP"])){
  98. $ip = $_SERVER["HTTP_CLIENT_IP"];
  99. }
  100. if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  101. $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
  102. if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
  103. for ($i = 0; $i < count($ips); $i++) {
  104. if (!preg_match ("/^(10│172.16│192.168)./", $ips[$i])) {
  105. $ip = $ips[$i];
  106. break;
  107. }
  108. }
  109. }
  110. return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
  111. }
  112. /**
  113. * 长按用户存日志
  114. */
  115. public function addLongLog(Request $request){
  116. $saler_id = $request->input('saler_id');
  117. $t_id = $request->input('t_id');
  118. $url = $request->server('HTTP_HOST');//获取请求地址域名 例如:seafood.726p.com
  119. $ip = $this->getRealIp();
  120. if($saler_id && $t_id && $t_id !=17){
  121. //记录行为
  122. $log = array();
  123. $log['t_id'] = $t_id;
  124. $log['t_url'] = TemplatesSource::where('id', $t_id)->pluck('url');
  125. $log['admin_id'] = $saler_id;
  126. $log['ip'] = $ip;
  127. $log['type'] = 2; //长按
  128. $log['idate'] = date('Y-m-d');
  129. $log['mdate'] = date('Y-m');
  130. $log['request_url'] = $url;
  131. TemplatesLog::insert($log);
  132. }
  133. return self::returnValue([]);
  134. }
  135. /**
  136. * 每日累计汇总数据
  137. */
  138. public function dayGrandTotal(Request $request){
  139. $days = (int)$request->input('days');
  140. if(!$days){
  141. $days = 7;
  142. }
  143. $stime = date('Y-m-d', strtotime('-'.$days.' day'));
  144. $data = array();
  145. $_start = '2019-09-04';
  146. //今日数据实时计算
  147. $idate = date('Y-m-d');
  148. $data['idate'] = $idate;
  149. //总投入
  150. $data['throw_cost'] = CustTotal::where('is_del',0)->where('dtime','>=',$_start)->sum('total_cost');
  151. //订单信息
  152. $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();
  153. $data['goods_cost'] = $order->goods_cost;
  154. $data['freight_cost'] = $order->freight_cost;
  155. $data['aftersale_cost'] = $order->aftersale_cost;
  156. $data['refund_fee'] = $order->refund_fee;
  157. $data['order_count'] = $order->order_count;
  158. $data['order_amount'] = $order->order_amount;
  159. $data['cust_count'] = $order->cust_count;
  160. //加粉
  161. $data['fan_count'] = CustDetail::where('is_del',0)->where('dtime','>=',$_start)->sum('fan_add');
  162. //毛利 = 总销售额-总商品成本-总运费-总售后
  163. $data['profit'] = $order->order_amount - $order->goods_cost - $order->freight_cost - $order->aftersale_cost - $data['throw_cost'] + $order->refund_fee;
  164. $data['roi'] = $data['throw_cost']>0? round($order->order_amount / $data['throw_cost'], 4) : '';
  165. //总新粉单数
  166. $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();
  167. $data['new_order_count'] = $new_order->order_count;
  168. //总老粉单数
  169. $data['old_order_count'] = $data['order_count'] - $data['new_order_count'];
  170. //总复购订单
  171. $data['fugou_order_count'] = $data['order_count'] - $data['cust_count'];
  172. //总复购率
  173. $data['fugou_rate'] = $data['cust_count']>0 ? round($data['fugou_order_count'] / $data['cust_count'], 4) : '';
  174. //查询历史数据
  175. $result = DB::table('day_grand_total')->where('idate', '>', $stime)->orderBy('idate', 'desc')->get();
  176. $result = json_decode(json_encode($result), true);
  177. foreach($result as $k=>&$v){
  178. $v['roi'] = $v['throw_cost']>0 ? round($v['order_amount'] / $v['throw_cost'], 4) : '';
  179. $v['fugou_rate'] = $v['cust_count']>0 ? round($v['fugou_order_count'] / $v['cust_count'], 4) : '';
  180. }
  181. $result = array_merge([$data], $result);
  182. exit(json_encode($result));
  183. }
  184. public function createArticleHtml(Request $request)
  185. {
  186. $article_html = $request->input('article_html');
  187. $note = $request->input('note');
  188. $host_str = $request->input('host_str');
  189. $host_info = parse_url($host_str);
  190. $host = $host_info['host'];
  191. $template = new Templates();
  192. $template->note = $note;
  193. $template->is_html = 1;
  194. $template->article_html = $article_html;
  195. $template->url = 'http://'.$host.'/seafood/articleTemplat/template1.html';
  196. $res = $template->save();
  197. return self::returnValue($res);
  198. }
  199. public function getArticleHtml(Request $request)
  200. {
  201. $id = (int)$request->input('id');
  202. $result = Templates::where('is_html', 1)->where(function($query) use($id){
  203. if($id) $query->where('id', $id);
  204. })->orderBy('id', 'desc')->get();
  205. return self::returnValue($result);
  206. }
  207. public function updateArticleHtml(Request $request)
  208. {
  209. $article_html = $request->input('article_html');
  210. $note = $request->input('note');
  211. $id = (int)$request->input('id');
  212. if( !$id ){
  213. return self::returnValue();
  214. }
  215. $template = Templates::where('id', $id)->first();
  216. $template->note = $note;
  217. $template->article_html = $article_html;
  218. $res = $template->save();
  219. return self::returnValue($res);
  220. }
  221. public function uploadHtmlPic(Request $request)
  222. {
  223. $imgBase64 = $request->input('img');
  224. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $imgBase64, $res)) {
  225. //获取图片类型
  226. $type = $res[2];
  227. // 临时文件
  228. $tmpfname = tempnam("/image/", "FOO");
  229. //保存图片
  230. $handle = fopen($tmpfname, "w");
  231. if (fwrite($handle, base64_decode(str_replace($res[1], '', $imgBase64))))
  232. {
  233. $ossClient=new oss();
  234. // 上传阿里云
  235. $file = $ossClient->upload($type, $tmpfname, 'upload/seafood_template/'.date("Y-m-d",time()));
  236. $img=$file['oss-request-url'];
  237. #关闭缓存
  238. fclose($handle);
  239. #删除本地该图片
  240. unlink($tmpfname);
  241. return self::returnValue(['img'=>$img]);
  242. }
  243. }
  244. return self::returnValue();
  245. }
  246. }