UsersV2Controller.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. namespace App\Http\Controllers\Wap\Oauth;
  3. use App\Modules\User\Services\WapReaderPageFissionService;
  4. use Illuminate\Http\Request;
  5. use App\Http\Controllers\Controller;
  6. use Cookie;
  7. use App\Modules\User\Services\UserService;
  8. use Log;
  9. use EasyWeChat\Foundation\Application;
  10. use DB;
  11. use Hashids;
  12. use Redis;
  13. class UsersV2Controller extends Controller
  14. {
  15. public function user_oauth(Request $request){
  16. Log::info('UsersV2Controller--user_oauth--enter-----------');
  17. Log::info($request->all());
  18. $url = $request->get('redirect_url');
  19. $appid = $request->get('gzh_app_id');
  20. $channel_id = $request->get('channel_id');
  21. $sid = $request->get('sid');
  22. $secter = $this->getAppSecretByAppId($appid);
  23. $params['appid'] = $appid;
  24. $params['redirect_url'] = $url;
  25. $params['channel_id'] = $channel_id;
  26. $params['sid'] = $sid;
  27. $options = [
  28. 'app_id'=>$appid,
  29. 'secret'=>$secter,
  30. 'oauth' => [
  31. 'scopes' => ['snsapi_base'],
  32. 'callback' => env('AUTH_CALLBACK_URL').'?'.http_build_query($params),
  33. ],
  34. ];
  35. Log::info('$options is:');
  36. Log::info($options);
  37. $app = new Application($options);
  38. return $app->oauth->redirect();
  39. }
  40. public function wcCallbackParse(Request $request){
  41. Log::info('UsersV2Controller--wcCallbackParse--enter-----------');
  42. Log::info($request->all());
  43. $appid = $request->get('appid');
  44. $secret = $this->getAppSecretByAppId($appid);
  45. $url = $request->get('redirect_url');
  46. $distribution_channel_id = $request->get('channel_id');
  47. $send_order_id = $request->get('sid');
  48. $url = urldecode($url);
  49. $options = [
  50. 'app_id'=>$appid,
  51. 'secret'=>$secret,
  52. ];
  53. $url_info = parse_url($url);
  54. $app = new Application($options);
  55. $user = null;
  56. try{
  57. $user = $app->oauth->user();
  58. }catch (\Exception $e){
  59. Log::error('------------wcCallbackParse---------error');
  60. Log::error($e);
  61. Log::error('error appid is: '.$appid);
  62. Log::error(json_encode($request->all()));
  63. Log::error('------------wcCallbackParse---------error end');
  64. }
  65. $user_data = $user['original'];
  66. Log::info('$user_data is: ');
  67. Log::info($user_data);
  68. if(!isset($user_data['unionid'])){
  69. $user_data['unionid'] = $user_data['openid'];
  70. }
  71. $openid = $user_data['openid'];
  72. $unionid = $user_data['unionid'];
  73. $user = UserService::getUserByUnionAndChannelId($openid,$distribution_channel_id);
  74. !isset($url_info['path']) && $url_info['path'] = '/';
  75. $rp = [];
  76. $ip = '';
  77. if(isset($url_info['query'])){
  78. parse_str($url_info['query'],$rp);
  79. if(isset($rp['ip'])){
  80. $ip = $rp['ip'];
  81. unset($rp['ip']);
  82. }
  83. }
  84. if(!$user){
  85. $user = $this->createUser($openid,$unionid,$distribution_channel_id,$send_order_id,$ip);
  86. $from = $request->get('from');
  87. if($from && $from == 'xiyue' && $distribution_channel_id == 6985){
  88. $this->xiyueUser($user->id,$openid);
  89. }
  90. }
  91. $query = ['auth_uid'=>$user->id,'atime'=>time()];
  92. $sign = get_sign($query);
  93. $query['sign'] = $sign;
  94. if($rp) $query = array_merge($query,$rp);
  95. $url = sprintf('%s://%s%s?%s',$url_info['scheme'],$url_info['host'],$url_info['path'],http_build_query($query));
  96. Log::info('wcCallbackParse back url is :');
  97. Log::info($url);
  98. if($send_order_id){
  99. try{
  100. Redis::hset('book_read:' . $user->id, 'send_order_id', $send_order_id);
  101. }catch (\Exception $e){}
  102. }
  103. //$previous = url()->previous();
  104. return redirect()->to($url)->withHeaders(['referer'=>url()->current()]);
  105. }
  106. public function wcCallbackParse2(Request $request){
  107. Log::info('UsersV2Controller--wcCallbackParse--enter-----------');
  108. Log::info($request->all());
  109. $appid = $request->get('appid');
  110. $secret = $this->getAppSecretByAppId($appid);
  111. $url = $request->get('redirect_url');
  112. $distribution_channel_id = $request->get('channel_id');
  113. $send_order_id = $request->get('sid');
  114. $auth_times = $request->get('auth_times',1);
  115. $url = urldecode($url);
  116. $options = [
  117. 'app_id'=>$appid,
  118. 'secret'=>$secret,
  119. ];
  120. $url_info = parse_url($url);
  121. $app = new Application($options);
  122. $user = null;
  123. try{
  124. $user = $app->oauth->user();
  125. }catch (\Exception $e){
  126. Log::error('----------------wcCallbackParse2 error----------------');
  127. Log::error($e);
  128. Log::error('error appid is: '.$appid);
  129. Log::error(json_encode($request->all()));
  130. Log::error('----------------wcCallbackParse2 error------end----------');
  131. }
  132. //$user = $app->oauth->user();
  133. $user_data = $user['original'];
  134. Log::info('$user_data is: ');
  135. Log::info($user_data);
  136. if(!isset($user_data['unionid'])){
  137. $user_data['unionid'] = $user_data['openid'];
  138. }
  139. $openid = $user_data['openid'];
  140. $unionid = $user_data['unionid'];
  141. $user = UserService::getUserByUnionAndChannelId($openid,$distribution_channel_id);
  142. !isset($url_info['path']) && $url_info['path'] = '/';
  143. $ip = '';
  144. $rp = [];
  145. if(isset($url_info['query'])){
  146. parse_str($url_info['query'],$rp);
  147. if(isset($rp['ip'])){
  148. $ip = $rp['ip'];
  149. unset($rp['ip']);
  150. }
  151. }
  152. if(!$user){
  153. $auth_redirect_two_appids = specialChannelAuthInfo();
  154. if($auth_times == 1 && $auth_redirect_two_appids && isset($auth_redirect_two_appids[$distribution_channel_id])){
  155. $params['redirect_url'] = $request->get('redirect_url');
  156. $params['timestamp'] = time();
  157. $params['channel_id'] = $distribution_channel_id;
  158. $params['appid'] = $auth_redirect_two_appids[$distribution_channel_id];
  159. $params['sid'] = $send_order_id;
  160. $params['auth_times'] = 2;
  161. //$params['sign'] = $this->getSign($params, env('OAUTH_KEY'));
  162. $secret = $this->getAppSecretByAppId($auth_redirect_two_appids[$distribution_channel_id]);
  163. $options = [
  164. 'app_id' => $auth_redirect_two_appids[$distribution_channel_id],
  165. 'secret' => $secret,
  166. 'oauth' => [
  167. 'scopes' => ['snsapi_base'],
  168. 'callback' => env('AUTH_CALLBACK_URL_V2_2') . '?' . http_build_query($params),],];
  169. $app = new Application($options);
  170. return $app->oauth->redirect();
  171. }else{
  172. $user = $this->createUser($openid,$unionid,$distribution_channel_id,$send_order_id,$ip);
  173. }
  174. }
  175. $query = ['auth_uid'=>$user->id,'atime'=>time()];
  176. $sign = get_sign($query);
  177. $query['sign'] = $sign;
  178. if($rp) $query = array_merge($query,$rp);
  179. $url = sprintf('%s://%s%s?%s',$url_info['scheme'],$url_info['host'],$url_info['path'],http_build_query($query));
  180. Log::info('wcCallbackParse back url is :');
  181. Log::info($url);
  182. if($send_order_id){
  183. try{
  184. Redis::hset('book_read:' . $user->id, 'send_order_id', $send_order_id);
  185. }catch (\Exception $e){}
  186. }
  187. //$previous = url()->previous();
  188. return redirect()->to($url);
  189. }
  190. private function createUser($openid,$unionid,$distribution_channel_id,$send_order_id,$ip){
  191. $user = null;
  192. try{
  193. $user = UserService::addUser(
  194. ['openid' => $openid,
  195. 'unionid' => $unionid,
  196. 'distribution_channel_id' =>$distribution_channel_id,
  197. 'send_order_id'=>$send_order_id,
  198. 'is_new'=>1,
  199. 'register_ip'=>$ip
  200. ]);
  201. }catch (\Exception $e){
  202. myLog('user-error')->info($e);
  203. }
  204. // 注册动作
  205. $action_type = 'Register';
  206. $param = [
  207. 'openid' => $openid,
  208. 'uid' => isset($user->id)?$user->id:'0',
  209. ];
  210. UserService::PushUserActionToQueue($action_type,$distribution_channel_id,$param);
  211. return $user;
  212. }
  213. /**
  214. * 根据apid获取secret
  215. * @param $appId
  216. * @return string
  217. */
  218. private function getAppSecretByAppId($appId){
  219. $info = DB::table('official_setting')->where('appid',$appId)->select('secret')->first();
  220. if($info){
  221. return $info->secret;
  222. }
  223. return '';
  224. }
  225. private function xiyueUser($uid,$openid){
  226. try{
  227. DB::table('xiyue_zsy_users')->insert([
  228. 'uid'=>$uid,
  229. 'xiyue_uid'=>0,
  230. 'openid'=>$openid,
  231. 'created_at'=>date('Y-m-d H:i:s'),
  232. 'updated_at'=>date('Y-m-d H:i:s')
  233. ]);
  234. }catch (\Exception $e){}
  235. }
  236. }