票务系统微信小程序后端(四)

继续控制器方法
1.微信小程序中授权手机号登录——byWeappPhone
路由:Route::post(‘login/byWeappPhone’, ‘byWeappPhone’);

public function byWeappPhone(Request $request)
    {
        $code = request('code', '');
        $encryptedData = request('encryptedData', '');
        $iv = request('iv', '');
        $miniProgram = \EasyWeChat::miniProgram();
        $session = $miniProgram->auth->session($code);
        if (isset($session['errcode'])) {
            return response()->json([
                'msg' => $session['errmsg']
            ]);
        }
        $decryptedData = $miniProgram->encryptor->decryptData($session['session_key'], $iv, $encryptedData);
        $user = User::where('weapp_openid', $session['openid'])->first();
        if (!$user) {
            $user = User::create([
                'phone' => $decryptedData['phoneNumber'],
                'name' => md5($session['openid']),
                'weapp_openid' => $session['openid'],
                'password' => $session['session_key'] . uniqid(),
            ]);
        }
        $attributes['phone'] = $decryptedData['phoneNumber'];
        $user->update($attributes);
        // 直接创建token并设置有效期
        $createToken = $user->createToken($user->weapp_openid);
        $createToken->token->expires_at = Carbon::now()->addDays(7);
        $createToken->token->save();
        $token = $createToken->accessToken;

        return response()->json([
            'access_token' => $token,
            'token_type' => "Bearer",
            'expires_in' => Carbon::now()->addDays(7),
            'data' => $user,
        ], 200);

    }

票务系统微信小程序后端(四)

2.微信小程序code、昵称、头像、手机一起登录——byWeappUserProfile

public function byWeappUserProfile(Request $request)
    {
        $code = $request['code'];
        // 根据 code 获取微信 openid 和 session_key
        $miniProgram = \EasyWeChat::miniProgram();
        $data = $miniProgram->auth->session($code);
        if (isset($data['errcode'])) {
            return $this->response->errorUnauthorized('code已过期或不正确');
        }
        $weappOpenid = $data['openid'];
        $weixinSessionKey = $data['session_key'];
        $nickname = $request['nickname'];
        $avatar = str_replace('/132', '/0', $request['avatar']);
        //找到 openid 对应的用户
        $user = User::where('weapp_openid', $weappOpenid)->first();
        if (!$user) {
            $user = User::create([
                'name' => md5($weappOpenid),
                'weapp_openid' => $weappOpenid,
                'password' => $weixinSessionKey . uniqid(),
                'weapp_avatar' => $avatar,
                'nick_name' => $nickname,
                'phone' => $request['phone'] ? $request['phone'] : NULL,
                'real_name' => $request['real_name'] ? $request['real_name'] : NULL,
                'id_card' => $request['id_card'] ? $request['id_card'] : NULL,
            ]);
        }

        $attributes['weapp_avatar'] = $avatar;
        $attributes['nick_name'] = $nickname;
        $attributes['updated_at'] = now();
        // 更新用户数据
        $user->update($attributes);


        // 直接创建token并设置有效期
        $createToken = $user->createToken($user->weapp_openid);
        $createToken->token->expires_at = Carbon::now()->addDays(365);
        $createToken->token->save();
        $token = $createToken->accessToken;

        return response()->json([
            'msg' => '登录成功',
            'access_token' => $token,
            'token_type' => "Bearer",
            'expires_in' => Carbon::now()->addDays(365),
            'data' => $user,
        ], 200);
    }

票务系统微信小程序后端(四)
3.User表的增删改查app/Http/Controllers/UserController.php

    public function index(Request $request){
        return User::paginate(14);

    }
    public function show($id){
        return User::find($id);
    }
    public function store(Request $request,User $user){
        return response()->json([
            'msg' => '请通过小程序注册',
        ]);
        $user->fill($request->all());
        $success = $user->save();
        if ($success) {
            return response()->json([
                'msg' => '创建成功',
                'data'=> $user
            ]);
        } else {
            return response()->json([
                'msg' => '创建失败',
                'data'=> $user
            ],201);
        }
    }
    public function update(Request $request){
        $user = Auth::guard('api')->user();
        $attributes['real_name'] = $request['real_name']?$request['real_name']:NULL;
        $attributes['id_card'] = $request['id_card']?$request['id_card']:NULL;
        $attributes['updated_at'] = now();
        $user->update($attributes);
        return response()->json([
            'msg' => '修改成功',
            'data'=> $user
        ]);
    }
    public function destroy(){
        $user = Auth::guard('api')->user();
        $attributes['is_hidden'] = 'T';
        $attributes['updated_at'] = now();
        $user->update($attributes);
        return response()->json([
            'msg'=>'删除成功',
        ]);
    }
本作品采用《CC 协议》,转载必须注明作者和本文链接
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!