BaseController.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace App\Http\Controllers\WapAlipay;
  3. use Illuminate\Routing\Controller;
  4. use Illuminate\Http\Request;
  5. use Cookie;
  6. use App\Modules\User\Services\UserService;
  7. class BaseController extends Controller
  8. {
  9. /**
  10. * 请求接口的地址前缀
  11. * @var mixed
  12. */
  13. protected $_url_prefix;
  14. /**
  15. * 用户id
  16. * @var int
  17. */
  18. protected $uid;
  19. /**
  20. * 公众号接口签名密钥
  21. * @var string
  22. */
  23. protected $secret_key = 'zhuishuyun#_2017';
  24. /**
  25. * 渠道id
  26. * @var
  27. */
  28. protected $distribution_channel_id;
  29. function __construct(Request $request)
  30. {
  31. $domain = _domain();
  32. $this->distribution_channel_id = str_replace('site','',explode('.',$domain)[0]);
  33. if(!is_numeric($this->distribution_channel_id)){
  34. $this->distribution_channel_id = decodeDistributionChannelId($this->distribution_channel_id);
  35. }
  36. $this->_url_prefix = env('PUBLIC_BASE_API');
  37. $user_cookie = Cookie::get(env('COOKIE_AUTH_WEB_WECHAT'));
  38. $this->uid = $user_cookie ? decrypt($user_cookie) : null;
  39. //$this->uid = 13;
  40. }
  41. protected function userInfo($uid){
  42. $user = UserService::getById($uid);
  43. return $user;
  44. }
  45. protected function checkUid(){
  46. if(!$this->uid) return false;
  47. return true;
  48. }
  49. public function __get($name)
  50. {
  51. if($name == '_user_info'){
  52. return $this->userInfo($this->uid);
  53. }
  54. return null;
  55. }
  56. protected function joinUrl($url)
  57. {
  58. if (stripos($url, '/') === 0) {
  59. $url = substr($url, 1);
  60. }
  61. return $this->_url_prefix . $url;
  62. }
  63. /**
  64. * 公众号签名@华灯初上
  65. * @param $params
  66. * @return string
  67. */
  68. protected function getSign($params)
  69. {
  70. $url = $this->arr_to_url($params, false);
  71. $url = $url . '&key=' . $this->secret_key;
  72. $sign = md5($url);
  73. return $sign;
  74. }
  75. /**
  76. * 公众号签名@华灯初上
  77. * @param $array
  78. * @param bool $has_sign
  79. * @return string
  80. */
  81. protected function arr_to_url($array, $has_sign = false)
  82. {
  83. ksort($array);
  84. reset($array);
  85. $arg = "";
  86. while (list ($name, $val) = each($array)) {
  87. if ($name == 'sign' && !$has_sign) continue;
  88. if (strpos($name, "_") === 0)
  89. continue;
  90. if (is_array($val))
  91. $val = join(',', $val);
  92. if ($val === "")
  93. continue;
  94. $arg .= $name . "=" . $val . "&";
  95. }
  96. $arg = substr($arg, 0, count($arg) - 2);
  97. return $arg;
  98. }
  99. }