Пример #1
0
 /**
  * 首页报告
  * @date   2016-06-14T00:09:23+0800
  * @author cnzhangxl@foxmail.com
  * @return [type]                   [description]
  */
 public function actionReport()
 {
     $res = ['status' => '1', 'time' => date('H:i:s'), 'today_rise_decline' => ['labels' => [], 'data' => []]];
     $data = StockLog::find();
     $data->andWhere(['>', 'pricechange', 0]);
     $data->andWhere(['>', 'created_at', strtotime(date('Y-m-d'))]);
     $today_rise_arr = $data->select('count(*) as num,pici')->asArray()->groupBy('pici')->all();
     $today_rise_list = [];
     foreach ($today_rise_arr as $row) {
         $today_rise_list[$row['pici']] = $row['num'];
     }
     $data = StockLog::find();
     $data->andWhere(['<', 'pricechange', 0]);
     $data->andWhere(['>', 'created_at', strtotime(date('Y-m-d'))]);
     $today_decline_arr = $data->select('count(*) as num,pici')->asArray()->groupBy('pici')->all();
     $today_decline_list = [];
     foreach ($today_decline_arr as $row) {
         $today_decline_list[$row['pici']] = $row['num'];
     }
     $chae = [];
     foreach ($today_decline_list as $pici => $num) {
         if (isset($today_rise_list[$pici])) {
             $res['today_rise_decline']['labels'][] = $pici;
             $res['today_rise_decline']['data'][] = $today_rise_list[$pici] - $num;
         }
     }
     return json_encode($res);
 }
Пример #2
0
 /**
  * 同步当前股票信息
  */
 public function actionSyncStock()
 {
     $sleep = 5;
     while (true) {
         try {
             $hourm = intval(date('Hm'));
             if ($hourm > 900 && $hourm < 1135 || $hourm > 1300 && $hourm < 1510) {
                 $pici = time();
                 $url_arr = ['http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getHQNodeData?page=1&num=10000&sort=changepercent&asc=0&node=sz_a&symbol=&_s_r_a=page&time=' . time(), 'http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getHQNodeData?page=1&num=10000&sort=changepercent&asc=0&node=sh_a&symbol=&_s_r_a=page'];
                 foreach ($url_arr as $url) {
                     $return_arr = Common::reqUrl($url, [], true, 30, true);
                     if ($return_arr['content']) {
                         $arr = Common::ext_json_decode(mb_convert_encoding($return_arr['content'], "utf-8", "gbk"), true);
                         if ($arr) {
                             foreach ($arr as $row) {
                                 $Stock = Stock::find()->where(['symbol' => $row['symbol']])->one();
                                 if ($Stock) {
                                     $Stock->scenario = 'update';
                                 } else {
                                     $Stock = new Stock(['scenario' => 'create']);
                                     $Stock->updated_at = time();
                                 }
                                 $save_arr = [];
                                 $save_arr['Stock'] = $row;
                                 if ($Stock->id && $row['trade'] != $Stock->trade) {
                                     $StockLog = new StockLog(['scenario' => 'create']);
                                     $StockLog->stock_id = $Stock->id;
                                     $StockLog->pici = $pici;
                                     $StockLog->setAttributes($Stock->attributes);
                                     $StockLog->id = '';
                                     $StockLog->save();
                                 }
                                 if ($Stock->load($save_arr) && $Stock->save()) {
                                 }
                             }
                         }
                     }
                 }
             }
             sleep($sleep);
         } catch (Exception $e) {
         }
     }
 }