$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']; } }