BaseService.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: vowkin
  5. * Date: 2017/5/22
  6. * Time: 13:15
  7. */
  8. namespace app\common\service;
  9. class BaseService
  10. {
  11. public $mall_host = '';
  12. private $_token = null;
  13. private $_account=null;
  14. public function __construct()
  15. {
  16. $this->_account=config('account');
  17. $this->mall_host = config('api_host');
  18. $this->_token = \cookie('vpi_token');
  19. if(!$this->_token){
  20. $this->getToken($this->_account);
  21. }
  22. }
  23. protected function Request_call($url, $param=[]) {
  24. if(!is_array($param)){
  25. throw new \RuntimeException('HTTP POST 参数必须是数组');
  26. }
  27. $param['token'] = $this->_token;
  28. $param['companyCode']=$this->_account['companyCode'];
  29. $param = json_encode($param);
  30. $url = $this->mall_host.$url;
  31. $tmpInfo = helperService::http_post($url,$param);
  32. $tmpInfo=json_decode($tmpInfo,true);
  33. if( 40044 == $tmpInfo['code']){
  34. $this->getToken($this->_account);
  35. $param = json_decode($param,true);
  36. $param['token'] = $this->_token;
  37. $param = json_encode($param);
  38. $tmpInfo = helperService::http_post($url,$param);
  39. }
  40. return $tmpInfo;
  41. }
  42. //获取token
  43. public function getToken($param){
  44. $res = $this->Request_call('index/auth/authorization',$param);
  45. $token = $res['data']['access_token'];
  46. setcookie('vpi_token',$token,time()+7200);
  47. $this->_token = $token;
  48. }
  49. }