control.php 6.0 KB

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