HomeController.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Modes\AccountLog;
  4. use App\Modes\BusinessGoal;
  5. use App\Modes\Config;
  6. use App\Modes\Proxy;
  7. use App\Modes\User;
  8. use App\Modes\Order;
  9. use App\Rules\Phone;
  10. use Carbon\Carbon;
  11. use Encore\Admin\Form\Field\Date;
  12. use Illuminate\Http\Request;
  13. use App\Http\Controllers\Controller;
  14. class HomeController extends Controller
  15. {
  16. /**
  17. * 获取收益
  18. * @author lyh
  19. * @date 2019/3/27
  20. * @param $uid
  21. * @param $start_time
  22. * @param $end_timel
  23. * @return mixed
  24. * @description
  25. * * 'uv' => $resItem['uv'], // pv代理收益次数
  26. * 'earnings' => $resItem['earnings'],// 总收益
  27. * 'inviteNum' => $resItem['proxyNum'], // 下级代理数量
  28. * 'proxyNum' => $resItem['proxyNum'], // 代理数量
  29. * 'proxyMoney' => $resItem['proxyMoney'], // 代理收益
  30. * 'adverNum' => $resItem['adverNum'], // 广告数量
  31. * 'adverMoney' => $resItem['adverMoney'] // 广告收益
  32. */
  33. private static function getEarnings($uid, $start_time, $end_time)
  34. {
  35. if (\Cache::has('getEarnings' . $start_time)) {
  36. return \Cache::get('getEarnings' . $start_time);
  37. }
  38. $type = [10, 11, 12, 13, 20, 21, 22, 23];
  39. \DB::connection()->enableQueryLog();
  40. $ids = User::getAllInvite($uid, null, $end_time, false);
  41. $inviteNum = count($ids);
  42. $uv = AccountLog::getUv($ids, $type, $start_time, $end_time);
  43. $earnings = AccountLog::getEarngings($ids, $type, $start_time, $end_time);
  44. $proxyNum = AccountLog::getInviteUv($ids, [20, 21, 22, 23], $start_time, $end_time, $ids);
  45. $proxyMoney = AccountLog::getEarngings($ids, [20, 21, 22, 23], $start_time, $end_time);
  46. $adverNum = AccountLog::getInviteUv($ids, [10, 11, 12, 13], $start_time, $end_time, $ids);
  47. $adverMoney = AccountLog::getEarngings($ids, [10, 11, 12, 13], $start_time, $end_time);
  48. $res = [
  49. 'uv' => $uv,
  50. 'earnings' => $earnings,
  51. 'inviteNum' => $inviteNum,
  52. 'proxyNum' => $proxyNum,
  53. 'proxyMoney' => $proxyMoney,
  54. 'adverNum' => $adverNum,
  55. 'adverMoney' => $adverMoney,
  56. 'uid' => $ids
  57. ];
  58. if (strtotime($start_time) + 86400 * 31 < time()) {
  59. \Cache::put('getEarnings' . $start_time, $res, 3600);
  60. }
  61. return $res;
  62. }
  63. public function getSms(Request $request)
  64. {
  65. $validator = \Validator::make($param = $request->post(), [
  66. 'mobile' => [
  67. 'required',
  68. // 'string',
  69. 'regex:/^[1][3,4,5,6,7,8,9][0-9]{9}$/',
  70. new Phone()
  71. ],
  72. 'type' => 'required|between:1,10'
  73. ]);
  74. if ($validator->fails()) {
  75. return showJson(102, $validator->errors()->first());
  76. }
  77. //检测用户的mobile是否存在
  78. if(!empty($param['send_type'])&&$param['send_type']>0){
  79. $user = User::whereMobile($param['mobile'])->first();
  80. if(empty($user) && $param['send_type']==1)return showJson('102','用户不存在,请扫码注册后再登录');
  81. if(empty($user) && $param['send_type']==2)return showJson('102','手机号不存在,请重新输入');
  82. }
  83. // var_dump('6666666666');exit;
  84. $smsProvider = \App::make('sms');
  85. $sms = $smsProvider->getSms($param['mobile'], 0);
  86. if ($sms == false) {
  87. return showJson(102, '获取短信验证码失败');
  88. }
  89. return showJsonSucc('获取验证码成功,请注意查收', [
  90. // 'sms' => $sms,
  91. ]);
  92. }
  93. /**
  94. * 收益记录
  95. * @author lyh
  96. * @date 2019/3/26
  97. * @description
  98. * 代理收益次数
  99. * 总收益
  100. * 下级代理数量
  101. * N 个 带来代理佣金总额
  102. * N 个 带来广告佣金收益总额
  103. */
  104. public function statistics(Request $request)
  105. {
  106. $res = [];
  107. $year = $request->post('year', 0);
  108. if ($year < 2018 && $year > 2040) {
  109. return showJsonErr('请选择查询的年份');
  110. }
  111. $month = $request->post('month', '');
  112. $day = $request->post('day', '');
  113. if (!empty($day)) {
  114. $time = [
  115. 'date' => "{$year}-{$month}-{$day}",
  116. 'start_time' => Carbon::create($year, $month, $day)->startOfDay(),
  117. 'end_time' => Carbon::create($year, $month, $day)->endOfDay()
  118. ];
  119. //获取下级总人数
  120. $chidsarr=User::getAllChilds(\Auth::id(),$time['end_time']);
  121. $resItem=AccountLog::getShouYi($chidsarr,$time['start_time'],$time['end_time']);
  122. $resItem['month']=$time['date'];
  123. $res[]=$resItem;
  124. return showJsonSucc(1001, $res);
  125. }
  126. //月
  127. if (!empty($month)) {
  128. $time = [
  129. 'date' => "{$year}-{$month}",
  130. 'start_time' => Carbon::create($year, $month)->firstOfMonth(),
  131. 'end_time' => Carbon::create($year, $month)->endOfMonth()
  132. ];
  133. //获取下级总人数
  134. $chidsarr=User::getAllChilds(\Auth::id(),$time['end_time']);
  135. $resItem=AccountLog::getShouYi($chidsarr,$time['start_time'],$time['end_time']);
  136. $resItem['month']=$time['date'];
  137. $res[]=$resItem;
  138. return showJsonSucc(1001, $res);
  139. }
  140. //年
  141. $time = [];
  142. $monthcoun=intval(date('m'));
  143. $curyear=intval(date('Y'));
  144. if($year<$curyear)$monthcoun=12;
  145. for ($i = 1; $i <= $monthcoun; $i++) {
  146. $time[] = [
  147. 'date' => "{$year}-{$i}",
  148. 'start_time' => Carbon::create($year, $i)->firstOfMonth(),
  149. 'end_time' => Carbon::create($year, $i)->subMonth(-1)->firstOfMonth()->subSecond(1)
  150. ];
  151. }
  152. //年
  153. foreach ($time as $item) {
  154. //获取下级总人数
  155. //$item['end_time']=date('Y-m-d H:i:S');
  156. //$item['start_time']='2019-07-01';
  157. $chidsarr=User::getAllChilds(\Auth::id(),$item['end_time']);
  158. $resItem=AccountLog::getShouYi($chidsarr,$item['start_time'],$item['end_time']);
  159. $resItem['month']=Carbon::instance($item['start_time'])->month;
  160. $res[]=$resItem;
  161. }
  162. return showJsonSucc(1001, $res);
  163. }
  164. /**
  165. * 任务进度轴
  166. * @author lyh
  167. * @date 2019/3/26
  168. * @description
  169. * edit by wsl
  170. * 20190628
  171. *
  172. */
  173. public function progress()
  174. {
  175. $goal = BusinessGoal::whereUid(\Auth::id())->orderByDesc('id')->paginate(perPage());
  176. //根据用户等级找到相关配置
  177. $userinfo=User::whereId(\Auth::id())->first()->toArray();
  178. if($userinfo['level']==7){
  179. $levels=7;
  180. }else $levels=$userinfo['level']+1;
  181. $setting=Proxy::getSettingByAreaAndLevel($levels,$userinfo['province'],$userinfo['city'],$userinfo['district']);
  182. //计算当月达到的任务量
  183. $tasks=$this->getCurTask($userinfo['id']);
  184. $goal = BusinessGoal::whereUid(\Auth::id())->orderByDesc('id')->paginate(perPage());
  185. $month = [];
  186. if ($goal->currentPage() == 1) {
  187. $month = [
  188. 'time' => date('Y-m'),
  189. 'goal' => $setting['upgrade_business_month'],
  190. 'actual' => round($tasks['month'],3),
  191. ];
  192. }
  193. return showJsonSucc(1001, [
  194. 'goal' => $setting['upgrade_business_year'], // 当前年度目标业绩
  195. 'actual' => round($tasks['year'],3),
  196. 'currentMonth' => $month,
  197. 'month' => $goal
  198. ]);
  199. }
  200. //计算年和月的任务总量,addbywsl,20190628
  201. function getCurTask($uid){
  202. $childarr=User::where('invitor','=',$uid)->get(['id']);
  203. $childarr=$childarr->toArray();
  204. $arr=[];
  205. if(!empty($childarr)){
  206. foreach ($childarr as $ke=>$ve){
  207. $arr[]=$ve['id'];
  208. }
  209. }
  210. // dd($arr);
  211. $montstat=Order::getMonthOrYearStat($uid,$arr,'month');
  212. $yearstat=Order::getMonthOrYearStat($uid,$arr,'year');
  213. return ['month'=>$montstat,'year'=>$yearstat];
  214. }
  215. }