*/ require_once(APP_PATH."/protected/vendor/autoload.php"); use Qiniu\Auth; use Qiniu\Storage\BucketManager; class AsyncWorkCommand extends CConsoleCommand{ public function run($args) { error_reporting(E_ALL); ini_set('memory_limit', '512M'); ignore_user_abort(true); set_time_limit(0); $time_H = time(); while (true) { sleep(3); $time = time(); if($time - $time_H > 3600){ exit(); } //用户注册后异步任务 $list = new ARedisList('after_user_reg'); $auth = new Auth(Yii::app()->params['qiniuConfig']['ak'],Yii::app()->params['qiniuConfig']['sk']); $bmgr = new BucketManager($auth); while ($list->getCount() > 0) { $user_id = $list->pop(); try { $userAr = RUser::get(new MongoId($user_id)); //抓取头像到七牛 $url = $userAr->avatar; if(strpos($url,'qiniu')===false){ $bucket = Yii::app()->params['qiniuConfig']['avatars']; $key = dechex(time()).rand(10,99).'.jpg'; list($ret, $err) = $bmgr->fetch($url, $bucket, $key); echo "=====> fetch $url to bucket: $bucket key: $key\n"; if ($err !== null) { file_put_contents(APP_PATH.'/download/log/after_user_reg.log',var_export($err,true),FILE_APPEND); } else { if(YII_DEBUG == true){ $userAr->avatar = 'http://odufxpjo7.bkt.clouddn.com/'.$key; }else{ $userAr->avatar = 'http://odug56iu7.bkt.clouddn.com/'.$key; } } } $userAr->update(array('avatar'),true); } catch (Exception $e) { var_dump($e); continue; } } //订单支付成功后异步任务 $list = new ARedisList('o2o_after_pay_success'); while ($list->getCount() > 0) { $res = $list->pop(); $input_data = json_decode($res,true); try { $order = ROrder::get(new MongoId($input_data['order_no'])); $criteria = new EMongoCriteria(); $criteria->user('==',$order->user); $criteria->status('notin',array(0,-1,-2)); $order_count = ROrder::model()->count($criteria); if($order_count == 1){ $criteria = new EMongoCriteria(); $criteria->channel('==','20150814'); $criteria->user('==',$order->user); $coupon_status = CouponCode::model()->count($criteria); if($coupon_status){ $criteria = new EMongoCriteria(); $criteria->date('==',(int)date('Ymd')); $offCountModel = OfflineOrderCount::model()->find($criteria); if(!$offCountModel){ $offCountModel = new OfflineOrderCount(); $offCountModel->date = intval(date('Ymd')); $offCountModel->count = 1; $offCountModel->save(); }else{ $offCountModel->count += 1; $offCountModel->update(array('count'),true); } } } $key = $input_data['id'].'_send_sms'; $cache = new ARedisCache(); $have_send = $cache->get($key); if(!$have_send){ $order_info = $order->parseRow($order); $month = date('m'); $day = date('d'); $address = $order_info['address']['poi']['name'].$order_info['address']['detail']; if(date('w') == 0 || date('w') == 6){ $info = '我们将在工作时间为您安排'; }else{ $info = '我们正在为您安排'; } $master = '保洁师';//训犬师 CommonSMS::send('order_pay_success',array('month'=>$month,'day'=>$day,'address'=>$address,'info'=>$info,'master'=>$master,'mobile'=>$order_info['address']['mobile'])); $cache->set($key,1,86400); } //计算订单完成时间 if($order->technician){ $cost_time = Service::factory('OrderTimeCalService')->evaluaTime($order->products); $cost_hours = ceil($cost_time/60); $time_stamp = $order->booking_time; FreeTimeRecord::TechUnsetFreetime($order->technician,$time_stamp - 3600); for ($i=0; $i < $cost_hours; $i++) { $unset_time_stamp = $time_stamp + $i*3600; FreeTimeRecord::TechUnsetFreetime($order->technician,$unset_time_stamp); } FreeTimeRecord::TechUnsetFreetime($order->technician,$unset_time_stamp + 3600); } // 通知被选择的保洁师 if ($order->technician) { $tech = TechInfo::get(intval($order->technician)); if ($tech->weixin_userid) { $wechat = O2oApp::getWechatActive(); $url_prefix = ENVIRONMENT == 'product' ? 'http://api.yiguanjia.me' : 'http://apitest.yiguanjia.me'; $wechat_data = [ 'touser' => $tech->weixin_userid, 'msgtype' => 'news', 'agentid' => '24', 'news' => [ 'articles' => [ [ 'title' => '壹管家提示-新订单', 'description' => $tech->name.'你好!刚刚有一个新的订单被分配给你,请点击查看。', 'url' => $url_prefix.'/index.php?r=o2o/myOrder/info&order='.(string)$order->_id.'&user='.$tech->_id, ], ], ], ]; $wechat->sendMessage($wechat_data); } } foreach ($order->products as $product) { $product_obj = Product::get($product['product']); if($product_obj){ switch ($product_obj->type) { case '1': $start_time = time(); $end_time = strtotime(date('Y-m-d',$start_time+2592000)); $coupon_id = new MongoId('556671c20eb9fb2e488be078'); Service::factory('CouponService')->giveCoupon($order->user,$coupon_id,$start_time,$end_time); break; case '2': $start_time = time(); $end_time = strtotime(date('Y-m-d',$start_time+2592000)); $coupon_id = new MongoId('55667f6c0eb9fb14518b6e0a'); Service::factory('CouponService')->giveCoupon($order->user,$coupon_id,$start_time,$end_time); break; case '3': $start_time = time(); $end_time = strtotime(date('Y-m-d',$start_time+2592000)); $coupon_id = new MongoId('55667f4a0eb9fb39518b6c06'); Service::factory('CouponService')->giveCoupon($order->user,$coupon_id,$start_time,$end_time); break; } } } } catch (Exception $e) { continue; } } //发送短信异步任务 $list = new ARedisList('send_sms_list'); $smsservice = Service::factory('SendSMSService'); while ($list->getCount() > 0) { $key = $list->pop(); try { $sms_data = json_decode($key,true); $smsservice->send_sms($sms_data['tpl'],$sms_data['mobile']); } catch (Exception $e) { continue; } } } } }