一.系統介紹
一款基于Uniapp+ThinkPHP開發健身系統,支持多城市、多門店,包含用戶端、教練端、門店端、平臺端四個身份。有團課、私教、訓練營三種課程類型,支持在線排課。私教可以通過上課獲得收益,在線申請提現功能,無加密源代碼
二.搭建環境
系統環境:CentOS、
運行環境:寶塔?Linux
網站環境:Nginx 1.26 + MySQL 5.7.46 + PHP-74
常見插件:fileinfo ; redis
系統搭建測試(圖片僅供參考,無實際運營)
?
---教練端----
?
?
?
?
?
?
?
?
?
系統源碼全開源,可二次開發。后端common/api文件代碼:
<?phpnamespace app\common\controller;use app\common\library\Auth;
use think\Config;
use think\exception\HttpResponseException;
use think\exception\ValidateException;
use think\Hook;
use think\Lang;
use think\Loader;
use think\Request;
use think\Response;
use think\Route;
use think\Validate;/*** API控制器基類*/
class Api
{/*** @var Request Request 實例*/protected $request;/*** @var bool 驗證失敗是否拋出異常*/protected $failException = false;/*** @var bool 是否批量驗證*/protected $batchValidate = false;/*** @var array 前置操作方法列表*/protected $beforeActionList = [];/*** 無需登錄的方法,同時也就不需要鑒權了* @var array*/protected $noNeedLogin = [];/*** 無需鑒權的方法,但需要登錄* @var array*/protected $noNeedRight = [];/*** 權限Auth* @var Auth*/protected $auth = null;/*** 默認響應輸出類型,支持json/xml* @var string*/protected $responseType = 'json';/*** 構造方法* @access public* @param Request $request Request 對象*/public function __construct(Request $request = null){$this->request = is_null($request) ? Request::instance() : $request;// 控制器初始化$this->_initialize();// 前置操作方法if ($this->beforeActionList) {foreach ($this->beforeActionList as $method => $options) {is_numeric($method) ?$this->beforeAction($options) :$this->beforeAction($method, $options);}}}/*** 初始化操作* @access protected*/protected function _initialize(){//跨域請求檢測check_cors_request();// 檢測IP是否允許check_ip_allowed();//移除HTML標簽$this->request->filter('trim,strip_tags,htmlspecialchars');$this->auth = Auth::instance();$modulename = $this->request->module();$controllername = Loader::parseName($this->request->controller());$actionname = strtolower($this->request->action());// token$token = $this->request->server('HTTP_TOKEN', $this->request->request('token', \think\Cookie::get('token')));$path = str_replace('.', '/', $controllername) . '/' . $actionname;// 設置當前請求的URI$this->auth->setRequestUri($path);// 檢測是否需要驗證登錄if (!$this->auth->match($this->noNeedLogin)) {//初始化$this->auth->init($token);//檢測是否登錄if (!$this->auth->isLogin()) {$this->error(__('Please login first'), null, 401);}// 判斷是否需要驗證權限if (!$this->auth->match($this->noNeedRight)) {// 判斷控制器和方法判斷是否有對應權限if (!$this->auth->check($path)) {$this->error(__('You have no permission'), null, 403);}}} else {// 如果有傳遞token才驗證是否登錄狀態if ($token) {$this->auth->init($token);}}$upload = \app\common\model\Config::upload();// 上傳信息配置后Hook::listen("upload_config_init", $upload);Config::set('upload', array_merge(Config::get('upload'), $upload));// 加載當前控制器語言包$this->loadlang($controllername);}/*** 加載語言文件* @param string $name*/protected function loadlang($name){$name = Loader::parseName($name);$name = preg_match("/^([a-zA-Z0-9_\.\/]+)\$/i", $name) ? $name : 'index';$lang = $this->request->langset();$lang = preg_match("/^([a-zA-Z\-_]{2,10})\$/i", $lang) ? $lang : 'zh-cn';Lang::load(APP_PATH . $this->request->module() . '/lang/' . $lang . '/' . str_replace('.', '/', $name) . '.php');}/*** 操作成功返回的數據* @param string $msg 提示信息* @param mixed $data 要返回的數據* @param int $code 錯誤碼,默認為1* @param string $type 輸出類型* @param array $header 發送的 Header 信息*/protected function success($msg = '', $data = null, $code = 1, $type = null, array $header = []){$this->result($msg, $data, $code, $type, $header);}/*** 操作失敗返回的數據* @param string $msg 提示信息* @param mixed $data 要返回的數據* @param int $code 錯誤碼,默認為0* @param string $type 輸出類型* @param array $header 發送的 Header 信息*/protected function error($msg = '', $data = null, $code = 0, $type = null, array $header = []){$this->result($msg, $data, $code, $type, $header);}/*** 返回封裝后的 API 數據到客戶端* @access protected* @param mixed $msg 提示信息* @param mixed $data 要返回的數據* @param int $code 錯誤碼,默認為0* @param string $type 輸出類型,支持json/xml/jsonp* @param array $header 發送的 Header 信息* @return void* @throws HttpResponseException*/protected function result($msg, $data = null, $code = 0, $type = null, array $header = []){$result = ['code' => $code,'msg' => $msg,'time' => Request::instance()->server('REQUEST_TIME'),'data' => $data,];// 如果未設置類型則使用默認類型判斷$type = $type ? : $this->responseType;if (isset($header['statuscode'])) {$code = $header['statuscode'];unset($header['statuscode']);} else {//未設置狀態碼,根據code值判斷$code = $code >= 1000 || $code < 200 ? 200 : $code;}$response = Response::create($result, $type, $code)->header($header);throw new HttpResponseException($response);}/*** 前置操作* @access protected* @param string $method 前置操作方法名* @param array $options 調用參數 ['only'=>[...]] 或者 ['except'=>[...]]* @return void*/protected function beforeAction($method, $options = []){if (isset($options['only'])) {if (is_string($options['only'])) {$options['only'] = explode(',', $options['only']);}if (!in_array($this->request->action(), $options['only'])) {return;}} elseif (isset($options['except'])) {if (is_string($options['except'])) {$options['except'] = explode(',', $options['except']);}if (in_array($this->request->action(), $options['except'])) {return;}}call_user_func([$this, $method]);}/*** 設置驗證失敗后是否拋出異常* @access protected* @param bool $fail 是否拋出異常* @return $this*/protected function validateFailException($fail = true){$this->failException = $fail;return $this;}/*** 驗證數據* @access protected* @param array $data 數據* @param string|array $validate 驗證器名或者驗證規則數組* @param array $message 提示信息* @param bool $batch 是否批量驗證* @param mixed $callback 回調方法(閉包)* @return array|string|true* @throws ValidateException*/protected function validate($data, $validate, $message = [], $batch = false, $callback = null){if (is_array($validate)) {$v = Loader::validate();$v->rule($validate);} else {// 支持場景if (strpos($validate, '.')) {list($validate, $scene) = explode('.', $validate);}$v = Loader::validate($validate);!empty($scene) && $v->scene($scene);}// 批量驗證if ($batch || $this->batchValidate) {$v->batch(true);}// 設置錯誤信息if (is_array($message)) {$v->message($message);}// 使用回調驗證if ($callback && is_callable($callback)) {call_user_func_array($callback, [$v, &$data]);}if (!$v->check($data)) {if ($this->failException) {throw new ValidateException($v->getError());}return $v->getError();}return true;}/*** 刷新Token*/protected function token(){$token = $this->request->param('__token__');//驗證Tokenif (!Validate::make()->check(['__token__' => $token], ['__token__' => 'require|token'])) {$this->error(__('Token verification error'), ['__token__' => $this->request->token()]);}//刷新Token$this->request->token();}
}
?
?