MailController.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2017/12/1/001
  6. * Time: 16:27
  7. */
  8. namespace backend\controllers;
  9. use backend\helpers\DateTimeHelper;
  10. use backend\helpers\MailHelper;
  11. use backend\helpers\PaginationHelper;
  12. use backend\helpers\ValidatorHelper;
  13. use backend\models\Config;
  14. use backend\models\Mail;
  15. use backend\models\MailEvent;
  16. use backend\models\MailRecord;
  17. use backend\models\MailConfig;
  18. class MailController extends BaseController
  19. {
  20. const EVENT_EMAIL_SEND = 'send';
  21. /**
  22. * 获取邮件列表
  23. */
  24. public function actionGetMailList()
  25. {
  26. $request = \Yii::$app->request->get();
  27. $data = [];
  28. $data['page'] = isset($request['page']) ? (int)$request['page'] : 1;
  29. $data['pageSize'] = isset($request['page']) ? (int)$request['pageSize'] : 20;
  30. $data['orderBy'] = isset($request['orderBy']) ? $request['orderBy'] : 'id desc';
  31. $data['search'] = isset($request['search']) ? $request['search'] : '';
  32. $data = ValidatorHelper::validateData($data, [
  33. ['page', 'integer', 'min' => 1],
  34. ['pageSize', 'integer', 'min' => 1],
  35. ['orderBy', 'string'],
  36. ], $errors);
  37. if ($data == false) {
  38. return $this->outJson(0, '', $errors);
  39. }
  40. $query = Mail::find()->orderBy($data['orderBy']);
  41. if (!empty($data['search'])) {
  42. $query->andFilterWhere(['like', 'subject', $data['search']]);
  43. }
  44. $result = PaginationHelper::queryPage($query, $data['page'], $data['pageSize']);
  45. return $this->outJson(1, $result);
  46. }
  47. /**
  48. * 获取邮件发送记录
  49. */
  50. public function actionGetMailRecordList()
  51. {
  52. $request = \Yii::$app->request->get();
  53. $data = [];
  54. $data['page'] = isset($request['page']) ? (int)$request['page'] : 1;
  55. $data['pageSize'] = isset($request['page']) ? (int)$request['pageSize'] : 20;
  56. $data['orderBy'] = isset($request['orderBy']) ? $request['orderBy'] : 'id desc';
  57. $data['search'] = isset($request['search']) ? $request['search'] : '';
  58. $data = ValidatorHelper::validateData($data, [
  59. ['page', 'integer', 'min' => 1],
  60. ['pageSize', 'integer', 'min' => 1],
  61. ['orderBy', 'string'],
  62. ], $errors);
  63. if ($data == false) {
  64. return $this->outJson(0, '', $errors);
  65. }
  66. $query = MailRecord::find()->orderBy($data['orderBy']);
  67. if (!empty($data['search'])) {
  68. $query->andFilterWhere(['like', 'receiver', $data['search']]);
  69. }
  70. $result = PaginationHelper::queryPage($query, $data['page'], $data['pageSize']);
  71. return $this->outJson(1, $result);
  72. }
  73. /**
  74. * 获取邮件详情
  75. */
  76. public function actionViewMail()
  77. {
  78. $request = \Yii::$app->request->get();
  79. $id = isset($request['id']) ? (int)$request['id'] : '';
  80. $result = Mail::find()->where(['id' => $id])->one();
  81. return $this->outJson(1, $result);
  82. }
  83. /**
  84. * 获取邮件记录详情
  85. */
  86. public function actionViewMailRecord()
  87. {
  88. $request = \Yii::$app->request->get();
  89. $id = isset($request['id']) ? (int)$request['id'] : '';
  90. $result = MailRecord::find()->where(['id' => $id])->one();
  91. return $this->outJson(1, $result);
  92. }
  93. /**
  94. * 新增邮件
  95. */
  96. public function actionSaveMail()
  97. {
  98. $request = \Yii::$app->request->post();
  99. $params['type'] = isset($request['type']) ? (int)$request['type'] : '';
  100. $params['subject'] = isset($request['subject']) ? $request['subject'] : '';
  101. $params['content'] = isset($request['content']) ? $request['content'] : '';
  102. $params['other'] = isset($request['other']) ? $request['other'] : '';
  103. $params['state'] = 0;
  104. $params['in_time'] = DateTimeHelper::microtime_float();
  105. $mail = new Mail();
  106. $mail->setAttributes($params);
  107. if ($mail->save()) {
  108. return $this->outJson(1, $mail->getAttributes());
  109. } else {
  110. return $this->outJson(0, [], $mail->getErrors());
  111. }
  112. }
  113. /**
  114. * 删除邮件
  115. */
  116. public function actionDelMail()
  117. {
  118. $request = \Yii::$app->request->post();
  119. $id = isset($request['id']) ? (int)$request['id'] : '';
  120. $mail = new Mail();
  121. $result = $mail->deleteAll(['id' => $id]);
  122. return $this->outJson(1, $result);
  123. }
  124. /**
  125. * 批量发送邮件(该方法慎用会员,代理商,会员+代理商)!!!!
  126. * 因为会往crm_mail_record插入大量state=0的记录
  127. * 自动计划脚本SendMailJob每分钟都在跑,该脚本就是发送state=0的邮件
  128. */
  129. public function actionSendMail()
  130. {
  131. $request = \Yii::$app->request->post();
  132. $id = isset($request['id']) ? (int)$request['id'] : '';
  133. $mail = Mail::findOne(['id' => $id]);
  134. if ($mail->state == 0) {
  135. // 事件的绑定
  136. // $result = EventHelper::saveMailRecord($id, $mail->type);
  137. $mailModel = new Mail();
  138. $mailEvent = new MailEvent();
  139. $mailEvent->mailId = $id;
  140. $mailEvent->type = $mail->type;
  141. $this->on(self::EVENT_EMAIL_SEND, [$mailModel, 'sendEvent']);
  142. // 事件的触发
  143. $this->trigger(self::EVENT_EMAIL_SEND, $mailEvent);
  144. $mail->state = 1;
  145. $mail->save();
  146. return $this->outJson(1, $mail, '操作成功');
  147. } else {
  148. return $this->outJson(0, [], '已发送,不能重复发送');
  149. }
  150. }
  151. /**
  152. * 邮件记录-重新发送邮件
  153. */
  154. public function actionSendMailRecord()
  155. {
  156. $request = \Yii::$app->request->post();
  157. $id = isset($request['id']) ? (int)$request['id'] : '';
  158. $mailRecord = MailRecord::findOne(['id' => $id]);
  159. $config = Config::findOne(1);
  160. $rs = MailHelper::sendButNotSaveRecord($mailRecord->subject, $mailRecord->receiver, $config->smtp_from_mail, $mailRecord->content, $id);
  161. if ($rs) {
  162. return $this->outJson(1, $rs, '发送成功');
  163. } else {
  164. return $this->outJson(0, [], '发送失败');
  165. }
  166. }
  167. /**
  168. * 邮件记录-删除
  169. */
  170. public function actionDelMailRecord()
  171. {
  172. $request = \Yii::$app->request->post();
  173. $id = isset($request['id']) ? (int)$request['id'] : '';
  174. $mail = new MailRecord();
  175. $result = $mail->deleteAll(['id' => $id]);
  176. return $this->outJson(1, $result);
  177. }
  178. /**
  179. * 邮箱验证码
  180. * @param $email
  181. * @param $code
  182. */
  183. public function actionSendMailCode()
  184. {
  185. $request = \Yii::$app->request->post();
  186. $email = $request['email'];
  187. $code = MailHelper::sendCodeSms($email);
  188. if (!$email || !$code) {
  189. return $this->outJson(0, [], '参数错误');
  190. }
  191. $config = Config::findOne(1);
  192. $paramArray = [
  193. 'email' => $email,
  194. 'code' => $code,
  195. ];
  196. // 发送邮件
  197. MailHelper::sendMail("邮箱验证码", $email, $paramArray, '', $config->mail_code);
  198. return $this->outJson(1, [], 'OK');
  199. }
  200. /**
  201. * 邮箱验证码
  202. * @param $email
  203. * @param $code
  204. */
  205. public function actionGetMailCode()
  206. {
  207. $email = trim(\Yii::$app->getRequest()->get('email'));
  208. if ($email == '') {
  209. return $this->outJson(0, [], '邮箱不能为空');
  210. }
  211. $result = MailHelper::getCodeSms($email);
  212. return $this->outJson(1, $result, 'OK');
  213. }
  214. public function actionStatistics()
  215. {
  216. $request = \Yii::$app->getRequest()->post();
  217. $id = isset($request['id']) ? $request['id'] : 0;
  218. $stime = $request['stime'];
  219. $etime = $request['etime'];
  220. if (empty($id)) {
  221. $config = MailConfig::find()->asArray()->all();
  222. } else {
  223. $config = MailConfig::find()->where(['id' => $id])->asArray()->all();
  224. }
  225. $data = array();
  226. foreach ($config as $k => $v) {
  227. $data[$k] = $v;
  228. $sql = "select count(id) as c from crm_mail_record where mail_num = $v[id] and in_time BETWEEN $stime and $etime";
  229. $data[$k]['total'] = MailRecord::findBySql($sql)->asArray()->all()[0]['c'];
  230. $sql = "select count(id) as c from crm_mail_record where mail_num = $v[id] and status = 0 and in_time BETWEEN $stime and $etime";
  231. $data[$k]['fail'] = MailRecord::findBySql($sql)->asArray()->all()[0]['c'];
  232. $sql = "select count(id) as c from crm_mail_record where mail_num = $v[id] and status = 1 and in_time BETWEEN $stime and $etime";
  233. $data[$k]['success'] = MailRecord::findBySql($sql)->asArray()->all()[0]['c'];
  234. $sql = "select count(id) as c from crm_mail_record where mail_num = $v[id] and status = 2 and in_time BETWEEN $stime and $etime";
  235. $data[$k]['undelivered'] = MailRecord::findBySql($sql)->asArray()->all()[0]['c'];
  236. }
  237. $sql = "select count(id) as c from crm_mail_record where in_time BETWEEN $stime and $etime";
  238. $sum['total'] = MailRecord::findBySql($sql)->asArray()->all()[0]['c'];
  239. $sql = "select count(id) as c from crm_mail_record where in_time BETWEEN $stime and $etime and status = 0";
  240. $sum['fail'] = MailRecord::findBySql($sql)->asArray()->all()[0]['c'];
  241. $sql = "select count(id) as c from crm_mail_record where in_time BETWEEN $stime and $etime and status = 1";
  242. $sum['success'] = MailRecord::findBySql($sql)->asArray()->all()[0]['c'];
  243. $sql = "select count(id) as c from crm_mail_record where in_time BETWEEN $stime and $etime and status = 2";
  244. $sum['undelivered'] = MailRecord::findBySql($sql)->asArray()->all()[0]['c'];
  245. $result['datalist'] = $data;
  246. $result['sum'] = $sum;
  247. return $this->outJson(1, $result);
  248. }
  249. }