157 lines
4.8 KiB
PHP
157 lines
4.8 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: yancheng<cheng@love.xiaoyan.me>
|
|
* Date: 2019/6/18
|
|
* Time: 6:56 PM
|
|
*/
|
|
|
|
namespace app\controller;
|
|
|
|
|
|
use app\BaseController;
|
|
use app\common\ApiController;
|
|
use app\model\UserInfo;
|
|
use app\service\EvaluationService;
|
|
use app\service\UserService;
|
|
use app\util\ErrorCode;
|
|
use app\util\ErrorResponse;
|
|
use app\util\SuccessResponse;
|
|
use app\util\WechatUtil;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Csv;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
use think\facade\Log;
|
|
|
|
class User extends ApiController
|
|
{
|
|
protected $middleware = [
|
|
'\app\middleware\ApiCheck' => ['except' => ['create', 'login']],
|
|
];
|
|
|
|
/**
|
|
* 小程序login
|
|
* 通过login获得code到微信服务器换区open_id等数据
|
|
* 并可以将数据直接保存到数据库
|
|
* @param string $code
|
|
* @return \think\response\Json
|
|
*/
|
|
public function login(string $code)
|
|
{
|
|
try {
|
|
$session = UserService::getSessionByCode($code);
|
|
// Log::debug("session data,".print_r($session,1));
|
|
return SuccessResponse::create($session); // ['openid' => $session['openid']]
|
|
} catch (\Exception $e) {
|
|
return ErrorResponse::createFromException($e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 创建微信用户 但是不会创建用户基本数据
|
|
* @return \think\Response
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
* @throws \think\exception\DbException
|
|
*/
|
|
public function create()
|
|
{
|
|
$data = $this->request->post(array_keys($_POST));
|
|
if (empty($data)) {
|
|
return ErrorResponse::createError(
|
|
ErrorCode::ERROR_PARAM_REQUIRED, '用户数据缺失'
|
|
);
|
|
}
|
|
$existsUser = UserInfo::where('open_id', $data['open_id'])->find();
|
|
if (empty($existsUser)) {
|
|
$userInfo = UserInfo::create($data);
|
|
if ($userInfo->id > 0) {
|
|
//创建用户成功 并返回数据
|
|
$data = $userInfo->toArray();
|
|
$data['realname'] = $data['nickname'];
|
|
$data['lastEvaluation'] = null;
|
|
return SuccessResponse::create($data);
|
|
}
|
|
return ErrorResponse::createError(
|
|
ErrorCode::USER_SAVE_FAIL, '保存用户数据失败'
|
|
);
|
|
} else {
|
|
return SuccessResponse::create(
|
|
$this->getUserInfo($existsUser)
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 更新数据
|
|
* @return \think\Response
|
|
*/
|
|
public function update()
|
|
{
|
|
$data = $this->request->post(array_keys($_POST));
|
|
try {
|
|
$detail = UserService::saveDetail($data,$this->getCurrentUserInfo());
|
|
return SuccessResponse::create();
|
|
} catch (\Exception $e) {
|
|
return ErrorResponse::createFromException($e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 用户基本信息,详情,最后一次评估
|
|
* @param UserInfo $userInfo
|
|
* @return array
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
* @throws \think\exception\DbException
|
|
*/
|
|
private function getUserInfo(UserInfo $userInfo)
|
|
{
|
|
$userInfo = $this->getCurrentUserInfo();
|
|
$detail = $userInfo->getParsedDetail();
|
|
$userData = $userInfo->toArray();
|
|
if (empty($detail)) {
|
|
$userData['realname'] = $userInfo->nickname;
|
|
$userData['lastEvaluation'] = null;
|
|
} else {
|
|
$lastEvaluation = EvaluationService::getLastEvaluationByUid($userInfo->id);
|
|
if (!empty($lastEvaluation)) {
|
|
$userData['lastEvaluation'] = $lastEvaluation['create_time'];
|
|
}
|
|
}
|
|
return $userData;
|
|
}
|
|
|
|
/**
|
|
* @return \think\Response
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
* @throws \think\exception\DbException
|
|
*/
|
|
public function info()
|
|
{
|
|
// if (empty($open_id)) {
|
|
// return ErrorResponse::createError(
|
|
// ErrorCode::ERROR_PARAM_ERROR, '用户open_id错误'
|
|
// );
|
|
// }
|
|
// $user = UserInfo::where('open_id', $open_id)->find();
|
|
// if (empty($user)) {
|
|
// return ErrorResponse::createError(
|
|
// ErrorCode::ERROR_USER_NOT_EXISTS, '用户不存在'
|
|
// );
|
|
// }
|
|
|
|
return SuccessResponse::create($this->getUserInfo($this->getCurrentUserInfo()));
|
|
}
|
|
|
|
public function detail()
|
|
{
|
|
return json(
|
|
array_merge(
|
|
$this->getCurrentUserInfo()->toArray(),
|
|
$this->getCurrentUserInfo()->getParsedDetail()->toArray()
|
|
)
|
|
);
|
|
}
|
|
} |