common.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <?php
  2. // 应用公共文件
  3. use think\facade\Filesystem;
  4. use think\facade\Config;
  5. use think\facade\Cache;
  6. use think\facade\Log;
  7. use app\model\AdminTokenModel;
  8. use app\model\AdminModel;
  9. use think\exception\ValidateException;
  10. use app\model\CommonModel;
  11. //返回响应数据
  12. if (!function_exists('json_show')) {
  13. function json_show(int $code = 0, string $message = '请求成功', array $data = [])
  14. {
  15. return json(['code' => $code, 'message' => $message, 'data' => $data]);
  16. }
  17. }
  18. //获取加密后的密码
  19. //@param $password string 密码
  20. //@param $salt string 盐值
  21. if (!function_exists('get_password')) {
  22. function get_password(string $password = '', string $salt = ''): string
  23. {
  24. return sha1($password . $salt);
  25. }
  26. }
  27. //获取token
  28. //@param $admin_id int 运营账号表id
  29. //@param $username string 账户
  30. //@param $salt string 盐值
  31. if (!function_exists('make_token')) {
  32. function make_token(int $admin_id = 0, string $username = '', string $salt = ''): string
  33. {
  34. $now = time();
  35. $str = $username . $salt . (string)$now;
  36. $token = base64_encode($str);
  37. AdminTokenModel::handleToken($admin_id, $token);
  38. return $token;
  39. }
  40. }
  41. //校验token
  42. //@param $salt string 盐值
  43. if (!function_exists('verify_token')) {
  44. function verify_token(string $token = '')
  45. {
  46. $has = AdminTokenModel::where(['token' => $token])->findOrEmpty();
  47. if ($has->isEmpty()) throw new ValidateException('token不存在');
  48. if (strtotime($has->expiretime) <= time()) throw new ValidateException('token已失效');
  49. $account = AdminModel::where(['id' => $has['adminid'], 'is_del' => CommonModel::$del_normal])->findOrEmpty();
  50. if ($account->isEmpty()) throw new ValidateException('未找到账户');
  51. if ($account->status != CommonModel::$status_normal) throw new ValidateException('账户已禁用');
  52. $token_str = base64_decode($token);
  53. $account_str = substr($token_str, 0, -10);
  54. if ($account_str == $account->username . $account->salt) {
  55. AdminTokenModel::where(['token' => $token])
  56. ->save(['expiretime' => date('Y-m-d H:i:s', time() + Config::get('common.expire'))]);
  57. return ['uid' => $account->id, 'uname' => $account->nickname, 'roleid' => $account->role_id];
  58. } else throw new ValidateException('账户token无效');
  59. }
  60. }
  61. //发送post的curl请求
  62. if (!function_exists('curl_request')) {
  63. function curl_request(string $url = '', array $data = [], array $header = [])
  64. {
  65. $curl = curl_init();
  66. curl_setopt($curl, CURLOPT_URL, $url);
  67. curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
  68. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  69. curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
  70. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  71. if ($data) {
  72. curl_setopt($curl, CURLOPT_POST, 1);
  73. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
  74. }
  75. curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
  76. if (!$header) $header = [
  77. 'uid:' . request()->development['id'],
  78. 'nickname:' . request()->development['contactor'],
  79. 'mobile:' . request()->development['mobile'],
  80. // 'email:',
  81. 'supplierNo:' . request()->development['supplierNo'],
  82. 'supplierName:' . request()->development['supplierName']
  83. ];
  84. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  85. curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  86. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  87. $data = curl_exec($curl);
  88. if (curl_errno($curl)) {
  89. return curl_error($curl);
  90. }
  91. curl_close($curl);
  92. return $data;
  93. }
  94. }
  95. //生成编码
  96. //@param $str string 前缀
  97. if (!function_exists('make_no')) {
  98. function make_no(string $str = ''): string
  99. {
  100. $date = date("ymdHis");
  101. // $year = date("Y")-2000;
  102. $msec = randomkeys(4);
  103. return $str . $msec . $date;
  104. }
  105. }
  106. //生成随机字符串
  107. //@param $length int 长度,默认4
  108. if (!function_exists('randomkeys')) {
  109. function randomkeys(int $length = 4): string
  110. {
  111. $returnStr = '';
  112. $pattern = 'abcdefghijklmnopqrstuvwxyz';//ABCDEFGHIJKLOMNOPQRSTUVWXYZ
  113. $min = 0;
  114. $max = strlen($pattern) - 1;
  115. for ($i = 0; $i < $length; $i++) {
  116. $returnStr .= $pattern[mt_rand($min, $max)]; //生成php随机数
  117. }
  118. return $returnStr;
  119. }
  120. }
  121. //上传图片
  122. /**
  123. * @param $files
  124. * @return array
  125. */
  126. if (!function_exists('upload_img')) {
  127. function upload_img($files): array
  128. {
  129. $savename = [];
  130. $files = !is_array($files) ? [$files] : $files;
  131. //验证
  132. validate([
  133. 'imgFile' => [
  134. 'fileSize' => 10240000,
  135. 'fileExt' => 'jpg,jpeg,png,bmp,gif',
  136. 'fileMime' => 'image/jpeg,image/png,image/gif'
  137. ]
  138. ])->check(['imgFile' => $files]);
  139. foreach ($files as $file) {
  140. $url = Filesystem::disk('public')
  141. ->putFile('topic/' . date("Ymd"), $file, function () use ($file) {
  142. return str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName() . "_" . date('YmdHis'));
  143. });
  144. $name = str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName());
  145. $temp = ["url" => $url, "name" => $name];
  146. $savename[] = $temp;
  147. }
  148. return $savename;
  149. }
  150. }
  151. //导出文件成压缩包
  152. if (!function_exists('excel_save')) {
  153. function excel_save($fileName = '', $headArr = [], $data = [])
  154. {
  155. $objPHPExcel = new PHPExcel();
  156. $objPHPExcel->getProperties();
  157. $keyA = 0; // 设置表头
  158. foreach ($headArr as $v) {
  159. $colum = PHPExcel_Cell::stringFromColumnIndex($keyA);
  160. $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);
  161. $keyA += 1;
  162. }
  163. $column = 2;
  164. $objActSheet = $objPHPExcel->getActiveSheet();
  165. foreach ($data as $key => $rows) { // 行写入
  166. $span = 0;
  167. foreach ($rows as $keyName => $value) { // 列写入
  168. //判断数据是否有数组,如果有数组,转换成字符串
  169. if (is_array($value)) $value = implode("、", $value);
  170. $objActSheet->setCellValue(PHPExcel_Cell::stringFromColumnIndex($span) . $column, $value);
  171. $span++;
  172. }
  173. $column++;
  174. }
  175. // var_dump($objActSheet->getActiveCell());
  176. $file = $fileName . ".xls";
  177. //$fileName .= "_" . date("Y_m_d", Request()->instance()->time()) . ".xls";
  178. //$fileName = iconv("utf-8", "gb2312", $fileName); // 重命名表
  179. $dir = root_path() . 'public/storage/report/' . date("YmdHis") . "/";
  180. if (!is_dir($dir)) mkdir($dir, 0777, true);
  181. PHPExcel_Settings::setCacheStorageMethod(PHPExcel_CachedObjectStorageFactory::cache_in_memory_serialized);
  182. $objPHPExcel->setActiveSheetIndex(0); // 设置活动单指数到第一个表,所以Excel打开这是第一个表
  183. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  184. $objWriter->save($dir . $file); // 文件通过浏览器下载
  185. $url = $dir . $file;
  186. if (!file_exists($url)) throw new \think\Exception('文件生成失败');
  187. $saveDir = root_path() . "public/storage/zip/";
  188. if (!is_dir($saveDir)) mkdir($saveDir, 0777, true);
  189. // $datetime = date("YmdHis");
  190. $file_dir = $saveDir . $fileName . ".zip";
  191. # 5.1 文件打包,提示:使用本类,linux需开启zlib,windows需取消php_zip.dll前的注释
  192. $zip = new \ZipArchive ();
  193. # 5.2 文件不存在则生成一个新的文件 用CREATE打开文件会追加内容至zip
  194. if ($zip->open($file_dir, \ZipArchive::OVERWRITE) !== true && $zip->open($file_dir, \ZipArchive::CREATE) !== true) throw new \think\Exception('无法打开文件或者文件创建失败');
  195. # 5.3 批量写入压缩包
  196. $zip->addEmptyDir($fileName);
  197. // @$zip->addFile($v['file_path'], 'resume'.DIRECTORY_SEPARATOR.basename($headername));
  198. @$zip->addFile($url, $fileName . DIRECTORY_SEPARATOR . basename($url));
  199. # 5.4 关闭压缩包写入
  200. $zip->close();
  201. @del_dir($dir);
  202. # 6. 检查文件是否存在,并输出文件
  203. if (!file_exists($file_dir)) throw new \think\Exception('导出文件不存在');
  204. ob_clean();
  205. flush();
  206. header("Cache-Control: max-age=0");
  207. header("Content-Description: File Transfer");
  208. header('Content-disposition: attachment; filename=' . basename($file_dir)); # 处理文件名
  209. header("Content-Type: application/octet-stream"); # 流文件输出
  210. header("Content-Transfer-Encoding: binary"); # 告诉浏览器,这是二进制文件
  211. // header('Content-Length: ' . filesize($file_dir)); # 告诉浏览器,文件大小
  212. // readfile($file_dir); # 输出文件
  213. $res = read_big_file($file_dir);
  214. foreach ($res as $val) {
  215. echo $val;
  216. }
  217. @ unlink($file_dir);
  218. exit();
  219. }
  220. }
  221. //读取大文件
  222. if (!function_exists('read_big_file')) {
  223. function read_big_file(string $file = '')
  224. {
  225. $handle = fopen($file, 'rb');
  226. while (feof($handle) === false) {
  227. yield fgets($handle);
  228. }
  229. fclose($handle);
  230. }
  231. }
  232. //递归删除目录内的所有文件和本目录
  233. if (!function_exists('del_dir')) {
  234. function del_dir($path)
  235. {
  236. //如果是目录则继续
  237. if (is_dir($path)) {
  238. //扫描一个文件夹内的所有文件夹和文件并返回数组
  239. $p = scandir($path);
  240. //如果 $p 中有两个以上的元素则说明当前 $path 不为空
  241. if (count($p) > 2) {
  242. foreach ($p as $val) {
  243. //排除目录中的.和..
  244. if ($val != "." && $val != "..") {
  245. //如果是目录则递归子目录,继续操作
  246. if (is_dir($path . $val)) {
  247. //子目录中操作删除文件夹和文件
  248. del_dir($path . $val . '/');
  249. } else {
  250. //如果是文件直接删除
  251. unlink($path . $val);
  252. }
  253. }
  254. }
  255. }
  256. }
  257. //删除目录
  258. return rmdir($path);
  259. }
  260. }
  261. //根据省市区编码获取省市区名称
  262. if (!function_exists('get_addr_name')) {
  263. function get_addr_name(string $addr_code = ''): string
  264. {
  265. $rs = \think\facade\Db::name('area')
  266. ->whereIn('code', $addr_code)
  267. ->column('name');
  268. return implode('', $rs);
  269. }
  270. }
  271. //根据省市区编码获取省市区名称,返回数组
  272. if (!function_exists('get_addr_name_array')) {
  273. function get_addr_name_array(string $addr_code = ''): array
  274. {
  275. return \think\facade\Db::name('area')
  276. ->whereIn('code', $addr_code)
  277. ->column('name');
  278. }
  279. }
  280. /**
  281. * 获取微信操作对象(单例模式)
  282. * @staticvar array $wechat 静态对象缓存对象
  283. * @param type $type 接口名称 ( Card|Custom|Device|Extend|Media|Oauth|Pay|Receive|Script|User )
  284. * @return \Wehcat\WechatReceive 返回接口对接
  285. */
  286. if (!function_exists('load_wechat')) {
  287. function & load_wechat(string $type = '')
  288. {
  289. static $wechat = array();
  290. $index = md5(strtolower($type));
  291. if (!isset($wechat[$index])) {
  292. // 定义微信公众号配置参数(这里是可以从数据库读取的哦)
  293. $options = array(
  294. 'token' => '', // 填写你设定的key
  295. 'appid' => '', // 填写高级调用功能的app id, 请在微信开发模式后台查询
  296. 'appsecret' => '', // 填写高级调用功能的密钥
  297. 'encodingaeskey' => '', // 填写加密用的EncodingAESKey(可选,接口传输选择加密时必需)
  298. 'mch_id' => '', // 微信支付,商户ID(可选)
  299. 'partnerkey' => '', // 微信支付,密钥(可选)
  300. 'ssl_cer' => '', // 微信支付,双向证书(可选,操作退款或打款时必需)
  301. 'ssl_key' => '', // 微信支付,双向证书(可选,操作退款或打款时必需)
  302. 'cachepath' => '', // 设置SDK缓存目录(可选,默认位置在Wechat/Cache下,请保证写权限)
  303. );
  304. Wechat\Loader::config($options);
  305. //修改sdk的缓存方式
  306. // 缓存写入
  307. // $name 缓存字段名称
  308. // $value 缓存字段内容值
  309. // $expired 缓存有效时间(单位秒),0 表示永久缓存
  310. Wechat\Loader::register('CacheSet', function ($name, $value, $expired) {
  311. return Cache::set($name, $value, $expired);
  312. });
  313. //缓存读取
  314. // $name 缓存字段名称,一定要有return回去哦
  315. Wechat\Loader::register('CacheGet', function ($name) {
  316. return Cache::get($name);
  317. });
  318. //缓存删除
  319. // $name 缓存字段名称
  320. Wechat\Loader::register('CacheDel', function ($name) {
  321. return Cache::delete($name);
  322. });
  323. //日志记录
  324. // $line 单行日志内容
  325. // $filename 日志的文件(可以重新定义)
  326. Wechat\Loader::register('CachePut', function ($line, $filename) {
  327. return Log::record($line, 'notice');
  328. });
  329. $wechat[$index] = Wechat\Loader::get($type);
  330. }
  331. return $wechat[$index];
  332. }
  333. }