control.php 9.0 KB

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