Authored by wuxiao

通过内建方法读取websocket推送服务地址

@@ -3,6 +3,7 @@ namespace backend\controllers; @@ -3,6 +3,7 @@ namespace backend\controllers;
3 3
4 use Yii; 4 use Yii;
5 use backend\components\Pagination; 5 use backend\components\Pagination;
  6 +use yii\helpers\ArrayHelper;
6 use common\lib\YohoApi\Client as YohoApiClient; 7 use common\lib\YohoApi\Client as YohoApiClient;
7 8
8 /** 9 /**
@@ -59,16 +60,25 @@ class ProductController extends BaseController @@ -59,16 +60,25 @@ class ProductController extends BaseController
59 60
60 if (!empty($ret['product_list'])){ 61 if (!empty($ret['product_list'])){
61 foreach ($ret['product_list'] as $product){ 62 foreach ($ret['product_list'] as $product){
62 - $list[$product['product_skn']]->product_name = $product['product_name'];  
63 - $list[$product['product_skn']]->sales_price = $product['sales_price']; 63 + if (!empty($list[$product['product_skn']])){
  64 + $list[$product['product_skn']]->setAttributes([
  65 + 'product_name' => $product['product_name'],
  66 + 'sales_price' => $product['sales_price'],
  67 + ]);
  68 + }
64 } 69 }
65 } 70 }
66 } 71 }
67 72
  73 + //推送服务地址
  74 + $wsAddrs = (new \common\models\SysConfig)->getServerAddr();
  75 + $wsAddr = @ArrayHelper::getValue($wsAddrs, ['websocket',0]);
  76 +
68 return $this->render('list',[ 77 return $this->render('list',[
69 'room'=> \app\models\Room::findOne(['room_id'=>$room_id]), 78 'room'=> \app\models\Room::findOne(['room_id'=>$room_id]),
70 'pagination'=>$pagination, 79 'pagination'=>$pagination,
71 'list'=>$list, 80 'list'=>$list,
  81 + 'wsAddr'=>$wsAddr
72 ]); 82 ]);
73 } 83 }
74 84
@@ -10,6 +10,7 @@ $this->registerJsFile('/js/php.js',array('postion'=>View::POS_END)); @@ -10,6 +10,7 @@ $this->registerJsFile('/js/php.js',array('postion'=>View::POS_END));
10 ?> 10 ?>
11 <script type="text/javascript"> 11 <script type="text/javascript">
12 <?php $this->beginBlock('javascript');?> 12 <?php $this->beginBlock('javascript');?>
  13 +var wsAddr = '<?=$wsAddr?>';
13 var i = 0; 14 var i = 0;
14 var room_id = <?=$room->room_id?>; 15 var room_id = <?=$room->room_id?>;
15 var interval_handle; 16 var interval_handle;
@@ -20,7 +21,7 @@ var default_interval = 3; @@ -20,7 +21,7 @@ var default_interval = 3;
20 */ 21 */
21 function callWebsocket(callback){ 22 function callWebsocket(callback){
22 if (!window.ws || window.ws.readyState === undefined || window.ws.readyState != 1) { 23 if (!window.ws || window.ws.readyState === undefined || window.ws.readyState != 1) {
23 - websocket('192.168.102.17',9501,'/',function(){ 24 + websocket(wsAddr,null,'/',function(){
24 consoleLog('连接服务器'); 25 consoleLog('连接服务器');
25 26
26 callback(); 27 callback();
@@ -124,6 +125,9 @@ function getPushlog(interval){ @@ -124,6 +125,9 @@ function getPushlog(interval){
124 * @returns {undefined} 125 * @returns {undefined}
125 */ 126 */
126 function consoleLog(msg){ 127 function consoleLog(msg){
  128 + if (msg){
  129 + console.log(msg);
  130 + }
127 $('#runtime').append('&nbsp;'+msg+'<br />'); 131 $('#runtime').append('&nbsp;'+msg+'<br />');
128 } 132 }
129 /** 133 /**
@@ -52,13 +52,13 @@ function websocket(host, port, path, onOpen, onMessage, onClose, onError) { @@ -52,13 +52,13 @@ function websocket(host, port, path, onOpen, onMessage, onClose, onError) {
52 if (!/:\/\//.test(host)){ 52 if (!/:\/\//.test(host)){
53 host = 'ws://' + host; 53 host = 'ws://' + host;
54 } 54 }
55 - if (/^:/.test(port)){ 55 + if (port && /^:/.test(port)){
56 port = Number(port.replace(':','')); 56 port = Number(port.replace(':',''));
57 } 57 }
58 - if (!/^\//.test(path)){ 58 + if (path && !/^\//.test(path)){
59 path = '/' + path; 59 path = '/' + path;
60 } 60 }
61 - url = host + ':' + port + path; 61 + url = host + (port ? ':' + port : '') + (path ? path : '/');
62 62
63 window.ws = new WebSocket(url); 63 window.ws = new WebSocket(url);
64 ws.onopen = (typeof onOpen == 'function') ? onOpen : function(evt) 64 ws.onopen = (typeof onOpen == 'function') ? onOpen : function(evt)