AppClient.php 3.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * APP 客户端
  4. * Created by PhpStorm.
  5. * User: guanxl
  6. * Date: 2018/5/24
  7. * Time: 15:09
  8. */
  9. namespace App\Libs;
  10. class AppClient
  11. {
  12. const BROWSER_TYPE_WECHAT = 'wechat';
  13. const BROWSER_TYPE_WESUITAPP = 'wesuit_app';
  14. const BROWSER_TYPE_CMB_MOBILE_OA = 'cmb_mobile_oa';
  15. const BROWSER_TYPE_PINGAN_ZHINIAO = 'pingan_zhiniao';
  16. const BROWSER_TYPE_YUNZHIJIA = 'yunzhijia';
  17. const BROWSER_TYPE_DINGTALK = 'dingtalk';
  18. const BROWSER_TYPE_KARA = 'kara';
  19. const BROWSER_TYPE_NOT_WX = '';
  20. /**
  21. * 得到APP client的类型
  22. * @return string
  23. */
  24. public static function browserType() {
  25. if(strpos($_SERVER['HTTP_USER_AGENT'], 'wxwork') !== false) {
  26. return self::BROWSER_TYPE_WESUITAPP;
  27. } elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'CMBMobileOA') !== false) {
  28. return self::BROWSER_TYPE_CMB_MOBILE_OA;
  29. } elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'PinganZhiniao') !== false) {
  30. return self::BROWSER_TYPE_PINGAN_ZHINIAO;
  31. } elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
  32. return self::BROWSER_TYPE_WECHAT;
  33. } elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Qing') !== false) {
  34. return self::BROWSER_TYPE_YUNZHIJIA;
  35. } elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'DingTalk') !== false) {
  36. return self::BROWSER_TYPE_DINGTALK;
  37. } elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'kara') !== false) {
  38. return self::BROWSER_TYPE_KARA;
  39. } else {
  40. return self::BROWSER_TYPE_NOT_WX;
  41. }
  42. }
  43. /**
  44. * 是否微信访问
  45. * @return bool
  46. */
  47. public static function isWxBrowser() {
  48. return (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) ||
  49. (strpos($_SERVER['HTTP_USER_AGENT'], 'wxwork') !== false);
  50. }
  51. // 查看是否为手机端的方法
  52. //判断是手机登录还是电脑登录
  53. public static function isMobile() {
  54. // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
  55. if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
  56. return true;
  57. //此条摘自TPM智能切换模板引擎,适合TPM开发
  58. if(isset ($_SERVER['HTTP_CLIENT']) &&'PhoneClient'==$_SERVER['HTTP_CLIENT'])
  59. return true;
  60. //如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
  61. if (isset ($_SERVER['HTTP_VIA']))
  62. //找不到为flase,否则为true
  63. return stristr($_SERVER['HTTP_VIA'], 'wap') ? true : false;
  64. //判断手机发送的客户端标志,兼容性有待提高
  65. if (isset ($_SERVER['HTTP_USER_AGENT'])) {
  66. $clientkeywords = array(
  67. 'nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile'
  68. );
  69. //从HTTP_USER_AGENT中查找手机浏览器的关键字
  70. if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
  71. return true;
  72. }
  73. }
  74. //协议法,因为有可能不准确,放到最后判断
  75. if (isset ($_SERVER['HTTP_ACCEPT'])) {
  76. // 如果只支持wml并且不支持html那一定是移动设备
  77. // 如果支持wml和html但是wml在html之前则是移动设备
  78. if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
  79. return true;
  80. }
  81. }
  82. return false;
  83. }
  84. }