BaseController.php 4.5 KB

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