工作部分功能实现

php解压
php压缩
php导入导出

浏览器兼容问题

一个是ie浏览器遇到下级弹窗不识别传过来的数据,还有就是猎豹极速模式貌似不识别隐藏域

积分清零

//yii框架 该文件ClearScoreCommand.php放在console文件夹里
class ClearScoreCommand extends ConsoleCommand
{

    /**
     * 中午12点推送消息,凌晨清积分
     */
    public function actionIndex()
    {
        $datetime = time();
        $dayString = date('d', $datetime);
        $hourString = date('Hi', $datetime);
        //提醒
        if ($hourString == '1200') $this->noonday($dayString, $datetime);  //设置redis

        //清除
        if ($hourString == '0000') $this->beforeDawn($datetime);//读取已经设置的redis,执行清楚逻辑

//        $hourString == 12 ? $this->noonday($dayString, $datetime) : $this->beforeDawn($datetime);

    }

    /**
     * 中午12点操作,推送用户信息
     * @param $dayString 当天
     * @param $datetime
     */
    public function noonday($dayString, $datetime)
    {
        $dataArray = [];//消息通知数组 3天后
        $clear = [];//

        //判断是否有积分清零的企业
        $clearTime = $this->clearTime($datetime);

        foreach ($clearTime as $k=> $v)
        {
            if ($dayString + 3 == date('d', $v['time'])){
                $dataArray[] = $v['id'];
            }

            if($dayString+1==date('d', $v['time'])){
                $clear[]=$v['id'];
            }
        }

        //有需要操作的企业
        if (!empty($dataArray))
        {
            //推送时用
            $this->dataWith($dataArray, 1, $datetime);
        }
        if (!empty($clear))
        {
            //设置凌晨需清除的企业
            Yii::app()->redis->set('clearScore', json_encode($clear));
        }

    }

    /**
     * 凌晨操作,清除用户积分
     * @param $datetime 当前时间
     */
    public function beforeDawn($datetime)
    {
        //获取需要操作的企业
        $dataObj = Yii::app()->redis->get('clearScore');
        var_dump($dataObj);
        if (!empty($dataObj))
        {
            $dataArray = json_decode($dataObj, true);

            //添加系统通知消息
            $this->dataWith($dataArray, 2, $datetime);

            //事务处理
            $conn = Yii::app()->db;
            $transaction = $conn->beginTransaction();

            try{
                //积分清除日志
                $scoreBool = $this->recordScoreLog($dataArray, $datetime);
                if (!$scoreBool)
                {
                    $transaction->rollback();die;
                }

                //清除用户积分
                $this->updateUserScore($dataArray, $datetime);

                //操作完之后清除缓存
                Yii::app()->redis->delete('clearScore');

                $transaction->commit();

            }catch (Exception $e){
                $transaction->rollback();
            }


        }
    }

    /**
     * 处理得到的数据,数组
     * @param $data
     * @param $type 1推送消息 2清除用户积分
     * @param $datetime
     */
    public function dataWith($data, $type, $datetime)
    {
        $extrasArray = array('type'=>7);
        if ($type == 1) $extrasArray['difference'] = 3;
        $title = $content = $type == 1 ? '积分清空提醒:您的积分将会在3天后清空,快快使用积分兑换商品吧~' : '积分清空提醒:您的积分已被清空~';

        foreach ($data as $k => $v)
        {
            $idString = $this->addMessage($title, $content, $datetime, $v, $type);
            if (!$idString) continue;
            $extrasArray['id'] = $idString;
            $extrasArray['eid'] = $v;

            //给用户推送消息
            $priseTag = Enterprise::model()->find("id =".$v);
            if (empty($priseTag->prise_tag)) continue;
            $this->messageJpush((array)$priseTag->prise_tag, $title, $extrasArray, $datetime);
        }
    }

    /**
     * 公开课积分清零时间
     */
    public function moboClearTime()
    {
        $sql = 'SELECT `value` FROM {{config}} WHERE `key` = "clearScoreTime"';
        $rowString = Yii::app()->db->createCommand($sql)->queryScalar();
        return $rowString;
    }

    /**
     * 满足积分清零时间的企业
     * @param $time
     */
    public function clearTime($time)
    {
        $sql = 'SELECT id,clearScoreTime `time` FROM {{enterprise}} WHERE clearScoreTime IS NOT NULL AND clearScoreTime > '.$time.' ORDER BY clearScoreTime ASC';
        $rowsArray = Yii::app()->db->createCommand($sql)->queryAll();
        if (empty($rowsArray)) return [];

        return $rowsArray;
    }


    /**
     * 增加消息记录
     * @param $title
     * @param $content
     * @param $datetime
     * @param $eid
     * @param $type 1表示积分清零前 2表示积分清零时
     * @param int $did
     * @param int $msgtype 企业标识
     */
    public function addMessage($title, $content, $datetime, $eid, $type, $did = 0, $msgtype = 1)
    {
        $conn = Yii::app()->db;
        $sql = 'SELECT COUNT(*) FROM {{message}} WHERE msgtype = 1 AND eid = '.$eid.' AND `type` = '.$type;
        $countString = $conn->createCommand($sql)->queryScalar();
        //未推送的推送,已经推送的忽略
        if (empty($countString))
        {
            $param = array(
                'title' => $title,
                'content' => $content,
                'did' => $did,
                'datetime' => $datetime,
                'msgtype' => $msgtype,
                'eid' => $eid,
                'type' => $type,
            );

            try{
                $messageModel = new Message();
                $messageModel->setAttributes($param,false);
                $messageModel->save();
                $idString = $messageModel->id;

                return $idString;
            }catch (Exception $e){
                $this->clearScoreLog($eid, $e->getMessage(), $datetime);
                return false;
            };
        }
    }

    /**
     * 给用户推送消息
     * @param $tag 推送的标识
     * @param $content 推送的内容
     * @param $extras
     */
    public function messageJpush($tag, $content, $extras, $datetime)
    {
        $JpushArray = Jpush::send($tag, $content, $extras, 2);
        if ($JpushArray['errcode'] != 0) $this->clearScoreLog($extras['eid'], $JpushArray['errmsg'], $datetime);
    }

    /**
     * 执行失败,记录日志
     * @param int $eid 企业id
     * @param $msg 报错信息
     * @param $datetime 报错时间
     */
    public function clearScoreLog($eid, $msg, $datetime)
    {
        $dir = $_SERVER['DOCUMENT_ROOT'] . '/runtime/' . 'clearScore' . '/' . date('Y/m/d', time());
        if(!is_dir($dir))
        {
            mkdir($dir,0777,true);
        }

        $dir = $dir . '/'. $eid . '.log';

        file_put_contents($dir, date('Y-m-d H:i:s', $datetime).' '.$msg.PHP_EOL, FILE_APPEND);
    }


    /**
     * 清除该企业所有用户的积分
     * @param $eid
     * @param $datetime
     */
    public function updateUserScore($eid, $datetime)
    {
        foreach ($eid as $k => $v)
        {


            try{
                $m=Userinfo::model()->findAll('eid=:eid',[':eid'=>$v]);
                foreach ($m as $v){
                    $v->score=0;
                    $v->save();
                }

            }catch (Exception $e){
                $this->clearScoreLog($v, $e->getMessage(), $datetime);
            };
        }

    }

    /**
     * 记录积分清除日志
     * @param $eid
     * @param $datetime
     */
    public function recordScoreLog($eid, $datetime)
    {
        $errMsg = [];
        foreach ($eid as $k => $v)
        {
            $table = Yii::getTableByNum('score_record', $v);

            $userScoreArray = $this->userScore($v);
            if (!empty($userScoreArray)) $errMsg[] = $this->addScoreRecord($table, $userScoreArray, $v, $datetime);
        }

        return in_array(0, $errMsg) ? false : true;
    }


    /**
     * 当前企业用户积分
     * @param $eid
     */
    public function userScore($eid)
    {
        $sql = 'SELECT uid,score FROM {{userinfo}} WHERE eid = '.$eid;
        return Yii::app()->db->createCommand($sql)->queryAll();
    }

    /**
     * 增加积分清零记录
     * @param $table
     * @param $data
     * @param $eid
     * @param $datetime
     */
    public function addScoreRecord($table, $data, $eid, $datetime)
    {
        try{

            foreach ($data as $k => $v)
            {

                $r=$model->save();

            }
            return 1;
        }catch (Exception $e)
        {
            $this->clearScoreLog($eid, $e->getMessage(), $datetime);

            return 0;
        }
    }
}
crantable配置文件加上
*/1 * * * *  /usr/local/php/bin/php(这部分的意思是用Php执行)  /mnt/wwwroot/x.xx.com/yiic.php ClearScore index

#

活动新增任务和去标签
表单

活动表

redis键过期机制

//yii框架 该文件TrainingFinishCommand.php放在console文件夹里  需要修改redis配置
<?php

class TrainingFinishCommand extends ConsoleCommand
{


    public $eid;
    public function actionUpdateLable(){
        $redisConfig=require(Yii::getPathOfAlias('redisCache_path'));
        $option=$redisConfig['options'];
        $host=$option['host'];
        $db=$option['database'];
        $redis=new Redis();
        $redis->connect($host,6379);
        ini_set('default_socket_timeout', -1);
        $redis->setOption(Redis::OPT_READ_TIMEOUT,-1);  //防止脚本超时
        set_time_limit(0);


        try {
            //键过期机制
            $redis->psubscribe(array('__keyevent@'.$db.'__:expired'),array($this,'psCallback'));
        } catch (Exception $e) {
            $redis->psubscribe(array('__keyevent@'.$db.'_:expired'),array($this,'psCallback'));
        }

    }
    function psCallback($redis, $pattern, $chan, $msg){
        $len=strlen($msg);
        echo $msg;
        //在生成一个活动时同时根据结束时间生成updateLable:$id的redis(带expire)
        if(strpos($msg,'updateLable')!==false){
            echo $msg;
            $id=substr($msg,12,$len);
            Training::UpdateUserLable($id);
        }
    }
     public function actioncheckNohup()
      {
          //定时任务
          $data=system('ps -fe|grep TrainingFinish |grep -v grep | wc -l');

          var_dump($data);
            //TrainingFinish 守护进程挂了

           if($data=='1'){
                $logFile = $this->mkDirs('appapi/console', '', 4);
                exec(" nohup /usr/local/php/bin/php " . ROOT_PATH . "/yiic.php TrainingFinish   UpdateLable >>$logFile &");
        }
    }
}
//linux开个守护进程
nohup php yiic.php TrainingFinish UpdateLable
system函数需要修改php.ini 搜索/exec
checkNohup为定时任务防止TrainingFinish守护进程挂掉

该方法后换成定时任务+redis sort解决 老方法会导致mysql挂掉

#

即时通讯
#
打算做个小网站

打算学习vue和,socket,nginx,php oauth2.0,laravel框架

书还是要继续坚持看
打算做个个人博客

可以转载,但必须说明出处

发表评论