191 lines
5.0 KiB
PHP
191 lines
5.0 KiB
PHP
|
|
<?php
|
||
|
|
namespace app\common\controller;
|
||
|
|
|
||
|
|
use app\common\controller\BaseController;
|
||
|
|
use app\common\service\OssService;
|
||
|
|
use app\common\traits\crud\CrudControllerTrait;
|
||
|
|
use app\common\service\UserService;
|
||
|
|
use app\common\service\WechatService;
|
||
|
|
use support\Request;
|
||
|
|
use support\Response;
|
||
|
|
|
||
|
|
class UserController extends BaseController
|
||
|
|
{
|
||
|
|
protected $noNeedLogin = ['getQrUrl', 'getUserInfo', 'bindUser', 'miniappLogin'];
|
||
|
|
|
||
|
|
protected $noNeedHeaderData = ['getQrUrl', 'getUserInfo', 'bindUser', 'miniappLogin'];
|
||
|
|
|
||
|
|
use CrudControllerTrait;
|
||
|
|
|
||
|
|
protected $service;
|
||
|
|
|
||
|
|
public function __construct()
|
||
|
|
{
|
||
|
|
$this->service = new UserService();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 列表页数据格式化
|
||
|
|
*
|
||
|
|
* @param array $data
|
||
|
|
* @return array
|
||
|
|
*/
|
||
|
|
// protected function indexFormat($data)
|
||
|
|
// {
|
||
|
|
// if (isset($data['created_at']))
|
||
|
|
// $data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
|
||
|
|
// if (isset($data['updated_at']))
|
||
|
|
// $data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
|
||
|
|
// return $data;
|
||
|
|
// }
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取微信二维码
|
||
|
|
*/
|
||
|
|
public function getQrUrl()
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$result = $this->service->makeQrUrl();
|
||
|
|
return $this->success($result);
|
||
|
|
} catch (\Throwable $th) {
|
||
|
|
return $this->error($th->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取用户信息
|
||
|
|
*/
|
||
|
|
public function getUserInfo(Request $request)
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$params = $request->all();
|
||
|
|
$result = $this->service->userInfo($params['code'], $params['state']);
|
||
|
|
unset($result['password']);
|
||
|
|
|
||
|
|
if (!isset($result['avatar'])){
|
||
|
|
return $this->success($result);
|
||
|
|
}
|
||
|
|
|
||
|
|
$ossservice = new OssService();
|
||
|
|
$result['avatar_pull'] = $result['avatar'];
|
||
|
|
if(strpos($result['avatar'], 'http') === false && !empty($result['avatar'])){
|
||
|
|
$result['avatar_pull'] = $ossservice->generatePrivateUrl($result['avatar']);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $this->success($result);
|
||
|
|
} catch (\Throwable $th) {
|
||
|
|
return $this->error($th->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 用户信息(登录后调用)
|
||
|
|
*/
|
||
|
|
public function info(Request $request)
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$uid = $request->user->uid;
|
||
|
|
$result = (new UserService())->getUserInfo($uid);
|
||
|
|
return $this->success($result, '获取用户信息');
|
||
|
|
} catch (\Throwable $e) {
|
||
|
|
return $this->error($e->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 绑定用户
|
||
|
|
*/
|
||
|
|
public function bindUser(Request $request)
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$params = $request->all();
|
||
|
|
$result = $this->service->bindUser($params);
|
||
|
|
unset($result['password']);
|
||
|
|
return $this->success($result);
|
||
|
|
} catch (\Throwable $th) {
|
||
|
|
return $this->error($th->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 修改密码
|
||
|
|
* @param Request $request
|
||
|
|
* @return \support\Response
|
||
|
|
* @throws \Random\RandomException
|
||
|
|
*/
|
||
|
|
public function savePassword(Request $request)
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$uid = $request->user->uid;
|
||
|
|
$data = $request->all();
|
||
|
|
$result = (new UserService())->savePassword($uid,$data);;
|
||
|
|
return $this->success($result, '编辑成功');
|
||
|
|
} catch (\Throwable $e) {
|
||
|
|
return $this->error($e->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 修改名字与头像
|
||
|
|
* @param Request $request
|
||
|
|
* @return \support\Response
|
||
|
|
* @throws \Random\RandomException
|
||
|
|
*/
|
||
|
|
public function save(Request $request)
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$uid = $request->user->uid;
|
||
|
|
$data = $request->all();
|
||
|
|
$result = (new UserService())->save($uid,$data);;
|
||
|
|
return $this->success($result, '编辑成功');
|
||
|
|
} catch (\Throwable $e) {
|
||
|
|
return $this->error($e->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
public function wechatLogin($request)
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$params = $request->all();
|
||
|
|
$result = $this->service->wechatLogin($params['login_code'], $params['phone_code']);
|
||
|
|
|
||
|
|
return $this->success($result);
|
||
|
|
} catch (\Throwable $th) {
|
||
|
|
return $this->error($th->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 小程序登录
|
||
|
|
*/
|
||
|
|
public function miniappLogin(Request $request): Response
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$result = $this->service->miniappLogin($request->all());
|
||
|
|
return $this->success($result);
|
||
|
|
} catch (\Throwable $th) {
|
||
|
|
return $this->error($th->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取手机号
|
||
|
|
*/
|
||
|
|
public function miniappPhoneNumber(Request $request): Response
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
$result = $this->service->miniappPhoneNumber($request->all(),$request);
|
||
|
|
return $this->success($result);
|
||
|
|
} catch (\Throwable $th) {
|
||
|
|
return $this->error($th->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|