123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- <?php
- namespace app;
- use app\services\system\store\SystemStoreServices;
- use think\exception\ValidateException;
- use think\facade\Lang;
- class Request extends \think\Request
- {
- private $adminInfo = null;
- private $user = null;
- private $adminStoreId = 0;
- private $adminStoreInfo = null;
- private $tokenData = null;
- private $longitude = 0;
- private $latitude = 0;
- private $lang = 'en-us';
- private $allow_lang = ['zh-cn', 'en-us'];
-
- protected $except = [
- 'menu_path', 'api_url', 'unique_auth',
- 'description', 'custom_form', 'product_detail_diy', 'value', 'member', 'product_category_diy'
- ];
- public function setAdmin($adminInfo)
- {
- $this->adminInfo = $adminInfo;
- if ($adminInfo['admin_type'] == 2) {
- $this->adminStoreId = $adminInfo['relation_id'];
- } else {
- $this->adminStoreId = 0;
- }
- }
- public function setLang($lang)
- {
- if (!in_array($lang, $this->allow_lang)) {
- $lang = 'en-us';
- }
- $this->lang = $lang;
- Lang::setLangSet($lang);
- if (file_exists(APP_PATH . 'lang/' . $lang . '/default.php'))
- Lang::load(APP_PATH . 'lang/' . $lang . '/default.php', $lang);
- if (file_exists(APP_PATH . 'lang/ ' . $lang . '/' . str_replace('.', '/', $this->controller()) . '.php'))
- Lang::load(APP_PATH . 'lang/ ' . $lang . '/' . str_replace('.', '/', $this->controller()) . '.php', $lang);
- }
- public function getLang()
- {
- return $this->lang;
- }
- public function setStoreInfo($storeId)
- {
-
- $storeService = app()->make(SystemStoreServices::class);
- $storeInfo = $storeService->get($storeId);
- if (!$storeInfo) {
- throw new ValidateException('店铺不存在');
- }
- $this->adminStoreInfo = $storeInfo;
- }
- public function setUser($user)
- {
- $this->user = $user;
- }
- public function setTokenData($data)
- {
- $this->tokenData = $data;
- }
- public function setLocation($latitude, $longitude)
- {
- $this->latitude = $latitude;
- $this->longitude = $longitude;
- }
-
- public function more(array $params, bool $suffix = false, callable $deal = null, int $update_id = 0, bool $filter = true): array
- {
- $p = [];
- $i = 0;
- foreach ($params as $param) {
- if (!is_array($param)) {
- $p[$suffix == true ? $i++ : $param] = $this->param($param);
- } else {
- if (!isset($param[1])) $param[1] = null;
- if (!isset($param[2])) $param[2] = '';
- if (!isset($param[3])) $param[3] = '';
- if (!isset($param[4])) $param[4] = '';
- if (is_array($param[0])) {
- $name = is_array($param[1]) ? $param[0][0] . '/a' : $param[0][0] . '/' . $param[0][1];
- $keyName = $param[0][0];
- } else {
- $name = is_array($param[1]) ? $param[0] . '/a' : $param[0];
- $keyName = $param[0];
- }
- $p[$suffix == true ? $i++ : ($param[3] ?: $keyName)] = $this->param($name, $param[1], $param[2]);
- }
- }
- if (!is_null($deal)) {
- $deal($p, $update_id);
- }
- if ($filter && $p) {
- $p = $this->filterArrayValues($p);
- }
- return $p;
- }
-
- public function filterArrayValues($array)
- {
- $result = [];
- foreach ($array as $key => $value) {
- if (is_array($value)) {
-
- $result[$key] = in_array($key, $this->except) ? $value : $this->filterArrayValues($value);
- } else {
- if (in_array($key, $this->except) || is_int($value) || is_null($value)) {
- $result[$key] = $value;
- } else {
-
- $result[$key] = filter_str($value);
- }
- }
- }
- return $result;
- }
-
- public function getMore(array $params, bool $suffix = false, callable $deal = null, bool $filter = true): array
- {
- return $this->more($params, $suffix, $deal, $filter);
- }
-
- public function postMore(array $params, bool $suffix = false, callable $deal = null, int $update_id = 0, bool $filter = true): array
- {
- return $this->more($params, $suffix, $deal, $update_id, $filter);
- }
-
- public function getFromType()
- {
- return $this->header('Form-type', '');
- }
-
- public function isTerminal(string $terminal)
- {
- return strtolower($this->getFromType()) === $terminal;
- }
-
- public function isH5()
- {
- return $this->isTerminal('h5');
- }
-
- public function isWechat()
- {
- return $this->isTerminal('wechat');
- }
-
- public function isRoutine()
- {
- return $this->isTerminal('routine');
- }
-
- public function isApp()
- {
- return $this->isTerminal('app');
- }
-
- public function isPc()
- {
- return $this->isTerminal('pc');
- }
-
- public function ip(): string
- {
- if ($this->server('HTTP_CLIENT_IP', '')) {
- $ip = $this->server('HTTP_CLIENT_IP', '');
- } elseif ($this->server('HTTP_X_REAL_IP', '')) {
- $ip = $this->server('HTTP_X_REAL_IP', '');
- } elseif ($this->server('HTTP_X_FORWARDED_FOR', '')) {
- $ip = $this->server('HTTP_X_FORWARDED_FOR', '');
- $ips = explode(',', $ip);
- $ip = $ips[0];
- } elseif ($this->server('REMOTE_ADDR', '')) {
- $ip = $this->server('REMOTE_ADDR', '');
- } else {
- $ip = '0.0.0.0';
- }
- return $ip;
- }
- public function isAdminLogin(): bool
- {
- return !is_null($this->adminInfo);
- }
- public function adminId()
- {
- return $this->adminInfo['id'] ?? 0;
- }
- public function adminInfo()
- {
- return $this->adminInfo ?: [];
- }
- public function user($key = null)
- {
- if ($key) {
- return $this->user[$key] ?? '';
- }
- return $this->user;
- }
- public function uid()
- {
- return $this->user['uid'] ?? 0;
- }
- public function tokenData()
- {
- return $this->tokenData;
- }
- public function isLogin()
- {
- return !is_null($this->user);
- }
- public function location()
- {
- return [$this->latitude, $this->longitude];
- }
- public function adminStoreId()
- {
- return $this->adminStoreId;
- }
- public function adminStoreInfo()
- {
- return $this->adminStoreInfo;
- }
- }
|