Oauth.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace app\api\controller\h5;
  3. use app\hander\HelpHander;
  4. use think\Controller;
  5. use think\Db;
  6. use think\Exception;
  7. class Oauth extends Controller
  8. {
  9. public function checkCode(){
  10. try{
  11. $type = input('type/d',1); //1=微信公众号 2=微信小城序 3=陪护小程序
  12. $code = input('code','','trim');
  13. $openid = "";
  14. $avatar = "";
  15. $nickname = "";
  16. if($type == 1){
  17. $config = config('app.wx_config');
  18. $app = \EasyWeChat\Factory::officialAccount($config);
  19. $user = $app->oauth->user();
  20. $openid = $user->id;
  21. $avatar = $user->avatar;
  22. $nickname = $user->nickname;
  23. }else if($type == 2){
  24. $config = config('app.wx_mini_config');
  25. $app = \EasyWeChat\Factory::miniProgram($config);
  26. $ret = $app->auth->session($code);
  27. if(!isset($ret['openid']) || !$ret['openid']){
  28. throw new Exception('登录失败');
  29. }
  30. $openid = $ret['openid'];
  31. }else if($type == 3){
  32. $config = config('app.wx_mini_config_ph');
  33. $app = \EasyWeChat\Factory::miniProgram($config);
  34. $ret = $app->auth->session($code);
  35. if(!isset($ret['openid']) || !$ret['openid']){
  36. throw new Exception('登录失败');
  37. }
  38. $openid = $ret['openid'];
  39. }
  40. if(!$openid){
  41. \exception('参数错误');
  42. }
  43. $info = Db::name('wxuser')->where('type',$type)->where('openid',$openid)->find();
  44. if($info){
  45. $token = create_token($info['id']);
  46. $userId = $info['id'];
  47. if($info['del'] != 0||$info['enable'] != 1){
  48. \exception('用户被禁用,请联系管理员');
  49. }
  50. $ret = Db::name('wxuser')->where('id',$info['id'])->update([
  51. 'nickname' => $nickname,
  52. 'token' => $token,
  53. 'img' => $avatar,
  54. 'update_time' => date('Y-m-d H:i:s')
  55. ]);
  56. if(!$ret){
  57. \exception('登录失败');
  58. }
  59. }else{
  60. $ret = Db::name('wxuser')->insertGetId([
  61. 'openid' => $openid,
  62. 'nickname' => $nickname,
  63. 'img' => $avatar,
  64. 'type' => $type,
  65. 'create_time' => date('Y-m-d H:i:s'),
  66. 'update_time' => date('Y-m-d H:i:s')
  67. ]);
  68. if(!$ret){
  69. \exception('注册失败');
  70. }
  71. $userId = $ret;
  72. $token = create_token($userId);
  73. $ret = Db::name('wxuser')->where('id',$userId)->update([
  74. 'token' => $token,
  75. ]);
  76. if(!$ret){
  77. \exception('登录失败');
  78. }
  79. }
  80. }catch (Exception $e){
  81. trace('微信登录:'.$e->getMessage());
  82. HelpHander::error('登录失败');
  83. }
  84. HelpHander::success(['token'=>$token,'user_id' => $userId]);
  85. }
  86. // 获取jssdk参数
  87. public function jssdk(){
  88. try{
  89. $url = input('url');
  90. $config = config('app.wx_config');
  91. $app = \EasyWeChat\Factory::officialAccount($config);
  92. $apis = ['checkJsApi','chooseWXPay','hideOptionMenu'];
  93. $app->jssdk->setUrl(urldecode($url));
  94. $ret = $app->jssdk->buildConfig($apis, false, false, false);
  95. trace($ret);
  96. }catch (\Exception $e){
  97. trace('获取分享参数:'.$e->getMessage());
  98. HelpHander::error('获取jssdk配置失败');
  99. }
  100. HelpHander::success($ret);
  101. }
  102. }