control.php 7.8 KB

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