Login.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace app\api\controller;
  3. use think\Db;
  4. use EasyWeChat\Factory;
  5. /**
  6. * @title 用户登录
  7. * @controller Login
  8. */
  9. class Login extends Base
  10. {
  11. /**
  12. * @title 微信登录(小程序)
  13. * @desc 微信登录(小程序)
  14. * @author qc
  15. * @url /api/Login/weChatLogin
  16. * @method POST
  17. * @tag 登录 授权
  18. * @param name:code type:int require:1 default:-- desc:code值
  19. * @return name:token type:string default:-- desc:用户登录成功后的token值
  20. */
  21. public function weChatLogin(){
  22. $code = input('code');
  23. $headimg = input('headimg','https://gaoyixia2.oss-cn-shanghai.aliyuncs.com/5e48b5b47f8f0770/55fdfd184a2b4c55.jpg');
  24. $name = input('name');
  25. if(empty($code) || empty($headimg) || empty($name)) $this->error('参数错误');
  26. $app = Factory::miniProgram(config('app.mini_program'));
  27. $data = $app->auth->session($code);
  28. if(empty($data['openid'])) $this->error($data['errmsg']);
  29. $member = Db::name('store_member')->field('id,phone')->where('openid',$data['openid'])->find();
  30. if(empty($member)){
  31. $member_data = [
  32. 'openid' => $data['openid'],
  33. 'headimg' => $headimg,
  34. 'create_at'=>date("Y-m-d H:i:s")
  35. ];
  36. Db::table('store_member')->insert($member_data);
  37. $uid = Db::getLastInsID();
  38. }else{
  39. $uid = $member['id'];
  40. }
  41. if(empty($uid)) $this->error('数据有误');
  42. $token =$this->createJwt($uid);
  43. $this->success('登录成功',['token'=>$token]);
  44. }
  45. }