|
|
@@ -153,7 +153,7 @@ class util
|
|
|
Yii::$app->end();
|
|
|
}
|
|
|
|
|
|
- //输出json数据并结束运行
|
|
|
+ //输出json数据并结束运行
|
|
|
public static function encode($data)
|
|
|
{
|
|
|
echo Json::encode($data);
|
|
|
@@ -275,6 +275,13 @@ class util
|
|
|
public static function fail($msg = '操作失败')
|
|
|
{
|
|
|
Yii::info($msg);
|
|
|
+
|
|
|
+ $report = isset(Yii::$app->params['errorReport']) ? Yii::$app->params['errorReport'] : 0;
|
|
|
+ if ($report == 1) {
|
|
|
+ throw new \Exception($msg);
|
|
|
+ return fale;
|
|
|
+ }
|
|
|
+
|
|
|
self::encode(['code' => 0, 'msg' => $msg, 'data' => []]);
|
|
|
}
|
|
|
|
|
|
@@ -313,7 +320,7 @@ class util
|
|
|
{
|
|
|
|
|
|
$redis = Yii::$app->redis;
|
|
|
- return $redis->setex($key, $expire,time());
|
|
|
+ return $redis->setex($key, $expire, time());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -324,10 +331,9 @@ class util
|
|
|
public static function checkLock($key)
|
|
|
{
|
|
|
$redis = Yii::$app->redis;
|
|
|
- if($redis->get($key)){
|
|
|
+ if ($redis->get($key)) {
|
|
|
return true;
|
|
|
- }
|
|
|
- else{
|
|
|
+ } else {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
@@ -339,28 +345,28 @@ class util
|
|
|
* @param Int $waitTime 等待时间(超时时间) 秒
|
|
|
* @return Boolean
|
|
|
*/
|
|
|
- public static function lock($key, $expire = 5,$waitTime = 5)
|
|
|
+ public static function lock($key, $expire = 5, $waitTime = 5)
|
|
|
{
|
|
|
$redis = Yii::$app->redis;
|
|
|
- $isLock = $redis->setex($key,$expire, time() + $expire);
|
|
|
+ $isLock = $redis->setex($key, $expire, time() + $expire);
|
|
|
$time = microtime(true);
|
|
|
$exitTime = $waitTime + $time;
|
|
|
// 不能获取锁
|
|
|
if (!$isLock) {
|
|
|
// 判断锁是否过期 锁已过期 删除锁 重新获取 防止过期时间设置长,结束时没主动调unlock
|
|
|
$lockTime = $redis->get($key);
|
|
|
- if($lockTime && time()>$lockTime){
|
|
|
+ if ($lockTime && time() > $lockTime) {
|
|
|
self::unlock($key);
|
|
|
- $isLock = $redis->setex($key,$expire ,time() + $expire);
|
|
|
+ $isLock = $redis->setex($key, $expire, time() + $expire);
|
|
|
}
|
|
|
- if(!$isLock){
|
|
|
- while (true){
|
|
|
+ if (!$isLock) {
|
|
|
+ while (true) {
|
|
|
usleep(50);
|
|
|
- $isLock = $redis->setex($key,$expire ,time() + $expire);
|
|
|
- if($isLock){
|
|
|
+ $isLock = $redis->setex($key, $expire, time() + $expire);
|
|
|
+ if ($isLock) {
|
|
|
break;
|
|
|
}
|
|
|
- if( microtime(true) > $exitTime){
|
|
|
+ if (microtime(true) > $exitTime) {
|
|
|
//超时了
|
|
|
break;
|
|
|
}
|