AsyncWorkCommand.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * 异步任务脚本
  4. * @author >
  5. */
  6. require_once(APP_PATH."/protected/vendor/autoload.php");
  7. use Qiniu\Auth;
  8. use Qiniu\Storage\BucketManager;
  9. class AsyncWorkCommand extends CConsoleCommand{
  10. public function run($args) {
  11. error_reporting(E_ALL);
  12. ini_set('memory_limit', '512M');
  13. ignore_user_abort(true);
  14. set_time_limit(0);
  15. $time_H = time();
  16. while (true) {
  17. sleep(3);
  18. $time = time();
  19. if($time - $time_H > 3600){
  20. exit();
  21. }
  22. //用户注册后异步任务
  23. $list = new ARedisList('after_user_reg');
  24. $auth = new Auth(Yii::app()->params['qiniuConfig']['ak'],Yii::app()->params['qiniuConfig']['sk']);
  25. $bmgr = new BucketManager($auth);
  26. while ($list->getCount() > 0) {
  27. $user_id = $list->pop();
  28. try {
  29. $userAr = RUser::get(new MongoId($user_id));
  30. //抓取头像到七牛
  31. $url = $userAr->avatar;
  32. if(strpos($url,'qiniu')===false){
  33. $bucket = Yii::app()->params['qiniuConfig']['avatars'];
  34. $key = dechex(time()).rand(10,99).'.jpg';
  35. list($ret, $err) = $bmgr->fetch($url, $bucket, $key);
  36. echo "=====> fetch $url to bucket: $bucket key: $key\n";
  37. if ($err !== null) {
  38. file_put_contents(APP_PATH.'/download/log/after_user_reg.log',var_export($err,true),FILE_APPEND);
  39. } else {
  40. if(YII_DEBUG == true){
  41. $userAr->avatar = 'http://odufxpjo7.bkt.clouddn.com/'.$key;
  42. }else{
  43. $userAr->avatar = 'http://odug56iu7.bkt.clouddn.com/'.$key;
  44. }
  45. }
  46. }
  47. $userAr->update(array('avatar'),true);
  48. } catch (Exception $e) {
  49. var_dump($e);
  50. continue;
  51. }
  52. }
  53. //订单支付成功后异步任务
  54. $list = new ARedisList('o2o_after_pay_success');
  55. while ($list->getCount() > 0) {
  56. $res = $list->pop();
  57. $input_data = json_decode($res,true);
  58. try {
  59. $order = ROrder::get(new MongoId($input_data['order_no']));
  60. $criteria = new EMongoCriteria();
  61. $criteria->user('==',$order->user);
  62. $criteria->status('notin',array(0,-1,-2));
  63. $order_count = ROrder::model()->count($criteria);
  64. if($order_count == 1){
  65. $criteria = new EMongoCriteria();
  66. $criteria->channel('==','20150814');
  67. $criteria->user('==',$order->user);
  68. $coupon_status = CouponCode::model()->count($criteria);
  69. if($coupon_status){
  70. $criteria = new EMongoCriteria();
  71. $criteria->date('==',(int)date('Ymd'));
  72. $offCountModel = OfflineOrderCount::model()->find($criteria);
  73. if(!$offCountModel){
  74. $offCountModel = new OfflineOrderCount();
  75. $offCountModel->date = intval(date('Ymd'));
  76. $offCountModel->count = 1;
  77. $offCountModel->save();
  78. }else{
  79. $offCountModel->count += 1;
  80. $offCountModel->update(array('count'),true);
  81. }
  82. }
  83. }
  84. $key = $input_data['id'].'_send_sms';
  85. $cache = new ARedisCache();
  86. $have_send = $cache->get($key);
  87. if(!$have_send){
  88. $order_info = $order->parseRow($order);
  89. $month = date('m');
  90. $day = date('d');
  91. $address = $order_info['address']['poi']['name'].$order_info['address']['detail'];
  92. if(date('w') == 0 || date('w') == 6){
  93. $info = '我们将在工作时间为您安排';
  94. }else{
  95. $info = '我们正在为您安排';
  96. }
  97. $master = '保洁师';//训犬师
  98. CommonSMS::send('order_pay_success',array('month'=>$month,'day'=>$day,'address'=>$address,'info'=>$info,'master'=>$master,'mobile'=>$order_info['address']['mobile']));
  99. $cache->set($key,1,86400);
  100. }
  101. //计算订单完成时间
  102. if($order->technician){
  103. $cost_time = Service::factory('OrderTimeCalService')->evaluaTime($order->products);
  104. $cost_hours = ceil($cost_time/60);
  105. $time_stamp = $order->booking_time;
  106. FreeTimeRecord::TechUnsetFreetime($order->technician,$time_stamp - 3600);
  107. for ($i=0; $i < $cost_hours; $i++) {
  108. $unset_time_stamp = $time_stamp + $i*3600;
  109. FreeTimeRecord::TechUnsetFreetime($order->technician,$unset_time_stamp);
  110. }
  111. FreeTimeRecord::TechUnsetFreetime($order->technician,$unset_time_stamp + 3600);
  112. }
  113. // 通知被选择的保洁师
  114. if ($order->technician) {
  115. $tech = TechInfo::get(intval($order->technician));
  116. if ($tech->weixin_userid) {
  117. $wechat = O2oApp::getWechatActive();
  118. $url_prefix = ENVIRONMENT == 'product' ? 'http://api.yiguanjia.me' : 'http://apitest.yiguanjia.me';
  119. $wechat_data = [
  120. 'touser' => $tech->weixin_userid,
  121. 'msgtype' => 'news',
  122. 'agentid' => '24',
  123. 'news' => [
  124. 'articles' => [
  125. [
  126. 'title' => '壹管家提示-新订单',
  127. 'description' => $tech->name.'你好!刚刚有一个新的订单被分配给你,请点击查看。',
  128. 'url' => $url_prefix.'/index.php?r=o2o/myOrder/info&order='.(string)$order->_id.'&user='.$tech->_id,
  129. ],
  130. ],
  131. ],
  132. ];
  133. $wechat->sendMessage($wechat_data);
  134. }
  135. }
  136. foreach ($order->products as $product) {
  137. $product_obj = Product::get($product['product']);
  138. if($product_obj){
  139. switch ($product_obj->type) {
  140. case '1':
  141. $start_time = time();
  142. $end_time = strtotime(date('Y-m-d',$start_time+2592000));
  143. $coupon_id = new MongoId('556671c20eb9fb2e488be078');
  144. Service::factory('CouponService')->giveCoupon($order->user,$coupon_id,$start_time,$end_time);
  145. break;
  146. case '2':
  147. $start_time = time();
  148. $end_time = strtotime(date('Y-m-d',$start_time+2592000));
  149. $coupon_id = new MongoId('55667f6c0eb9fb14518b6e0a');
  150. Service::factory('CouponService')->giveCoupon($order->user,$coupon_id,$start_time,$end_time);
  151. break;
  152. case '3':
  153. $start_time = time();
  154. $end_time = strtotime(date('Y-m-d',$start_time+2592000));
  155. $coupon_id = new MongoId('55667f4a0eb9fb39518b6c06');
  156. Service::factory('CouponService')->giveCoupon($order->user,$coupon_id,$start_time,$end_time);
  157. break;
  158. }
  159. }
  160. }
  161. } catch (Exception $e) {
  162. continue;
  163. }
  164. }
  165. //发送短信异步任务
  166. $list = new ARedisList('send_sms_list');
  167. $smsservice = Service::factory('SendSMSService');
  168. while ($list->getCount() > 0) {
  169. $key = $list->pop();
  170. try {
  171. $sms_data = json_decode($key,true);
  172. $smsservice->send_sms($sms_data['tpl'],$sms_data['mobile']);
  173. } catch (Exception $e) {
  174. continue;
  175. }
  176. }
  177. }
  178. }
  179. }