REAPI/app/core/Service.php

231 lines
5.5 KiB
PHP
Raw Normal View History

2024-09-29 15:43:18 +08:00
<?php
namespace app\core;
use think\admin\Service as Base;
use app\gateway\service\RedisService;
use app\gateway\service\AuthService;
use app\gateway\service\CurlService;
use dever\Log;
use think\Db;
/**
* 数据模型基类
* Class Cate
* @package app\core
*/
class Service extends Base
{
/**
* 应用实例
* @var Db
*/
protected $db;
/**
* 表名
* @var table
*/
protected $table = '';
/**
* 初始化服务
* @return $this
*/
protected function initialize()
{
parent::initialize();
}
public function db($table = false)
{
$table = $table ? $table : $this->table;
$this->db = $this->app->db->name($table);
return $this->db;
}
# 缓存
protected function cache($key, $value = false, $expire = 0)
{
// TODO 下面被注释
return false;
$redis = RedisService::getInstance();
if (!$value) {
return $redis->get($key);
} else {
if ($value == 'del') {
return $redis->delete($key);
} else {
return $redis->set($key, $value, $expire);
}
}
}
# 提交事务
protected function commit($callback, $data)
{
Db::startTrans();
try {
$msg = call_user_func($callback, $data);
if ($msg === true) {
Db::commit();
return 'success';
} else {
Db::rollback();
return myresponse(0, $msg);
}
} catch (\Exception $e) {
// 回滚事务
Db::rollback();
throw $e;
}
}
# 队列
protected function queue($key, $value = false)
{
$redis = RedisService::getInstance();
if (!$value) {
return $redis->pop($key);
} else {
return $redis->push($key, $value);
}
}
protected function getParam($param, $token, $sign_type = 1)
{
return AuthService::get($param, $token, $sign_type);
}
# 请求数据
protected function curl($method, $url, $param = array(), $json = false, $header = false)
{
if ($param) {
$log['type'] = 'request';
$log['url'] = $url;
$log['param'] = $param;
$this->log($log);
}
$curl = CurlService::getInstance($url, $param, $method, $json, $header);
$curl->setTimeOut(3600);
return $curl->result();
}
protected function maketime($v)
{
if (!$v) {
return '';
}
if (is_numeric($v)) {
return $v;
}
if (is_array($v)) {
$v = $v[1];
}
if (strstr($v, ' ')) {
$t = explode(' ', $v);
$v = $t[0];
$s = explode(':', $t[1]);
} else {
$s = array(0, 0, 0);
}
if (!isset($s[1])) {
$s[1] = 0;
}
if (!isset($s[2])) {
$s[2] = 0;
}
if (strstr($v, '-')) {
$t = explode('-', $v);
} elseif (strstr($v, '/')) {
$u = explode('/', $v);
$t[0] = $u[2];
$t[1] = $u[0];
$t[2] = $u[1];
}
if (!isset($t)) {
$t = array(0, 0, 0);
}
if (!isset($t[1])) {
$t[1] = 0;
}
if (!isset($t[2])) {
$t[2] = 0;
}
$v = mktime($s[0], $s[1], $s[2], $t[1], $t[2], $t[0]);
return $v;
}
# 获取手机运营商
public function isp($mobile)
{
return $this->getphonetype($mobile);
$url = 'http://cx.shouji.360.cn/phonearea.php?number=' . $mobile;
$data = $this->curl('get', $url);
if ($data) {
$data = json_decode($data, true);
if ($data && isset($data['data']['sp'])) {
$sp = $data['data']['sp'];
if ($sp == '移动') {
return 1;
} elseif ($sp == '联通') {
return 2;
} elseif ($sp == '电信') {
return 3;
}
}
}
return 1;
}
public function ispguishudi($mobile)
{
// return $this->getphonetype($mobile);
$url = 'http://cx.shouji.360.cn/phonearea.php?number=' . $mobile;
$data = $this->curl('get', $url);
if ($data) {
$data = json_decode($data, true);
return $data['data'];
}
return 1;
}
public function getphonetype($phone)
{
// var_dump($phone);die;
$phone = trim($phone);
// var_dump(111);die;
$isChinaMobile = "/^134[0-8]\d{7}$|^(?:13[5-9]|147|198|15[0-27-9]|172|195|178|18[2-478])\d{8}$/"; //移动方面最新答复
$isChinaUnion = "/^(?:13[0-2]|145|15[56]|166|176|196|175|18[56])\d{8}$/"; //向联通微博确认并未回复
$isChinaTelcom = "/^(?:133|153|191|190|193|162|177|199|173|18[019])\d{8}$/"; //1349号段 电信方面没给出答复,视作不存在
// $isChinaTelcom = "/^(?:133|153|191|190|193|162|177|199|173|18[019])\d{8}$/"; //1349号段 电信方面没给出答复,视作不存在
// $isOtherTelphone = "/^170([059])\\d{7}$/";//其他运营商
if(preg_match($isChinaMobile, $phone)){
return 1;
}elseif(preg_match($isChinaUnion, $phone)){
return 2;
}elseif(preg_match($isChinaTelcom, $phone)){
return 3;
}else{
return 1;
}
}
}