BaseController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. if($this->uid == 114933690) {
  67. $this->uid = 108535910;
  68. }
  69. $this->innerSendOrderIdFromFromtype($this->uid);
  70. $this->send_order_id = $send_order_id;
  71. $this->from_type = Cookie::get('from') ? Cookie::get('from') : 'main';
  72. //$this->uid = 13;
  73. }
  74. private function innerSendOrderIdFromFromtype($uid){
  75. //$inner_send_order_id = ReadRecordService::getInnerSendOrderId($uid);
  76. $inner_send_order_id = false;
  77. if(!$inner_send_order_id){
  78. $from = Cookie::get('from');
  79. if($from && (starts_with($from,'custom') || starts_with($from,'template')) && count(explode('_',$from)) == 2){
  80. ReadRecordService::setInnerSendOrderId($uid,$from);
  81. }
  82. }
  83. }
  84. protected function userInfo($uid){
  85. $user = UserService::getById($uid);
  86. return $user;
  87. }
  88. protected function checkUid(){
  89. if(!$this->uid) return false;
  90. return true;
  91. }
  92. public function __get($name)
  93. {
  94. if($name == '_user_info'){
  95. return $this->userInfo($this->uid);
  96. }
  97. return null;
  98. }
  99. protected function joinUrl($url)
  100. {
  101. if (stripos($url, '/') === 0) {
  102. $url = substr($url, 1);
  103. }
  104. return $this->_url_prefix . $url;
  105. }
  106. /**
  107. * 公众号签名@华灯初上
  108. * @param $params
  109. * @return string
  110. */
  111. protected function getSign($params)
  112. {
  113. $url = $this->arr_to_url($params, false);
  114. $url = $url . '&key=' . $this->secret_key;
  115. $sign = md5($url);
  116. return $sign;
  117. }
  118. /**
  119. * 公众号签名@华灯初上
  120. * @param $array
  121. * @param bool $has_sign
  122. * @return string
  123. */
  124. protected function arr_to_url($array, $has_sign = false)
  125. {
  126. ksort($array);
  127. reset($array);
  128. $arg = "";
  129. while (list ($name, $val) = each($array)) {
  130. if ($name == 'sign' && !$has_sign) continue;
  131. if (strpos($name, "_") === 0)
  132. continue;
  133. if (is_array($val))
  134. $val = join(',', $val);
  135. if ($val === "")
  136. continue;
  137. $arg .= $name . "=" . $val . "&";
  138. }
  139. $arg = substr($arg, 0, count($arg) - 2);
  140. return $arg;
  141. }
  142. }