control.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. public function __destruct()
  42. {
  43. //unset($_SESSION['is_app']);
  44. }
  45. protected function android()
  46. {
  47. return $_SESSION['client_type'] == 'android';
  48. }
  49. protected function page_size()
  50. {
  51. return $this->page_size;
  52. }
  53. protected function page_no()
  54. {
  55. return $this->cur_page;
  56. }
  57. protected function pages($count)
  58. {
  59. return intval($count / $this->page_size()) + ($count % $this->page_size() == 0 ? 0 : 1);
  60. }
  61. private static function eclipse_time()
  62. {
  63. return (microtime(true) - self::$startime);
  64. }
  65. protected function check_app_type()
  66. {
  67. $client = strtolower(trim($_SERVER['HTTP_CLIENT_TYPE']));
  68. $version = trim($_SERVER['HTTP_CLIENT_VERSION']);
  69. if (empty($client)) {
  70. $client = $_POST['client_type'];
  71. } else {
  72. $_SESSION['is_app'] = true;
  73. }
  74. if (empty($client) || !in_array($client, $this->client_type_array)) {
  75. $_SESSION['client_type'] = 'wap';
  76. } else {
  77. $_SESSION['client_type'] = $client;
  78. }
  79. if (!empty($version)) {
  80. $_SESSION['client_version'] = $version;
  81. }
  82. return true;
  83. }
  84. static public function outerr($code, $msg = '', $page = '',$type = NULL)
  85. {
  86. static $json_clients = array('android', 'ios');
  87. if(!is_null($type)) {
  88. $show_type = $type;
  89. } else {
  90. $show_type = $_SESSION['client_type'];
  91. }
  92. if (in_array($show_type, $json_clients))
  93. {
  94. joutput_error($code, $msg);
  95. }
  96. else if ($show_type == 'wap')
  97. {
  98. Tpl::clear();
  99. Tpl::output("error", $msg);
  100. if (!empty($page)) {
  101. Tpl::showpage($page);
  102. }
  103. }
  104. else if ($show_type == 'ajax')
  105. {
  106. $callback = $_GET['callback'];
  107. if(!isset($callback) || empty($callback)) {
  108. joutput_error($code, $msg);
  109. } else {
  110. echo "{$callback}(";
  111. joutput_error($code, $msg);
  112. echo ");";
  113. }
  114. }
  115. else
  116. {
  117. if(empty($msg)) {
  118. $msg = errcode::msg($code);
  119. }
  120. $start = microtime(true);
  121. echo joutput_error($code, $msg, 'web') . "<br/>";
  122. perfor_period("joutput",$start,"web");
  123. echo sprintf("eclipse_time = %.6f <br/><br/>", self::eclipse_time());
  124. echo "性能关键统计:<br/><br/>";
  125. performance_helper::format_log();
  126. $sqls = Log::sql_log();
  127. echo "sql count = " . count($sqls) . "<br/><br/>";
  128. foreach ($sqls as $sql) {
  129. echo "{$sql}<br/>";
  130. }
  131. }
  132. return true;
  133. }
  134. static public function outsuccess($data, $page = '',$type = NULL)
  135. {
  136. static $json_clients = array('android', 'ios');
  137. if(!is_null($type)) {
  138. $show_type = $type;
  139. } else {
  140. $show_type = $_SESSION['client_type'];
  141. }
  142. if (in_array($show_type, $json_clients))
  143. {
  144. joutput_data($data);
  145. }
  146. else if ($show_type == 'wap')
  147. {
  148. Tpl::clear();
  149. if (is_array($data)) {
  150. foreach ($data as $key => $val) {
  151. Tpl::output($key, $val);
  152. }
  153. }
  154. if (!empty($page)) {
  155. Tpl::showpage($page);
  156. }
  157. }
  158. else if ($show_type == 'ajax')
  159. {
  160. $callback = $_GET['callback'];
  161. if(!isset($callback) || empty($callback)) {
  162. joutput_data($data);
  163. } else {
  164. echo "{$callback}(";
  165. joutput_data($data);
  166. echo ");";
  167. }
  168. }
  169. else
  170. {
  171. echo 'success: return data=<br/>';
  172. $start = microtime(true);
  173. joutput_data($data, 'web');
  174. perfor_period("joutput",$start,"web");
  175. echo "<br/><br/>";
  176. echo sprintf("eclipse_time = %.6f <br/><br/>", self::eclipse_time());
  177. echo "性能关键统计:<br/><br/>";
  178. performance_helper::format_log();
  179. $sqls = Log::sql_log();
  180. echo "sql count = " . count($sqls) . "<br/><br/>";
  181. foreach ($sqls as $sql) {
  182. echo "{$sql}<br/>";
  183. }
  184. }
  185. return true;
  186. }
  187. public function separate_page($items,&$pages)
  188. {
  189. $arr_items = array_chunk($items,$this->page_size());
  190. $pages = count($arr_items);
  191. $page_no = $pages >= $this->page_no() ? $this->page_no() : $pages;
  192. return ($arr_items[$page_no - 1]);
  193. }
  194. }
  195. class mobileHomeControl extends mobileControl
  196. {
  197. public function __construct()
  198. {
  199. parent::__construct();
  200. }
  201. }
  202. /**
  203. * Class mbMemberControl
  204. */
  205. class mbMemberControl extends mobileControl
  206. {
  207. public $err_code = errcode::Success;
  208. public function __construct()
  209. {
  210. parent::__construct();
  211. if ($_SESSION['is_login'] != 1) {
  212. throw new UnloginException();
  213. }
  214. }
  215. }
  216. function bonus_version()
  217. {
  218. return "v=20161222";
  219. }
  220. function shop_version()
  221. {
  222. return "v=20161226";
  223. }