Login.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\validate\UserVali;
  4. use app\common\model\User;
  5. use app\common\model\UserWallet;
  6. use library\service\CaptchaService;
  7. use library\tools\Data;
  8. use think\Db;
  9. use EasyWeChat\Factory;
  10. use app\common\model\InviteInfo;
  11. use function AlibabaCloud\Client\value;
  12. /**
  13. * @title 用户登录
  14. * @controller Login
  15. */
  16. class Login extends Base
  17. {
  18. /**
  19. * @title 用户统一登录
  20. * @desc 用户登录
  21. * @author qc
  22. * @url /api/Login/unifiedLogin
  23. * @method POST
  24. * @tag 登录 授权
  25. * @param name:account_type type:int require:1 default:1 desc:账号类型1企业2个人
  26. * @param name:account type:int require:1 default:-- desc:账号
  27. * @param name:code type:int require:1 default:-- desc:验证码
  28. * @return name:token type:string default:-- desc:用户登录成功后的token值
  29. */
  30. public function unifiedLogin()
  31. {
  32. $code = input('post.code','');
  33. $account = input('post.account','');
  34. $account_type = input('post.account_type', 1);
  35. $ret_data = ['code' => 200, 'token' => ''];
  36. $msg = '登录成功';
  37. try {
  38. $check_code = $this->checkPhoneCode($account,$code);
  39. if(!$check_code) $this->exception('验证码错误');
  40. $this->updatePhoneCode($check_code);
  41. $where = [];
  42. $where[] = $account_type == 1 ? ['email','=',$account] : ['phone','=',$account];
  43. $user_info = User::where($where)->find();
  44. if(!$user_info) {
  45. $reg_data = [];
  46. $reg_data['account_type'] = $account_type;
  47. $account_type == 1 ? $reg_data['email'] = $account : $ret_data['phone'] = $account;
  48. $account_type == 1 ? $reg_data['name'] = 'G企业用户_'.explode(',',$account)[0] : 'G'. substr_replace($account,'****',3,4);
  49. $user_info = User::create($reg_data);
  50. }
  51. $ret_data['token'] = $this->createJwt($user_info->id);;
  52. }catch (\Exception $e){
  53. $ret_data['code'] = 201;
  54. $msg =$e->getMessage();
  55. }
  56. $ret_data['code'] == 200 ? $this->success($msg,$ret_data):$this->error($msg,$ret_data);
  57. }
  58. /**
  59. * @title 获取验证码
  60. * @desc 获取验证码
  61. * @author qc
  62. * @url /api/Login/getCaptcha
  63. * @method GET
  64. * @return name:image type:string default:-- desc:图片
  65. * @return name:uniqid type:string default:-- desc:uniqid
  66. */
  67. public function getCaptcha()
  68. {
  69. $image = CaptchaService::instance();
  70. $captcha = ['image' => $image->getData(), 'uniqid' => $image->getUniqid()];
  71. $this->success('生成验证码成功', $captcha);
  72. }
  73. }