control.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /**
  3. * mobile父类
  4. *
  5. *
  6. */
  7. //use Shopnc\Tpl;
  8. defined('InShopNC') or exit('Access Invalid!');
  9. require_once (BASE_ROOT_PATH . "/helper/statistics_helper.php");
  10. /********************************** 前台control父类 **********************************************/
  11. class mobileControl
  12. {
  13. //客户端类型
  14. protected $client_type_array = array('android', 'wap', 'wechat', 'ios', 'ajax', 'web');
  15. //列表默认分页数
  16. protected $page_size;
  17. protected $cur_page;
  18. //任务开始时间
  19. private static $startime = 0;
  20. public function __construct()
  21. {
  22. self::$startime = microtime(true);
  23. Language::read('mobile');
  24. //分页数处理
  25. if (is_numeric($_GET['page']) && intval(trim($_GET['page'])) > 0) {
  26. $this->page_size = intval(trim($_GET['page']));
  27. } else {
  28. $this->page_size = 20;
  29. }
  30. if (is_numeric($_GET['curpage']) && intval(trim($_GET['curpage'])) > 0) {
  31. $this->cur_page = intval(trim($_GET['curpage']));
  32. } else {
  33. $this->cur_page = 1;
  34. }
  35. $page = $this->cur_page;
  36. Log::record("curpage = {$page}",Log::DEBUG);
  37. initpage($this->page_size, $this->cur_page);
  38. $this->check_app_type();
  39. statistics_helper::instance()->add_call($_GET);
  40. }
  41. protected function android()
  42. {
  43. return $_SESSION['client_type'] == 'android';
  44. }
  45. protected function page_size()
  46. {
  47. return $this->page_size;
  48. }
  49. protected function page_no()
  50. {
  51. return $this->cur_page;
  52. }
  53. protected function pages($count)
  54. {
  55. return intval($count / $this->page_size()) + ($count % $this->page_size() == 0 ? 0 : 1);
  56. }
  57. private static function eclipse_time()
  58. {
  59. return (microtime(true) - self::$startime);
  60. }
  61. protected function check_app_type()
  62. {
  63. $_SESSION['is_app'] = false;
  64. $client = strtolower(trim($_SERVER['HTTP_CLIENT_TYPE']));
  65. $version = trim($_SERVER['HTTP_CLIENT_VERSION']);
  66. if (empty($client)) {
  67. $client = $_POST['client_type'];
  68. } else {
  69. $_SESSION['is_app'] = true;
  70. }
  71. if (empty($client) || !in_array($client, $this->client_type_array)) {
  72. $_SESSION['client_type'] = 'wap';
  73. } else {
  74. $_SESSION['client_type'] = $client;
  75. }
  76. if (!empty($version)) {
  77. $_SESSION['client_version'] = $version;
  78. }
  79. return true;
  80. }
  81. static public function outerr($code, $msg = '', $page = '',$type = NULL)
  82. {
  83. static $json_clients = array('android', 'ios');
  84. if(!is_null($type)) {
  85. $show_type = $type;
  86. } else {
  87. $show_type = $_SESSION['client_type'];
  88. }
  89. if (in_array($show_type, $json_clients))
  90. {
  91. joutput_error($code, $msg);
  92. }
  93. else if ($show_type == 'wap')
  94. {
  95. Tpl::output("error", $msg);
  96. if (!empty($page)) {
  97. Tpl::showpage($page);
  98. }
  99. }
  100. else if ($show_type == 'ajax')
  101. {
  102. $callback = $_GET['callback'];
  103. if(!isset($callback) || empty($callback)) {
  104. joutput_error($code, $msg);
  105. } else {
  106. echo "{$callback}(";
  107. joutput_error($code, $msg);
  108. echo ");";
  109. }
  110. }
  111. else
  112. {
  113. if(empty($msg)) {
  114. $msg = errcode::msg($code);
  115. }
  116. echo joutput_error($code, $msg, 'web') . "<br/>";
  117. echo sprintf("eclipse_time = %.6f <br/><br/>", self::eclipse_time());
  118. echo "性能关键统计:<br/><br/>";
  119. performance_helper::format_log();
  120. $sqls = Log::sql_log();
  121. echo "sql count = " . count($sqls) . "<br/><br/>";
  122. foreach ($sqls as $sql) {
  123. echo "{$sql}<br/>";
  124. }
  125. }
  126. return true;
  127. }
  128. static public function outsuccess($data, $page = '',$type = NULL)
  129. {
  130. static $json_clients = array('android', 'ios');
  131. if(!is_null($type)) {
  132. $show_type = $type;
  133. } else {
  134. $show_type = $_SESSION['client_type'];
  135. }
  136. if (in_array($show_type, $json_clients))
  137. {
  138. joutput_data($data);
  139. }
  140. else if ($show_type == 'wap')
  141. {
  142. if (is_array($data)) {
  143. foreach ($data as $key => $val) {
  144. Tpl::output($key, $val);
  145. }
  146. }
  147. if (!empty($page)) {
  148. Tpl::showpage($page);
  149. }
  150. }
  151. else if ($show_type == 'ajax')
  152. {
  153. $callback = $_GET['callback'];
  154. if(!isset($callback) || empty($callback)) {
  155. joutput_data($data);
  156. } else {
  157. echo "{$callback}(";
  158. joutput_data($data);
  159. echo ");";
  160. }
  161. }
  162. else
  163. {
  164. echo 'success: return data=<br/>';
  165. joutput_data($data, 'web');
  166. echo "<br/><br/>";
  167. echo sprintf("eclipse_time = %.6f <br/><br/>", self::eclipse_time());
  168. echo "性能关键统计:<br/><br/>";
  169. performance_helper::format_log();
  170. $sqls = Log::sql_log();
  171. echo "sql count = " . count($sqls) . "<br/><br/>";
  172. foreach ($sqls as $sql) {
  173. echo "{$sql}<br/>";
  174. }
  175. }
  176. return true;
  177. }
  178. public function separate_page($items,&$pages)
  179. {
  180. $arr_items = array_chunk($items,$this->page_size());
  181. $pages = count($arr_items);
  182. $page_no = $pages >= $this->page_no() ? $this->page_no() : $pages;
  183. return ($arr_items[$page_no - 1]);
  184. }
  185. }
  186. class mobileHomeControl extends mobileControl
  187. {
  188. public function __construct()
  189. {
  190. parent::__construct();
  191. }
  192. }
  193. /**
  194. * Class mbMemberControl
  195. */
  196. class mbMemberControl extends mobileControl
  197. {
  198. public $err_code = errcode::Success;
  199. public function __construct()
  200. {
  201. parent::__construct();
  202. if ($_SESSION['is_login'] != 1) {
  203. throw new UnloginException();
  204. }
  205. }
  206. }