control.php 9.3 KB

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