common.php 来获取 token 来解析 uid
isPost());
define('IS_GET', request()->isGet());
class Common extends Controller
{
protected $header;
protected $uid;
protected $Language;
public $request_data;//当前请求数据
public $size = 10;
public $page = 1;
public $scale = 4;//小数位数
public function __construct(App $app = null)
{
parent::__construct($app);
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Credentials: true');
header("Access-Control-Request-Method:GET,POST");
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, AuthKey, sessionid,Lang,authkey,language");
$token = input('user_token', '');
if ($this->request->has('user_token')) {
$token = $this->request->param('user_token');
} elseif ($this->request->header('token')) {
$token = $this->request->header('token');
}
// cache('applogin_status_'.$token,1);
// cache('appusertoken_'.$this->uid,$token);
// die();
$type = input('authkey', 'app');
$this->Language = input('language', 'en');
if ($this->request->param('language')) {
$this->Language = $this->request->param('language');
} elseif ($this->request->header('language')) {
$this->Language = $this->request->header('language');
} else {
$this->Language = 'zh';
}
$this->getLanguage($this->Language);
//全局语言变量
request()->lang = in_array($this->Language, ['zh', 'en']) ? $this->Language : 'zh';
//dump($this->request->param(''));
if ($type == 'app') {
$this->uid = cache('applogin_status_' . $token);
$cache_token = cache('appusertoken_' . $this->uid);
if ($cache_token != $token) {
return resultArray(lang('NO_USER'), '', 102);
}
} else {
$this->uid = cache('login_status_' . $token);
$cache_token = cache('usertoken_' . $this->uid);
if ($cache_token != $token) {
return resultArray(lang('NO_USER'), '', 102);
}
}
$info = \app\common\model\User::where(['id' => $this->uid])->find();
if (!$info || $info['status'] == 0 || $info['is_deleted'] == 1) {
$this->uid = '';
}
//
$this->apiIpVisitLimit(Request::action());
//请求限制
//$this->apiVisitLimit($token);
$this->size = (input('size') > 0) ? (int)input('size', 10) : 10;
$this->page = (input('page') > 0) ? (int)input('page', 1) : 1;
$this->request_data = Request::param();
}
/**
* 检查是否缺少了必填参数
* @param array $arguments_require 需要的参数数组
* @author:WFS开发团队
* @date:2018/10/26 11:55
*/
public function checkArgumentMiss(array $arguments_require)
{
$arguments_request = array_keys(array_filter($this->request_data, function ($arr) {
//因为上面的array_filter函数默认也会过滤掉值为0的元素,但是接口可能会有0值,所以重写过滤函数,只过滤空和null值
if ($arr === '' || $arr === null) {
return false;
}
return true;
}));
if (!empty(array_diff($arguments_require, $arguments_request))) {
return argumentMiss();
}
}
/**
* 接口访问频率限制,以用户token为限制单位,每个token每秒请求的次数是有限的
* @param $token
* @author:LS GROUP
* @date:2019/7/29 16:53
*/
public function apiVisitLimit($token)
{
$second_visit_times = 30;//每秒请求接口次数限制
$redis_name = 'API_VISIT_LIMIT';
$time = time();
$key = $redis_name . '_' . $token . '_' . $time;//标识
if ($cache = cache($key)) {//如果该标识存在的话判断下标识对应的值(也就是访问次数)是不是大于限制次数
if (intval($cache) > $second_visit_times) {
exit(json_encode(['code' => 400, 'msg' => 'Requests are frequent. Please try again later', 'data' => '']));
}
//增加访问次数
cache($key, $cache + 1);
} else {//如果标识不存在的话,新增标识,并设置过期时间,防止redis中存储字段过多
cache($key, 1, 60);
}
}
/**
* 接口访问频率限制,以用户ip为限制单位,每个ip每秒请求的次数是有限的
* @param $route
* @author:gww-hs
* @date:2019/7/29 16:53
*/
public function apiIpVisitLimit($route)
{
$second_visit_times = 30;//每秒请求接口次数限制
$redis_name = 'IP_API_VISIT_LIMIT';
$time = time();
$ip = request()->ip();
$arr = [
'192.168.54.122',
'46.8.197.122',
'192.168.54.199',
'46.8.197.199',
'117.172.255.20',
];
if (!in_array($ip, $arr)) {
$key = $redis_name . '_' . $ip . '_' . $route . '_' . $time;//标识
//限制次数为5
$check = cache($key);
if ($check) {
cache($key, $check + 1);
$count = $check;
if ($count > $second_visit_times) {
exit(json_encode(['code' => 400, 'msg' => 'Requests are frequent. Please try again later', 'data' => '']));
}
} else {
//限制时间为60秒
cache($key, 1, 60);
}
}
}
/**
* 效验密码
* @param $uid
* @param $pwd
* @param string $type
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function checkpwd($uid, $pwd, $type = 'login')
{
$user = User::where(['id' => $uid])->field('password,pay_pwd')->find();
$pay_pwd = Rsa::decode($pwd);
$u_pay_pwd = Rsa::decode($user['pay_pwd']);
$u_pwd = Rsa::decode($user['password']);
if ($type == 'login') {
if ($pay_pwd != $u_pay_pwd) {//交易密码效验不正确
return showMsg(lang("PWD_ERROR"));
}
} else {
if (!$user['pay_pwd']) {//交易密码不存在
return showMsg(lang("SET_PAY_PWD"));
} else {
if ($u_pwd == $u_pay_pwd) {
return showMsg(lang("PWD_NOT_SAME"));
}
if ($pay_pwd != $u_pay_pwd) {//交易密码效验不正确
return showMsg(lang("PAY_PWD_ERROR"));
}
}
}
return showMsg('ok', 200, '');
}
}
order .php :
class Order extends Common
{
/**
* @var OrderLogic
*/
private $logic;
private $validate;
public function __construct(App $app = null)
{
parent::__construct($app);
$this->logic = new OrderLogic();
$this->validate = new OrderValidate();
}
//表单验证
/**
* 发布奖购订单
* @return array|string|Json
*/
public function awardGoods()
{
if (request()->isPost()) {
if (!$this->uid) return resultArray(lang('NO_USER'), "", 102);
$post = request()->post();
//表单验证
$this->validate = new OrderValidate();
$this->validate->scene('order')->check($post);
if ($this->validate->getError()) {
return resultArray(lang(strval($this->validate->getError())));
}
//调用发布
return $this->logic->createRewardOrder($this->uid, $post['goodsName'], $post['goodsImage'], $post['tags'], $post['price'], $post['introduction'], $post['contact'], $post['property'],$post['publish_area']);
}
return '';
}
在order.php 中能够获取到 $tgis->uid。
Fpe.php : 不能够获取到 $this->uid ::
class Fpe extends Common
{
public function __construct()
{
$this->businessAlliance = new BusinessFederationLogic();
$this->validate = new BusinessAlliance();
}
/**
* 域类型列表
* @return Json
*/
public function typesOption()
{
if (!$this->uid) return resultArray(lang('NO_USER'), "", 102);
$logic = new GroupLogic();
return $logic->getTypesLists($this->uid, $this->Language);
}
public function submit()
{
if(request()->isPost()){
$post = request()->post();
$this->validate->scene('submit')->check($post);
if ($this->validate->getError()) {
return resultArray(lang(strval($this->validate->getError())));
}
dump($this->uid);
dump('ssss');
die();
return $this->businessAlliance->submit($this->uid,$post['account'],$post['name'],$post['phone'],$post['province'],$post['city'],$post['zone'],$post['address'],$post['wechat'],$post['qq']);
}
return '';
}
打印的结果:
解决办法 :
在__construct 函数中 调用父 构造函数
public function __construct(App $app = null) { parent::__construct($app); $this->businessAlliance = new BusinessFederationLogic(); $this->validate = new BusinessAlliance(); }