control.php 5.8 KB

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