Chat.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace app\common\model\plus\chat;
  3. use app\common\model\BaseModel;
  4. /**
  5. * 客服消息模型
  6. */
  7. class Chat extends BaseModel
  8. {
  9. protected $pk = 'chat_id';
  10. protected $name = 'chat';
  11. /**
  12. * 关联会员表
  13. */
  14. public function user()
  15. {
  16. return $this->hasOne("app\\common\\model\\user\\User", 'user_id', 'user_id');
  17. }
  18. /**
  19. * 关联会员表
  20. */
  21. public function supplier()
  22. {
  23. return $this->hasOne("app\\common\\model\\supplier\\Supplier", 'shop_supplier_id', 'shop_supplier_id');
  24. }
  25. //获取聊天验证id
  26. public function getIdentify($user_id, $muser_id)
  27. {
  28. if ($user_id > $muser_id) {
  29. $identify = $user_id . '_' . $muser_id;
  30. } else {
  31. $identify = $muser_id . '_' . $user_id;
  32. }
  33. return $identify;
  34. }
  35. //添加信息
  36. public function add($data)
  37. {
  38. // 开启事务
  39. $this->startTrans();
  40. try {
  41. $ChatRelation = new ChatRelation();
  42. $this->save($data);
  43. $info = $ChatRelation->where('user_id', '=', $data['user_id'])->where('supplier_user_id', '=', $data['supplier_user_id'])->find();
  44. if (!$info) {
  45. $ChatRelation->save($data);
  46. } else {
  47. $info->save(['update_time' => time()]);
  48. }
  49. $this->commit();
  50. return true;
  51. } catch (\Exception $e) {
  52. log_write($e->getMessage());
  53. $this->rollback();
  54. return false;
  55. }
  56. }
  57. }