cxfoot/commands/Script1minController.php
2023-11-22 20:10:26 +08:00

182 lines
7.1 KiB
PHP

<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace app\commands;
use app\components\SiteHelper;
use app\models\Activity;
use app\models\ActivityUserLog;
use app\models\ActivityUserPrize;
use app\models\ActivityUserRate;
use app\models\BallCart;
use app\models\BallMark;
use app\models\Order;
use app\models\Report;
use app\models\UserCoupon;
use app\models\UserOauth;
use app\models\wechat\WechatApp;
use app\modules\api\components\Mqtt;
use Wechat\Wechat;
use yii\console\Controller;
use yii\console\ExitCode;
use \PhpMqtt\Client\MQTTClient;
use \PhpMqtt\Client\ConnectionSettings;
use app\models\Store;
/**
* This command echoes the first argument that you have entered.
*
* This command is provided as an example for you to learn how to create console commands.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Script1minController extends Controller
{
/**
* This command echoes what you have entered as the message.
* @param string $message the message to be echoed.
* @return int Exit code
*/
public function actionIndex()
{
$redis_name = "cxaibc:console:script1min_lock";
try {
$get = \Yii::$app->redis->setnx($redis_name,1);
if(empty($get)){
$ttl = \Yii::$app->redis->ttl($redis_name);
if($ttl === -1){
\Yii::$app->redis->expire($redis_name,60);
}
var_dump("1分钟脚本正在执行中");
exit();
}
\Yii::$app->redis->expire($redis_name,60*10);
// 执行体
$this->ActivityData();
// 结束执行
\Yii::$app->redis->del($redis_name);
}catch (\Exception $e){
\Yii::$app->redis->del($redis_name);
var_dump($e->getMessage());
}
echo "1分钟脚本";
exit();
}
/**
* 活动数据
*/
public function ActivityData(){
try{
$select = Report::find()->andWhere([
'step' => 2,
])->all();
foreach ($select as $key=>$val){
$json_de = json_decode($val['json'],true);
// 如果模型没有上传,则状态变更为未完成
if(empty($json_de['left_img']) || empty($json_de['right_img'])){
$val->step = 0;
$val->save();
continue;
}
if(!empty($json_de['updated_at']) && $json_de['updated_at']+5 > time()){
# 暂时不生成报告
continue;
}
// 判断模型文件名称
$left_img = explode('/',$json_de['left_img']);
$end = explode('.',end($left_img));
$two = explode('-',$end[0]);
$json_de['left_id'] = $two[1]??0;
$right_img = explode('/',$json_de['right_img']);
$end = explode('.',end($right_img));
$two = explode('-',$end[0]);
$json_de['right_id'] = $two[1]??0;
$val->json = json_encode($json_de,JSON_UNESCAPED_UNICODE);
$val->step = 3;
$val->save();
// 查找此用户绑定的openid
$user_oauth = UserOauth::findOne([
'user_id' => $val->user_id,
]);
if(empty($user_oauth)){
continue;
}
// 获取门店名称
$find_store = Store::findOne([
'id' => $val->store_id,
]);
// 推送客服消息
$wechat_app = WechatApp::findOne([
'cx_mch_id' => 0,
'is_delete' => 0
]);
if($wechat_app){
if (!is_dir(\Yii::$app->runtimePath . '/pem')) {
mkdir(\Yii::$app->runtimePath . '/pem');
file_put_contents(\Yii::$app->runtimePath . '/pem/index.html', '');
}
$cert_pem_file = null;
if ($wechat_app->cert_pem) {
$cert_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_app->cert_pem);
if (!file_exists($cert_pem_file))
file_put_contents($cert_pem_file, $wechat_app->cert_pem);
}
$key_pem_file = null;
if ($wechat_app->key_pem) {
$key_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_app->key_pem);
if (!file_exists($key_pem_file))
file_put_contents($key_pem_file, $wechat_app->key_pem);
}
$wechat_mp = new Wechat([
'appId' => $wechat_app->app_id,
'appSecret' => $wechat_app->app_secret,
'mchId' => $wechat_app->mch_id,
'apiKey' => $wechat_app->key,
'certPem' => $cert_pem_file,
'keyPem' => $key_pem_file,
]);
// 推送客服消息
$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$wechat_mp->getAccessToken();
$to_url = urlencode(SiteHelper::getCustomiseOptionByKey("siteDomain", "hump")."/site/todata");
$state = $val->id;
$res_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$wechat_app->app_id}&redirect_uri={$to_url}&response_type=code&scope=snsapi_userinfo&state={$state}#wechat_redirect";
try{
$to_user = $user_oauth->openid;
}catch (\Exception $e){
$to_user = $user_oauth->openid;
}
$arr = [
'touser' => $to_user,
'msgtype' => 'news',
'news' => [
'articles' => [
[
'title' => '您的报告已生成',
'description' => '请点击查看您的报告 - '.$find_store->name,
'url' => $res_url,
'picurl' => SiteHelper::getCustomiseOptionByKey("siteDomain", "hump").'/upload/0/1/upload/image/2023/1026/1698312821606159.jpg',
]
],
],
];
$wechat_mp->curl->post($url,json_encode($arr,JSON_UNESCAPED_UNICODE));
}
}
}catch (\Exception $e){
var_dump($e->getMessage());
var_dump($e->getLine());
}
}
}