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

1.在config/auth.php中新增api

 'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'api' => [
            'driver' => 'passport',
            'provider' => 'users',
        ],
    ],

2.在routes/api.php中新增接口

Route::group(['prefix' => '/v1/'], function () {
    //登录
    Route::controller(\App\Http\Controllers\LoginController::class)->group(function(){
        Route::post('login/byWeappCode', 'byWeappCode');//根据小程序码登录
        Route::post('login/byWeappPhone', 'byWeappPhone');//根据微信小程序手机登录
        Route::post('login/byWeappUserProfile', 'byWeappUserProfile');//小程序码登录收集微信信息
    });
    //用户
    Route::controller(\App\Http\Controllers\UserController::class)->group(function(){
        Route::post('user/index', 'index')->middleware('auth:api');//列表
        Route::get('user/show/{id}', 'show')->middleware('auth:api');//根据id展示单个信息,传id
        Route::post('user/store', 'store')->middleware('auth:api');//创建,传需创建的字段
        Route::post('user/update', 'update')->middleware('auth:api');//更新,传需更新的字段
        Route::get('user/destroy/{id}', 'destroy')->middleware('auth:api');//删除,传id
    });
    //票务
    Route::controller(\App\Http\Controllers\TicketController::class)->group(function(){
        Route::post('ticket/index', 'index')->middleware('auth:api');//列表
        Route::get('ticket/show/{id}', 'show')->middleware('auth:api');//根据id展示单个信息,传id
        Route::post('ticket/store', 'store')->middleware('auth:api');//创建,传需创建的字段
        Route::post('ticket/update', 'update')->middleware('auth:api');//更新,传需更新的字段
        Route::get('ticket/destroy/{id}', 'destroy')->middleware('auth:api');//删除,传id
    });
    //支付订单
    Route::controller(\App\Http\Controllers\PaymentController::class)->group(function(){
        Route::post('payment/index', 'index')->middleware('auth:api');//列表
        Route::get('payment/show/{id}', 'show')->middleware('auth:api');//根据id展示单个信息,传id
        Route::post('payment/store', 'store')->middleware('auth:api');//创建,传需创建的字段
        Route::post('payment/update', 'update')->middleware('auth:api');//更新,传需更新的字段
        Route::get('payment/destroy/{id}', 'destroy')->middleware('auth:api');//删除,传id
    });
    //生成票务码
    Route::controller(\App\Http\Controllers\CodeController::class)->group(function(){
        Route::post('code/index', 'index')->middleware('auth:api');//列表
        Route::get('code/show/{id}', 'show')->middleware('auth:api');//根据id展示单个信息,传id
        Route::post('code/store', 'store')->middleware('auth:api');//创建,传需创建的字段
        Route::post('code/update', 'update')->middleware('auth:api');//更新,传需更新的字段
        Route::get('code/destroy/{id}', 'destroy')->middleware('auth:api');//删除,传id
    });
    //退票申请
    Route::controller(\App\Http\Controllers\RefundController::class)->group(function(){
        Route::post('refund/index', 'index')->middleware('auth:api');//列表
        Route::get('refund/show/{id}', 'show')->middleware('auth:api');//根据id展示单个信息,传id
        Route::post('refund/store', 'store')->middleware('auth:api');//创建,传需创建的字段
        Route::post('refund/update', 'update')->middleware('auth:api');//更新,传需更新的字段
        Route::get('refund/destroy/{id}', 'destroy')->middleware('auth:api');//删除,传id
    });
});

3.先做微信小程序登录
打开app/Models/User.php
将HasApiTokens用passport的
use Laravel\Passport\HasApiTokens;

  protected $fillable = [
        'id',
        'name',
        'email',
        'password',
        'real_name',
        'real_avatar',
        'id_card',
        'phone',
        'unionid',
        'weapp_openid',
        'weapp_avatar',
        'nick_name',
        'ali_id',
        'alipay_id',
        'address',
        'house',
        'location',
        'level',
        'is_admin',
        'is_hidden',
        'created_at',
        'updated_at',
    ];

打开app/Http/Controllers/LoginController.php
新建byWeappCode方法

public function byWeappCode(Request $request){
        $code = $request['code'];
        // 根据 code 获取微信 openid 和 session_key
        $miniProgram = \EasyWeChat::miniProgram();
        $data = $miniProgram->auth->session($code);
        if (isset($data['errcode'])) {
            return response()->json([
                'msg'=>$data['errmsg']
            ]);
        }
        $weappOpenid = $data['openid'];
        $weixinSessionKey = $data['session_key'];
        //找到 openid 对应的用户
        $user = User::where('weapp_openid', $weappOpenid)->first();

        if (!$user) {
            $user = User::create([
                'name' => md5($weappOpenid),
                'weapp_openid' => $weappOpenid,
                'password' => $weixinSessionKey . uniqid(),
            ]);
        }

        $attributes['updated_at'] = now();
        // 更新用户数据
        $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([
            'msg' => '登录成功',
            'access_token' => $token,
            'token_type' => "Bearer",
            'expires_in' => Carbon::now()->addDays(7),
            'data' => $user,
        ]);
    }

打开微信小程序开发工具新建个小程序输入我们申请的小程序ID,拿到一个code

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

postman测试下

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

成功,数据库中也有一个用户了

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

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 2

你这个小程序模板时小程序开发工具中的那个模板。 :smile: 正在用这套前端搞一个开源的商城。

2周前 评论
jinduo (楼主) 2周前

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