control.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. require_once (BASE_ROOT_PATH . "/helper/session_helper.php");
  11. require_once (BASE_ROOT_PATH . "/helper/bonus_helper.php");
  12. /********************************** 前台control父类 **********************************************/
  13. class mobileControl
  14. {
  15. //客户端类型
  16. private static $stClienTypes = ['android', 'wap', 'wechat', 'ios', 'ajax', 'web','mini'];
  17. //列表默认分页数
  18. protected $page_size;
  19. protected $cur_page;
  20. //任务开始时间
  21. private static $startime = 0;
  22. public function __construct()
  23. {
  24. self::$startime = microtime(true);
  25. //Language::read('mobile');
  26. //分页数处理
  27. if (is_numeric($_GET['page']) && intval(trim($_GET['page'])) > 0) {
  28. $this->page_size = intval(trim($_GET['page']));
  29. } else {
  30. $this->page_size = 20;
  31. }
  32. if (is_numeric($_GET['curpage']) && intval(trim($_GET['curpage'])) > 0) {
  33. $this->cur_page = intval(trim($_GET['curpage']));
  34. } else {
  35. $this->cur_page = 1;
  36. }
  37. $this->initpage($this->page_size, $this->cur_page);
  38. $this->check_app_type();
  39. $param = $_GET;
  40. $param['client_type'] = $_SESSION['client_type'];
  41. $this->set_relay();
  42. statistics_helper::instance()->add_call($param);
  43. }
  44. public function __destruct()
  45. {
  46. $record = [];
  47. $record['user_session'] = session_helper::session_id();
  48. $record['member_id'] = session_helper::memberid();
  49. $record['relay_id'] = session_helper::relay_id();
  50. $record['add_time'] = time();
  51. $record['exec_time'] = intval((microtime(true) - self::$startime) * 1000000);
  52. $record['content'] = $_SERVER['original_querystring'];
  53. $record['client_type'] = session_helper::client_type();
  54. if(!empty($_GET['from'])) {
  55. $from = base64_decode($_GET['from']);
  56. Log::record("from={$from}",Log::DEBUG);
  57. }
  58. Log::record_path(json_encode($record));
  59. }
  60. private function set_relay()
  61. {
  62. if(isset($_GET['relay_id'])) {
  63. session_helper::set_relay($_GET['relay_id']);
  64. }
  65. elseif (isset($_COOKIE['relay_id'])) {
  66. session_helper::set_relay($_COOKIE['relay_id']);
  67. }
  68. }
  69. protected function initpage($page_size,$cur_page)
  70. {
  71. pagecmd('seteachnum', $page_size);
  72. pagecmd('setnowpage', $cur_page);
  73. }
  74. protected function price_calcer()
  75. {
  76. if(session_helper::logined()) {
  77. return new bonus\account(session_helper::memberid(),true);
  78. } else {
  79. return new bonus\normal_calc();
  80. }
  81. }
  82. protected function android()
  83. {
  84. return $_SESSION['client_type'] == 'android';
  85. }
  86. protected function page_size()
  87. {
  88. return $this->page_size;
  89. }
  90. protected function page_no()
  91. {
  92. return $this->cur_page;
  93. }
  94. protected function pages($count)
  95. {
  96. return intval($count / $this->page_size()) + ($count % $this->page_size() == 0 ? 0 : 1);
  97. }
  98. /**
  99. * @throws UnloginException
  100. */
  101. protected function need_login()
  102. {
  103. if ($_SESSION['is_login'] != 1) {
  104. throw new UnloginException();
  105. }
  106. }
  107. private static function eclipse_time()
  108. {
  109. return (microtime(true) - self::$startime);
  110. }
  111. protected function check_app_type()
  112. {
  113. $client = strtolower(trim($_SERVER['HTTP_CLIENT_TYPE']));
  114. $version = trim($_SERVER['HTTP_CLIENT_VERSION']);
  115. if (empty($client)) {
  116. $client = $_POST['client_type'];
  117. } else {
  118. $_SESSION['is_app'] = true;
  119. }
  120. if (empty($client) || !in_array($client, self::$stClienTypes)) {
  121. $_SESSION['client_type'] = 'wap';
  122. } else {
  123. $_SESSION['client_type'] = $client;
  124. }
  125. if (!empty($version)) {
  126. $_SESSION['client_version'] = $version;
  127. }
  128. $version = intval($version * 100 + 0.5);
  129. if($client == 'ios')
  130. {
  131. $cur_ver = $GLOBALS['setting_config']['mobile_ios_version'];
  132. $lastest_version = intval($cur_ver * 100 + 0.5);
  133. if($version >= $lastest_version) {
  134. $_SESSION['is_lasted'] = true;
  135. } else {
  136. $_SESSION['is_lasted'] = false;
  137. }
  138. }
  139. elseif($client == 'android')
  140. {
  141. $cur_ver = $GLOBALS['setting_config']['mobile_apk_version'];
  142. $lastest_version = intval($cur_ver * 100 + 0.5);
  143. if($version >= $lastest_version) {
  144. $_SESSION['is_lasted'] = true;
  145. } else {
  146. $_SESSION['is_lasted'] = false;
  147. }
  148. }
  149. return true;
  150. }
  151. static public function outerr($code, $msg = '', $page = '',$type = NULL)
  152. {
  153. static $json_clients = ['android', 'ios','mini'];
  154. if(!empty($type)) {
  155. $show_type = $type;
  156. } else {
  157. $show_type = $_SESSION['client_type'];
  158. }
  159. if (in_array($show_type, $json_clients))
  160. {
  161. joutput_error($code, $msg);
  162. }
  163. else if ($show_type == 'wap')
  164. {
  165. Tpl::clear();
  166. Tpl::output("error", $msg);
  167. if (!empty($page)) {
  168. Tpl::showpage($page);
  169. }
  170. }
  171. else if ($show_type == 'ajax')
  172. {
  173. $callback = $_GET['callback'];
  174. if(!isset($callback) || empty($callback)) {
  175. joutput_error($code, $msg);
  176. } else {
  177. echo "{$callback}(";
  178. joutput_error($code, $msg);
  179. echo ");";
  180. }
  181. }
  182. else
  183. {
  184. if(empty($msg)) {
  185. $msg = errcode::msg($code);
  186. }
  187. $start = microtime(true);
  188. echo joutput_error($code, $msg, 'web') . "<br/>";
  189. perfor_period("joutput",$start,"web");
  190. echo sprintf("eclipse_time = %.6f <br/><br/>", self::eclipse_time());
  191. echo "性能关键统计:<br/><br/>";
  192. performance_helper::format_log();
  193. $sqls = Log::sql_log();
  194. echo "sql count = " . count($sqls) . "<br/><br/>";
  195. foreach ($sqls as $sql) {
  196. echo "{$sql}<br/>";
  197. }
  198. }
  199. return true;
  200. }
  201. static public function outsuccess($data, $page = '',$type = NULL)
  202. {
  203. static $json_clients = ['android', 'ios','mini'];
  204. if(!empty($type)) {
  205. $show_type = $type;
  206. } else {
  207. $show_type = $_SESSION['client_type'];
  208. }
  209. if (in_array($show_type, $json_clients))
  210. {
  211. joutput_data($data);
  212. }
  213. else if ($show_type == 'wap')
  214. {
  215. Tpl::clear();
  216. if (is_array($data)) {
  217. foreach ($data as $key => $val) {
  218. Tpl::output($key, $val);
  219. }
  220. }
  221. if (!empty($page)) {
  222. Tpl::showpage($page);
  223. }
  224. }
  225. else if ($show_type == 'ajax')
  226. {
  227. $callback = $_GET['callback'];
  228. if(!isset($callback) || empty($callback)) {
  229. joutput_data($data);
  230. } else {
  231. echo "{$callback}(";
  232. joutput_data($data);
  233. echo ");";
  234. }
  235. }
  236. else
  237. {
  238. echo 'success: return data=<br/>';
  239. $start = microtime(true);
  240. joutput_data($data, 'web');
  241. perfor_period("joutput",$start,"web");
  242. echo "<br/><br/>";
  243. echo sprintf("eclipse_time = %.6f <br/><br/>", self::eclipse_time());
  244. echo "性能关键统计:<br/><br/>";
  245. performance_helper::format_log();
  246. $sqls = Log::sql_log();
  247. echo "sql count = " . count($sqls) . "<br/><br/>";
  248. foreach ($sqls as $sql) {
  249. echo "{$sql}<br/>";
  250. }
  251. }
  252. return true;
  253. }
  254. public function separate_page($items,&$pages)
  255. {
  256. $arr_items = array_chunk($items,$this->page_size());
  257. $pages = count($arr_items);
  258. $page_no = $pages >= $this->page_no() ? $this->page_no() : $pages;
  259. return ($arr_items[$page_no - 1]);
  260. }
  261. }
  262. class mobileHomeControl extends mobileControl
  263. {
  264. public function __construct()
  265. {
  266. parent::__construct();
  267. }
  268. }
  269. class mbMemberControl extends mobileControl
  270. {
  271. public $err_code = errcode::Success;
  272. public function __construct()
  273. {
  274. parent::__construct();
  275. if (!session_helper::logined()) {
  276. throw new UnloginException();
  277. }
  278. }
  279. }
  280. function bonus_version()
  281. {
  282. return "v=2018091001";
  283. }
  284. function shop_version()
  285. {
  286. return "v=2018091001";
  287. }