AsyncWorkCommand.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. /**
  3. * 异步任务脚本
  4. * @author leeon <leeon_on@qq.com>
  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. $z_group = new ZGroup();
  32. $default_groups = $z_group->get_default_fllow_group();
  33. $mongo = new MongoClient(DB_CONNETC);
  34. $db = $mongo->wozhua_data;
  35. $collection = $db->selectCollection('groups');
  36. $conditions = array(
  37. '_id' => array('$in' => $default_groups)
  38. );
  39. $collection->update(
  40. $conditions,
  41. array('$inc' => array("users_count" => 1)),
  42. array("multiple" => true)
  43. );
  44. //注册后默认关注几个用户
  45. $default_fllow_users = Yii::app()->params['default_fllow_users'];
  46. $user_node = new UserNodeRecord($userAr->_id);
  47. foreach ($default_fllow_users as $fuser_id) {
  48. $user_node->follow($fuser_id);
  49. }
  50. //抓取头像到七牛
  51. $url = $userAr->avatar;
  52. if(strpos($url,'qiniu')===false){
  53. $bucket = Yii::app()->params['qiniuConfig']['avatars'];
  54. $key = dechex(time()).rand(10,99).'.jpg';
  55. list($ret, $err) = $bmgr->fetch($url, $bucket, $key);
  56. echo "=====> fetch $url to bucket: $bucket key: $key\n";
  57. if ($err !== null) {
  58. file_put_contents(APP_PATH.'/download/log/after_user_reg.log',var_export($err,true),FILE_APPEND);
  59. } else {
  60. if(YII_DEBUG == true){
  61. $userAr->avatar = 'http://wozhua-test.qiniudn.com/'.$key;
  62. }else{
  63. $userAr->avatar = 'http://avatars.wozhua.mobi/'.$key;
  64. }
  65. }
  66. }
  67. $userAr->update(array('avatar'),true);
  68. //发送私信推送
  69. $model = new ZMessage();
  70. $from_user = Yii::app()->params['sys_user'];
  71. $message_data = array(
  72. 'from_user' => $from_user,
  73. 'to_user' => $user_id,
  74. 'content' => '感谢您注册壹管家,这里是新人必看的<a href="http://www.wozhua.mobi/topic/556551ec0eb9fb131f8b60c1">壹管家操作指南</a>,同时一定要遵守<a href="http://www.wozhua.mobi/topic/564d9fdea84ea080498cedaa">壹管家社区总规则</a>哦~',
  75. 'pics' => array(),
  76. 'voice' => array(),
  77. 'video'=> array()
  78. );
  79. $result = $model->addMessage($message_data);
  80. } catch (Exception $e) {
  81. var_dump($e);
  82. continue;
  83. }
  84. }
  85. //七牛语音转码消息队列
  86. $list = new ARedisList(Yii::app()->params['voice_conv_list']);
  87. $qiniu_config = Yii::app()->params['qiniuConfig'];
  88. $bucket = $qiniu_config['voice'];
  89. while ($list->getCount() > 0) {
  90. $key = $list->pop();
  91. try {
  92. $status = CommonFn::ConvVoice($bucket,$key);
  93. } catch (Exception $e) {
  94. continue;
  95. }
  96. }
  97. //打标签异步任务
  98. $list = new ARedisList('add_tag');
  99. while ($list->getCount() > 0) {
  100. $res = $list->pop();
  101. if(ENVIRONMENT != 'product'){
  102. continue;
  103. }
  104. $data = unserialize($res);
  105. try {
  106. $getui = Service::factory('PushService');
  107. $cids = $getui->queryCID($data['user_id']);
  108. if($cids){
  109. $tags = [];
  110. foreach ($cids as $cid){
  111. $res = $getui->getCidTags($cid);
  112. $new_tags = explode(' ',$res);
  113. $new_tags[] = $data['tag'];
  114. $res = $getui->setTag($cid,$new_tags);
  115. var_dump($res);
  116. }
  117. }
  118. } catch (Exception $e) {
  119. continue;
  120. }
  121. }
  122. //订单支付成功后异步任务
  123. $list = new ARedisList('o2o_after_pay_success');
  124. while ($list->getCount() > 0) {
  125. $res = $list->pop();
  126. $input_data = json_decode($res,true);
  127. try {
  128. $order = ROrder::get(new MongoId($input_data['order_no']));
  129. $criteria = new EMongoCriteria();
  130. $criteria->user('==',$order->user);
  131. $criteria->status('notin',array(0,-1,-2));
  132. $order_count = ROrder::model()->count($criteria);
  133. if($order_count == 1){
  134. $criteria = new EMongoCriteria();
  135. $criteria->channel('==','20150814');
  136. $criteria->user('==',$order->user);
  137. $coupon_status = CouponCode::model()->count($criteria);
  138. if($coupon_status){
  139. $criteria = new EMongoCriteria();
  140. $criteria->date('==',(int)date('Ymd'));
  141. $offCountModel = OfflineOrderCount::model()->find($criteria);
  142. if(!$offCountModel){
  143. $offCountModel = new OfflineOrderCount();
  144. $offCountModel->date = intval(date('Ymd'));
  145. $offCountModel->count = 1;
  146. $offCountModel->save();
  147. }else{
  148. $offCountModel->count += 1;
  149. $offCountModel->update(array('count'),true);
  150. }
  151. }
  152. }
  153. $key = $input_data['id'].'_send_sms';
  154. $cache = new ARedisCache();
  155. $have_send = $cache->get($key);
  156. if(!$have_send){
  157. $order_info = $order->parseRow($order);
  158. $month = date('m');
  159. $day = date('d');
  160. $address = $order_info['address']['poi']['name'].$order_info['address']['detail'];
  161. if(date('w') == 0 || date('w') == 6){
  162. $info = '我们将在工作时间为您安排';
  163. }else{
  164. $info = '我们正在为您安排';
  165. }
  166. $master = '保洁师';//训犬师
  167. CommonSMS::send('order_pay_success',array('month'=>$month,'day'=>$day,'address'=>$address,'info'=>$info,'master'=>$master,'mobile'=>$order_info['address']['mobile']));
  168. $cache->set($key,1,86400);
  169. }
  170. //计算订单完成时间
  171. if($order->technician){
  172. $cost_time = Service::factory('OrderTimeCalService')->evaluaTime($order->products);
  173. $cost_hours = ceil($cost_time/60);
  174. $time_stamp = $order->booking_time;
  175. FreeTimeRecord::TechUnsetFreetime($order->technician,$time_stamp - 3600);
  176. for ($i=0; $i < $cost_hours; $i++) {
  177. $unset_time_stamp = $time_stamp + $i*3600;
  178. FreeTimeRecord::TechUnsetFreetime($order->technician,$unset_time_stamp);
  179. }
  180. FreeTimeRecord::TechUnsetFreetime($order->technician,$unset_time_stamp + 3600);
  181. }
  182. // 通知被选择的保洁师
  183. if ($order->technician) {
  184. $tech = TechInfo::get(intval($order->technician));
  185. if ($tech->weixin_userid) {
  186. $wechat = O2oApp::getWechatActive();
  187. $url_prefix = ENVIRONMENT == 'product' ? 'http://api.wozhua.mobi' : 'http://apitest.wozhua.mobi';
  188. $wechat_data = [
  189. 'touser' => $tech->weixin_userid,
  190. 'msgtype' => 'news',
  191. 'agentid' => '24',
  192. 'news' => [
  193. 'articles' => [
  194. [
  195. 'title' => '壹管家提示-新订单',
  196. 'description' => $tech->name.'你好!刚刚有一个新的订单被分配给你,请点击查看。',
  197. 'url' => $url_prefix.'/index.php?r=o2o/myOrder/info&order='.(string)$order->_id.'&user='.$tech->_id,
  198. ],
  199. ],
  200. ],
  201. ];
  202. $wechat->sendMessage($wechat_data);
  203. }
  204. }
  205. foreach ($order->products as $product) {
  206. $product_obj = Product::get($product['product']);
  207. if($product_obj){
  208. switch ($product_obj->type) {
  209. case '1':
  210. $start_time = time();
  211. $end_time = strtotime(date('Y-m-d',$start_time+2592000));
  212. $coupon_id = new MongoId('556671c20eb9fb2e488be078');
  213. Service::factory('CouponService')->giveCoupon($order->user,$coupon_id,$start_time,$end_time);
  214. break;
  215. case '2':
  216. $start_time = time();
  217. $end_time = strtotime(date('Y-m-d',$start_time+2592000));
  218. $coupon_id = new MongoId('55667f6c0eb9fb14518b6e0a');
  219. Service::factory('CouponService')->giveCoupon($order->user,$coupon_id,$start_time,$end_time);
  220. break;
  221. case '3':
  222. $start_time = time();
  223. $end_time = strtotime(date('Y-m-d',$start_time+2592000));
  224. $coupon_id = new MongoId('55667f4a0eb9fb39518b6c06');
  225. Service::factory('CouponService')->giveCoupon($order->user,$coupon_id,$start_time,$end_time);
  226. break;
  227. }
  228. }
  229. }
  230. } catch (Exception $e) {
  231. continue;
  232. }
  233. }
  234. //删帖后异步任务
  235. $list = new ARedisList('topic_del_new_list');
  236. $from_user = Yii::app()->params['sys_user'];
  237. $z_message = new ZMessage();
  238. $z_topic = new ZTopic();
  239. while ($list->getCount() > 0) {
  240. $res = $list->pop();
  241. try {
  242. $data = unserialize($res);
  243. $topic = Topic::get(new MongoId($data['id']));
  244. if($topic){
  245. Service::factory('ScoreService')->cutDownScore((string)$topic->user,'score_deltopic',2);
  246. $z_topic->syncData($topic->_id,'del');
  247. if($data['reason']){
  248. $content = '你的帖子 『'.mb_substr($topic->content,0,20,'utf-8').'』 被管理员删除了。'.$data['reason'];
  249. $data = array(
  250. 'from_user' => $from_user,
  251. 'to_user' => (string)$topic->user,
  252. 'content' => $content,
  253. 'pics' => array(),
  254. 'voice' => array(),
  255. 'video'=> array()
  256. );
  257. $z_message->addMessage($data);
  258. }
  259. }
  260. } catch (Exception $e) {
  261. continue;
  262. }
  263. }
  264. //点喜欢后异步任务
  265. $list = new ARedisList('after_like_work');
  266. while ($list->getCount() > 0) {
  267. $key = $list->pop();
  268. try {
  269. $likeAr = Like::get(new MongoId($key));
  270. if($likeAr && $likeAr->type == 'topic'){
  271. // $parsed_like = $likeAr->parseRow($likeAr);
  272. // $getui = Service::factory('PushService');
  273. // $gt_content = [
  274. // 'title' => $parsed_like['user']['user_name'],
  275. // 'content' => '喜欢了你的帖子',
  276. // 'type' => 'like',
  277. // 'data' => $parsed_like
  278. // ];
  279. // $getui->pushMessageToSingle($getui->TransmissionTemplate(['transmissionType'=> 2,'alert' => $parsed_like['user']['user_name'].'喜欢了你的帖子','transmissionContent'=>$gt_content]),$parsed_like['like_obj']['user']['id']);
  280. $like_obj = Topic::get($likeAr->like_obj);
  281. $author = RUser::get($like_obj->user);
  282. $z_like = new ZLike();
  283. $z_like->addAction($likeAr);
  284. //帖子的被赞数加一
  285. $like_obj->like_count = $like_obj->like_count+1;
  286. //帖子的作者的被赞数加一
  287. $author->liked_count = $author->liked_count+1;
  288. $like_obj->update(array('like_count'), true);
  289. $author->update(array('liked_count'), true);
  290. //用户行为记录
  291. CommonFn::setInteraction($likeAr->user,$author->_id,'like');
  292. }
  293. } catch (Exception $e) {
  294. continue;
  295. }
  296. }
  297. //发送短信异步任务
  298. $list = new ARedisList('send_sms_list');
  299. $smsservice = Service::factory('SendSMSService');
  300. while ($list->getCount() > 0) {
  301. $key = $list->pop();
  302. try {
  303. $sms_data = json_decode($key,true);
  304. $smsservice->send_sms($sms_data['tpl'],$sms_data['mobile']);
  305. } catch (Exception $e) {
  306. continue;
  307. }
  308. }
  309. //帖子查看数异步处理
  310. $list = new ARedisList('topic_viewcounter_list');
  311. while ($list->getCount() > 0) {
  312. $topic_id = $list->pop();
  313. $topic = Topic::get(new MongoId($topic_id));
  314. $topic->visit_count = $topic->visit_count+10;
  315. $topic->update(array('visit_count'),true);
  316. }
  317. }
  318. }
  319. }