common.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. function page_result($code = 0, $msg = '', $data = [])
  3. {
  4. $res = ['code' => $code, 'msg' => $msg, 'data' => $data];
  5. $response = \think\Response::create($res, 'json');
  6. throw new \think\exception\HttpResponseException($response);
  7. }
  8. // 应用公共文件
  9. function jump($msg = '', $url = null, $wait = 3)
  10. {
  11. if (is_null($url)) {
  12. $url = 'javascript:history.back(-1);';
  13. } else {
  14. $url = "location.href = '" . url($url) . "'";
  15. }
  16. $result = [
  17. 'msg' => $msg,
  18. 'url' => $url,
  19. 'wait' => $wait,
  20. ];
  21. $html = view('/public/jump', $result);
  22. throw new \think\exception\HttpResponseException($html);
  23. }
  24. function ajax_success($data = '')
  25. {
  26. $res = ['code' => 0, 'msg' => '成功', 'data' => $data];
  27. $response = \think\Response::create($res, 'json');
  28. throw new \think\exception\HttpResponseException($response);
  29. }
  30. function ajax_return($code = 0, $msg = '', $data = [])
  31. {
  32. $res = ['code' => $code, 'msg' => $msg, 'data' => $data];
  33. $response = \think\Response::create($res, 'json');
  34. throw new \think\exception\HttpResponseException($response);
  35. }
  36. function get_user_id()
  37. {
  38. $sessionUserId = session('mobile.user.id');
  39. if (empty($sessionUserId)) {
  40. session('back_url', request()->url());
  41. $response = redirect('/mobile/login/login');
  42. throw new \think\exception\HttpResponseException($response);
  43. }
  44. return $sessionUserId;
  45. }
  46. function get_user()
  47. {
  48. $sessionUserId = session('mobile.user.id');
  49. if (empty($sessionUserId)) {
  50. if (request()->isAjax()) {
  51. $res = ['code' => 401, 'msg' => '请登录'];
  52. $response = \think\Response::create($res, 'json');
  53. throw new \think\exception\HttpResponseException($response);
  54. } else {
  55. session('back_url', request()->url());
  56. $response = redirect('/mobile/login/login');
  57. throw new \think\exception\HttpResponseException($response);
  58. }
  59. }
  60. $user = \app\common\model\User::where('id', $sessionUserId)->find();
  61. if (empty($user)) {
  62. jump('该用户已删除');
  63. }
  64. return $user;
  65. }
  66. function get_broker()
  67. {
  68. $broker_id = session('mobile.broker.id');
  69. if (empty($broker_id)) {
  70. $user = get_user();
  71. $broker = \app\common\model\Broker::where('userid', $user['id'])->find();
  72. if (empty($broker)) {
  73. if (request()->isAjax()) {
  74. $res = ['code' => 401, 'msg' => '请登录'];
  75. $response = \think\Response::create($res, 'json');
  76. throw new \think\exception\HttpResponseException($response);
  77. } else {
  78. session('back_url', request()->url());
  79. $response = redirect('/mobile/login/login');
  80. throw new \think\exception\HttpResponseException($response);
  81. }
  82. }
  83. if ($broker['type'] == 1) {
  84. jump('仅限省外经纪人登录');
  85. }
  86. session('mobile.broker.id', $broker_id);
  87. } else {
  88. $broker = \app\common\model\Broker::where('id', $broker_id)->find();
  89. }
  90. return $broker;
  91. }