Base.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace app\index\controller;
  3. use think\facade\Request;
  4. use app\model\UsersSessions;
  5. use app\model\UuidRelation;
  6. use app\model\TextReplace;
  7. class Base extends \app\BaseController
  8. {
  9. protected $userInfo = [];
  10. protected function initialize()
  11. {
  12. $this->checkTokenAuth();
  13. }
  14. protected function checkTokenAuth()
  15. {
  16. global $_W;
  17. $token = $this->getAppToken();
  18. $ptype = getclient();
  19. $data['api'] = Author()::getlan();
  20. $tokenInfo = UsersSessions::where(['token' => $token, 'ptype' => $ptype])->find();
  21. if (!empty($tokenInfo)) {
  22. $tokenInfo = $tokenInfo->toArray();
  23. $this->userInfo = iunserializer($tokenInfo['data']);
  24. if (empty($this->userInfo['nickname']) || empty($this->userInfo['avatar'])) {
  25. if (!empty($this->userInfo['openid'])) {
  26. $uid = \app\model\Openid::getuidbyopenid($this->userInfo['openid']);
  27. if (!empty($uid)) {
  28. $member = \app\model\Member::find($uid);
  29. if (!empty($member)) {
  30. $this->userInfo['nickname'] = $member->nickname;
  31. $this->userInfo['avatar'] = $member->userpic;
  32. }
  33. }
  34. }
  35. }
  36. if (empty($_W['uniacid']) && !empty($tokenInfo['weid'])) {
  37. $_W['uniacid'] = $tokenInfo['weid'];
  38. }
  39. if (!empty($this->userInfo['cityinfo']['ocid'])) {
  40. $_W['ocid'] = $this->userInfo['cityinfo']['ocid'];
  41. }
  42. if (!empty($this->userInfo['cityinfo']['tz_id'])) {
  43. $_W['tz_id'] = $this->userInfo['cityinfo']['tz_id'];
  44. }
  45. if (empty($_W['fans']['openid']) && !empty($this->userInfo['openid'])) {
  46. $_W['fans']['openid'] = $this->userInfo['openid'];
  47. }
  48. $_W['fans']['ptype'] = $ptype;
  49. event('DoLog', $this->userInfo['username']); //写入操作日志
  50. } else {
  51. if (!empty($_W['fans']['openid'])) {
  52. $data['openid'] = $_W['fans']['openid'];
  53. }
  54. $data['ptype'] = $ptype;
  55. $data['weid'] = weid();
  56. $this->setAppToken($data, $token);
  57. }
  58. }
  59. //设置token
  60. protected function setAppToken($data, $token = '')
  61. {
  62. $weid = $data['weid'];
  63. if (empty($weid)) {
  64. $weid = weid();
  65. }
  66. if (empty($token)) {
  67. $token = md5(uniqid());
  68. }
  69. if (empty($data['ptype'])) {
  70. $data['ptype'] = getclient();
  71. }
  72. $ptype = $data['ptype'];
  73. $data['api'] = Author()::getlan();
  74. //登录的时候把token写入数据表
  75. $tokenInfo = UsersSessions::where(['token' => $token, 'ptype' => $ptype])->find();
  76. if (empty($tokenInfo)) {
  77. UsersSessions::create([
  78. 'weid' => $weid,
  79. 'token' => $token,
  80. 'ptype' => $ptype,
  81. 'ip' => getRealIP(),
  82. 'expire_time' => time(),
  83. 'data' => serialize($data),
  84. 'status' => 1
  85. ]);
  86. } else {
  87. UsersSessions::where(['token' => $token, 'ptype' => $ptype])->update([
  88. 'weid' => $weid,
  89. 'token' => $token,
  90. 'ptype' => $ptype,
  91. 'ip' => getRealIP(),
  92. 'expire_time' => time(),
  93. 'data' => serialize($data),
  94. 'status' => 1
  95. ]);
  96. }
  97. return $token;
  98. }
  99. //取token
  100. protected function getAppToken()
  101. {
  102. $Authorization = Request::header('Authorization');
  103. $state = input('param.xmtoken', '', 'serach_in');
  104. if (empty($state)) {
  105. $state = $Authorization;
  106. }
  107. if (empty($state)) {
  108. $state = input('get.state', '', 'serach_in');
  109. }
  110. if (empty($state)) {
  111. $state = input('param.state', '', 'serach_in');
  112. }
  113. $token = str_replace("we7sid-", "", $state);
  114. return $token;
  115. }
  116. protected function json($result)
  117. {
  118. if (empty($result['errno'])) {
  119. $result['errno'] = 0;
  120. }
  121. if(empty($result['no_replace'])){
  122. $result = TextReplace::setreplace($result);
  123. }
  124. return json($result);
  125. }
  126. }