193 lines
5.1 KiB
PHP
193 lines
5.1 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;
|
|
|
|
class UserController extends BaseController
|
|
{
|
|
protected $noNeedLogin = ['getQrUrl', 'getUserInfo', 'bindUser'];
|
|
|
|
protected $noNeedHeaderData = ['getQrUrl', 'getUserInfo', 'bindUser'];
|
|
|
|
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());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
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 wechatLogin1($request)
|
|
{
|
|
$loginCode = $request->post('login_code');
|
|
$phoneCode = $request->post('phone_code');
|
|
|
|
if (!$loginCode || !$phoneCode) {
|
|
return json(['code'=>0, 'msg'=>'缺少参数']);
|
|
}
|
|
|
|
// 1) code2Session
|
|
$session = $this->code2Session($loginCode);
|
|
if (!isset($session['openid'])) {
|
|
return json(['code'=>0, 'msg'=>'登录失败', 'data'=>$session]);
|
|
}
|
|
|
|
$openid = $session['openid'];
|
|
|
|
// 2) access_token
|
|
$tokenData = $this->getAccessToken();
|
|
$token = $tokenData['access_token'];
|
|
|
|
// 3) 换手机号
|
|
$phoneData = $this->getPhoneNumber($token, $phoneCode);
|
|
|
|
if (!isset($phoneData['phone_info']['phoneNumber'])) {
|
|
return json(['code'=>0, 'msg'=>'获取手机号失败', 'data'=>$phoneData]);
|
|
}
|
|
|
|
$phone = $phoneData['phone_info']['phoneNumber'];
|
|
|
|
// 4) 写入数据库或登录逻辑
|
|
// 比如自动注册
|
|
$user = User::firstOrCreate(['openid'=>$openid], [
|
|
'phone' => $phone
|
|
]);
|
|
|
|
return json([
|
|
'code' => 1,
|
|
'msg' => '登录成功',
|
|
'data' => [
|
|
'openid' => $openid,
|
|
'phone' => $phone,
|
|
'token' => md5($openid.time()), // 你自己的登录 token
|
|
]
|
|
]);
|
|
}
|
|
} |