菜谱项目

Base.php 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace App\Models;
  3. use App\User;
  4. use Illuminate\Database\Eloquent\Model;
  5. class Base extends Model
  6. {
  7. public static function formatValidator($validator)
  8. {
  9. return current($validator->errors()->messages())[0];
  10. }
  11. public static function getUserInfo()
  12. {
  13. $user_info = User::getCurrentUser();
  14. return $user_info;
  15. }
  16. public static function getUserId()
  17. {
  18. $user_info = self::getUserInfo();
  19. $user_id = $user_info->id;
  20. return $user_id;
  21. }
  22. public static function endDataPut($data)
  23. {
  24. if (empty($data)) {
  25. $data = (object) array();
  26. }
  27. return $data;
  28. }
  29. public static function formatImgUrl($data, $params = array())
  30. {
  31. if (!$params || empty($data)) {
  32. return $data;
  33. }
  34. foreach ($data as $item) {
  35. foreach ($params as $v) {
  36. $item->$v = env('IMAGE_URL') . $item->$v;
  37. }
  38. }
  39. return $data;
  40. }
  41. public static function getDevice()
  42. {
  43. $device = 1; //android
  44. $user_agent = $_SERVER['HTTP_USER_AGENT'];
  45. if (strpos($user_agent, 'iPhone') || strpos($user_agent, 'iphone')) {
  46. $device = 2; //ios
  47. }
  48. return $device;
  49. }
  50. }