Nessuna descrizione

FxOrder.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2017/12/5
  6. * Time: 15:07
  7. */
  8. namespace App;
  9. use Illuminate\Database\Eloquent\Model;
  10. use PHPExcel;
  11. use PHPExcel_Writer_Excel2007;
  12. class FxOrder extends Model
  13. {
  14. public $timestamps = false;
  15. protected $table = "fx_order";
  16. public static function olist(){
  17. $result = FxOrder::orderBy('id', 'desc')->get();
  18. return json_decode(json_encode($result),true);
  19. }
  20. # 生成外部订单号
  21. public static function createOuterCode(){
  22. $order_sn = 'fx'.rand(100, 9999). substr(time(), 5, 5). rand(10, 9999);
  23. return $order_sn;
  24. }
  25. /*更新城市天气信息*/
  26. public static function updateWeather($order_id) {
  27. $redisKey = 'seafood_order_city_weather';
  28. $appKey = self::getAppKey();
  29. //查询订单收件人所在的城市(过滤关键字 市)
  30. $city = self::where('id', $order_id)->pluck('receiverCity');
  31. $city = str_replace('市','',$city);
  32. $specialCity = array('朝阳区','嘉定区','海淀区','昌平区','密云区','大兴区','房山区','通州区','普陀区',);
  33. if(in_array($city, $specialCity)) {
  34. $city = str_replace('区','',$city);
  35. }
  36. //查询redis中是否有该键,若没有则查询
  37. $weather = RedisModel::get($redisKey.'_'.$city);
  38. if($weather) {
  39. $sellerMemo = self::where('id', $order_id)->pluck('sellerMemo');
  40. if(strstr($sellerMemo, $city.'天气状况:'.$weather)) {
  41. return true;
  42. }
  43. $sellerMemo = $sellerMemo.' '.$city.'天气状况:'.$weather;
  44. self::where('id', $order_id)->update(['sellerMemo'=>$sellerMemo]);
  45. } else {
  46. $url = 'http://apis.juhe.cn/simpleWeather/query?city='.urlencode($city).'&key='.$appKey;
  47. $result = file_get_contents($url);
  48. $result = json_decode($result, true);
  49. if($result['error_code'] == '0') {
  50. //将数据缓存到redis里
  51. $today = date('Y-m-d',time());
  52. $tomorrow = date('Y-m-d',strtotime('+1 days'));
  53. $weather = '';
  54. foreach ($result['result']['future'] as $value) {
  55. if($value['date'] == $today) {
  56. $weather .= $today.'日气温:'.$value['temperature'].'; ';
  57. }
  58. if($value['date'] == $tomorrow) {
  59. $weather .= $tomorrow.'日气温:'.$value['temperature'];
  60. }
  61. }
  62. RedisModel::set($redisKey.'_'.$city, $weather);
  63. $endTime = strtotime(date('Y-m-d 00:00:00',strtotime('+1 days'))) -1;
  64. $expire = $endTime - time();
  65. RedisModel::expire($redisKey.'_'.$city, $expire);
  66. $sellerMemo = self::where('id', $order_id)->pluck('sellerMemo');
  67. if(strstr($sellerMemo, $city.'天气状况:'.$weather)) {
  68. return true;
  69. }
  70. $sellerMemo = $sellerMemo.' '.$city.'天气状况:'.$weather;
  71. self::where('id', $order_id)->update(['sellerMemo'=>$sellerMemo]);
  72. } else if($result['error_code'] == '10012'){
  73. //判断是否为第二个APPkey也用完了
  74. $key = (string) RedisModel::get('seafood_order_city_weather_key');
  75. if($key == '0') {
  76. //超过100次/天上限 更换APPKey
  77. RedisModel::set('seafood_order_city_weather_key', '1');
  78. $endTime = strtotime(date('Y-m-d 00:00:00',strtotime('+1 days'))) -1;
  79. $expire = $endTime - time();
  80. RedisModel::expire('seafood_order_city_weather_key', $expire);
  81. self::updateWeather($order_id);
  82. }
  83. }
  84. }
  85. return true;
  86. }
  87. public static function getAppKey() {
  88. $redisKey = 'seafood_order_city_weather_key';
  89. $appKeyArr = ['32c8f844f8055f70f702fc28ec930e52', '85bfc19adb25998b2559d7ceba950e13'];
  90. $appKey = (string)RedisModel::get($redisKey);
  91. if($appKey != '') {
  92. return $appKeyArr[$appKey];
  93. } else {
  94. RedisModel::set($redisKey, '0');
  95. $endTime = strtotime(date('Y-m-d 00:00:00',strtotime('+1 days'))) -1;
  96. $expire = $endTime - time();
  97. RedisModel::expire($redisKey, $expire);
  98. return $appKeyArr['0'];
  99. }
  100. }
  101. }