123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2017/12/5
- * Time: 15:07
- */
- namespace App;
- use Illuminate\Database\Eloquent\Model;
- use PHPExcel;
- use PHPExcel_Writer_Excel2007;
- class FxOrder extends Model
- {
- public $timestamps = false;
- protected $table = "fx_order";
-
- public static function olist(){
- $result = FxOrder::orderBy('id', 'desc')->get();
- return json_decode(json_encode($result),true);
- }
- # 生成外部订单号
- public static function createOuterCode(){
- $order_sn = 'fx'.rand(100, 9999). substr(time(), 5, 5). rand(10, 9999);
- return $order_sn;
- }
- /*更新城市天气信息*/
- public static function updateWeather($order_id) {
- $redisKey = 'seafood_order_city_weather';
- $appKey = self::getAppKey();
- //查询订单收件人所在的城市(过滤关键字 市)
- $city = self::where('id', $order_id)->pluck('receiverCity');
- $city = str_replace('市','',$city);
- $specialCity = array('朝阳区','嘉定区','海淀区','昌平区','密云区','大兴区','房山区','通州区','普陀区',);
- if(in_array($city, $specialCity)) {
- $city = str_replace('区','',$city);
- }
- //查询redis中是否有该键,若没有则查询
- $weather = RedisModel::get($redisKey.'_'.$city);
- if($weather) {
- $sellerMemo = self::where('id', $order_id)->pluck('sellerMemo');
- if(strstr($sellerMemo, $city.'天气状况:'.$weather)) {
- return true;
- }
- $sellerMemo = $sellerMemo.' '.$city.'天气状况:'.$weather;
- self::where('id', $order_id)->update(['sellerMemo'=>$sellerMemo]);
- } else {
- $url = 'http://apis.juhe.cn/simpleWeather/query?city='.urlencode($city).'&key='.$appKey;
- $result = file_get_contents($url);
- $result = json_decode($result, true);
- if($result['error_code'] == '0') {
- //将数据缓存到redis里
- $today = date('Y-m-d',time());
- $tomorrow = date('Y-m-d',strtotime('+1 days'));
- $weather = '';
- foreach ($result['result']['future'] as $value) {
- if($value['date'] == $today) {
- $weather .= $today.'日气温:'.$value['temperature'].'; ';
- }
- if($value['date'] == $tomorrow) {
- $weather .= $tomorrow.'日气温:'.$value['temperature'];
- }
- }
- RedisModel::set($redisKey.'_'.$city, $weather);
- $endTime = strtotime(date('Y-m-d 00:00:00',strtotime('+1 days'))) -1;
- $expire = $endTime - time();
- RedisModel::expire($redisKey.'_'.$city, $expire);
- $sellerMemo = self::where('id', $order_id)->pluck('sellerMemo');
- if(strstr($sellerMemo, $city.'天气状况:'.$weather)) {
- return true;
- }
- $sellerMemo = $sellerMemo.' '.$city.'天气状况:'.$weather;
- self::where('id', $order_id)->update(['sellerMemo'=>$sellerMemo]);
- } else if($result['error_code'] == '10012'){
- //判断是否为第二个APPkey也用完了
- $key = (string) RedisModel::get('seafood_order_city_weather_key');
- if($key == '0') {
- //超过100次/天上限 更换APPKey
- RedisModel::set('seafood_order_city_weather_key', '1');
- $endTime = strtotime(date('Y-m-d 00:00:00',strtotime('+1 days'))) -1;
- $expire = $endTime - time();
- RedisModel::expire('seafood_order_city_weather_key', $expire);
- self::updateWeather($order_id);
- }
- }
- }
- return true;
- }
- public static function getAppKey() {
- $redisKey = 'seafood_order_city_weather_key';
- $appKeyArr = ['32c8f844f8055f70f702fc28ec930e52', '85bfc19adb25998b2559d7ceba950e13'];
- $appKey = (string)RedisModel::get($redisKey);
- if($appKey != '') {
- return $appKeyArr[$appKey];
- } else {
- RedisModel::set($redisKey, '0');
- $endTime = strtotime(date('Y-m-d 00:00:00',strtotime('+1 days'))) -1;
- $expire = $endTime - time();
- RedisModel::expire($redisKey, $expire);
- return $appKeyArr['0'];
- }
- }
- }
|