Chat.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace app\api\model\plus\chat;
  3. use app\common\model\plus\chat\Chat as ChatModel;
  4. use app\api\model\supplier\Supplier as SupplierModel;
  5. use app\api\model\user\User as UserModel;
  6. use app\api\model\settings\Setting as SettingModel;
  7. /**
  8. * 客服消息模型类
  9. */
  10. class Chat extends ChatModel
  11. {
  12. /**
  13. * 隐藏字段
  14. */
  15. protected $hidden = [
  16. 'app_id',
  17. 'identify',
  18. 'status',
  19. 'update_time'
  20. ];
  21. //消息列表
  22. public function myList($user)
  23. {
  24. $ChatRelation = new ChatRelation();
  25. $list = $ChatRelation->with(['user', 'supplier.logo'])
  26. ->where(['user_id' => $user['user_id']])
  27. ->order('update_time desc')
  28. ->select();
  29. foreach ($list as $key => &$value) {
  30. $where['supplier_user_id'] = $value['supplier_user_id'];
  31. $where['user_id'] = $user['user_id'];
  32. $where['status'] = 0;
  33. $value['num'] = $this->where($where)->count();
  34. unset($where['status']);
  35. $value['newMessage'] = $this->where($where)->order('chat_id desc')->field('content,create_time,type')->find();
  36. }
  37. return $list;
  38. }
  39. //消息列表
  40. public function mySupplierList($supplier_user_id)
  41. {
  42. $ChatRelation = new ChatRelation();
  43. $list = $ChatRelation->with(['user', 'supplier.logo'])
  44. ->where(['supplier_user_id' => $supplier_user_id])
  45. ->order('update_time desc')
  46. ->select();
  47. foreach ($list as $key => &$value) {
  48. $where['supplier_user_id'] = $supplier_user_id;
  49. $where['user_id'] = $value['user_id'];
  50. $where['status'] = 0;
  51. $value['num'] = $this->where($where)->count();
  52. unset($where['status']);
  53. $value['newMessage'] = $this->where($where)->order('chat_id desc')->field('content,create_time,type')->find();
  54. }
  55. return $list;
  56. }
  57. //获取聊天信息
  58. public function getMessage($data, $user)
  59. {
  60. $where['supplier_user_id'] = $data['supplier_user_id'];
  61. $where['user_id'] = $user['user_id'];
  62. $list = $this->where($where)
  63. ->with(['user', 'supplier.logo'])
  64. ->order('chat_id desc')
  65. ->paginate($data);
  66. $this->where($where)->update(['status' => 1]);
  67. return $list;
  68. }
  69. //获取消息条数
  70. public function mCount($user)
  71. {
  72. $num = 0;
  73. if ($user) {
  74. $where[] = ['user_id', '=', $user['user_id']];
  75. $where[] = ['status', '=', 0];
  76. $where[] = ['msg_type', '=', 1];
  77. $num = $this->where($where)->count();
  78. }
  79. return $num;
  80. }
  81. //获取用户信息
  82. public function getInfo($data)
  83. {
  84. $userInfo = UserModel::detail($data['user_id']);
  85. $supplierInfo = SupplierModel::detail($data['shop_supplier_id'], ['logo']);
  86. $data['avatarUrl'] = $userInfo['avatarUrl'];
  87. $data['logo'] = $supplierInfo['logo']['file_path'];
  88. $data['url'] = SettingModel::getSysConfig()['url'];
  89. return $data;
  90. }
  91. public static function getNoReadCount($supplier_user_id){
  92. return self::where('supplier_user_id', '=', $supplier_user_id)
  93. ->where('status', '=', 0)
  94. ->where('msg_type', '=', 2)
  95. ->count();
  96. }
  97. }