Daytoutiaoserver.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2018/3/11
  6. * Time: 下午3:08
  7. */
  8. namespace backend\server;
  9. use yii\base\Component;
  10. use common\models\DayToutiao;
  11. use common\enums\EmailEnum;
  12. use common\models\ListClassnev;
  13. use Yii;
  14. use yii\db\Exception;
  15. class Daytoutiaoserver extends Component
  16. {
  17. /*
  18. * 获取头条报名列表
  19. * */
  20. public function getList($input)
  21. {
  22. $model = new DayToutiao();
  23. $data = $model->getList($input);
  24. return $data;
  25. }
  26. /*
  27. * 数据添加
  28. * */
  29. public function addList($input)
  30. {
  31. try {
  32. $list_id = $input['list_id'];
  33. $emailInfo = ListClassnev::findOne($list_id);
  34. $email = $emailInfo['day_email'];
  35. $data = json_decode($input['data'], true);
  36. $info = [];
  37. $saveInfo = [];
  38. foreach ($data as $k => $v) {
  39. $emailData['username'] = $v['name'];
  40. $emailData['phone'] = $v['phone'];
  41. $info[] = $emailData;
  42. //保存的数据
  43. $sava['list_id'] = $list_id;
  44. $sava['name'] = $v['name'];
  45. $sava['phone'] = $v['phone'];
  46. $sava['send_email'] = $email;
  47. $sava['send_state'] = 2;
  48. $sava['create_at'] = time();
  49. $sava['update_at'] = time();
  50. $saveInfo[] = $sava;
  51. }
  52. $tui = (new \common\service\common\MailerService())->ordinarySend($email, ['data' => $info], 'toutiao', $emailInfo['day_title']);
  53. if ($tui) {
  54. $k = array_keys($saveInfo[0]);
  55. Yii::$app->db->createCommand()->batchInsert('pfg_day_toutiao', $k, $saveInfo)->execute();
  56. return '发送中';
  57. } else {
  58. foreach ($saveInfo as &$val) {
  59. $val['send_state'] = 1;
  60. }
  61. $k = array_keys($saveInfo[0]);
  62. Yii::$app->db->createCommand()->batchInsert('pfg_day_toutiao', $k, $saveInfo)->execute();
  63. return '发送失败';
  64. }
  65. } catch (\Exception $e) {
  66. return $e->getMessage();
  67. }
  68. }
  69. /*
  70. * 重新单个发送
  71. * */
  72. public function anewSend($id)
  73. {
  74. try {
  75. $row = DayToutiao::findOne($id);
  76. if ($row['send_state'] != 1) {
  77. return '该信息已在' . date('H/m/d H:i:s', $row['update_at']) . '发送过';
  78. }
  79. $emailInfo = ListClassnev::findOne($row['list_id']);
  80. $email = $emailInfo['day_email'];
  81. $info = [['username' => $row['name'], 'phone' => $row['phone']]];
  82. $tui = (new \common\service\common\MailerService())->ordinarySend($email, ['data' => $info], 'toutiao', $emailInfo['day_title']);
  83. if ($tui) {
  84. $row->send_state = 2;
  85. $row->update(false);
  86. return '发送中';
  87. } else {
  88. return '发送失败';
  89. }
  90. } catch (\Exception $e) {
  91. return $e->getMessage();
  92. }
  93. }
  94. public function delOne($id)
  95. {
  96. $model = DayToutiao::findOne($id);
  97. if ($model->delete()) return true;
  98. return false;
  99. }
  100. public function setWx($did)
  101. {
  102. return Yii::$app->db->createCommand()->update('pfg_day_toutiao', ['efficacious' => 2], ['id' => $did])->execute();
  103. }
  104. public function editData($input)
  105. {
  106. $row = DayToutiao::findOne($input['id']);
  107. $row->list_id = $input['list_id'];
  108. $row->name = $input['name'];
  109. $row->phone = $input['phone'];
  110. $row->efficacious = $input['efficacious'];
  111. if(!empty($input['like']) && $input['like'] === 'yes'){
  112. $row->send_state = 1;
  113. }
  114. if($row->update(false)){
  115. if(!empty($input['like']) && $input['like'] === 'yes'){
  116. return $this->anewSend($input['id']);
  117. }
  118. return '修改完成';
  119. }else{
  120. return false;
  121. }
  122. }
  123. }