BaseController.php 4.4 KB

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