common.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. use think\Env;
  3. /**
  4. * 检查权限
  5. * @param $userId int 要检查权限的用户 ID
  6. * @param $name string|array 需要验证的规则列表,支持逗号分隔的权限规则或索引数组
  7. * @param $relation string 如果为 'or' 表示满足任一条规则即通过验证;如果为 'and'则表示需满足所有规则才能通过验证
  8. * @return boolean 通过验证返回true;失败返回false
  9. */
  10. function cmf_auth_check($userId, $name = null, $relation = 'or')
  11. {
  12. if (empty($userId)) {
  13. return false;
  14. }
  15. if ($userId == 1) {
  16. return true;
  17. }
  18. $authObj = new \app\common\logic\Auth();
  19. if (empty($name)) {
  20. $request = request();
  21. $module = $request->module();
  22. $controller = $request->controller();
  23. $action = $request->action();
  24. $name = strtolower($module . "/" . $controller . "/" . $action);
  25. }
  26. return $authObj->check($userId, $name, $relation);
  27. }
  28. /**
  29. * 获取当前登录的管理员ID
  30. * @return int
  31. */
  32. function mg_get_current_admin_id()
  33. {
  34. return session('ADMIN_ID');
  35. }
  36. /**
  37. * 获取游戏名称
  38. */
  39. function get_game_nickname($id)
  40. {
  41. $info = \Think\Db::name('cy_game')->field('name')->where(['id' => $id])->find();
  42. return $info['name'];
  43. }
  44. /**
  45. * 获取当前渠道名称
  46. */
  47. function get_current_channel_name($id)
  48. {
  49. $info = \Think\Db::name('nw_channel')->field('name')->where(['id' => $id])->find();
  50. return $info['name'];
  51. }
  52. /**
  53. * 根据权限是否显示UI
  54. *
  55. * @param bool $show
  56. * @param string $rule_name
  57. * @return bool
  58. */
  59. function authUI($rule_name = '', $show = false)
  60. {
  61. return $show ?: cmf_auth_check(session('ADMIN_ID'), $rule_name);
  62. }
  63. /**
  64. * 获取渠道
  65. */
  66. function get_channel_arr($channelId)
  67. {
  68. $result = [];
  69. $level = 1;
  70. $channelModel = \think\Db::name('nw_channel');
  71. while ($info = $channelModel->field('name,parent_id')->where(['id' => $channelId])->find()) {
  72. $info['level'] = $level;
  73. $result[] = $info;
  74. if ($info['parent_id'] == 0) {
  75. return array_reverse($result);
  76. }
  77. $channelId = $info['parent_id'];
  78. $level++;
  79. }
  80. }
  81. /**
  82. * AES加密
  83. * @param string $str 需加密的字符串
  84. * @param string $key 秘钥
  85. *
  86. * @return string
  87. */
  88. function aesEncode($str, $key)
  89. {
  90. $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  91. $ivStr = strtoupper(md5($key));
  92. $iv = substr($ivStr, 0, 16);
  93. mcrypt_generic_init($td, $key, $iv);
  94. $b = mcrypt_generic($td, padPkcs5($str, 16));
  95. mcrypt_generic_deinit($td);
  96. mcrypt_module_close($td);
  97. return bin2hex($b);
  98. }
  99. /**
  100. * AES解密
  101. * @param string $str 需解密的字符串
  102. * @param string $key 秘钥
  103. *
  104. * @return string
  105. */
  106. function aesDecode($str, $key)
  107. {
  108. $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  109. $ivStr = strtoupper(md5($key));
  110. $iv = substr($ivStr, 0, 16);
  111. mcrypt_generic_init($td, $key, $iv);
  112. $a = unpadPkcs5(mdecrypt_generic($td, hex2bin($str)));
  113. mcrypt_generic_deinit($td);
  114. mcrypt_module_close($td);
  115. return $a;
  116. }
  117. function padPkcs5($text, $blocksize)
  118. {
  119. $pad = $blocksize - (strlen($text) % $blocksize);
  120. return $text . str_repeat(chr($pad), $pad);
  121. }
  122. function unpadPkcs5($text)
  123. {
  124. $pad = ord($text{strlen($text) - 1});
  125. if ($pad > strlen($text)) {
  126. return false;
  127. }
  128. if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {
  129. return false;
  130. }
  131. return substr($text, 0, -1 * $pad);
  132. }
  133. /**
  134. * 当传递的value为空时,返回设置的默认值
  135. * @param mixed $value
  136. * @param mixed $default
  137. * @return mixed
  138. */
  139. function setDefault($value, $default = '---')
  140. {
  141. if (empty($value)) {
  142. return $default;
  143. } else {
  144. return $value;
  145. }
  146. }
  147. /**
  148. * 获取一级渠道列表
  149. */
  150. function getChannelByTop()
  151. {
  152. $parent_id = config('TOP_CHANNEL_ID');
  153. $channel_list = model('Common/Channel')->where('parent_id', "{$parent_id}")->select();
  154. $result = [];
  155. foreach ($channel_list as $v) {
  156. $result[$v['id']] = $v['name'];
  157. }
  158. return $result;
  159. }
  160. /**
  161. * 获取日期查询条件
  162. * @param int $start_time 开始时间
  163. * @param int $end_time 结束时间
  164. * @param bool $isdefault 是否默认今天日期
  165. * @return
  166. */
  167. function getTimeCondition($start_time, $end_time, $isdefault = true)
  168. {
  169. $time = [];
  170. //开始时间和结束时间不为空时
  171. if ($start_time != '' && $end_time != '') {
  172. $time = [
  173. ['>=', strtotime($start_time)],
  174. ['<=', strtotime($end_time . ' 23:59:59')],
  175. ];
  176. } //开始时间不为空时
  177. elseif ($start_time != '') {
  178. $time = ['>=', strtotime($start_time)];
  179. } //结束时间不为空时
  180. elseif ($end_time != '') {
  181. $time = ['<=', strtotime($end_time . ' 23:59:59')];
  182. } else {
  183. if ($isdefault) {
  184. $start_time = $end_time = date('Y-m-d', time());
  185. $time = [
  186. ['>=', strtotime($start_time)],
  187. ['<=', strtotime($end_time . ' 23:59:59')],
  188. ];
  189. }
  190. }
  191. return $time;
  192. }
  193. /**
  194. * 获取日期查询条件
  195. * @param int $start_time 开始时间
  196. * @param int $end_time 结束时间
  197. * @param bool $isdefault 是否默认今天日期
  198. * @return
  199. */
  200. function getDateTimeCondition($start_time, $end_time, $isdefault = true)
  201. {
  202. $time = [];
  203. //开始时间和结束时间不为空时
  204. if ($start_time != '' && $end_time != '') {
  205. $time = [
  206. ['>=', $start_time],
  207. ['<=', $end_time],
  208. ];
  209. } //开始时间不为空时
  210. elseif ($start_time != '') {
  211. $time = ['>=', $start_time];
  212. } //结束时间不为空时
  213. elseif ($end_time != '') {
  214. $time = ['<=', $end_time];
  215. } else {
  216. if ($isdefault) {
  217. $start_time = $end_time = date('Y-m-d', time());
  218. $time = [
  219. ['>=',$start_time],
  220. ['<=', $end_time],
  221. ];
  222. }
  223. }
  224. return $time;
  225. }
  226. // 获取env配置参数
  227. function getEnvs($name, $default = ''){
  228. return Env::get($name, $default);
  229. }
  230. /**
  231. * 数组键增加前缀
  232. *
  233. * @param array $array 处理的数组
  234. * @param string $prefix 新增的前缀
  235. *
  236. * @return array
  237. */
  238. function addPrefixToKeys(array $array, string $prefix): array {
  239. // 为所有键添加前缀
  240. $prefixedKeys = array_map(function($key) use ($prefix) {
  241. return $prefix . $key;
  242. }, array_keys($array));
  243. // 使用新键名和原值创建新数组
  244. return array_combine($prefixedKeys, $array);
  245. }
  246. /**
  247. * 获取未来时间戳与当前时间之间的剩余天数
  248. *
  249. * @param int $futureTimestamp 目标时间戳(秒)
  250. * @return int|string 剩余天数;如果时间已过或无效,返回空字符串
  251. */
  252. function getRemainingDays(int $futureTimestamp) {
  253. // 当前时间戳
  254. $now = time();
  255. // 判断目标时间是否早于当前时间
  256. if ($futureTimestamp <= $now) {
  257. return "-";
  258. }
  259. // 计算剩余秒数,并换算为天数(向上取整)
  260. $remainingSeconds = $futureTimestamp - $now;
  261. $remainingDays = (int)ceil($remainingSeconds / (60 * 60 * 24));
  262. return $remainingDays;
  263. }