123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- namespace app\api\controller\v1;
- use app\hander\HelpHander;
- use think\Controller;
- class Login extends Controller
- {
- public function checkLogin(){
- $accountName = input('accountName','','trim');
- $password = input('password','','trim');
- if(!$accountName || !$password){
- HelpHander::error('账号密码不能为空');
- }
- $data = model('User')->login($accountName,$password);
- if(!$data){
- HelpHander::error(model('User')->getError());
- }
- HelpHander::success($data);
- }
- // 公共静态页链接
- public function staticUrl(){
- $data = [
- 'aboutUrl' => (string) url('/h5/index/sysarticle',['name'=>'about'],true,true),
- 'privacyUrl' => (string) url('/h5/index/sysarticle',['name'=>'privacy'],true,true),
- 'disclaimerUrl' =>(string) url('/h5/index/sysarticle',['name'=>'disclaimer'],true,true),
- ];
- HelpHander::success($data);
- }
- //发送验证码
- public function sendSms(){
- HelpHander::success([],'操作成功');
- }
- // 小程序手机号登录
- public function wxpLogin(){
- $code = input('code','','trim');
- $encryptedData = input('encryptedData','','trim');
- $iv = input('iv','','trim');
- $orgId = input('orgId/d',0);
- // $orgId = $orgId <= 0 ? 3 : $orgId;
- if($orgId <= 0){
- HelpHander::error('请扫描项目二维码进入小程序');
- }
- try{
- $config = config('app.wx_mini_config');
- $app = \EasyWeChat\Factory::miniProgram($config);
- $ret = $app->auth->session($code);
- if(!isset($ret['openid']) || !$ret['openid']){
- exception('登录失败');
- }
- $decryptedData = $app->encryptor->decryptData($ret['session_key'], $iv, $encryptedData);
- if(!isset($decryptedData['phoneNumber']) || !$decryptedData['phoneNumber']){
- exception('解密失败');
- }
- // $data = model('User')->wxLogin($ret['openid'],$decryptedData['nickName'],$decryptedData['avatarUrl'],$orgId);
- }catch (\Exception $e){
- trace('小程序登录:'.$e->getMessage());
- HelpHander::error('登录失败',-101);
- }
- $data = model('User')->wxpLogin($decryptedData['phoneNumber'],$orgId);
- if(!$data){
- HelpHander::error('登录失败',-101);
- }
- HelpHander::success($data);
- }
- // 小程序登录
- public function wxLogin(){
- $code = input('code','','trim');
- $encryptedData = input('encryptedData','','trim');
- $iv = input('iv','','trim');
- $orgId = input('orgId/d',0);
- // $orgId = $orgId <= 0 ? 3 : $orgId;
- if($orgId <= 0){
- HelpHander::error('请扫描项目二维码进入小程序');
- }
- try{
- $config = config('app.wx_mini_config');
- $app = \EasyWeChat\Factory::miniProgram($config);
- $ret = $app->auth->session($code);
- if(!isset($ret['openid']) || !$ret['openid']){
- exception('登录失败');
- }
- $decryptedData = $app->encryptor->decryptData($ret['session_key'], $iv, $encryptedData);
- if(!isset($decryptedData['nickName']) || !$decryptedData['nickName']){
- exception('解密失败');
- }
- $data = model('User')->wxLogin($ret['openid'],$decryptedData['nickName'],$decryptedData['avatarUrl'],$orgId);
- if(!$data){
- exception(model('User')->getError());
- }
- }catch (\Exception $e){
- trace('小程序登录:'.$e->getMessage());
- HelpHander::error('登录失败',-101);
- }
- HelpHander::success($data);
- }
- // 小程序预登陆
- public function wxPreLogin(){
- $code = input('code','','trim');
- $orgId = input('orgId/d',0);
- // $orgId = $orgId <= 0 ? 3 : $orgId;
- if($orgId <= 0){
- HelpHander::error('请扫描项目二维码');
- }
- $config = config('app.wx_mini_config');
- try{
- $app = \EasyWeChat\Factory::miniProgram($config);
- $ret = $app->auth->session($code);
- if(!isset($ret['openid']) || !$ret['openid']){
- exception('登录失败');
- }
- $data = model('User')->wxPreLogin($ret['openid'],$orgId);
- if(!$data){
- exception(model('User')->getError());
- }
- }catch (\Exception $e){
- trace($e->getMessage());
- HelpHander::error('登录失败'.$e->getMessage(),-101);
- }
- HelpHander::success($data);
- }
- public function wxRegister(){
- $data = [
- 'mobile' => input('phone','','trim'),
- 'real_name' => input('name','','trim'),
- 'password' => input('pass','','trim'),
- 'depId' => input('depId/d',0),
- 'orgId' => input('orgId/d',0)
- ];
- if($data['orgId'] <= 0){
- HelpHander::error('请扫描项目二维码');
- }
- $res = model('User')->wxRegister($data);
- if(!$res){
- HelpHander::error(model('User')->getError());
- }
- HelpHander::success($res);
- }
- }
|