common.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. }
  55. function array_to_vue($arr, $key = "", $value = "")
  56. {
  57. $res = [];
  58. foreach ($arr as $k => $v) {
  59. if (!empty($key)) {
  60. $res[] = ['text' => $v[$key], 'value' => $v[$value]];
  61. } else {
  62. $res[] = ['text' => $v, 'value' => $k];
  63. }
  64. }
  65. return json_encode($res);
  66. }