MessageForm.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace common\models\forms;
  3. use common\helpers\Date;
  4. use common\components\Model;
  5. use common\helpers\Form;
  6. use common\models\MessageText;
  7. use common\models\UserInfo;
  8. use yii\base\Exception;
  9. /**
  10. * Login form
  11. */
  12. class MessageForm extends Model
  13. {
  14. public $id;
  15. public $title;
  16. public $content;
  17. public $toUserName;
  18. private $_userId;
  19. /**
  20. * @inheritdoc
  21. */
  22. public function rules()
  23. {
  24. return [
  25. [['id', 'title', 'content', 'toUserName'], 'trim'],
  26. [['id', 'title', 'content'], 'required'],
  27. [['id'], 'exist', 'targetClass'=>MessageText::class, 'targetAttribute'=>'ID'],
  28. [['toUserName'], 'exist', 'targetClass'=>UserInfo::class, 'targetAttribute'=>'USER_NAME'],
  29. ];
  30. }
  31. public function attributeLabels()
  32. {
  33. return [
  34. 'id' => 'ID',
  35. 'title' => '标题',
  36. 'content' => '内容',
  37. 'toUserName' => '发给会员编号',
  38. ];
  39. }
  40. /**
  41. * 指定校验场景
  42. * @return array
  43. */
  44. public function scenarios()
  45. {
  46. $parentScenarios = parent::scenarios();
  47. $customScenarios = [
  48. 'add' => ['title', 'content', 'toUserName'],
  49. 'delete' => ['id'],
  50. ];
  51. return array_merge($parentScenarios, $customScenarios);
  52. }
  53. /**
  54. * 添加
  55. * @return MessageText|null
  56. * @throws \yii\db\Exception
  57. */
  58. public function edit(){
  59. if(!$this->validate()){
  60. return null;
  61. }
  62. $db = \Yii::$app->db;
  63. $transaction = $db->beginTransaction();
  64. try {
  65. if($this->toUserName){
  66. $toUser = UserInfo::findOneAsArray('USER_NAME=:USER_NAME', [':USER_NAME'=>$this->toUserName], 'USER_ID');
  67. $this->_userId = $toUser['USER_ID'];
  68. } else {
  69. $this->_userId = null;
  70. }
  71. $model = new MessageText();
  72. $model->TITLE = $this->title;
  73. $model->CONTENT = $this->content;
  74. $model->TO_UID = $this->_userId ?? '0';
  75. $model->ADMIN_ID = \Yii::$app->user->id;
  76. $model->CREATED_AT = Date::nowTime();
  77. if(!$model->save()){
  78. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  79. }
  80. // 给前台会员发送webSocket消息,通知其抓取消息
  81. if($this->send() === false){
  82. throw new Exception('发送失败');
  83. }
  84. $transaction->commit();
  85. } catch (Exception $e) {
  86. $transaction->rollBack();
  87. $this->addError('edit', $e->getMessage());
  88. return null;
  89. }
  90. return $model;
  91. }
  92. /**
  93. * 给前台会员发送websocket消息,通知其抓取站内信
  94. */
  95. public function send(){
  96. if(!$this->_userId){
  97. $this->_userId = null;
  98. }
  99. return \Yii::$app->swooleAsyncTimer->pushMsgToUser($this->_userId, '有新的消息');
  100. }
  101. }