生成核销二维码

This commit is contained in:
Yao 2024-10-03 17:19:54 +08:00
parent feaa1fdb22
commit 726fd3b9da
3 changed files with 68 additions and 25 deletions

@ -11,9 +11,13 @@ class Verify extends Api
{
public $like_not_need_login = ['getVerifyCode'];
/**
* @return \think\response\Json
* 获取核销码
*/
public function getVerifyCode(){
$get = $this->request->get();
$user_id = $this->user_id;
(new VerifyValidate())->goCheck('getVerifyCode', $get);
$data = VerifyLogic::getVerifyCode($get);
if (false === $data) {
@ -23,5 +27,21 @@ class Verify extends Api
}
/**
* @return void
* 扫码核销
*/
public function dispose(){
$post = $this->request->post();
$user_id = $this->user_id;
$post['user_id'] = $user_id;
(new VerifyValidate())->goCheck('dispose', $post);
$data = VerifyLogic::dispose($post);
if (false === $data) {
return JsonServer::error(VerifyLogic::getError());
}
return JsonServer::success('核销成功!', $data);
}
}

@ -4,9 +4,11 @@ namespace app\api\logic;
use app\common\basics\Logic;
use app\common\model\localstore\LocalGoodsOrder;
use app\common\model\localstore\LocalStoreVerify;
use app\common\server\JsonServer;
use app\common\server\WeChatServer;
use EasyWeChat\Factory;
use Endroid\QrCode\QrCode;
use think\Exception;
use think\facade\Db;
use think\facade\Request;
@ -20,7 +22,6 @@ class VerifyLogic extends Logic
*/
public static function getVerifyCode($get)
{
$url = $get['url'];
$lsg_order_id = $get['lsg_order_id'];
try {
@ -37,34 +38,57 @@ class VerifyLogic extends Logic
}
$scene = 'lsg_order_id='.$order['lsg_order_id'];
$save_dir = ROOT_PATH .'/uploads/verifycode/';
!file_exists($save_dir) && mkdir($save_dir,0777,true);
$a = time().$order['lsg_order_id'];
$qr_src = $a . '.jpg';
$config = WeChatServer::getMnpConfig();
$app = Factory::miniProgram($config);
$response = $app->app_code->getUnlimit($scene, [
'page' => $url,
// 'env_version' => 'trial',
'env_version' => 'develop'
]);
if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
$response->saveAs($save_dir, $a);
$us = Request::instance()->domain();
$d['set_image'] = $data['url'] = $us.'/uploads/verifycode/'.$qr_src;
$set = LocalGoodsOrder::where('lsg_order_id',$lsg_order_id)->update([
$path = $save_dir.$a.$qr_src;
$qrCode = new QrCode();
$qrCode->setText($scene);
$qrCode->setSize(1000);
$qrCode->setWriterByName('png');
$qrCode->writeFile($path);
$us = Request::instance()->domain();
$d['set_image'] = $data['url'] = $us.'/uploads/verifycode/'.$qr_src;
$set = LocalGoodsOrder::where('lsg_order_id',$lsg_order_id)->update([
'verify_code_image' =>$d['set_image']
]);
if($set){
return $data;
}else{
throw new Exception('获取失败,订单错误');
}
return $data;
}catch (Exception $e) {
self::$error = $e->getMessage();
return false;
}
}
public static function dispose($post){
$lsg_order_id = $post['lsg_order_id'];
$user_id = $post['user_id'];
try {
$order = LocalGoodsOrder::where([
'lsg_order_id'=>$lsg_order_id,
'state'=>1
])->find();
if(!$order){
throw new Exception('获取失败,订单错误');
}
if(isset($response['errcode']) && 41030 === $response['errcode']){
throw new Exception('商城小程序未上线或商城小程序页面发布');
$lstore_id = $order['lstore_id'];
$StoreVerify = LocalStoreVerify::where([
'user_id'=>$user_id,
'lstore_id'=>$lstore_id,
'is_delete'=>0
])->find();
if(!$StoreVerify){
throw new Exception('核销失败,当前用户无权限');
}
return $response;
LocalGoodsOrder::where('lsg_order_id',$lsg_order_id)->update([
'verify_status'=>1
]);
return [];
}catch (Exception $e) {
self::$error = $e->getMessage();
return false;

@ -7,17 +7,16 @@ use app\common\basics\Validate;
class VerifyValidate extends Validate
{
protected $rule = [
'url' => 'require',
'lsg_order_id' => 'require',
];
protected $message = [
'url' => '缺少跳转路径',
'lsg_order_id' => '缺少订单id',
];
protected $scene = [
'getVerifyCode' => ['url','lsg_order_id'],
'dispose' => ['lsg_order_id'],
];
}