BaseController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. declare(strict_types=1);
  3. namespace app;
  4. use think\App;
  5. use think\exception\ValidateException;
  6. use think\Validate;
  7. use think\facade\Db;
  8. use think\cache\driver\Redis;
  9. /**
  10. * 控制器基础类
  11. */
  12. abstract class BaseController
  13. {
  14. /**
  15. * Request实例
  16. * @var \think\Request
  17. */
  18. protected $request;
  19. /**
  20. * 应用实例
  21. * @var \think\App
  22. */
  23. protected $app;
  24. /**
  25. * 是否批量验证
  26. * @var bool
  27. */
  28. protected $batchValidate = false;
  29. /**
  30. * 控制器中间件
  31. * @var array
  32. */
  33. protected $middleware = [];
  34. /**
  35. * 无需登录鉴权的方法名
  36. * @var array
  37. */
  38. protected $noNeedLogin = [];
  39. /**
  40. * redis对象
  41. * @var array
  42. */
  43. protected $redis;
  44. private $token;
  45. private $userData = null;
  46. /**
  47. * 构造方法
  48. * @access public
  49. * @param App $app 应用对象
  50. */
  51. public function __construct(App $app)
  52. {
  53. $this->app = $app;
  54. $this->request = $this->app->request;
  55. $this->redis = new Redis();
  56. // 控制器初始化
  57. $this->initialize();
  58. }
  59. // 初始化
  60. protected function initialize()
  61. {
  62. //noNeedLogin全部转换小写
  63. foreach ($this->noNeedLogin as &$action) {
  64. $action = strtolower($action);
  65. }
  66. //验证需要鉴权的方法名
  67. $callMethodName = strtolower($this->request->action());
  68. if (!in_array($callMethodName, $this->noNeedLogin)) {
  69. $userToken = Db::table('tb_user_token')
  70. ->where(['token' => $this->request->header('token')])
  71. ->where('expired_at', '>', time())
  72. ->find();
  73. if (!$userToken) {
  74. $this->fail(401, '请先登录后再操作');
  75. header('Location: /login');
  76. }
  77. $this->token = $this->request->header('token');
  78. }
  79. session_start();
  80. }
  81. protected function getUser()
  82. {
  83. if ($this->userData == null) {
  84. $this->userData = Db::table('tb_user_token')
  85. ->where(['token' => $this->token])
  86. ->find();
  87. }
  88. return (object) $this->userData;
  89. }
  90. /**
  91. * 验证数据
  92. * @access protected
  93. * @param array $data 数据
  94. * @param string|array $validate 验证器名或者验证规则数组
  95. * @param array $message 提示信息
  96. * @param bool $batch 是否批量验证
  97. * @return array|string|true
  98. * @throws ValidateException
  99. */
  100. protected function validate(array $data, $validate, array $message = [], bool $batch = false)
  101. {
  102. if (is_array($validate)) {
  103. $v = new Validate();
  104. $v->rule($validate);
  105. } else {
  106. if (strpos($validate, '.')) {
  107. // 支持场景
  108. [$validate, $scene] = explode('.', $validate);
  109. }
  110. $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
  111. $v = new $class();
  112. if (!empty($scene)) {
  113. $v->scene($scene);
  114. }
  115. }
  116. $v->message($message);
  117. // 是否批量验证
  118. if ($batch || $this->batchValidate) {
  119. $v->batch(true);
  120. }
  121. return $v->failException(true)->check($data);
  122. }
  123. protected function success($msg, $data)
  124. {
  125. header('Content-Type:application/json; charset=utf-8');
  126. echo json_encode([
  127. 'code' => 200,
  128. 'msg' => $msg,
  129. 'data' => $data,
  130. 'time' => time()
  131. ]);
  132. exit();
  133. }
  134. protected function fail($code, $msg)
  135. {
  136. header('Content-Type:application/json; charset=utf-8');
  137. echo json_encode([
  138. 'code' => $code,
  139. 'msg' => $msg,
  140. 'time' => time()
  141. ]);
  142. exit();
  143. }
  144. //发送TCP消息
  145. protected function sendTcpMessage()
  146. {
  147. }
  148. //金币增加
  149. protected function addGold($value)
  150. {
  151. $row = Db::table('tb_user')
  152. ->where(['id' => $this->getUser()->user_id])
  153. ->find();
  154. Db::table('tb_user')
  155. ->where(['id' => $this->getUser()->user_id])
  156. ->update([
  157. 'gold' => $row['gold'] + $value,
  158. ]);
  159. }
  160. //增加经验
  161. protected function addExp($value)
  162. {
  163. $row = Db::table('tb_user')
  164. ->where(['id' => $this->getUser()->user_id])
  165. ->find();
  166. Db::table('tb_user')
  167. ->where(['id' => $this->getUser()->user_id])
  168. ->update([
  169. 'exp' => $row['exp'] + $value,
  170. ]);
  171. }
  172. //增加积分
  173. protected function addScore($value)
  174. {
  175. $row = Db::table('tb_user')
  176. ->where(['id' => $this->getUser()->user_id])
  177. ->find();
  178. Db::table('tb_user')
  179. ->where(['id' => $this->getUser()->user_id])
  180. ->update([
  181. 'score' => $row['score'] + $value,
  182. ]);
  183. }
  184. // 解析获取头像Url
  185. public function getUserAvatar($avatar)
  186. {
  187. if (strpos($avatar, 'system://') === 0) {
  188. $data = explode('//', $avatar);
  189. $avatarId = isset($data[1]) ? intval($data[1]) : 0;
  190. if ($avatarId <= 0) {
  191. return $avatar;
  192. }
  193. $avatarRow = Db::table('tb_system_avatar')
  194. ->where(['id' => $avatarId])
  195. ->find();
  196. return $avatarRow ? $avatarRow['image_url'] : $avatar;
  197. }
  198. return $avatar;
  199. }
  200. }