common.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Think.Admin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://think.ctolog.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/Think.Admin
  12. // +----------------------------------------------------------------------
  13. use service\DataService;
  14. use service\FileService;
  15. use service\NodeService;
  16. use Wechat\Loader;
  17. use think\Db;
  18. /**
  19. * 打印输出数据到文件
  20. * @param mixed $data
  21. * @param bool $replace
  22. * @param string|null $pathname
  23. */
  24. function p($data, $replace = false, $pathname = null)
  25. {
  26. is_null($pathname) && $pathname = RUNTIME_PATH . date('Ymd') . '.txt';
  27. $str = (is_string($data) ? $data : (is_array($data) || is_object($data)) ? print_r($data, true) : var_export($data, true)) . "\n";
  28. $replace ? file_put_contents($pathname, $str) : file_put_contents($pathname, $str, FILE_APPEND);
  29. }
  30. /**
  31. * 获取微信操作对象
  32. * @param string $type
  33. * @return \Wechat\WechatReceive|\Wechat\WechatUser|\Wechat\WechatPay|\Wechat\WechatScript|\Wechat\WechatOauth|\Wechat\WechatMenu|\Wechat\WechatMedia
  34. */
  35. function & load_wechat($type = '')
  36. {
  37. static $wechat = [];
  38. $index = md5(strtolower($type));
  39. if (!isset($wechat[$index])) {
  40. $config = [
  41. 'token' => sysconf('wechat_token'),
  42. 'appid' => sysconf('wechat_appid'),
  43. 'appsecret' => sysconf('wechat_appsecret'),
  44. 'encodingaeskey' => sysconf('wechat_encodingaeskey'),
  45. 'mch_id' => sysconf('wechat_mch_id'),
  46. 'partnerkey' => sysconf('wechat_partnerkey'),
  47. 'ssl_cer' => sysconf('wechat_cert_cert'),
  48. 'ssl_key' => sysconf('wechat_cert_key'),
  49. 'cachepath' => CACHE_PATH . 'wxpay' . DS,
  50. ];
  51. $wechat[$index] = Loader::get($type, $config);
  52. }
  53. return $wechat[$index];
  54. }
  55. /**
  56. * UTF8字符串加密
  57. * @param string $string
  58. * @return string
  59. */
  60. function encode($string)
  61. {
  62. list($chars, $length) = ['', strlen($string = iconv('utf-8', 'gbk', $string))];
  63. for ($i = 0; $i < $length; $i++) {
  64. $chars .= str_pad(base_convert(ord($string[$i]), 10, 36), 2, 0, 0);
  65. }
  66. return $chars;
  67. }
  68. /**
  69. * UTF8字符串解密
  70. * @param string $string
  71. * @return string
  72. */
  73. function decode($string)
  74. {
  75. $chars = '';
  76. foreach (str_split($string, 2) as $char) {
  77. $chars .= chr(intval(base_convert($char, 36, 10)));
  78. }
  79. return iconv('gbk', 'utf-8', $chars);
  80. }
  81. /**
  82. * 本地化网络图片
  83. * @param string $url
  84. * @return string
  85. */
  86. function local_image($url)
  87. {
  88. if (is_array(($result = FileService::download($url)))) {
  89. return $result['url'];
  90. }
  91. return $url;
  92. }
  93. /**
  94. * 设备或配置系统参数
  95. * @param string $name 参数名称
  96. * @param bool $value 默认是false为获取值,否则为更新
  97. * @return string|bool
  98. */
  99. function sysconf($name, $value = false)
  100. {
  101. static $config = [];
  102. if ($value !== false) {
  103. list($config, $data) = [[], ['name' => $name, 'value' => $value]];
  104. return DataService::save('SystemConfig', $data, 'name');
  105. }
  106. if (empty($config)) {
  107. $config = Db::name('SystemConfig')->column('name,value');
  108. }
  109. return isset($config[$name]) ? $config[$name] : '';
  110. }
  111. /**
  112. * RBAC节点权限验证
  113. * @param string $node
  114. * @return bool
  115. */
  116. function auth($node)
  117. {
  118. return NodeService::checkAuthNode($node);
  119. }
  120. /**
  121. * array_column 函数兼容
  122. */
  123. if (!function_exists("array_column")) {
  124. function array_column(array &$rows, $column_key, $index_key = null)
  125. {
  126. $data = [];
  127. foreach ($rows as $row) {
  128. if (empty($index_key)) {
  129. $data[] = $row[$column_key];
  130. } else {
  131. $data[$row[$index_key]] = $row[$column_key];
  132. }
  133. }
  134. return $data;
  135. }
  136. }