Api.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. <?php
  2. /**
  3. * sdk接口公共控制器类
  4. *
  5. */
  6. namespace app\api\controller;
  7. use app\common\controller\Base;
  8. use app\common\model\App;
  9. use think\Config;
  10. use think\Cache;
  11. use think\Db;
  12. use app\common\model\PolychannelGame;
  13. use think\Env;
  14. use think\Request;
  15. class Api extends Base
  16. {
  17. protected $noNeedLogin = []; // 不验证登录的接口, 例如:['index', 'login']
  18. protected $appInfo = ''; //游戏的app信息
  19. protected $input = []; //客户端提交的参数
  20. protected $isApiDebug = false; //接 口测试请求时
  21. protected $code_num = 0; // 开启报错自定义code码
  22. protected $request_frequency_handle = ['v2.user/personalIdentity']; // 请求频率管控的路由
  23. protected $authKey = 'auth_key'; // token和密码 加密的key
  24. protected $aesKey = 'aes_key'; // 加密解密(安卓/IOS/H5)
  25. protected $device = 0; // 设备来源:1=PC, 2=安卓, 3=苹果, 4=H5
  26. protected $version = ''; // 当前请求版本
  27. /**
  28. * 定义当前设备来源,并判断相关签名方式
  29. */
  30. protected function initDevice(){
  31. // 获取 header 设备号(主要针对 H5)
  32. $device = Request::instance()->header('device', 0, 'intval');
  33. if ($device > 0) {
  34. $this->device = $device;
  35. // if($device == 4){
  36. // $this->aesKey = 'aes_key_h5';
  37. // }
  38. }
  39. }
  40. /**
  41. * 初始化操作
  42. */
  43. public function _initialize()
  44. {
  45. self::initDevice();
  46. //密文
  47. $data = file_get_contents('php://input');
  48. log_message('Api@_initialize.sign_input:' . $data, 'log', LOG_PATH . 'apilog/');
  49. $decryptStr = $this->opensslDecrypt($data, Env::get($this->aesKey)); // 解密后的明文
  50. $api_debug = Request::instance()->header('qmapidebug', false, 'boolval'); // 是否开启API调试模式
  51. // 解密后的明文为空时
  52. if (!$decryptStr || $api_debug == true) {
  53. // 设置不加密
  54. if ($api_debug == true) {
  55. $this->isApiDebug = true;
  56. }
  57. $this->input = input();
  58. if(count($this->input) <= 0){
  59. $this->jsonResult('', -111, 'API数据解密失败,请确认加密数据!');
  60. }
  61. } else {
  62. $params = json_decode($decryptStr, true);
  63. if (JSON_ERROR_NONE == json_last_error()) {
  64. $this->input = $params;
  65. } else {
  66. $this->jsonResult('', -112, 'API解码失败,请检查数据格式!');
  67. }
  68. }
  69. // 获取 body 的设备号(主要针对 安卓/IOS客户端)
  70. $device = $this->input('device', 0, 'intval');
  71. if ($device > 0 && $this->device == 0) {
  72. $this->device = $device;
  73. }
  74. // trace('input:' . json_encode($this->input), 'Api@_initialize.input');
  75. log_message("Api@_initialize.input: {$device} -- " . json_encode($this->input), 'log', LOG_PATH . 'apilog/');
  76. /********************* 接口签名验证 start *********************/
  77. $appid = $this->input('appid'); //游戏的appid
  78. if (empty($appid)) {
  79. $this->jsonResult('', -121, '密钥ID不能为空');
  80. } else {
  81. $appModel = new App;
  82. $this->appInfo = $appModel->field('id,appkey,client_appkey,gameid')->where(['id' => $appid])->find();
  83. if (empty($this->appInfo)) {
  84. $this->jsonResult('', -122, '当前应用不存在!');
  85. } elseif (empty($this->appInfo['appkey'])) {
  86. $this->jsonResult('', -123, '当前应用配置有误!');
  87. }
  88. //签名验证
  89. if (!$this->md5Verify($this->input, $this->input('sign')) && Config::get('api_debug') == false && $api_debug == false) {
  90. $this->jsonResult('', -124, '签名不能通过,被拒绝访问');
  91. }
  92. }
  93. /********************* 接口签名验证 end *********************/
  94. // 母包渠道ID
  95. if (empty($this->input['channel_id'])) {
  96. $this->input['channel_id'] = Config::get('initial_channel_id');
  97. }
  98. $this->version = str_ireplace('v', '', $this->input('version'));
  99. // 校验登录
  100. $this->checkLogin();
  101. }
  102. /**
  103. * md5方式接口数据验签
  104. *
  105. * @param $data array 待签名数据
  106. * @param $sign string 签名结果
  107. *
  108. * @return boolean 验签结果
  109. */
  110. private function md5Verify($data, $sign)
  111. {
  112. $data = $this->paraFilter($data);
  113. ksort($data); // 按key升序排列
  114. reset($data);
  115. //拼接待签名字符串
  116. $arg = "";
  117. foreach ($data as $key => $val) {
  118. $arg .= $key . "=" . $val;
  119. }
  120. //如果存在转义字符,那么去掉转义
  121. if (get_magic_quotes_gpc()) {
  122. $arg = stripslashes($arg);
  123. }
  124. //添加客户端appkey参数值
  125. $arg .= $this->appInfo['client_appkey'];
  126. //全部转化成小写
  127. $arg = mb_strtolower($arg, 'UTF-8');
  128. $arg = $this->romanToUppercase($arg);
  129. $mysgin = md5($arg);
  130. if (strcasecmp($mysgin, $sign) == 0){
  131. // trace('数据验签 - 客户端sign:' . $sign . ' - 服务端sign:' . $mysgin . ' - ' . $arg, 'Api@md5Verify.error');
  132. return true;
  133. } else {
  134. trace('数据验签失败 - 客户端sign:' . $sign . ' - 服务端sign:' . $mysgin . ' - ' . $arg, 'Api@md5Verify.error');
  135. return false;
  136. }
  137. }
  138. private function romanToUppercase($roman)
  139. {
  140. $uppercase = array(
  141. 'Ⅰ', 'Ⅱ', 'Ⅲ', 'Ⅳ', 'Ⅴ', 'Ⅵ', 'Ⅶ', 'Ⅷ', 'Ⅸ', 'Ⅹ', 'Ⅺ', 'Ⅻ'
  142. );
  143. $lowercase = array(
  144. 'ⅰ', 'ⅱ', 'ⅲ', 'ⅳ', 'ⅴ', 'ⅵ', 'ⅶ', 'ⅷ', 'ⅸ', 'ⅹ', 'ⅺ', 'ⅻ'
  145. );
  146. return str_replace($uppercase, $lowercase, $roman);
  147. }
  148. /**
  149. * 过滤不参与签名的参数
  150. *
  151. * @param $para array 待签名数据
  152. *
  153. * @return array
  154. */
  155. private function paraFilter($para)
  156. {
  157. $para_filter = array();
  158. // while (list ($key, $val) = each ($para)) {
  159. foreach ($para as $key => $val) {
  160. if ($key == "sign") continue;
  161. else $para_filter[$key] = $para[$key];
  162. }
  163. return $para_filter;
  164. }
  165. /**
  166. * 使用openssl库进行加密
  167. * @param string $data 加密数据
  168. * @param string $key 加密用的key
  169. * @param string $method 加密方法
  170. * @return string 加密后的密文
  171. */
  172. private function opensslEncrypt($data, $key, $method = 'AES-128-ECB')
  173. {
  174. $str = openssl_encrypt($data, $method, $key);
  175. return $str;
  176. }
  177. /**
  178. * 使用openssl库进行解密
  179. * @param string $data 解密密文
  180. * @param string $key 解密用的key
  181. * @param string $method 解密方法
  182. *
  183. * @return string 解密后的明文
  184. */
  185. protected function opensslDecrypt($data, $key, $method = 'AES-128-ECB')
  186. {
  187. try {
  188. $str = openssl_decrypt($data, $method, $key);
  189. } catch (\Exception $e) {
  190. $this->jsonResult('', 0, '接口参数解密失败' . $e->getMessage() . ',请检查参数格式!');
  191. }
  192. return $str;
  193. }
  194. /**
  195. * 获取参数
  196. * @param string $key 数组index
  197. * @param string $default 默认值
  198. * @return mixed|null
  199. */
  200. protected function input($key, $default = null)
  201. {
  202. return isset($this->input[$key]) ? $this->input[$key] : ($default !== null ? $default : null);
  203. }
  204. /**
  205. * 校验登录
  206. */
  207. protected function checkLogin()
  208. {
  209. $controller = request()->controller();
  210. $action = request()->action();
  211. if (strpos($controller, '.') !== false) {
  212. list($version, $controller) = explode('.', $controller);
  213. }
  214. // 需要检验登录的操作
  215. if (!in_array($controller, ['register', 'sendSms', 'CheckUpdate', 'complexLogin', 'startup', 'forget', 'privacy', 'config', 'Third']) && !in_array($action, $this->noNeedLogin)) {
  216. $imeil = $this->input('imeil');
  217. if (empty($imeil)) {
  218. $this->jsonResult('', -131, '设备IMEI不能为空! ');
  219. }
  220. $token = $this->input('token', Request::instance()->header('token'));
  221. if (empty($token)) {
  222. $this->jsonResult('', -132, '未登录! ');
  223. }
  224. $redisImeil = Cache::store('redis')->get($token);
  225. if (!$redisImeil) {
  226. $this->jsonResult('', -133, '登陆失效,请重新登录!', 'json');
  227. } elseif ($redisImeil != $imeil) {
  228. $this->jsonResult('', -134, '您的账号已在其他设备上登录!');
  229. }
  230. try {
  231. $tokenStr = auth_code($token, "DECODE", Env::get($this->authKey));
  232. if(!$tokenStr){
  233. $this->jsonResult('', -135, '登陆异常,请重新登录!', 'json');
  234. }
  235. $tokenArr = array_pad(explode('|', $tokenStr), 7, '');
  236. // version > 3.6.8 之后才会有 v3 标识,用于自动登录时候判断老版本的jwt_token的
  237. list($userid, $username, $gameid, $sub_username, $token_random, $type, $token_version) = $tokenArr; // 103487|13121711111|244|103487|1tRh9iUu|sdk|v3
  238. // if($this->input('device') == 4){
  239. // // $userInfo = model('common/Member')->where(['userid' => $userid, 'gameid' => $gameid])->find();
  240. // }
  241. }catch (\Exception $e){
  242. $this->jsonResult('', -136, '登陆异常,请重新登录!!', 'json');
  243. }
  244. $redisTokenRandom = Cache::store('redis')->get('token|' . $type . '|' . $userid . '|' . $gameid);
  245. //redis中玩家登录安全控制的随机码不存在时
  246. if (!$redisTokenRandom) {
  247. Cache::store('redis')->rm($token);
  248. $this->jsonResult('', -137, '登陆异常,请重新登录!', 'json');
  249. } //两个token随机码不同时
  250. elseif ($redisTokenRandom != $token_random) {
  251. Cache::store('redis')->rm($token);
  252. $this->jsonResult('', -138, '登陆失败,请重新登录!', 'json');
  253. }
  254. $this->input['userid'] = $userid;
  255. $this->input['gameid'] = $gameid;
  256. $this->input['username'] = $username;
  257. $this->input['token'] = $token;
  258. $this->input['sub_username'] = $sub_username;
  259. $this->input['member_channel_id'] = getChannelId($userid, $gameid); // 当前玩家绑定的渠道
  260. $this->input['token_version'] = $token_version;
  261. $path = request()->path();
  262. if(in_array($path, $this->request_frequency_handle)){
  263. if(requestFrequencyHandle($username, $path) == false){
  264. $this->jsonResult('', -138, '操作过于频繁!', 'json');
  265. }
  266. }
  267. }
  268. }
  269. /**
  270. * 重新给客户端提交的参数赋值
  271. *
  272. * @param $key :键
  273. * @param $value :值
  274. */
  275. public function setInput($key, $value)
  276. {
  277. $this->input[$key] = $value;
  278. }
  279. /**
  280. * 保存设备信息
  281. *
  282. * @param $game_id : int 游戏ID
  283. * @param $imei : string 游戏imei码
  284. * @param $platform : int 0:安卓 1:IOS
  285. *
  286. * @return boolean
  287. */
  288. protected function saveDevices($game_id, $imei, $platform)
  289. {
  290. $os_version = trim($this->input('os_version')); //操作系统版本
  291. if (empty($os_version) || $platform == null || empty($this->input('phone_brand')) || empty($this->input('phone_model'))) {
  292. return false;
  293. }
  294. //设备信息记录不存在时
  295. if (!Db::table('nw_devices')->where(['game_id' => $game_id, 'imei' => $imei, 'os_version' => $os_version])->find()) {
  296. $devicesData['game_id'] = $game_id;
  297. $devicesData['platform'] = !empty($platform) ? 1 : 0;
  298. $devicesData['imei'] = $imei;
  299. $devicesData['phone_brand'] = $this->input('phone_brand');
  300. $devicesData['phone_model'] = $this->input('phone_model');
  301. $devicesData['os_version'] = $os_version;
  302. $devicesData['update_time'] = NOW_TIMESTAMP;
  303. $devicesData['create_time'] = NOW_TIMESTAMP;
  304. Db::table('nw_devices')->insert($devicesData);
  305. return true;
  306. }
  307. return false;
  308. }
  309. /**
  310. * 保存玩家游戏区服信息
  311. *
  312. * @param $game_id : int 游戏ID
  313. * @param $imei : string 游戏imei码
  314. * @param $platform : int 0:安卓 1:IOS
  315. *
  316. * @return boolean
  317. */
  318. protected function saveGameServer($userid, $gameid, $imeil = '', $source = 'role')
  319. {
  320. $serverid = $this->input('serverid', '', 'trim');
  321. $servername = $this->input('servername', '', 'trim');
  322. $roleid = $this->input('roleid', '', 'trim');
  323. $rolename = $this->input('rolename', '', 'trim');
  324. $viplevel = $this->input('viplevel', '', 'trim');
  325. $rolelevel = $this->input('rolelevel', '', 'trim');
  326. //游戏ID不能为空
  327. if (empty($gameid)) {
  328. return false;
  329. } elseif (empty($userid) || empty($servername) || empty($serverid) || empty($roleid) || empty($rolename) || empty($imeil)) {
  330. return false;
  331. }
  332. $memberGameInfo = model('MemberChannelGame')->where(['member_id' => $userid, 'game_id' => $gameid])->find();
  333. if (empty($memberGameInfo)) {
  334. return false;
  335. }
  336. // 修复部分仙剑的区服ID位数不对问题
  337. if($handleServcerId = repairRoleServerid($gameid, $serverid)){
  338. $serverid = $handleServcerId;
  339. }
  340. $memberGameServerInfo = model('MemberGameServer')->where(['member_id' => $userid, 'game_id' => $gameid, 'roleid' => $roleid])->find();
  341. if ($memberGameServerInfo) {
  342. $updRoleData = array();
  343. $updRoleData['serverid'] = $serverid;
  344. $updRoleData['servername'] = $servername;
  345. $updRoleData['roleid'] = $roleid;
  346. $updRoleData['rolename'] = $rolename;
  347. if ($rolelevel && $rolelevel <> '0') {
  348. $updRoleData['rolelevel'] = $rolelevel;
  349. }
  350. if ($viplevel && $viplevel <> '0') {
  351. $updRoleData['viplevel'] = $viplevel;
  352. }
  353. $updRoleData['last_ip'] = request()->ip();
  354. $updRoleData['last_imeil'] = $imeil;
  355. $updRoleData['update_time'] = NOW_TIMESTAMP;
  356. $updResult = model('MemberGameServer')->where(['member_id' => $userid, 'game_id' => $gameid, 'roleid' => $roleid])->update($updRoleData);
  357. } else {
  358. $cahce_key = md5($userid . $gameid . $serverid . $roleid);
  359. if(!Cache::get(':Role_add:MemberGameServer:'.$cahce_key)){
  360. Cache::set(':Role_add:MemberGameServer:'.$cahce_key,'1',3600);
  361. if ($memberGameInfo) {
  362. $addRoleData = array();
  363. $addRoleData['member_id'] = $memberGameInfo['member_id'];
  364. $addRoleData['channel_id'] = $memberGameInfo['channel_id'];
  365. $addRoleData['game_id'] = $memberGameInfo['game_id'];
  366. $addRoleData['serverid'] = $serverid;
  367. $addRoleData['ip'] = request()->ip();
  368. $addRoleData['imeil'] = $imeil;
  369. $addRoleData['create_time'] = NOW_TIMESTAMP;
  370. $addRoleData['servername'] = $servername;
  371. $addRoleData['roleid'] = $roleid;
  372. $addRoleData['rolename'] = $rolename;
  373. $addRoleData['rolelevel'] = $rolelevel;
  374. $addRoleData['viplevel'] = $viplevel;
  375. $addRoleData['last_ip'] = request()->ip();
  376. $addRoleData['last_imeil'] = $imeil;
  377. $addRoleData['update_time'] = NOW_TIMESTAMP;
  378. $insertId = model('MemberGameServer')->insert($addRoleData);
  379. }
  380. }
  381. }
  382. $gameServerInfo = model('GameServer')->where(['game_id' => $gameid, 'serverid' => $serverid])->find();
  383. if ($gameServerInfo) {
  384. $updServerData = array();
  385. $updServerData['servername'] = $servername;
  386. $updServerData['update_time'] = NOW_TIMESTAMP;
  387. $updServerResult = model('GameServer')->where(['game_id' => $gameid, 'serverid' => $serverid])->update($updServerData);
  388. } else {
  389. $cahce_key = md5($gameid.$serverid);
  390. if(!Cache::get(':Role_add:GameServer:'.$cahce_key)) {
  391. Cache::set(':Role_add:MemberGameServer:' . $cahce_key, '1', 3600);
  392. $addServerData = array();
  393. $addServerData['game_id'] = $gameid;
  394. $addServerData['serverid'] = $serverid;
  395. $addServerData['create_time'] = NOW_TIMESTAMP;
  396. $addServerData['servername'] = $servername;
  397. $addServerData['update_time'] = NOW_TIMESTAMP;
  398. $insertServerId = model('GameServer')->insert($addServerData);
  399. }
  400. }
  401. $clientIp = request()->ip();
  402. if ($gameid && $serverid && $clientIp) {
  403. $addServerIpData = array();
  404. $addServerIpData['channel_id'] = $memberGameInfo['channel_id'];
  405. $addServerIpData['member_id'] = $memberGameInfo['member_id'];
  406. $addServerIpData['game_id'] = $gameid;
  407. $addServerIpData['serverid'] = $serverid;
  408. $addServerIpData['create_src'] = $source;
  409. $addServerIpData['create_time'] = NOW_TIMESTAMP;
  410. $addServerIpData['servername'] = $servername;
  411. $addServerIpData['ip'] = $clientIp;
  412. $insertServerIpId = model('GameServerIp')->insert($addServerIpData);
  413. }
  414. if ($gameid && $serverid && $imeil) {
  415. $addServerImeilData = array();
  416. $addServerImeilData['channel_id'] = $memberGameInfo['channel_id'];
  417. $addServerImeilData['member_id'] = $memberGameInfo['member_id'];
  418. $addServerImeilData['game_id'] = $gameid;
  419. $addServerImeilData['serverid'] = $serverid;
  420. $addServerImeilData['create_src'] = $source;
  421. $addServerImeilData['create_time'] = NOW_TIMESTAMP;
  422. $addServerImeilData['servername'] = $servername;
  423. $addServerImeilData['imeil'] = $imeil;
  424. try {
  425. $insertServerImeilId = model('GameServerImeil')->insert($addServerImeilData);
  426. }catch (\Exception $e){
  427. }
  428. }
  429. return true;
  430. }
  431. /**
  432. * 获取游戏配置参数(有些游戏有多个参数时需要特殊处理)
  433. * @param $game_id 游戏id
  434. * @param $channel_mark 渠道标识
  435. * @param string $data 返回的数据
  436. * @param string $code 返回的code
  437. * @param string $msg 返回的msg
  438. * @return mixed
  439. */
  440. public function getGameParam($game_id, $channel_mark, $data = '', $code = '0', $msg = '该游戏的验签参数还未设置')
  441. {
  442. $polyChannelGameModel = new PolychannelGame;
  443. $param = $polyChannelGameModel->alias('a')
  444. ->join('nw_channel b', 'b.id = a.channel_id')
  445. ->where(['a.game_id' => $game_id, 'b.mark' => $channel_mark])
  446. ->value('a.param');
  447. if (empty($param)) {
  448. log_message('后台渠道游戏中,用于该游戏的验签参数还未设置,game_id=' . $game_id . ',渠道标识=' . $channel_mark, 'error', LOG_PATH . '../complexPaylog/');
  449. $this->jsonResult($data, $code, $msg);
  450. }
  451. $param = unserialize($param);
  452. return $param;
  453. }
  454. /**登录验证返回的数据
  455. * @param $code 1 登录验证成功 0验证失败
  456. * @param string $msg 登录验证提示语
  457. * @param array $data 需要重写的参数 key=》val key必须和complexLogin 里的key一致
  458. * @param string $exparam 扩展参数
  459. * @return array
  460. */
  461. public function reParam($code, $msg = '', $data = [], $exparam = '')
  462. {
  463. return ['code' => $code, 'msg' => $msg, 'extparam' => $exparam, 'data' => $data];
  464. }
  465. /**
  466. * 返回封装后的 API json数据到客户端
  467. * @access protected
  468. * @param mixed $data 要返回的数据
  469. * @param int $code 返回的 code
  470. * @param mixed $msg 提示信息
  471. * @param $isExit boolean 是否强制退出,默认true
  472. * @return void
  473. *
  474. */
  475. public function jsonResult($data, $code = 0, $msg = '', $isExit = true)
  476. {
  477. $err_code = $code;
  478. // if($this->device != 4){
  479. // if ($code >= 200) {
  480. // $code = 1;
  481. // } else {
  482. // $code = 0;
  483. // }
  484. // }
  485. if ($code > 0) {
  486. $code = 1;
  487. } else {
  488. $code = 0;
  489. }
  490. $result = [
  491. 'code' => $code,
  492. 'msg' => $msg,
  493. 'time' => request()->server('REQUEST_TIME'),
  494. 'data' => $data,
  495. 'err_code' => $err_code,
  496. ];
  497. log_message('Api@jsonResult.result:'. ' code:'. $code." - result:". json_encode($result), 'log', LOG_PATH . 'apilog/');
  498. // debug模式下返回明文
  499. if ($this->isApiDebug) {
  500. // echo json_encode($result);
  501. if($err_code < 0){
  502. $msg .= '(err:'.$err_code.')';
  503. }
  504. $this->result($data,$code,$msg,'json'); //api debug模式下fastcgi_finish_request无法使用
  505. } //正式环境返回密文
  506. else {
  507. log_message('Api@jsonResultUnescape.result:'. $this->opensslEncrypt(json_encode($result, JSON_UNESCAPED_SLASHES), Env::get($this->aesKey)), 'log', LOG_PATH . 'apilog/');
  508. echo $this->opensslEncrypt(json_encode($result), Env::get($this->aesKey));
  509. }
  510. //为了使用fastcgi_finish_request等函数时,后续的执行能够继续生效
  511. if ($isExit) exit;
  512. }
  513. public function jsonResultUnescape($data, $code = 0, $msg = '', $isExit = true)
  514. {
  515. // if($this->device != 4){
  516. // if ($code > 200) {
  517. // $code = 1;
  518. // } else {
  519. // $code = 0;
  520. // }
  521. // }
  522. if ($code > 0) {
  523. $code = 1;
  524. } else {
  525. $code = 0;
  526. }
  527. $result = [
  528. 'code' => $code,
  529. 'msg' => $msg,
  530. 'time' => request()->server('REQUEST_TIME'),
  531. 'data' => $data,
  532. ];
  533. log_message('Api@jsonResultUnescape.result:'. ' code:'. $code ." - result:". json_encode($result), 'log', LOG_PATH . 'apilog/');
  534. //debug模式下返回明文
  535. if ($this->isApiDebug) {
  536. echo json_encode($result, JSON_UNESCAPED_SLASHES);
  537. //$this->result($data,$code,$msg,'json'); //api debug模式下fastcgi_finish_request无法使用
  538. } //正式环境返回密文
  539. else {
  540. // log_message('Api@jsonResultUnescape.result:'. $this->opensslEncrypt(json_encode($result, JSON_UNESCAPED_SLASHES), Env::get($this->aesKey)), 'log', LOG_PATH . 'apilog/');
  541. echo $this->opensslEncrypt(json_encode($result, JSON_UNESCAPED_SLASHES), Env::get($this->aesKey));
  542. }
  543. //为了使用fastcgi_finish_request等函数时,后续的执行能够继续生效
  544. if ($isExit) exit;
  545. }
  546. /**
  547. * 返回封装后的 API json数据到客户端
  548. * @access protected
  549. * @param mixed $data 要返回的数据
  550. * @param int $code 返回的 code
  551. * @param mixed $msg 提示信息
  552. * @param $isExit boolean 是否强制退出,默认true
  553. * @return void
  554. *
  555. */
  556. public function jsonResultTrue($data, $code = 0, $msg = '',$isExit=true)
  557. {
  558. // if($this->device != 4){
  559. // if ($code > 0) {
  560. // $code = 1;
  561. // } else {
  562. // $code = 0;
  563. // }
  564. // }
  565. if ($code > 0) {
  566. $code = 1;
  567. } else {
  568. $code = 0;
  569. }
  570. $result = [
  571. 'code' => $code,
  572. 'msg' => $msg,
  573. 'time' => request()->server('REQUEST_TIME'),
  574. 'data' => $data,
  575. ];
  576. //debug模式下返回明文
  577. echo json_encode($result);
  578. //为了使用fastcgi_finish_request等函数时,后续的执行能够继续生效
  579. if ($isExit) exit;
  580. }
  581. // 空方法
  582. public function _empty()
  583. {
  584. return "url error!";
  585. }
  586. }