Login.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. namespace app\worker\controller;
  3. use app\common\model\MessageTemplate;
  4. use app\common\model\Worker as WorkerModel;
  5. use Aliyun\DySDKLite\Sms\SendSms;
  6. use app\common\service\SmsService;
  7. use App\Http\Controllers\Sdk\GeetestLib;
  8. use think\facade\Session;
  9. use think\facade\Db;
  10. class Login
  11. {
  12. public function index()
  13. {
  14. return view('login/login', [
  15. 'domain' => request()->domain(),
  16. ]);
  17. }
  18. public function login()
  19. {
  20. $mobile = trim(input('mobile'));
  21. $yzm = trim(input('yzm'));
  22. $data = Db::name('worker')
  23. ->where('mobile', $mobile)
  24. ->find();
  25. if (empty($data)) {
  26. exit(json_encode([
  27. 'code' => 1,
  28. 'msg' => '该手机号未注册,请先注册',
  29. ]));
  30. }
  31. if (!env('APP_DEBUG')) {
  32. $yzm_code = Session::get('yzm_code');
  33. if ($yzm_code != $yzm) {
  34. exit(json_encode([
  35. 'code' => 1,
  36. 'msg' => '手机验证码错误',
  37. ]));
  38. }
  39. }
  40. $workeradmin = WorkerModel::where(['mobile' => $mobile])->findOrEmpty()->toArray();
  41. session('access_worker', $workeradmin);
  42. $res['code'] = 0;
  43. return $res;
  44. }
  45. //验证手机号
  46. public function yz_mobile()
  47. {
  48. $mobile = trim(input('mobile'));
  49. if ($mobile) {
  50. $data = Db::name('worker')
  51. ->where('mobile', $mobile)
  52. ->where('status', '<>', 2)
  53. ->find();
  54. if (empty($data)) {
  55. $data = Db::name('worker')
  56. ->where('mobile', $mobile)
  57. ->find();
  58. }
  59. if (empty($data)) {
  60. $code = 1;
  61. $msg = '该手机号未注册,请先注册';
  62. } elseif ($data['status'] == 2) {
  63. $code = 1;
  64. $msg = '该账号已经被禁用,请联系管理员';
  65. } else {
  66. $code = 0;
  67. $msg = '该手机号正确';
  68. }
  69. exit(json_encode([
  70. 'code' => $code,
  71. 'msg' => $msg,
  72. ]));
  73. } else {
  74. $msg = '请填写正确的手机号';
  75. }
  76. exit(json_encode([
  77. 'code' => 1,
  78. 'msg' => $msg,
  79. ]));
  80. }
  81. public function jy()
  82. {
  83. include("../extend/jy/geetest_config.php");
  84. include("../extend/jy/GeetestLib.php");
  85. $GtSdk = new \App\Http\Controllers\Sdk\GeetestLib(GEETEST_ID, GEETEST_KEY);
  86. $get_ip = get_client_ip();
  87. $user_id = 'login';
  88. $digestmod = "md5";
  89. $params = [
  90. "digestmod" => $digestmod,
  91. "user_id" => $user_id, # 网站用户id
  92. "client_type" => "web", #web:电脑上的浏览器;h5:手机上的浏览器,包括移动应用内完全内置的web_view;native:通过原生SDK植入APP应用的方式
  93. "ip_address" => $get_ip, # 请在此处传输用户请求验证时所携带的IP
  94. ];
  95. // $status = $GtSdk->pre_process($data, 1);
  96. $result = $GtSdk->register($digestmod, $params);
  97. session(GeetestLib::GEETEST_SERVER_STATUS_SESSION_KEY, $result->getStatus());
  98. session("userId", $user_id);
  99. return $result->getData();
  100. }
  101. //短息验证码 发送
  102. public function yzm()
  103. {
  104. Session::delete('yzm_code');
  105. $rand = rand(000001, 999999);
  106. $mobile = trim(input('post.mobile'));
  107. $preg_phone = '/^1[34578]\d{9}$/ims';
  108. $rtn = [];
  109. if (!$mobile) {
  110. $rtn['code'] = 1;
  111. $rtn['message'] = '手机号为空';
  112. } else {
  113. if (preg_match($preg_phone, $mobile)) {
  114. $sms = new SmsService();
  115. $res = $sms->send($mobile, 'verification', [$rand]);
  116. if ($res['code'] == 0) {
  117. $rtn['code'] = 0;
  118. Session::set('yzm_code', $rand);
  119. } else {
  120. $rtn['code'] = 1;
  121. $rtn['message'] = '网络故障,请重试';
  122. }
  123. } else {
  124. $rtn['code'] = 1;
  125. $rtn['message'] = '手机号格式不正确';
  126. }
  127. }
  128. return $rtn;
  129. }
  130. //验证码 验证
  131. public function yzm_verify()
  132. {
  133. $mobile = trim(input('mobile'));
  134. $data = Db::name('worker')
  135. ->where('mobile', $mobile)
  136. ->find();
  137. $yzm = (int)trim(input('yzm'));
  138. $yzm_code = Session::get('yzm_code');
  139. $res = [];
  140. if ($data['status'] == 2) {
  141. $res['code'] = 1;
  142. $res['message'] = '该账号已经被禁用,请联系管理员';
  143. } elseif (empty($data)) {
  144. $res['code'] = 1;
  145. $res['message'] = '该手机号未注册,请先注册';
  146. } else {
  147. if ($yzm == $yzm_code) {
  148. $workeradmin = WorkerModel::where(['mobile' => $mobile])->findOrEmpty()->toArray();
  149. session('access_worker', $workeradmin);
  150. $res['code'] = 0;
  151. echo json_encode($res);
  152. } else {
  153. $res['code'] = 1;
  154. $res['message'] = '验证码不正确';
  155. echo json_encode($res);
  156. }
  157. }
  158. }
  159. public function change()
  160. {
  161. $id = input('id',0);
  162. if (empty($id)) {
  163. session(null);
  164. return redirect(url('/login/index'));
  165. }
  166. //用户不存在
  167. $workeradmin = WorkerModel::where(['id' => $id])->findOrEmpty()->toArray();
  168. if (empty($workeradmin)) {
  169. session(null);
  170. return redirect(url('/login/index'));
  171. }
  172. //非常登录其他用户
  173. $access_worker = session('access_worker');
  174. if ($workeradmin['userid'] != $access_worker['userid']) {
  175. session(null);
  176. return redirect(url('/login/index'));
  177. }
  178. //登录成功
  179. session('access_worker', $workeradmin);
  180. return redirect(url('/home/index'));
  181. }
  182. public function logout()
  183. {
  184. session(null);
  185. return redirect(url('/login/index'));
  186. }
  187. }