StasticalController.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace App\Http\Controllers\Channel\XChengXu;
  3. use App\Modules\User\Services\UserService;
  4. use Illuminate\Http\Request;
  5. use App\Http\Controllers\Channel\BaseController;
  6. use Redis;
  7. use GuzzleHttp\Client;
  8. use Log;
  9. use App\Modules\User\Models\Xcxuser;
  10. use App\Modules\Subscribe\Models\Order;
  11. use App\Modules\User\Models\XcxUserStat;
  12. use App\Http\Controllers\Channel\XChengXu\Transformers\StasticalTransformer;
  13. class StasticalController extends BaseController
  14. {
  15. /**
  16. * @apiDefine XCX 小程序
  17. */
  18. public function sendLink(){
  19. $domain = _domain();
  20. $url = 'https://'.$domain.'/xcx/yun?channel_id='.$this->getChannelId();
  21. return response()->success(['link'=>$url]);
  22. }
  23. public function qrcode(Request $request){
  24. @ header("Content-Type:image/png");
  25. $channel_id = $request->get('channel_id');
  26. $uid = $request->has('uid')?$request->get('uid'):0;
  27. $u = $request->has('u')?$request->get('u'):0;
  28. echo $this->getQrcode($channel_id,$uid,$u);
  29. }
  30. public function page(Request $request){
  31. $channel_id = (int)$request->get('channel_id',14);
  32. $u = (int)$request->get('xcx_send_order_id',0);
  33. $redirect = $request->has('redirect')?$request->get('redirect'):'';
  34. $openid = $request->has('openid')?$request->get('openid'):'';
  35. if(!$redirect && empty($openid)){
  36. $redirect_url = 'https://'.env('CHANNEL_DOMAIN').'/xcx/yun?redirect=1&channel_id='.$channel_id;
  37. $appid = env('WECHAT_AUTH_APPID');
  38. $auth_url = env('AUTH_URL').'?gzh_app_id='.$appid.'&redirect_url='.urlencode($redirect_url);
  39. return redirect()->to($auth_url);
  40. }
  41. if(empty($channel_id)){
  42. $url = 'https://yueduyun.oss-cn-hangzhou.aliyuncs.com/distribution/xcx/img/qrcode.png';
  43. }else{
  44. $uid = $this->user($channel_id,$openid);
  45. $url = '/xcx/qrcode?channel_id='.$channel_id.'&uid='.$uid.'&u='.$u;
  46. }
  47. return view('channel.web.welcome.qrcode',['qrcode'=>$url]);
  48. }
  49. /**
  50. * @apiVersion 1.0.0
  51. * @apiDescription 今日数据
  52. * @api {get} xcx/todayData 今日数据
  53. * @apiGroup XCX
  54. * @apiName todayData
  55. * @apiSuccess {int} code 状态码
  56. * @apiSuccess {String} msg 信息
  57. * @apiSuccess {object} data 结果集
  58. * @apiSuccess {Int} data.today_recharge 今日充值
  59. * @apiSuccess {String} data.new_user_num 今日新增用户
  60. * @apiSuccess {Int} data.total_recharge 累计充值
  61. * @apiSuccess {Int} data.total_user_num 累计用户
  62. * @apiSuccessExample {json} Success-Response:
  63. * HTTP/1.1 200 OK
  64. * {
  65. * "code": 0,
  66. * "msg": "",
  67. * "data": {
  68. * "today_recharge": 5,
  69. * "new_user_num": "第1240章 不是我",
  70. * "total_recharge": 1239,
  71. * "total_user_num": 1,
  72. * }
  73. * }
  74. */
  75. public function todayData(Request $request){
  76. $channel_id = $this->getChannelId();
  77. $recharge = Order::where('status','PAID')
  78. ->where('pay_merchant_source','XIAOCHENGXU')
  79. ->where('created_at','>=',date('Y-m-d'))
  80. ->where('created_at','<=',date('Y-m-d',time()+86400))
  81. ->where('distribution_channel_id',$channel_id)
  82. ->sum('price');
  83. $total_recharge = Order::where('status','PAID')
  84. ->where('pay_merchant_source','XIAOCHENGXU')
  85. ->where('created_at','<',date('Y-m-d'))
  86. ->where('distribution_channel_id',$channel_id)
  87. ->sum('price');
  88. $new_user_num = Xcxuser::where('created_at','>=',date('Y-m-d'))
  89. ->where('created_at','<=',date('Y-m-d',time()+86400))
  90. ->where('distribution_channel_id',$channel_id)
  91. ->count();
  92. $total_user_num = Xcxuser::where('created_at','<',date('Y-m-d'))
  93. ->where('distribution_channel_id',$channel_id)
  94. ->count();
  95. $res = [
  96. 'recharge'=>$recharge,
  97. 'new_user_num'=>$new_user_num,
  98. 'total_recharge'=>$total_recharge,
  99. 'total_user_num'=>$total_user_num,
  100. ];
  101. return response()->success($res);
  102. }
  103. /**
  104. * @apiVersion 1.0.0
  105. * @apiDescription 统计数据
  106. * @api {get} xcx/statisticalData 统计数据
  107. * @apiGroup XCX
  108. * @apiName statisticalData
  109. * @apiParam {int} page 页码
  110. * @apiSuccess {int} code 状态码
  111. * @apiSuccess {String} msg 信息
  112. * @apiSuccess {object} data 结果集
  113. * @apiSuccess {Int} data.date 日期
  114. * @apiSuccess {Int} data.new_user_num 新增用户数
  115. * @apiSuccess {String} data.success_order_num 成功订单数
  116. * @apiSuccess {Int} data.order_num 订单数
  117. * @apiSuccess {Int} data.recharge 充值金额
  118. * @apiSuccessExample {json} Success-Response:
  119. * HTTP/1.1 200 OK
  120. * {
  121. * "code": 0,
  122. * "msg": "",
  123. * "data": [.
  124. * {
  125. * "date":'2018-03-01'
  126. * "new_user_num": 5,
  127. * "success_order_num": 12321,
  128. * "order_num": 1239,
  129. * "recharge": 1,
  130. * }
  131. * ]
  132. * }
  133. */
  134. public function statisticalData(Request $request){
  135. $page_size = $request->input('page_size',15);
  136. $channel_id = $this->getChannelId();
  137. $res = XcxUserStat::where('distribution_channel_id',$channel_id)
  138. ->select('date','new_user_num','success_order_num','order_num','recharge')
  139. ->orderBy('date','desc')
  140. ->paginate($page_size);
  141. return response()->pagination(new StasticalTransformer(), $res);
  142. }
  143. private function getQrcode($channel_id,$uid,$u){
  144. $appid = 'wxa0c8331eba3b34d5';
  145. $secret = '17c01c26dd7873e557710601436774c7';
  146. $key = "appid:".$appid.":accesstoken";
  147. $access_token = Redis::get($key);
  148. $client = new Client(['timeout' => 3.0,]);
  149. if(!$access_token){
  150. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
  151. try{
  152. $qrcode_url_res = $client->request('get',$url)->getBody()->getContents();
  153. if($qrcode_url_res){
  154. $qrcode_res = json_decode($qrcode_url_res,true);
  155. $access_token = $qrcode_res['access_token'];
  156. Redis::setex($key,7200,$access_token);
  157. }
  158. }catch (\Exception $e){
  159. }
  160. }
  161. $access_url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$access_token;
  162. $scence = '%s_%s_%s';
  163. $param = ['scene'=>sprintf($scence,$channel_id,$uid,$u)];
  164. $res = $this->send($access_url,$param);
  165. return $res;
  166. }
  167. static function send($url,$data)
  168. {
  169. //$headers = array("Content-type: application/json;charset=UTF-8","Accept: application/json","Cache-Control: no-cache", "Pragma: no-cache");
  170. $data=json_encode($data);
  171. $curl = curl_init();
  172. curl_setopt($curl, CURLOPT_URL, $url);
  173. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  174. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  175. if (!empty($data)){
  176. curl_setopt($curl, CURLOPT_POST, 1);
  177. curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
  178. }
  179. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  180. //curl_setopt( $curl, CURLOPT_HTTPHEADER, $headers );
  181. $output = curl_exec($curl);
  182. curl_close($curl);
  183. return $output;
  184. }
  185. private function user($channel_id,$openid){
  186. $user = UserService::getUserByUnionAndChannelId($openid,$channel_id);
  187. if($user){
  188. return $user->id;
  189. }
  190. $user = UserService::addUser([
  191. 'openid'=>$openid,
  192. 'unionid'=>$openid,
  193. 'distribution_channel_id'=>$channel_id
  194. ]);
  195. return $user->id;
  196. }
  197. }