BaseController.php 4.5 KB

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