common.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. // 应用公共文件
  3. function jump($msg = '', $url = null, $wait = 3)
  4. {
  5. if (is_null($url)) {
  6. $url = 'javascript:history.back(-1);';
  7. } else {
  8. $url = "location.href = '" . url($url) . "'";
  9. }
  10. $result = [
  11. 'msg' => $msg,
  12. 'url' => $url,
  13. 'wait' => $wait,
  14. ];
  15. $html = view('/public/jump', $result);
  16. throw new \think\exception\HttpResponseException($html);
  17. }
  18. function ajax_success($data)
  19. {
  20. $res = ['code' => 0, 'msg' => '成功', 'data' => $data];
  21. $response = \think\Response::create($res, 'json');
  22. throw new \think\exception\HttpResponseException($response);
  23. }
  24. function get_user()
  25. {
  26. $sessionUserId = session('mobile.user.id');
  27. if (empty($sessionUserId)) {
  28. if (request()->isAjax()) {
  29. $res = ['code' => 401, 'msg' => '请登录'];
  30. $response = \think\Response::create($res, 'json');
  31. throw new \think\exception\HttpResponseException($response);
  32. } else {
  33. session('back_url',request()->url());
  34. $response = redirect('/mobile/login/login');
  35. throw new \think\exception\HttpResponseException($response);
  36. }
  37. }
  38. $user = \app\common\model\UserModel::where('id',$sessionUserId)->hidden(['password','salt'])->find();
  39. if (empty($user)) {
  40. jump('该用户已删除');
  41. }
  42. if ($user['status'] != \app\common\model\UserModel::STATUS_PASS) {
  43. jump('该账号暂时无法使用');
  44. }
  45. return $user;
  46. }
  47. function get_user_id()
  48. {
  49. $sessionUserId = session('mobile.user.id');
  50. if (empty($sessionUserId)) {
  51. return 0;
  52. }
  53. return $sessionUserId;
  54. }