Oauth.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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=微信小城序
  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. }
  32. if(!$openid){
  33. \exception('参数错误');
  34. }
  35. $info = Db::name('wxuser')->where('type',$type)->where('openid',$openid)->find();
  36. if($info){
  37. $token = create_token($info['id']);
  38. $userId = $info['id'];
  39. if($info['del'] != 0||$info['enable'] != 1){
  40. \exception('用户被禁用,请联系管理员');
  41. }
  42. $ret = Db::name('wxuser')->where('id',$info['id'])->update([
  43. 'nickname' => $nickname,
  44. 'token' => $token,
  45. 'img' => $avatar,
  46. 'update_time' => date('Y-m-d H:i:s')
  47. ]);
  48. if(!$ret){
  49. \exception('登录失败');
  50. }
  51. }else{
  52. $ret = Db::name('wxuser')->insertGetId([
  53. 'openid' => $openid,
  54. 'nickname' => $nickname,
  55. 'img' => $avatar,
  56. 'type' => $type,
  57. 'create_time' => date('Y-m-d H:i:s'),
  58. 'update_time' => date('Y-m-d H:i:s')
  59. ]);
  60. if(!$ret){
  61. \exception('注册失败');
  62. }
  63. $userId = $ret;
  64. $token = create_token($userId);
  65. $ret = Db::name('wxuser')->where('id',$userId)->update([
  66. 'token' => $token,
  67. ]);
  68. if(!$ret){
  69. \exception('登录失败');
  70. }
  71. }
  72. }catch (Exception $e){
  73. trace('微信登录:'.$e->getMessage());
  74. HelpHander::error('登录失败');
  75. }
  76. HelpHander::success(['token'=>$token,'user_id' => $userId]);
  77. }
  78. // 获取jssdk参数
  79. public function jssdk(){
  80. try{
  81. $url = input('url');
  82. $config = config('app.wx_config');
  83. $app = \EasyWeChat\Factory::officialAccount($config);
  84. $apis = ['checkJsApi','chooseWXPay','hideOptionMenu'];
  85. $app->jssdk->setUrl(urldecode($url));
  86. $ret = $app->jssdk->buildConfig($apis, false, false, false);
  87. trace($ret);
  88. }catch (\Exception $e){
  89. trace('获取分享参数:'.$e->getMessage());
  90. HelpHander::error('获取jssdk配置失败');
  91. }
  92. HelpHander::success($ret);
  93. }
  94. }