Login.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace app\api\controller\v1;
  3. use app\hander\HelpHander;
  4. use think\Controller;
  5. class Login extends Controller
  6. {
  7. public function checkLogin(){
  8. $accountName = input('accountName','','trim');
  9. $password = input('password','','trim');
  10. if(!$accountName || !$password){
  11. HelpHander::error('账号密码不能为空');
  12. }
  13. $data = model('User')->login($accountName,$password);
  14. if(!$data){
  15. HelpHander::error(model('User')->getError());
  16. }
  17. HelpHander::success($data);
  18. }
  19. // 公共静态页链接
  20. public function staticUrl(){
  21. $data = [
  22. 'aboutUrl' => (string) url('/h5/index/sysarticle',['name'=>'about'],true,true),
  23. 'privacyUrl' => (string) url('/h5/index/sysarticle',['name'=>'privacy'],true,true),
  24. 'disclaimerUrl' =>(string) url('/h5/index/sysarticle',['name'=>'disclaimer'],true,true),
  25. ];
  26. HelpHander::success($data);
  27. }
  28. //发送验证码
  29. public function sendSms(){
  30. HelpHander::success([],'操作成功');
  31. }
  32. // 小程序手机号登录
  33. public function wxpLogin(){
  34. $code = input('code','','trim');
  35. $encryptedData = input('encryptedData','','trim');
  36. $iv = input('iv','','trim');
  37. $orgId = input('orgId/d',0);
  38. // $orgId = $orgId <= 0 ? 3 : $orgId;
  39. if($orgId <= 0){
  40. HelpHander::error('请扫描项目二维码进入小程序');
  41. }
  42. try{
  43. $config = config('app.wx_mini_config');
  44. $app = \EasyWeChat\Factory::miniProgram($config);
  45. $ret = $app->auth->session($code);
  46. if(!isset($ret['openid']) || !$ret['openid']){
  47. exception('登录失败');
  48. }
  49. $decryptedData = $app->encryptor->decryptData($ret['session_key'], $iv, $encryptedData);
  50. if(!isset($decryptedData['phoneNumber']) || !$decryptedData['phoneNumber']){
  51. exception('解密失败');
  52. }
  53. // $data = model('User')->wxLogin($ret['openid'],$decryptedData['nickName'],$decryptedData['avatarUrl'],$orgId);
  54. }catch (\Exception $e){
  55. trace('小程序登录:'.$e->getMessage());
  56. HelpHander::error('登录失败',-101);
  57. }
  58. $data = model('User')->wxpLogin($decryptedData['phoneNumber'],$orgId);
  59. if(!$data){
  60. HelpHander::error('登录失败',-101);
  61. }
  62. HelpHander::success($data);
  63. }
  64. // 小程序登录
  65. public function wxLogin(){
  66. $code = input('code','','trim');
  67. $encryptedData = input('encryptedData','','trim');
  68. $iv = input('iv','','trim');
  69. $orgId = input('orgId/d',0);
  70. // $orgId = $orgId <= 0 ? 3 : $orgId;
  71. if($orgId <= 0){
  72. HelpHander::error('请扫描项目二维码进入小程序');
  73. }
  74. try{
  75. $config = config('app.wx_mini_config');
  76. $app = \EasyWeChat\Factory::miniProgram($config);
  77. $ret = $app->auth->session($code);
  78. if(!isset($ret['openid']) || !$ret['openid']){
  79. exception('登录失败');
  80. }
  81. $decryptedData = $app->encryptor->decryptData($ret['session_key'], $iv, $encryptedData);
  82. if(!isset($decryptedData['nickName']) || !$decryptedData['nickName']){
  83. exception('解密失败');
  84. }
  85. $data = model('User')->wxLogin($ret['openid'],$decryptedData['nickName'],$decryptedData['avatarUrl'],$orgId);
  86. if(!$data){
  87. exception(model('User')->getError());
  88. }
  89. }catch (\Exception $e){
  90. trace('小程序登录:'.$e->getMessage());
  91. HelpHander::error('登录失败',-101);
  92. }
  93. HelpHander::success($data);
  94. }
  95. // 小程序预登陆
  96. public function wxPreLogin(){
  97. $code = input('code','','trim');
  98. $orgId = input('orgId/d',0);
  99. // $orgId = $orgId <= 0 ? 3 : $orgId;
  100. if($orgId <= 0){
  101. HelpHander::error('请扫描项目二维码');
  102. }
  103. $config = config('app.wx_mini_config');
  104. try{
  105. $app = \EasyWeChat\Factory::miniProgram($config);
  106. $ret = $app->auth->session($code);
  107. if(!isset($ret['openid']) || !$ret['openid']){
  108. exception('登录失败');
  109. }
  110. $data = model('User')->wxPreLogin($ret['openid'],$orgId);
  111. if(!$data){
  112. exception(model('User')->getError());
  113. }
  114. }catch (\Exception $e){
  115. trace($e->getMessage());
  116. HelpHander::error('登录失败'.$e->getMessage(),-101);
  117. }
  118. HelpHander::success($data);
  119. }
  120. public function wxRegister(){
  121. $data = [
  122. 'mobile' => input('phone','','trim'),
  123. 'real_name' => input('name','','trim'),
  124. 'password' => input('pass','','trim'),
  125. 'depId' => input('depId/d',0),
  126. 'orgId' => input('orgId/d',0)
  127. ];
  128. if($data['orgId'] <= 0){
  129. HelpHander::error('请扫描项目二维码');
  130. }
  131. $res = model('User')->wxRegister($data);
  132. if(!$res){
  133. HelpHander::error(model('User')->getError());
  134. }
  135. HelpHander::success($res);
  136. }
  137. }