control.php 7.7 KB

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