Notify.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace app\admin\controller\csminvite;
  3. use addons\csminvite\Csminvite;
  4. use addons\csminvite\library\CsmBackend;
  5. use Exception;
  6. use think\Db;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. /**
  10. * 通知类型的邀请函
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Notify extends CsmBackend
  15. {
  16. /**
  17. * Notify模型对象
  18. *
  19. * @var \app\admin\model\csminvite\Notify
  20. */
  21. protected $model = null;
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\csminvite\Notify();
  26. $this->view->assign("notifywayList", $this->model->getNotifywayList());
  27. $this->view->assign("statusList", $this->model->getStatusList());
  28. }
  29. /**
  30. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  31. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  32. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  33. */
  34. /**
  35. * 添加
  36. */
  37. public function add()
  38. {
  39. if ($this->request->isPost()) {
  40. $params = $this->request->post("row/a");
  41. if ($params) {
  42. $params = $this->preExcludeFields($params);
  43. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  44. $params[$this->dataLimitField] = $this->auth->id;
  45. }
  46. $result = false;
  47. Db::startTrans();
  48. try {
  49. //是否采用模型验证
  50. if ($this->modelValidate) {
  51. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  52. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  53. $this->model->validateFailException(true)->validate($validate);
  54. }
  55. $params['invitekey'] = md5(time() . mt_rand(1,1000000));
  56. $result = $this->model->allowField(true)->save($params);
  57. Db::commit();
  58. } catch (ValidateException $e) {
  59. Db::rollback();
  60. $this->error($e->getMessage());
  61. } catch (PDOException $e) {
  62. Db::rollback();
  63. $this->error($e->getMessage());
  64. } catch (Exception $e) {
  65. Db::rollback();
  66. $this->error($e->getMessage());
  67. }
  68. if ($result !== false) {
  69. $this->success();
  70. } else {
  71. $this->error(__('No rows were inserted'));
  72. }
  73. }
  74. $this->error(__('Parameter %s can not be empty', ''));
  75. }
  76. return $this->view->fetch();
  77. }
  78. public function notifyUserajax($ids = null)
  79. {
  80. $row = $this->model->where("id", "=", $ids)->find();
  81. if (! $row) {
  82. return;
  83. }
  84. if($row->effectendtime<time()){
  85. $this->error("活动已过期,无法发送邮件通知。");
  86. }
  87. $dao = new \app\admin\model\csminvite\Notifyuser();
  88. $list = $dao->alias("t")
  89. ->where("csminvite_notify_id", "=", $row->id)
  90. ->where("status", "=", "normal")
  91. ->where("userstatus", "=", "unnotify")
  92. ->select();
  93. $emailtitle = $row->emailtitle;
  94. $emailcontent = $row->content;
  95. if(count($list)==0){
  96. $this->error("没有需要发送邮件通知");
  97. }
  98. $obj = \app\common\library\Email::instance();
  99. // echo $dao->getLastSql();
  100. // echo("sendmail");
  101. foreach ($list as $item) {
  102. //var_dump($item->email . ' ' . $this->convertMailContent($emailtitle, $row,$item) . ' ' . $this->convertMailContent($emailcontent, $row,$item));
  103. $obj->to($item->email)
  104. ->subject($this->convertMailContent($emailtitle, $row,$item))
  105. ->message($this->convertMailContent($emailcontent, $row,$item))
  106. ->send();
  107. $param = [
  108. "userstatus" => "nofitied",
  109. "notifytime" => time(),
  110. "updatetime" => time()
  111. ];
  112. $dao->where("id", "=", $item->id)->update($param);
  113. }
  114. $this->success();
  115. }
  116. private function convertMailContent($str, $row,$rowuser)
  117. {
  118. $str = str_replace(array("\r\n", "\r", "\n"), "<BR>", $str);
  119. $str = str_replace('%username%', $rowuser->username, $str);
  120. $str = str_replace('%email%', $rowuser->email, $str);
  121. $csminvite = "notify_".$row->invitekey."_".$rowuser->id;
  122. $csminvitesign = Csminvite::generateSign($csminvite);
  123. $loginurl = $_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["SERVER_NAME"] .config('view_replace_str.__PUBLIC__') . "index/user/register.html?csminvite={$csminvite}&csminvitesign={$csminvitesign}";
  124. $str = str_replace('%register_url%', $loginurl, $str);
  125. return $str;
  126. }
  127. }