129 lines
3.6 KiB
PHP
129 lines
3.6 KiB
PHP
![]() |
<?php
|
|||
|
|
|||
|
namespace app\common\service;
|
|||
|
|
|||
|
use app\common\model\Photographer;
|
|||
|
use app\common\model\User;
|
|||
|
use app\constant\ResponseCode;
|
|||
|
use think\Exception;
|
|||
|
use Tinywan\Jwt\JwtToken;
|
|||
|
use Webman\Config;
|
|||
|
|
|||
|
class UserService
|
|||
|
{
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* @desc 登录
|
|||
|
* @param $request
|
|||
|
* @return array
|
|||
|
*/
|
|||
|
public function login($request)
|
|||
|
{
|
|||
|
try {
|
|||
|
if (!isset($request['code']) || empty($request['code'])) {
|
|||
|
throw new Exception('参数code错误');
|
|||
|
}
|
|||
|
if (!isset($request['avatar']) || empty($request['avatar'])) {
|
|||
|
throw new Exception('参数avatar错误');
|
|||
|
}
|
|||
|
if (!isset($request['nickname']) || empty($request['nickname'])) {
|
|||
|
throw new Exception('参数code错误');
|
|||
|
}
|
|||
|
$openid = self::getUserOpenId($request['code']);
|
|||
|
if (!$openid) {
|
|||
|
throw new Exception('获取openid失败');
|
|||
|
}
|
|||
|
|
|||
|
$user = User::where(['openid' => $openid])->findOrEmpty();
|
|||
|
if ($user->isEmpty()) {
|
|||
|
$user = User::create([
|
|||
|
'nickname' => $request['nickname'],
|
|||
|
'openid' => $openid,
|
|||
|
'avatar' => $request['avatar'],
|
|||
|
]);
|
|||
|
}
|
|||
|
$token_data = [
|
|||
|
'id' => $user->id
|
|||
|
];
|
|||
|
$token = JwtToken::generateToken($token_data);
|
|||
|
unset($token['refresh_token']);
|
|||
|
return [
|
|||
|
'code' => ResponseCode::SUCCESS,
|
|||
|
'data' => $token,
|
|||
|
'msg' => 'success'
|
|||
|
];
|
|||
|
|
|||
|
} catch (Exception $e) {
|
|||
|
return [
|
|||
|
'code' => ResponseCode::FAIL,
|
|||
|
'msg' => $e->getMessage()
|
|||
|
];
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public function select($request)
|
|||
|
{
|
|||
|
try {
|
|||
|
|
|||
|
}catch (Exception $e){
|
|||
|
return [
|
|||
|
'code' => ResponseCode::FAIL,
|
|||
|
'msg' => $e->getMessage()
|
|||
|
];
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* @desc 用户信息
|
|||
|
* @param $request
|
|||
|
* @return array
|
|||
|
*/
|
|||
|
public function userInfo($request)
|
|||
|
{
|
|||
|
try {
|
|||
|
$user = User::where(['id' => $request->user->id])->with(['studio', 'photographer'])->field('id,nickname,openid,avatar,is_photographer,studio_id')->findOrEmpty();
|
|||
|
if ($user->isEmpty()) {
|
|||
|
throw new Exception('未找到用户');
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
return [
|
|||
|
'code' => ResponseCode::SUCCESS,
|
|||
|
'data' => $user,
|
|||
|
'msg' => 'success'
|
|||
|
];
|
|||
|
} catch (Exception $e) {
|
|||
|
return [
|
|||
|
'code' => ResponseCode::FAIL,
|
|||
|
'msg' => $e->getMessage()
|
|||
|
];
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* 获取openid
|
|||
|
* @param $code
|
|||
|
* @return false|mixed
|
|||
|
*/
|
|||
|
public static function getUserOpenId($code)
|
|||
|
{
|
|||
|
$appid = getenv('APP_ID');//小程序的appid
|
|||
|
$appSecret = getenv('APP_SECRET');// 小程序的$appSecret
|
|||
|
|
|||
|
$wxUrl = 'https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code';
|
|||
|
$getUrl = sprintf($wxUrl, $appid, $appSecret, $code);//把appid,appsecret,code拼接到url里
|
|||
|
$result = curl_get($getUrl);//请求拼接好的url
|
|||
|
raw_log('wechat/get_openid', ['get_url' => $getUrl, 'result' => $result]);
|
|||
|
$wxResult = json_decode($result, true);
|
|||
|
if (!$wxResult) {
|
|||
|
return false;
|
|||
|
}
|
|||
|
if (array_key_exists('errcode', $wxResult)) {
|
|||
|
return false;
|
|||
|
}
|
|||
|
return $wxResult['openid'];
|
|||
|
}
|
|||
|
|
|||
|
}
|