BaseController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace App\Http\Controllers\Wap;
  3. use Illuminate\Routing\Controller;
  4. use App\Modules\User\Services\ReadRecordService;
  5. use Illuminate\Http\Request;
  6. use Cookie;
  7. use App\Modules\User\Services\UserService;
  8. class BaseController extends Controller
  9. {
  10. /**
  11. * 请求接口的地址前缀
  12. * @var mixed
  13. */
  14. protected $_url_prefix;
  15. /**
  16. * 用户id
  17. * @var int
  18. */
  19. protected $uid;
  20. /**
  21. * 公众号接口签名密钥
  22. * @var string
  23. */
  24. protected $secret_key = 'zhuishuyun#_2017';
  25. /**
  26. * 渠道id
  27. * @var
  28. */
  29. protected $distribution_channel_id;
  30. /**
  31. * 加密的渠道
  32. * @var string
  33. */
  34. protected $en_distribution_channel_id;
  35. /**
  36. * 派单id
  37. * @var int
  38. */
  39. protected $send_order_id;
  40. /**
  41. * from来源
  42. * @var string
  43. */
  44. protected $from_type;
  45. function __construct(Request $request)
  46. {
  47. $domain = _domain();
  48. $this->distribution_channel_id = str_replace('site','',explode('.',$domain)[0]);
  49. if(!is_numeric($this->distribution_channel_id)){
  50. $this->en_distribution_channel_id = $this->distribution_channel_id;
  51. $this->distribution_channel_id = decodeDistributionChannelId($this->distribution_channel_id);
  52. }else{
  53. $this->en_distribution_channel_id = encodeDistributionChannelId($this->distribution_channel_id);
  54. }
  55. $this->_url_prefix = env('PUBLIC_BASE_API');
  56. $user_cookie = Cookie::get(env('COOKIE_AUTH_WEB_WECHAT'));
  57. $this->uid = $user_cookie ? decrypt($user_cookie) : null;
  58. $send_order_id = Cookie::get('send_order_id');
  59. if(!$send_order_id){
  60. if($this->uid){
  61. $send_order_id = ReadRecordService::getSendOrderId($this->uid);
  62. }else{
  63. $send_order_id = 0;
  64. }
  65. }
  66. $this->innerSendOrderIdFromFromtype($this->uid);
  67. $this->send_order_id = $send_order_id;
  68. $this->from_type = Cookie::get('from') ? Cookie::get('from') : 'main';
  69. //$this->uid = 13;
  70. }
  71. private function innerSendOrderIdFromFromtype($uid){
  72. //$inner_send_order_id = ReadRecordService::getInnerSendOrderId($uid);
  73. $inner_send_order_id = false;
  74. if(!$inner_send_order_id){
  75. $from = Cookie::get('from');
  76. if($from && (starts_with($from,'custom') || starts_with($from,'template')) && count(explode('_',$from)) == 2){
  77. ReadRecordService::setInnerSendOrderId($uid,$from);
  78. }
  79. }
  80. }
  81. protected function userInfo($uid){
  82. $user = UserService::getById($uid);
  83. return $user;
  84. }
  85. protected function checkUid(){
  86. if(!$this->uid) return false;
  87. return true;
  88. }
  89. public function __get($name)
  90. {
  91. if($name == '_user_info'){
  92. return $this->userInfo($this->uid);
  93. }
  94. return null;
  95. }
  96. protected function joinUrl($url)
  97. {
  98. if (stripos($url, '/') === 0) {
  99. $url = substr($url, 1);
  100. }
  101. return $this->_url_prefix . $url;
  102. }
  103. /**
  104. * 公众号签名@华灯初上
  105. * @param $params
  106. * @return string
  107. */
  108. protected function getSign($params)
  109. {
  110. $url = $this->arr_to_url($params, false);
  111. $url = $url . '&key=' . $this->secret_key;
  112. $sign = md5($url);
  113. return $sign;
  114. }
  115. /**
  116. * 公众号签名@华灯初上
  117. * @param $array
  118. * @param bool $has_sign
  119. * @return string
  120. */
  121. protected function arr_to_url($array, $has_sign = false)
  122. {
  123. ksort($array);
  124. reset($array);
  125. $arg = "";
  126. while (list ($name, $val) = each($array)) {
  127. if ($name == 'sign' && !$has_sign) continue;
  128. if (strpos($name, "_") === 0)
  129. continue;
  130. if (is_array($val))
  131. $val = join(',', $val);
  132. if ($val === "")
  133. continue;
  134. $arg .= $name . "=" . $val . "&";
  135. }
  136. $arg = substr($arg, 0, count($arg) - 2);
  137. return $arg;
  138. }
  139. }