ChatController.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace business\controllers;
  3. use biz\goods\services\GoodsService;
  4. use biz\merchant\services\MerchantService;
  5. use biz\user\services\UserService;
  6. use common\components\business;
  7. use Yii;
  8. use common\components\util;
  9. use GatewayClient\Gateway;
  10. class ChatController extends BaseController
  11. {
  12. //聊天 shish 2019.12.28
  13. public function actionIndex()
  14. {
  15. $get = Yii::$app->request->get();
  16. $userId = isset($get['userId']) ? $get['userId'] : 0;
  17. $user = UserService::getById($userId);
  18. UserService::valid($user, $this->merchantId);
  19. return $this->renderPartial('index', ['userId' => $userId]);
  20. }
  21. //绑定 shish 2019.12.28
  22. public function actionBind()
  23. {
  24. $post = Yii::$app->request->post();
  25. $clientId = isset($post['clientId']) ? $post['clientId'] : '';
  26. $id = 'merchant_' . $this->merchantId;
  27. Gateway::bindUid($clientId, $id);
  28. }
  29. //发消息给客户 shish 2019.12.28
  30. public function actionSend()
  31. {
  32. $post = Yii::$app->request->post();
  33. $type = isset($post['type']) ? $post['type'] : 'text';
  34. $userId = isset($post['userId']) ? $post['userId'] : 0;
  35. $user = UserService::getById($userId);
  36. UserService::valid($user, $this->merchantId);
  37. $arr = [];
  38. $arr['type'] = $type;
  39. switch ($type) {
  40. case 'text':
  41. $content = $post['content'];
  42. $arr['content'] = $content;
  43. break;
  44. case 'img':
  45. $shortUrl = $post['shortUrl'];
  46. $return = business::formatUploadImg($shortUrl);
  47. $arr['url'] = $return['url'];
  48. $arr['smallUrl'] = $return['smallUrl'];
  49. break;
  50. case 'link':
  51. $id = $post['id'];
  52. $respond = MerchantService::getCommonUrl($id);
  53. $arr['name'] = $respond['name'];
  54. $arr['img'] = $respond['img'];
  55. $arr['url'] = $respond['url'];
  56. break;
  57. case 'goods':
  58. $id = $post['id'];
  59. $info = GoodsService::getGoodsInfo($id);dd($info);
  60. GoodsService::valid($info, $this->merchantId);
  61. $arr['goodsName'] = $info['goodsName'];
  62. break;
  63. }
  64. $data[] = $arr;
  65. //每隔三分钟显示时间
  66. $minute = (int)date("i");
  67. if ($minute % 3 == 0) {
  68. $time = date("i:s");
  69. $data[] = ['type' => 'time', 'time' => $time];
  70. }
  71. Gateway::sendToUid($userId, json_encode($data));
  72. }
  73. }