ChatController.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. //文字
  42. $content = $post['content'];
  43. $arr['content'] = $content;
  44. break;
  45. case 'img':
  46. //图片
  47. $shortUrl = $post['shortUrl'];
  48. $return = business::formatUploadImg($shortUrl);
  49. $arr['url'] = $return['url'];
  50. $arr['smallUrl'] = $return['smallUrl'];
  51. break;
  52. case 'link':
  53. //常用链接
  54. $id = $post['id'];
  55. $respond = MerchantService::getCommonUrl($id);
  56. $arr['name'] = $respond['name'];
  57. $arr['img'] = $respond['img'];
  58. $arr['url'] = $respond['url'];
  59. break;
  60. case 'goods':
  61. //商品
  62. $id = $post['id'];
  63. $info = GoodsService::getGoodsInfo($id);
  64. GoodsService::valid($info, $this->merchantId);
  65. $arr['goodsName'] = $info['goodsName'];
  66. $arr['img'] = isset($info['smallImgList'][0]) ? $info['smallImgList'][0] : '';
  67. $arr['price'] = isset($info['price']) ? $info['price'] : 300;
  68. break;
  69. }
  70. $data[] = $arr;
  71. //每隔三分钟显示时间
  72. $minute = (int)date("i");
  73. if ($minute % 3 == 0) {
  74. $time = date("i:s");
  75. $data[] = ['type' => 'time', 'time' => $time];
  76. }
  77. Gateway::sendToUid($userId, json_encode($data));
  78. util::success($data);
  79. }
  80. }