Messages.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 中闽 < 1464674022@qq.com >
  5. * Date: 2019/12/5
  6. * Time: 17:44
  7. */
  8. namespace app\common\model;
  9. use think\Model;
  10. class Messages extends Model
  11. {
  12. const STATUS_WAIT = 0;
  13. const STATUS_PASS = 1;
  14. const STATUS_UNPASS = -1;
  15. const STATUS = [
  16. self::STATUS_WAIT => '待审核',
  17. self::STATUS_PASS => '已审核',
  18. self::STATUS_UNPASS => '已拒审'
  19. ];
  20. //status_text
  21. public function getStatusTextAttr($value, $data)
  22. {
  23. return self::STATUS[$data['status']]??'';
  24. }
  25. //message
  26. public function getMessageAttr($value, $data)
  27. {
  28. return htmlspecialchars($value);
  29. }
  30. /**
  31. * 截取N个字
  32. * @return mixed|string
  33. */
  34. public function getShortText($len = 50)
  35. {
  36. if (mb_strlen($this->message, "utf-8") > $len) {
  37. return subStrCN($this->message, $len) . "...";
  38. }
  39. return $this->message;
  40. }
  41. /**
  42. * 获取本消息的回复个数
  43. */
  44. public function replyCount()
  45. {
  46. return $this->hasMany('Messages', 'to_msg_id')->where('status', self::STATUS_PASS)->count();
  47. }
  48. /**
  49. * 获取管理员后台的回复
  50. */
  51. public function adminReplays()
  52. {
  53. return $this->hasMany('Messages', 'to_msg_id')->where(['status' => self::STATUS_PASS, 'from_user_id' => 0]);
  54. }
  55. public function fromUser()
  56. {
  57. return $this->belongsTo('app\common\model\User', 'from_user_id');
  58. }
  59. public function toUser()
  60. {
  61. return $this->belongsTo('app\common\model\User', 'to_user_id');
  62. }
  63. }