WechatTemplateService.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Created by sublime.
  4. * User: wosinC
  5. * Date: 2017/12/2
  6. * Time: 上午11:39
  7. */
  8. namespace App\Modules\OfficialAccount\Services;
  9. use App\Modules\OfficialAccount\Models\WechatTemplateMsgs;
  10. use App\Modules\OfficialAccount\Models\CustomSendMsgs;
  11. use App\Modules\OfficialAccount\Models\BatchCustomSendMsgs;
  12. use Redis;
  13. class WechatTemplateService
  14. {
  15. /**
  16. * 更新模板消息或者客服消息状态(灵杰 wechat 调用)
  17. */
  18. static function updateWechatTemplateStatus($wechatTemplatePrams)
  19. {
  20. try {
  21. \Log::info('==========================更新 模板消息/客服消息推送状态'.$wechatTemplatePrams['id']);
  22. $task_id = $wechatTemplatePrams['id'];
  23. $taskidArray = explode('_', $task_id);
  24. if (count($taskidArray)>1) {
  25. if ($taskidArray[0] == 'custom') {
  26. \Log::info('updateWechatTemplateStatus:'.$task_id.' status:'.$wechatTemplatePrams['status']);
  27. $customSendMsgs = CustomSendMsgs::customSendMsgsByTaskid($task_id);
  28. if(!empty($customSendMsgs)) {
  29. $customSendMsgs['status'] = $wechatTemplatePrams['status'];
  30. $customSendMsgs->save();
  31. $officialAccountArray = $customSendMsgs->toArray();
  32. Redis::hset('send_wechat_msg:task_id:'.$customSendMsgs['task_id'], 'wechat_msg', json_encode($officialAccountArray));
  33. // 如果是智能托管有批次号,更新下批次状态
  34. if($customSendMsgs['trusteeship'] == 1 && $wechatTemplatePrams['status'] == 3){
  35. \Log::info('try_update_batch_custom_send_msgs_status:'.$customSendMsgs['id']);
  36. $batchCustomMsg = BatchCustomSendMsgs::batchCustomMsgById($customSendMsgs['batch_no']);
  37. if(isset($batchCustomMsg->status) && $batchCustomMsg->status !=3 ){
  38. \Log::info('update_batch_custom_send_msgs_status:'.$customSendMsgs['id'].' batch_no:'.$customSendMsgs['batch_no']);
  39. BatchCustomSendMsgs::updateBatchCustomSendMsgStatusById($customSendMsgs['batch_no'],3,0);
  40. }else{
  41. \Log::info('already_update_batch_custom_send_msgs_status:'.$customSendMsgs['id'].' batch_no:'.$customSendMsgs['batch_no']);
  42. }
  43. }else{
  44. \Log::info('not_update_batch_custom_send_msgs_status:'.$customSendMsgs['id']);
  45. }
  46. return 1;
  47. }else{
  48. \Log::info('==========================客服消息 数据未找到'.$task_id);
  49. return 2;
  50. }
  51. }
  52. }else{
  53. \Log::info('==========================模板消息推送'.$task_id);
  54. $wechatTemplateMsgs = WechatTemplateMsgs::wechatTemplateMsgsById($wechatTemplatePrams['id']);
  55. if(!empty($wechatTemplateMsgs)) {
  56. $wechatTemplateMsgs['status'] = $wechatTemplatePrams['status'];
  57. $wechatTemplateMsgs->save();
  58. $officialAccountArray = $wechatTemplateMsgs->toArray();
  59. Redis::hset('send_wechat_msg:task_id:'.$wechatTemplateMsgs['id'], 'wechat_msg', json_encode($officialAccountArray));
  60. // Redis::hset('send_wechat_msg:task_id:'.$wechatTemplateMsgs['id'], 'wechat_msg', '[{id:'.$wechatTemplateMsgs['id'].'},{appid:'.$wechatTemplateMsgs['appid'].'},{common_template_id:'.$wechatTemplateMsgs['common_template_id'].'},{name:'.$wechatTemplateMsgs['name'].'},{send_time:'.$wechatTemplateMsgs['send_time'].'},{template_content:'.$wechatTemplateMsgs['template_content'].'},{redirect_url:'.$wechatTemplateMsgs['redirect_url'].'},{distribution_channel_id:'.$wechatTemplateMsgs['distribution_channel_id'].'},{remark:'.$wechatTemplateMsgs['remark'].'},{subscribe_time:'.$wechatTemplateMsgs['subscribe_time'].'},{sex:'.$wechatTemplateMsgs['sex'].'},{balance:'.$wechatTemplateMsgs['balance'].'},{order_type:'.$wechatTemplateMsgs['order_type'].'},{category_id:'.$wechatTemplateMsgs['category_id'].'}]');
  61. return 1;
  62. }else{
  63. \Log::info('==========================模板消息 数据未找到'.$task_id);
  64. return 2;
  65. }
  66. }
  67. } catch (\Exception $e) {
  68. \Log::info($e->getMessage());
  69. return 0;
  70. }
  71. }
  72. }