Laravel8后台实现小程序兑换码功能,EasyWeChat\Factory生成小程序二维码,Ajax实现二维码显示预览

use EasyWeChat\Factory;
use Illuminate\Support\Facades\Cache;

/**
 * 获取小程序二维码
 */
public function getAppCode(Request $request)
{
    $path = $request->input('app_src');
    $config = [
        'app_id' => $this->appid,
        'secret' => $this->secret,
        // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
        'response_type' => 'array',
        'log' => [
            'level' => 'debug',
            'file' => __DIR__.'/wechat.log',
        ],
    ];
    $app = Factory::miniProgram($config);
    $app_url = $this->getBaseCodes($path,$app);
    return json_encode(['status'=>'success','msg'=>'获取成功','app_url'=>$app_url]);;
}

public function getBaseCodes($path,$codes, $base64 = true)
{
    $minutes = 30;
    if (is_string($codes)) {
        $md5 = md5($codes . $path . 'app');
        $codes = $this->get($codes);
    }
    else {
        $md5 = md5($path . 'app');
    }
    if (!$base64) {
        return $code->app_code->get($path);
    }
    $baseCodes = Cache::remember($md5, $minutes, function () use ($codes, $path) {
        $code = $codes->app_code->get($path);
        return base64_encode($code);
    });
    return $baseCodes;
}

你可能感兴趣的:(PHP,larrael,小程序,小程序,ajax,前端)