From 9db9ac7c3c0377473bf8223ac2126f41c7f43ab3 Mon Sep 17 00:00:00 2001 From: callmeyan Date: Sun, 23 Jun 2019 11:38:31 +0800 Subject: [PATCH] update user login --- .example.env | 2 +- app/controller/Evaluation.php | 22 ++++++++++++++-- app/controller/User.php | 21 +++++++++++++-- app/service/EvaluationService.php | 6 +++++ app/util/ErrorResponse.php | 11 +++++--- app/util/WechatUtil.php | 44 +++++++++++++++++++++++++++++++ config/wechat.php | 14 ++++++++++ 7 files changed, 112 insertions(+), 8 deletions(-) create mode 100644 app/util/WechatUtil.php create mode 100644 config/wechat.php diff --git a/.example.env b/.example.env index 52de451..1063a8e 100644 --- a/.example.env +++ b/.example.env @@ -1 +1 @@ -APP_DEBUG = true [APP] DEFAULT_TIMEZONE = Asia/Shanghai ADMIN_MULTI_LOGIN = true [DATABASE] TYPE = mysql HOSTNAME = 127.0.0.1 DATABASE = test USERNAME = username PASSWORD = password HOSTPORT = 3306 CHARSET = utf8 DEBUG = true [LANG] default_lang = zh-cn \ No newline at end of file +APP_DEBUG = true [APP] DEFAULT_TIMEZONE = Asia/Shanghai ADMIN_MULTI_LOGIN = true [WECHAT] KEY = wxba6738e3d81f8461 SECRET = wxba6738e3d81f8461 [DATABASE] TYPE = mysql HOSTNAME = 127.0.0.1 DATABASE = test USERNAME = username PASSWORD = password HOSTPORT = 3306 CHARSET = utf8 DEBUG = true [LANG] default_lang = zh-cn \ No newline at end of file diff --git a/app/controller/Evaluation.php b/app/controller/Evaluation.php index 25f2648..60c62bb 100644 --- a/app/controller/Evaluation.php +++ b/app/controller/Evaluation.php @@ -38,16 +38,34 @@ class Evaluation extends ApiController ErrorCode::ERROR_PARAM_REQUIRED, '评估数据缺失' ); } + // 计算总分 + $score_keys = array_keys(Config::get('app.evaluation.subject')); + $total_score = 0; + foreach ($score_keys as $key){ + $total_score += intval($data[$key]); + } + $score_keys = array_keys(Config::get('app.evaluation.subject'));//['dizzy','gastrointestinal','headache','tired']; $evaluation = EvaluationHistory::create(array_merge([ - 'uid' => $this->getCurrentUserInfo()->id + 'uid' => $this->getCurrentUserInfo()->id, + 'score' => $total_score ], $data)); if ($evaluation->id > 0) { - return SuccessResponse::create(); + return SuccessResponse::create(['id'=>$evaluation->id]); } return ErrorResponse::createError( ErrorCode::EVALUATION_SAVE_FAIL, '保存评估数据失败' ); } + public function detail(int $id) + { + $evaluation = EvaluationService::findById($id); + if(empty($evaluation)){ + return ErrorResponse::createError( + ErrorCode::EVALUATION_NOT_EXISTS, '评估数据不存在或已被删除' + ); + } + return SuccessResponse::create($evaluation); + } /** * 获取用户评估的所有项目 diff --git a/app/controller/User.php b/app/controller/User.php index f89a9d7..07a8a57 100644 --- a/app/controller/User.php +++ b/app/controller/User.php @@ -14,6 +14,7 @@ use app\common\ApiController; use app\model\UserInfo; use app\util\ErrorCode; use app\util\ErrorResponse; +use app\util\WechatUtil; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Csv; use PhpOffice\PhpSpreadsheet\Writer\Xlsx; @@ -21,10 +22,9 @@ use PhpOffice\PhpSpreadsheet\Writer\Xlsx; class User extends ApiController { protected $middleware = [ - '\app\middleware\ApiCheck' => ['except' => ['create']], + '\app\middleware\ApiCheck' => ['except' => ['create', 'login']], ]; - public function search() { list($is_first, $gender, $city, $name) = $this->getParams( @@ -39,6 +39,23 @@ class User extends ApiController ->toArray()); } + /** + * 小程序login + * 通过login获得code到微信服务器换区open_id等数据 + * 并可以将数据直接保存到数据库 + * @param string $code + */ + public function login(string $code) + { + $wechat = new WechatUtil(); + return json( + [ + WX_APP_ID, + WX_APP_SECRET + ] + ); + } + public function create() { $dataType = $this->request->post('type'); diff --git a/app/service/EvaluationService.php b/app/service/EvaluationService.php index 9af1e99..830edd7 100644 --- a/app/service/EvaluationService.php +++ b/app/service/EvaluationService.php @@ -37,6 +37,11 @@ class EvaluationService ['属于重度高反', '联系120,医院急诊留观或进入抢救室'], ]; + public static function findById(int $id){ + $evaluation = EvaluationHistory::find($id); + if(empty($evaluation)) return null; + return self::parseEvaluationData($evaluation->toArray()); + } public static function parseEvaluationData($item) { $data = []; @@ -62,6 +67,7 @@ class EvaluationService elseif ($data['score'] <= 9) $level = 2; else $level = 3; } + $data['level'] = $level; $data['result'] = $results[$level][0]; $data['suggest'] = $results[$level][1]; } diff --git a/app/util/ErrorResponse.php b/app/util/ErrorResponse.php index 0f69fb3..eeaa297 100644 --- a/app/util/ErrorResponse.php +++ b/app/util/ErrorResponse.php @@ -50,15 +50,20 @@ class ErrorCode * 评估 */ const EVALUATION_SAVE_FAIL = 23005; + const EVALUATION_NOT_EXISTS = 23006; } class ErrorResponse extends Json { - public static function createError($errorCode, $errorMessage = ''): Response + public static function createError($errorCode, $errorMessage = '',$extData = null): Response { - return parent::create([ + $ret = [ "code" => $errorCode, "message" => $errorMessage - ]); // TODO: Change the autogenerated stub + ]; + if($extData){ + $ret['data'] = $extData; + } + return parent::create($ret); // TODO: Change the autogenerated stub } } \ No newline at end of file diff --git a/app/util/WechatUtil.php b/app/util/WechatUtil.php new file mode 100644 index 0000000..396b2ce --- /dev/null +++ b/app/util/WechatUtil.php @@ -0,0 +1,44 @@ + + * Date: 2019/6/23 + * Time: 11:29 AM + */ + +namespace app\util; +use think\facade\Config; + +define('WX_SERVER_URL', 'https://api.weixin.qq.com'); +define('CODE_2_SESSION', WX_SERVER_URL . '/sns/jscode2session'); +define('WX_APP_ID',Config::get('wechat.key')); +define('WX_APP_SECRET',Config::get('wechat.secret')); +class WechatUtil +{ + public function __construct() + { + } + + public function codeToSession(strign $code): array + { + return json_decode( + $this->request( + CODE_2_SESSION, [ + 'js_code' => 'JSCODE', + 'grant_type' => 'authorization_code' + ] + ), true + ); + } + + private function request(string $api, array $params = [], string $method = 'GET') + { + $params['appid'] = WX_APP_ID; + $params['secret'] = WX_APP_SECRET; + $url = WX_SERVER_URL . $api; + if (strtolower($method) == 'get') { + $url .= http_build_query($params); + } + return file_get_contents($url); + } +} \ No newline at end of file diff --git a/config/wechat.php b/config/wechat.php new file mode 100644 index 0000000..06a04d4 --- /dev/null +++ b/config/wechat.php @@ -0,0 +1,14 @@ + + * Date: 2019/6/23 + * Time: 11:24 AM + */ + +use think\facade\Env; + +return [ + 'key' => Env::get('wechat.key'), + 'secret' => Env::get('wechat.secret'), +]; \ No newline at end of file