123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- <?php
- // 应用公共文件
- use think\facade\Filesystem;
- use think\facade\Config;
- use app\model\AdminTokenModel;
- use app\model\AdminModel;
- use think\exception\ValidateException;
- //返回响应数据
- if (!function_exists('json_show')) {
- function json_show(int $code = 0, string $message = '请求成功', array $data = [])
- {
- return json(['code' => $code, 'message' => $message, 'data' => $data]);
- }
- }
- //获取密码
- //@param $password string 密码
- //@param $salt string 盐值
- if (!function_exists('getPassword')) {
- function getPassword(string $password = '', string $salt = ''): string
- {
- return sha1($password . $salt);
- }
- }
- //获取token
- //@param $admin_id int 运营账号表id
- //@param $username string 账户
- //@param $salt string 盐值
- if (!function_exists('makeToken')) {
- function makeToken(int $admin_id = 0, string $username = '', string $salt = ''): string
- {
- $now = time();
- $str = $username . $salt . (string)$now;
- $token = base64_encode($str);
- AdminTokenModel::handleToken($admin_id, $token);
- return $token;
- }
- }
- //校验token
- //@param $salt string 盐值
- if (!function_exists('verifyToken')) {
- function verifyToken(string $token = '')
- {
- $has = AdminTokenModel::where(['token' => $token])->findOrEmpty();
- if ($has->isEmpty()) throw new ValidateException('token不存在');
- if (strtotime($has->expiretime) <= time()) throw new ValidateException('token已失效');
- $account = AdminModel::where(['id' => $has['adminid'], 'is_del' => AdminModel::$del_normal])->findOrEmpty();
- if ($account->isEmpty()) throw new ValidateException('未找到账户');
- if ($account->status != AdminModel::$status_normal) throw new ValidateException('账户已禁用');
- $token_str = base64_decode($token);
- $account_str = substr($token_str, 0, -10);
- if ($account_str == $account->username . $account->salt) {
- AdminTokenModel::where(['token' => $token])
- ->save(['expiretime' => date('Y-m-d H:i:s', time() + Config::get('common.expire'))]);
- return ['uid' => $account->id, 'uname' => $account->nickname, 'roleid' => $account->role_id];
- } else throw new ValidateException('账户token无效');
- }
- }
- //发送post的curl请求
- if (!function_exists('curl_request')) {
- function curl_request(string $url = '', array $data = [], array $header = [])
- {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
- curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- if ($data) {
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
- }
- curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
- if (!$header) $header = [
- 'uid:' . request()->development['id'],
- 'nickname:' . request()->development['contactor'],
- 'mobile:' . request()->development['mobile'],
- // 'email:',
- 'supplierNo:' . request()->development['supplierNo'],
- 'supplierName:' . request()->development['supplierName']
- ];
- curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
- curl_setopt($curl, CURLOPT_TIMEOUT, 10);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- $data = curl_exec($curl);
- if (curl_errno($curl)) {
- return curl_error($curl);
- }
- curl_close($curl);
- return $data;
- }
- }
- //上传图片
- if (!function_exists('upload_img')) {
- function upload_img($files)
- {
- $savename = [];
- $files = !is_array($files) ? [$files] : $files;
- //验证
- validate([
- 'imgFile' => [
- 'fileSize' => 10240000,
- 'fileExt' => 'jpg,jpeg,png,bmp,gif',
- 'fileMime' => 'image/jpeg,image/png,image/gif'
- ]
- ])->check(['imgFile' => $files]);
- $domain = request()->domain() . Config::get('filesystem.disks.public.url') . DIRECTORY_SEPARATOR;
- foreach ($files as $file) {
- $url = Filesystem::disk('public')->putFile('topic/' . date('Ymd'), $file, function () use ($file) {
- return str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName() . '_' . date('YmdHis'));
- });
- $name = str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName());
- $temp = ['url' => $domain . $url, 'name' => $name];
- $savename[] = $temp;
- }
- return $savename;
- }
- }
- //生成编码
- //@param $str string 前缀
- if (!function_exists('makeNo')) {
- function makeNo(string $str = ''): string
- {
- $date = date("ymdHis");
- // $year = date("Y")-2000;
- $msec = randomkeys(4);
- return $str . $msec . $date;
- }
- }
- //生成随机字符串
- //@param $length int 长度,默认4
- if (!function_exists('randomkeys')) {
- function randomkeys(int $length = 4): string
- {
- $returnStr = '';
- $pattern = 'abcdefghijklmnopqrstuvwxyz';//ABCDEFGHIJKLOMNOPQRSTUVWXYZ
- $min = 0;
- $max = strlen($pattern) - 1;
- for ($i = 0; $i < $length; $i++) {
- $returnStr .= $pattern[mt_rand($min, $max)]; //生成php随机数
- }
- return $returnStr;
- }
- }
- //上传图片
- /**
- * @param $files
- * @return array
- */
- if (!function_exists('UploadImg')) {
- function UploadImg($files): array
- {
- $savename = [];
- $files = !is_array($files) ? [$files] : $files;
- //验证
- validate([
- 'imgFile' => [
- 'fileSize' => 10240000,
- 'fileExt' => 'jpg,jpeg,png,bmp,gif',
- 'fileMime' => 'image/jpeg,image/png,image/gif'
- ]
- ])->check(['imgFile' => $files]);
- foreach ($files as $file) {
- $url = Filesystem::disk('public')
- ->putFile('topic/' . date("Ymd"), $file, function () use ($file) {
- return str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName() . "_" . date('YmdHis'));
- });
- $name = str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName());
- $temp = ["url" => $url, "name" => $name];
- $savename[] = $temp;
- }
- return $savename;
- }
- }
|