BaseStoreApi.php 11 KB

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