BaseApi.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace app\shopapi\controller;
  11. use app\exception\ApiException;
  12. use app\model\shop\Shop;
  13. use app\model\system\Api;
  14. use app\model\system\Group as GroupModel;
  15. use app\model\system\Site;
  16. use app\model\system\User as UserModel;
  17. use extend\RSA;
  18. use think\facade\Cache;
  19. class BaseApi
  20. {
  21. public $lang;
  22. public $params;
  23. public $token;
  24. protected $user_info;
  25. protected $uid;
  26. protected $url;
  27. protected $site_id;
  28. protected $website_id;
  29. protected $group_info;
  30. protected $shop_info;
  31. public $app_type;
  32. protected $app_module = 'shop';
  33. protected $api_config;
  34. protected $addon = '';
  35. public function __construct()
  36. {
  37. if ($_SERVER[ 'REQUEST_METHOD' ] == 'OPTIONS') {
  38. exit;
  39. }
  40. $this->url = strtolower(request()->parseUrl());
  41. $this->addon = request()->addon() ? request()->addon() : '';
  42. //获取参数
  43. $this->params = input();
  44. $this->getApiConfig();
  45. $this->decryptParams();
  46. $this->site_id = request()->siteid();
  47. //todo 基于将这个类所谓api基类的解决方案(主观应该提取公共部分重新封装)
  48. if($this->app_module == 'shop'){
  49. if (!addon_is_exit('mobileshop', $this->site_id)) {
  50. $error = $this->error([], 'ADDON_NOT_EXIST');
  51. throw new ApiException($error['code'], $error['message']);
  52. }
  53. }
  54. }
  55. /**
  56. * api请求参数解密
  57. */
  58. private function decryptParams()
  59. {
  60. if ($this->api_config[ 'is_use' ] && !empty($this->api_config[ 'value' ]) && isset($this->params[ 'encrypt' ])) {
  61. $decrypted = RSA::decrypt(
  62. $this->params[ 'encrypt' ],
  63. $this->api_config[ 'value' ][ 'private_key' ],
  64. $this->api_config[ 'value' ][ 'public_key' ]
  65. );
  66. if ($decrypted[ 'code' ] >= 0) {
  67. $this->params = json_decode($decrypted[ 'data' ], true);
  68. } else {
  69. $this->params = [];
  70. }
  71. }
  72. }
  73. /**
  74. * 获取api配置
  75. */
  76. private function getApiConfig()
  77. {
  78. $api_model = new Api();
  79. $config_result = $api_model->getApiConfig();
  80. $this->api_config = $config_result[ "data" ];
  81. }
  82. /**
  83. * 检测token(使用私钥检测)
  84. */
  85. protected function checkToken() : array
  86. {
  87. if (empty($this->params[ 'token' ])) {
  88. return $this->error('', 'TOKEN_NOT_EXIST');
  89. }
  90. if ($this->api_config[ 'is_use' ] && isset($this->api_config[ 'value' ][ 'private_key' ])
  91. && !empty($this->api_config[ 'value' ][ 'private_key' ])) {
  92. $decrypt = decrypt($this->params[ 'token' ], $this->api_config[ 'value' ][ 'private_key' ]);
  93. } else {
  94. $decrypt = decrypt($this->params[ 'token' ]);
  95. }
  96. if (empty($decrypt)) {
  97. return $this->error('', 'TOKEN_ERROR');
  98. }
  99. $data = json_decode($decrypt, true);
  100. if (!empty($data[ 'expire_time' ]) && $data[ 'expire_time' ] > time()) {
  101. return $this->error('', 'TOKEN_EXPIRE');
  102. }
  103. $this->user_info = $data[ 'user_info' ];
  104. $this->app_module = $this->user_info['app_module'];
  105. $this->uid = $data[ 'user_info' ][ 'uid' ];
  106. $this->getShopInfo();
  107. $this->getGroupInfo();
  108. //判断权限
  109. if (!$this->checkAuth()) {
  110. $error = $this->error([], 'NO_PERMISSION');
  111. throw new ApiException($error['code'], $error['message']);
  112. }
  113. return success(0, '', $data);
  114. }
  115. /**
  116. * 创建token
  117. * @param $user_info
  118. * @param int $expire_time 有效时间 0为永久 单位s
  119. * @return string
  120. */
  121. protected function createToken($user_info)
  122. {
  123. $data = [
  124. 'user_info' => $user_info,
  125. 'expire_time' => $this->api_config[ 'value' ]['long_time'] * 3600
  126. ];
  127. if ($this->api_config[ 'is_use' ] && isset($this->api_config[ 'value' ][ 'private_key' ])
  128. && !empty($this->api_config[ 'value' ][ 'private_key' ])) {
  129. $token = encrypt(json_encode($data), $this->api_config[ 'value' ][ 'private_key' ]);
  130. } else {
  131. $token = encrypt(json_encode($data));
  132. }
  133. return $token;
  134. }
  135. public function getShopInfo()
  136. {
  137. //获取店铺信息
  138. $condition = array (
  139. [ "site_id", "=", $this->site_id ]
  140. );
  141. $shop_info_result = (new Shop())->getShopInfo($condition);
  142. $site_info = (new Site())->getSiteInfo($condition);
  143. $this->shop_info = array_merge($shop_info_result['data'], $site_info['data']);
  144. }
  145. /**
  146. * 获取当前用户的用户组
  147. */
  148. private function getGroupInfo()
  149. {
  150. $group_model = new GroupModel();
  151. $group_info_result = $group_model->getGroupInfo([ [ "group_id", "=", $this->user_info[ "group_id" ] ], [ "site_id", "=", $this->site_id ], [ "app_module", "=", $this->app_module ] ]);
  152. $this->group_info = $group_info_result[ "data" ];
  153. }
  154. /**
  155. * 返回数据
  156. * @param $data
  157. * @return false|string
  158. */
  159. public function response($data)
  160. {
  161. $data[ 'timestamp' ] = time();
  162. return json_encode($data, JSON_UNESCAPED_UNICODE);
  163. }
  164. /**
  165. * 操作成功返回值函数
  166. * @param string $data
  167. * @param string $code_var
  168. * @return array
  169. */
  170. public function success($data = '', $code_var = 'SUCCESS')
  171. {
  172. $lang_array = $this->getLang();
  173. $code_array = $this->getCode();
  174. $lang_var = isset($lang_array[ $code_var ]) ? $lang_array[ $code_var ] : $code_var;
  175. $code_var = isset($code_array[ $code_var ]) ? $code_array[ $code_var ] : $code_array[ 'SUCCESS' ];
  176. return success($code_var, $lang_var, $data);
  177. }
  178. /**
  179. * 操作失败返回值函数
  180. * @param string $data
  181. * @param string $code_var
  182. * @return array
  183. */
  184. public function error($data = '', $code_var = 'ERROR')
  185. {
  186. $lang_array = $this->getLang();
  187. $code_array = $this->getCode();
  188. $lang_var = isset($lang_array[ $code_var ]) ? $lang_array[ $code_var ] : $code_var;
  189. $code_var = isset($code_array[ $code_var ]) ? $code_array[ $code_var ] : $code_array[ 'ERROR' ];
  190. return error($code_var, $lang_var, $data);
  191. }
  192. /**
  193. * 获取语言包数组
  194. * @return array|mixed
  195. */
  196. private function getLang()
  197. {
  198. $default_lang = config("lang.default_lang");
  199. $addon = request()->addon();
  200. $addon = isset($addon) ? $addon : '';
  201. $cache_common = Cache::get("lang_app/shopapi/lang/" . $default_lang);
  202. if (!empty($addon)) {
  203. $addon_cache_common = Cache::get("lang_app/shopapi/lang/" . $addon . '_' . $default_lang);
  204. if (!empty($addon_cache_common)) {
  205. $cache_common = array_merge($cache_common, $addon_cache_common);
  206. }
  207. }
  208. if (empty($cache_common)) {
  209. $cache_common = include 'app/shopapi/lang/' . $default_lang . '.php';
  210. Cache::tag("lang")->set("lang_app/shopapi/lang/" . $default_lang, $cache_common);
  211. if (!empty($addon)) {
  212. try {
  213. $addon_cache_common = include 'addon/' . $addon . '/shopapi/lang/' . $default_lang . '.php';
  214. if (!empty($addon_cache_common)) {
  215. $cache_common = array_merge($cache_common, $addon_cache_common);
  216. Cache::tag("lang")->set(
  217. "lang_app/shopapi/lang/" . $addon . '_' . $default_lang,
  218. $addon_cache_common
  219. );
  220. }
  221. } catch (\Exception $e) {
  222. }
  223. }
  224. }
  225. $lang_path = isset($this->lang) ? $this->lang : '';
  226. if (!empty($lang_path)) {
  227. $cache_path = Cache::get("lang_" . $lang_path . "/" . $default_lang);
  228. if (empty($cache_path)) {
  229. $cache_path = include $lang_path . "/" . $default_lang . '.php';
  230. Cache::tag("lang")->set("lang_" . $lang_path . "/" . $default_lang, $cache_path);
  231. }
  232. $lang = array_merge($cache_common, $cache_path);
  233. } else {
  234. $lang = $cache_common;
  235. }
  236. return $lang;
  237. }
  238. /**
  239. * 获取code编码
  240. * @return array|mixed
  241. */
  242. private function getCode()
  243. {
  244. $addon = request()->addon();
  245. $addon = isset($addon) ? $addon : '';
  246. $cache_common = Cache::get("lang_code_app/shopapi/lang");
  247. if (!empty($addon)) {
  248. $addon_cache_common = Cache::get("lang_code_app/shopapi/lang/" . $addon);
  249. if (!empty($addon_cache_common)) {
  250. $cache_common = array_merge($cache_common, $addon_cache_common);
  251. }
  252. }
  253. if (empty($cache_common)) {
  254. $cache_common = include 'app/shopapi/lang/code.php';
  255. Cache::tag("lang_code")->set("lang_code_app/shopapi/lang", $cache_common);
  256. if (!empty($addon)) {
  257. try {
  258. $addon_cache_common = include 'addon/' . $addon . '/shopapi/lang/code.php';
  259. if (!empty($addon_cache_common)) {
  260. Cache::tag("lang_code")->set("lang_code_app/shopapi/lang/" . $addon, $addon_cache_common);
  261. $cache_common = array_merge($cache_common, $addon_cache_common);
  262. }
  263. } catch (\Exception $e) {
  264. }
  265. }
  266. }
  267. $lang_path = isset($this->lang) ? $this->lang : '';
  268. if (!empty($lang_path)) {
  269. $cache_path = Cache::get("lang_code_" . $lang_path);
  270. if (empty($cache_path)) {
  271. $cache_path = include $lang_path . '/code.php';
  272. Cache::tag("lang")->set("lang_code_" . $lang_path, $cache_path);
  273. }
  274. $lang = array_merge($cache_common, $cache_path);
  275. } else {
  276. $lang = $cache_common;
  277. }
  278. return $lang;
  279. }
  280. /**
  281. * 检测权限
  282. */
  283. protected function checkAuth()
  284. {
  285. if (empty($addon)) {
  286. $auth_name = 'config/auth_shopapi.php';
  287. } else {
  288. $auth_name = 'addon/' . $addon . '/config/auth_shopapi.php';
  289. }
  290. $auth_array = require $auth_name;
  291. $this->url = strtolower($this->url);
  292. if ($this->group_info[ 'is_system' ] == 1) {
  293. return true;
  294. }
  295. if (!isset($auth_array[ $this->url ])) {
  296. return true;
  297. }
  298. $auth_control = event("AuthControl", [ 'key' => $auth_array[ $this->url ], 'app_module' => $this->app_module, 'ajax' => 1 ], 1);
  299. if (!empty($auth_control)) {
  300. if ($auth_control[ 'code' ] < 0) {
  301. return false;
  302. }
  303. }
  304. if (array_key_exists($this->url, $auth_array)) {
  305. if (strpos(',' . $this->group_info[ 'menu_array' ] . ',', ',' . $auth_array[ $this->url ] . ',')) {
  306. return true;
  307. } else {
  308. return false;
  309. }
  310. } else {
  311. return true;
  312. }
  313. }
  314. /**
  315. * 添加日志
  316. * @param unknown $action_name
  317. * @param unknown $data
  318. */
  319. protected function addLog($action_name, $data = [])
  320. {
  321. $user = new UserModel();
  322. $user->addUserLog($this->uid, $this->user_info[ 'username' ], $this->site_id, $action_name, $data);
  323. }
  324. }