Files
cunkebao_v3/Server/application/cunkebao/controller/plan/PosterWeChatMiniProgram.php
2025-07-10 17:53:42 +08:00

213 lines
7.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\cunkebao\controller\plan;
use think\Controller;
use think\Request;
use EasyWeChat\Factory;
// use EasyWeChat\Kernel\Exceptions\DecryptException;
use EasyWeChat\Kernel\Http\StreamResponse;
use think\Db;
use app\common\util\AliyunOSS;
class PosterWeChatMiniProgram extends Controller
{
public function index()
{
return 'Hello, World!';
}
const MINI_PROGRAM_CONFIG = [
'app_id' => 'wx789850448e26c91d',
'secret' => 'd18f75b3a3623cb40da05648b08365a1',
'response_type' => 'array'
];
// 生成小程序码,存客宝-操盘手调用
public function generateMiniProgramCodeWithScene($taskId = '') {
if(empty($taskId)) {
$taskId = request()->param('id');
}
$app = Factory::miniProgram(self::MINI_PROGRAM_CONFIG);
// scene参数长度限制为32位
// $scene = 'taskId=' . $taskId;
$scene = 'id=' . $taskId;
// 调用接口生成小程序码
$response = $app->app_code->getUnlimit($scene, [
'page' => 'pages/poster/index', // 必须是已经发布的小程序页面
'width' => 430, // 二维码的宽度默认430
// 'auto_color' => false, // 自动配置线条颜色
// 'line_color' => ['r' => 0, 'g' => 0, 'b' => 0], // 颜色设置
// 'is_hyaline' => false, // 是否需要透明底色
]);
// 保存小程序码到文件
if ($response instanceof StreamResponse) {
$savePath = ROOT_PATH . '/runtime/img';
// 确保目录存在
if (!is_dir($savePath)) {
mkdir($savePath, 0755, true);
}
$filename = $response->saveAs($savePath, 'appcode_'.$taskId.'.png');
// 上传到OSS
if ($filename) {
$ossUrl = $this->uploadToOSS($filename);
// 删除本地文件
if (file_exists($filename)) {
unlink($filename);
}
// 如果OSS上传成功返回OSS URL否则返回本地文件路径
return $ossUrl ?: $filename;
}
}
// return false;
return null;
}
/**
* 上传文件到OSS
* @param string $filePath 本地文件路径
* @return string|false 成功返回OSS URL失败返回false
*/
private function uploadToOSS($filePath)
{
try {
// 生成OSS对象名称
$objectName = AliyunOSS::generateObjectName('appcode_' . time() . '.png');
// 上传到OSS
$result = AliyunOSS::uploadFile($filePath, $objectName);
if ($result['success']) {
// 返回完整的OSS URL
return AliyunOSS::ossUrl . '/' . $objectName;
} else {
// 记录错误日志
\think\facade\Log::error('OSS上传失败: ' . $result['error'] . ' 文件: ' . $filePath);
return false;
}
} catch (\Exception $e) {
// 记录错误日志
\think\facade\Log::error('OSS上传异常: ' . $e->getMessage() . ' 文件: ' . $filePath);
return false;
}
}
// getPhoneNumber
public function getPhoneNumber() {
$taskId = request()->param('id');
$code = request()->param('code');
// code 不能为空
if (!$code) {
return json([
'code' => 400,
'message' => 'code不能为空'
]);
}
$task = Db::name('customer_acquisition_task')->where('id', $taskId)->find();
if (!$task) {
return json([
'code' => 400,
'message' => '任务不存在'
]);
}
$app = Factory::miniProgram(self::MINI_PROGRAM_CONFIG);
$result = $app->phone_number->getUserPhoneNumber($code);
if ($result['errcode'] == 0 && isset($result['phone_info']['phoneNumber'])) {
// TODO 拿到手机号之后的后续操作:
// 1. 先写入 ck_traffic_pool 表 identifier mobile 都是 用 phone字段的值
$trafficPool = Db::name('traffic_pool')->where('identifier', $result['phone_info']['phoneNumber'])->find();
if (!$trafficPool) {
Db::name('traffic_pool')->insert([
'identifier' => $result['phone_info']['phoneNumber'],
'mobile' => $result['phone_info']['phoneNumber']
]);
}
// 2. 写入 ck_task_customer: 以 task_id ~~identifier~~ phone 为条件如果存在则忽略使用类似laravel的firstOrcreate但我不知道thinkphp5.1里的写法)
// $taskCustomer = Db::name('task_customer')->where('task_id', $taskId)->where('identifier', $result['phone_info']['phoneNumber'])->find();
$taskCustomer = Db::name('task_customer')->where('task_id', $taskId)->where('phone', $result['phone_info']['phoneNumber'])->find();
if (!$taskCustomer) {
Db::name('task_customer')->insert([
'task_id' => $taskId,
// 'identifier' => $result['phone_info']['phoneNumber'],
'phone' => $result['phone_info']['phoneNumber']
]);
}
// return $result['phone_info']['phoneNumber'];
return json([
'code' => 200,
'message' => '获取手机号成功',
'data' => $result['phone_info']['phoneNumber']
]);
} else {
// return null;
return json([
'code' => 400,
'message' => '获取手机号失败: ' . $result['errmsg'] ?? '未知错误'
]);
}
// return $result;
}
// todo 获取海报获客任务的任务/海报数据 -- 表还没设计好,不急 ck_customer_acquisition_task
public function getPosterTaskData() {
$id = request()->param('id');
$task = Db::name('customer_acquisition_task')->where(['id' => $id,'deleteTime' => 0])->find();
if (!$task) {
return json([
'code' => 400,
'message' => '任务不存在'
]);
}
if($task['status'] == 0) {
return json([
'code' => 400,
'message' => '任务已结束'
]);
}
$sceneConf = json_decode($task['sceneConf'], true);
if(isset($sceneConf['posters'][0]['preview'])) {
$posterUrl = $sceneConf['posters'][0]['preview'];
} else {
$posterUrl = '';
}
$data = [
'id' => $task['id'],
'name' => $task['name'],
'poster' => ['sUrl' => $posterUrl],
'sTip' => '啦啦啦啦',
];
// todo 只需 返回 poster_url success_tip
return json([
'code' => 200,
'message' => '获取海报获客任务数据成功',
'data' => $data
]);
}
}