control.php 8.1 KB

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