69 lines
2.0 KiB
PHP
69 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Store;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Dcat\Admin\Traits\HasDateTimeFormatter;
|
|
use EasyWeChat\Kernel\Http\StreamResponse;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class Desk extends Model
|
|
{
|
|
use HasDateTimeFormatter;
|
|
|
|
protected $table = 'store_desks';
|
|
|
|
protected $fillable = ['extra', 'name', 'remarks', 'status', 'store_id', 'wxcode'];
|
|
|
|
protected $attributes = [
|
|
'status' => 1
|
|
];
|
|
|
|
protected $casts = [
|
|
'extra' => 'json'
|
|
];
|
|
|
|
public function store()
|
|
{
|
|
return $this->belongsTo(Store::class, 'store_id');
|
|
}
|
|
|
|
public function generateWxCode()
|
|
{
|
|
$model = $this;
|
|
$scene = http_build_query([
|
|
'd' => $model->id,
|
|
'c' => data_get($model->extra, 'category_id'),
|
|
's' => $model->store_id
|
|
]);
|
|
|
|
if (config('app.env') == 'local') {
|
|
$url = 'https://ui-avatars.com/api/?name=pd&scene=' . urlencode($scene);
|
|
} else {
|
|
// 生成小程序码
|
|
$app = \EasyWeChat\Factory::miniProgram(config('wechat.mini_program.default'));
|
|
$response = $app->app_code->getUnlimit($scene, [
|
|
'page' => 'pages/welcome/index',
|
|
'check_path' => false,
|
|
// release: 正式版, develop: 开发版, trial: 体验版
|
|
'env_version' => config('app.debug') ? 'trial' : 'release',
|
|
'width' => 200,
|
|
]);
|
|
|
|
// 保存小程序码
|
|
if ($response instanceof StreamResponse) {
|
|
$disk = Storage::disk('public');
|
|
$filepath = 'store-desk';
|
|
$filename = $model->id . '.png';
|
|
$response->saveAs($disk->path($filepath), $filename);
|
|
$url = $disk->url($filepath . '/' . $filename);
|
|
} else {
|
|
logger('store-desk 小程序码生成失败', $response);
|
|
return $response;
|
|
}
|
|
}
|
|
$model->update(['wxcode' => $url]);
|
|
return true;
|
|
}
|
|
}
|