xielin 5 years ago
parent
commit
96f12674d7

+ 22 - 0
app/Behavior.php

@@ -0,0 +1,22 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: wangzhiqiang
+ * Date: 2019/4/24
+ * Time: 15:21
+ */
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\SoftDeletes;
+
+class Behavior extends Model
+{
+    //
+    use SoftDeletes;
+    protected $table = 'behavior';
+    protected $guarded = [];
+
+
+}

+ 22 - 0
app/BindPhoneRecord.php

@@ -0,0 +1,22 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: wangzhiqiang
+ * Date: 2019/4/24
+ * Time: 15:21
+ */
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\SoftDeletes;
+
+class BindPhoneRecord extends Model
+{
+    //
+    use SoftDeletes;
+    protected $table = 'bind_phone_record';
+    protected $guarded = [];
+
+
+}

+ 22 - 0
app/BindWxRecord.php

@@ -0,0 +1,22 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: wangzhiqiang
+ * Date: 2019/4/24
+ * Time: 15:21
+ */
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\SoftDeletes;
+
+class BindWxRecord extends Model
+{
+    //
+    use SoftDeletes;
+    protected $table = 'bind_wx_record';
+    protected $guarded = [];
+
+
+}

+ 22 - 0
app/CommentAccountRecord.php

@@ -0,0 +1,22 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: wangzhiqiang
+ * Date: 2019/4/24
+ * Time: 15:21
+ */
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\SoftDeletes;
+
+class CommentAccountRecord extends Model
+{
+    //
+    use SoftDeletes;
+    protected $table = 'comment_account_record';
+    protected $guarded = [];
+
+
+}

+ 103 - 0
app/Console/Commands/VirusRule.php

@@ -0,0 +1,103 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019-05-13
+ * Time: 10:52
+ */
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Log;
+use PhpAmqpLib\Connection\AMQPStreamConnection;
+
+class VirusRule extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'virus:rule';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '订阅VIRUS事件通知';
+
+    protected $connection;
+    protected $channel;
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    public function getConnection()
+    {
+        $conn = false;
+        if ($this->connection) {
+            return $this->connection;
+        }
+        for ($i = 0; $i < 3; $i++) {
+            $connection = $this->createConn();
+            if ($connection) {
+                $conn = $connection;
+                break;
+            }
+            Log::info("create amqp conn retry=" . $i);
+        }
+        return $conn;
+    }
+
+    public function createConn()
+    {
+        try {
+            $connection = new AMQPStreamConnection(env('MQ_HOST'), env('MQ_PORT'), env('MQ_USERNAME'), env('MQ_PWD'), env('MQ_VHOST'));
+        } catch (\Exception $exception) {
+            Log::info("AMQP connection Error" . $exception->getMessage());
+            $connection = false;
+        }
+        return $connection;
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        //监听virus队列
+        $this->queue = 'virus_';
+        $this->connection = $this->getConnection();
+        $this->channel = $this->connection->channel();
+        $this->channel->exchange_declare($this->queue, 'fanout', false, true, false);
+
+        $queue_name = $this->queue.'-community-service';
+        $this->channel->queue_declare($queue_name, false, true, false, false);
+        $this->channel->queue_bind($queue_name, $this->queue);
+        $callback = function ($msg) {
+            $data = \GuzzleHttp\json_decode($msg->body,true);
+            $this->line(date('Y-m-d H:i:s')."收到数据---------$msg->body-----------");
+            $result = $this->productCartRepository->deleteCarts($data);
+            $this->line(date('Y-m-d H:i:s')."返回---------$result-----------");
+            $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
+        };
+        $this->channel->basic_qos(null, 1, null);
+        $this->channel->basic_consume($queue_name, '', false, false, false, false, $callback);
+        while (count($this->channel->callbacks)) {
+            $this->channel->wait();
+        }
+        $this->channel->close();
+        $this->connection->close();
+    }
+}

+ 22 - 0
app/GeneralLedgerRecord.php

@@ -0,0 +1,22 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: wangzhiqiang
+ * Date: 2019/4/24
+ * Time: 15:21
+ */
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\SoftDeletes;
+
+class GeneralLedgerRecord extends Model
+{
+    //
+    use SoftDeletes;
+    protected $table = 'general_ledger_record';
+    protected $guarded = [];
+
+
+}

+ 22 - 0
app/RegisteredAccountsRecord.php

@@ -0,0 +1,22 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: wangzhiqiang
+ * Date: 2019/4/24
+ * Time: 15:21
+ */
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\SoftDeletes;
+
+class RegisteredAccountsRecord extends Model
+{
+    //
+    use SoftDeletes;
+    protected $table = 'registered_accounts_record';
+    protected $guarded = [];
+
+
+}

+ 22 - 0
app/ReleaseRecord.php

@@ -0,0 +1,22 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: wangzhiqiang
+ * Date: 2019/4/24
+ * Time: 15:21
+ */
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\SoftDeletes;
+
+class ReleaseRecord extends Model
+{
+    //
+    use SoftDeletes;
+    protected $table = 'release_record';
+    protected $guarded = [];
+
+
+}

+ 235 - 0
app/Repositories/BehaviorRecordRepositories.php

@@ -0,0 +1,235 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: edz
+ * Date: 2019-06-10
+ * Time: 17:53
+ */
+
+namespace App\Repositories;
+
+
+use App\Models\BindPhoneRecord;
+use App\Models\BindWxRecord;
+use App\Models\CommentAccountRecord;
+use App\Models\GeneralLedgerRecord;
+use App\Models\RegisteredAccountsRecord;
+use App\Models\ReleaseRecord;
+use Illuminate\Database\QueryException;
+use Illuminate\Support\Facades\Log;
+
+class BehaviorRecordRepositories
+{
+    public function __construct(RegisteredAccountsRecord $registeredAccountsRecord,
+                                GeneralLedgerRecord $generalLedgerRecord,
+                                ReleaseRecord $releaseRecord,
+                                CommentAccountRecord $commentAccountRecord,
+                                BindPhoneRecord $bindPhoneRecord,
+                                BindWxRecord $bindWxRecord)
+    {
+        $this->registeredAccountsRecord = $registeredAccountsRecord;
+        $this->generalLedgerRecord = $generalLedgerRecord;
+        $this->releaseRecord = $releaseRecord;
+        $this->commentAccountRecord = $commentAccountRecord;
+        $this->bindPhoneRecord = $bindPhoneRecord;
+        $this->bindWxRecord = $bindWxRecord;
+    }
+
+    /**
+     * 根据行为标识获取行为记录表
+     * @param $behavior
+     */
+    public function getBehaviorTable($behavior)
+    {
+        $tables = [
+            'register' => 'registered_accounts_record',
+            'publish' => 'release_record',
+            'read' => 'general_ledger_record',
+            'like' => 'general_ledger_record',
+            'unlike' => 'general_ledger_record',
+            'forward' => 'general_ledger_record',
+            'collect' => 'general_ledger_record',
+            'comment' => 'comment_account_record',
+            'bind_phone' => 'bind_phone_record',
+            'bind_wechat' => 'bind_wx_record'
+        ];
+        return isset($tables[$behavior]) ? $tables[$behavior] : false;
+    }
+
+    /**
+     * 记录账本
+     * @param $info
+     * @return bool
+     */
+    public function addRecord($info)
+    {
+        Log::debug('add-record-request:' . json_encode($info));
+        $behavior = $info['behavior_flag'];
+        $behaviorTable = $this->getBehaviorTable($behavior);
+        Log::debug('add-record-behavior-table:' . $behaviorTable);
+        if ($behaviorTable) {
+            if ($behaviorTable == 'registered_accounts_record') {
+                return $this->addRegisterRecord($info);
+            } elseif ($behaviorTable == 'release_record') {
+                return $this->addReleaseRecord($info);
+            } elseif ($behaviorTable == 'general_ledger_record') {
+                return $this->addGeneralRecord($info);
+            } elseif ($behaviorTable == 'comment_account_record') {
+                return $this->addCommentRecord($info);
+            } elseif ($behaviorTable == 'bind_phone_record') {
+                return $this->addBindPhoneRecord($info);
+            } elseif ($behaviorTable == 'bind_wx_record') {
+                return $this->addBindWxRecord($info);
+            }
+        }
+        return false;
+    }
+
+    /**
+     * 记录账本-用户注册
+     * @param $register
+     * @return bool
+     */
+    public function addRegisterRecord($register)
+    {
+        try {
+            $data['virus_behavior_id'] = $register['virus_behavior_id'];
+            $data['uid'] = $register['uid'];
+            $data['trigger_time'] = $register['trigger_time'];
+            $data['physical_exertion'] = $register['physical_exertion'];
+            $data['trigger_type'] = $register['trigger_type'];
+            $data['generation_type'] = $register['generation_type'];
+            $data['absolute_progression'] = $register['absolute_progression'];
+            $data['superior_uid'] = $register['superior_uid'];
+            $data['release_status'] = $register['release_status'];
+            $data['generation_quantity'] = $register['generation_quantity'];
+            $data['quantity_issued'] = $register['quantity_issued'];
+            return $this->registeredAccountsRecord->create($data);
+        } catch (QueryException $exception) {
+            return false;
+        }
+    }
+
+    /**
+     * 记录账本-内容发布
+     * @param $release
+     * @return bool
+     */
+    public function addReleaseRecord($release)
+    {
+        try {
+            $data['virus_behavior_id'] = $release['virus_behavior_id'];
+            $data['uid'] = $release['uid'];
+            $data['trigger_time'] = $release['trigger_time'];
+            $data['related_content_id'] = $release['related_content_id'];
+            $data['physical_exertion'] = $release['physical_exertion'];
+            $data['trigger_type'] = $release['trigger_type'];
+            $data['generation_type'] = $release['generation_type'];
+            $data['release_status'] = $release['release_status'];
+            $data['generation_quantity'] = $release['generation_quantity'];
+            $data['quantity_issued'] = $release['quantity_issued'];
+            return $this->releaseRecord->create($data);
+        } catch (QueryException $exception) {
+            return false;
+        }
+    }
+
+    /**
+     * 记录账本-阅读,转发,点赞,收藏,不喜欢
+     * @param $general
+     * @return bool
+     */
+    public function addGeneralRecord($general)
+    {
+        try {
+            $data['virus_behavior_id'] = $general['virus_behavior_id'];
+            $data['uid'] = $general['uid'];
+            $data['trigger_time'] = $general['trigger_time'];
+            $data['related_content_id'] = $general['related_content_id'];
+            $data['content_author_id'] = $general['content_author_id'];
+            $data['physical_exertion'] = $general['physical_exertion'];
+            $data['trigger_type'] = $general['trigger_type'];
+            $data['generation_type'] = $general['generation_type'];
+            $data['release_status'] = $general['release_status'];
+            $data['generation_quantity'] = $general['generation_quantity'];
+            $data['quantity_issued'] = $general['quantity_issued'];
+            return $this->generalLedgerRecord->create($data);
+        } catch (QueryException $exception) {
+            return false;
+        }
+    }
+
+    /**
+     * 记录账本-评论
+     * @param $comment
+     * @return bool
+     */
+    public function addCommentRecord($comment)
+    {
+        try {
+            $data['virus_behavior_id'] = $comment['virus_behavior_id'];
+            $data['uid'] = $comment['uid'];
+            $data['upper_trigger_time'] = $comment['upper_trigger_time'];
+            $data['related_content_id'] = $comment['related_content_id'];
+            $data['content_author_id'] = $comment['content_author_id'];
+            $data['superior_commentator_id'] = $comment['superior_commentator_id'];
+            $data['physical_exertion'] = $comment['physical_exertion'];
+            $data['trigger_type'] = $comment['trigger_type'];
+            $data['generation_type'] = $comment['generation_type'];
+            $data['release_status'] = $comment['release_status'];
+            $data['generation_quantity'] = $comment['generation_quantity'];
+            $data['quantity_issued'] = $comment['quantity_issued'];
+            return $this->generalLedgerRecord->create($data);
+        } catch (QueryException $exception) {
+            return false;
+        }
+    }
+
+    /**
+     * 记录账本-绑定手机
+     * @param $bindphone
+     * @return bool
+     */
+    public function addBindPhoneRecord($bindphone)
+    {
+        try {
+            $data['virus_behavior_id'] = $bindphone['virus_behavior_id'];
+            $data['uid'] = $bindphone['uid'];
+            $data['trigger_time'] = $bindphone['trigger_time'];
+            $data['phone'] = $bindphone['phone'];
+            $data['physical_exertion'] = $bindphone['physical_exertion'];
+            $data['trigger_type'] = $bindphone['trigger_type'];
+            $data['generation_type'] = $bindphone['generation_type'];
+            $data['release_status'] = $bindphone['release_status'];
+            $data['generation_quantity'] = $bindphone['generation_quantity'];
+            $data['quantity_issued'] = $bindphone['quantity_issued'];
+            return $this->bindPhoneRecord->create($data);
+        } catch (QueryException $exception) {
+            return false;
+        }
+    }
+
+    /**
+     * 记录账本-绑定微信
+     * @param $bindwx
+     * @return bool
+     */
+    public function addBindWxRecord($bindwx)
+    {
+        try {
+            $data['virus_behavior_id'] = $bindwx['virus_behavior_id'];
+            $data['uid'] = $bindwx['uid'];
+            $data['trigger_time'] = $bindwx['trigger_time'];
+            $data['weixin'] = $bindwx['weixin'];
+            $data['physical_exertion'] = $bindwx['physical_exertion'];
+            $data['trigger_type'] = $bindwx['trigger_type'];
+            $data['generation_type'] = $bindwx['generation_type'];
+            $data['release_status'] = $bindwx['release_status'];
+            $data['generation_quantity'] = $bindwx['generation_quantity'];
+            $data['quantity_issued'] = $bindwx['quantity_issued'];
+            return $this->bindWxRecord->create($data);
+        } catch (QueryException $exception) {
+            return false;
+        }
+    }
+}

+ 61 - 0
app/Repositories/BehaviorRepositories.php

@@ -0,0 +1,61 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: edz
+ * Date: 2019-06-10
+ * Time: 17:53
+ */
+
+namespace App\Repositories;
+
+
+use App\Models\Behavior;
+use Illuminate\Support\Facades\Log;
+
+class BehaviorRepositories
+{
+    public function __construct(MemberRepository $memberRepository)
+    {
+        $this->memberRepository = $memberRepository;
+    }
+
+
+    /**
+     * 消费virus通知
+     * @param array $request
+     */
+    public function consumeNotify($request)
+    {
+        $identify = $request['behavior_flag'];
+        $behavior = $this->getBehavior($identify);
+        $userInfo = $this->memberRepository->getUserInfo();
+        if (empty($userInfo) || empty($behavior)) {
+            return true;
+        }
+
+        $behaviorStatus = 0;    //触发类型
+        if ($userInfo && $userInfo['strength'] > 0) {
+            $behaviorStatus = 1;
+        }
+        $strength = $behaviorStatus ? $behavior['physical_strength'] : 0;
+        $beans = $behaviorStatus ? $behavior['rainbow_beans'] : 0;
+    }
+
+
+    /**
+     * 获取行为绑定规则
+     * @param $info
+     * @return array
+     */
+    public function getBehavior($identify)
+    {
+        $info = Behavior::where([['is_open', '=', 1], ['behavior_identification', '=', $identify]])->first();
+        Log::debug('behavior:' . json_encode($info));
+        if ($info) {
+            return $info;
+        }
+        return [];
+    }
+
+
+}

+ 55 - 0
app/Repositories/MemberRepository.php

@@ -0,0 +1,55 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019-05-05
+ * Time: 14:18
+ */
+
+namespace App\Repositories;
+
+
+use App\Service\RabbitMqUtil;
+use Tymon\JWTAuth\Facades\JWTAuth;
+
+class MemberRepository
+{
+    public function getUserInfo()
+    {
+        try {
+            $sign = generateSign([], config('customer.app_secret'));
+            $url = config("customer.user_service_url") . '/userInfo';
+            $array = [
+                'json' => ['sign' => $sign], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
+            ];
+            return http($url, $array);
+        } catch (\Exception $e) {
+            return [];
+        }
+    }
+
+    /**
+     * 修改用户体力--异步
+     * @param $value +5   -10
+     * @return bool
+     */
+    public function updateStrength($uid, $value, $remark)
+    {
+        $rabbitmq = RabbitMqUtil::getInstance();
+        $queueName = "user_strength_queue";
+        $rabbitmq->push($queueName, ['uid' => $uid, 'value' => $value, 'remark' => $remark]);
+    }
+
+    /**
+     * 修改用户彩虹豆--异步
+     * @param $value +5   -10
+     * @param $remark 备注
+     * @return bool
+     */
+    public function updateRainbownBean($uid, $value, $remark)
+    {
+        $rabbitmq = RabbitMqUtil::getInstance();
+        $queueName = "user_rainbownbean_queue";
+        $rabbitmq->push($queueName, ['uid' => $uid, 'value' => $value, 'remark' => $remark]);
+    }
+}

+ 15 - 4
app/Service/RabbitMqUtil.php

@@ -14,9 +14,9 @@ use PhpAmqpLib\Message\AMQPMessage;
 
 class RabbitMqUtil
 {
-    protected $connection;
-    protected $queue;
-    protected $channel;
+    private static $instance = null;
+    protected $connection = null;
+    protected $channel = null;
 
     public function __construct()
     {
@@ -24,6 +24,17 @@ class RabbitMqUtil
         $this->channel = $this->connection->channel();
     }
 
+    /**
+     * 单例实例化入口;
+     */
+    public static function getInstance()
+    {
+        if (!self::$instance instanceof self) {
+            self::$instance = new self();
+        }
+        return self::$instance;
+    }
+
     public function getConnection()
     {
         $conn = false;
@@ -93,8 +104,8 @@ class RabbitMqUtil
 
     public function __destruct()
     {
-        // TODO: Implement __destruct() method.
         $this->channel->close();
         $this->connection->close();
+        self::$instance = null;
     }
 }

+ 1 - 4
composer.json

@@ -10,17 +10,14 @@
         "php-amqplib/php-amqplib": "^2.9",
         "dingo/api": "^2.2",
         "guzzlehttp/guzzle": "^6.3",
-        "hhxsv5/laravel-s": "~3.4.0",
         "illuminate/redis": "^5.8",
         "laravel/lumen-framework": "5.8.*",
         "league/fractal": "^0.17.0",
         "predis/predis": "^1.1",
         "tymon/jwt-auth": "1.0.0-rc.4.1",
         "vlucas/phpdotenv": "^3.3",
-        "ramsey/uuid": "^3.8",
         "moontoast/math": "^1.1",
-        "multilinguals/apollo-client": "^0.1.2",
-        "yansongda/laravel-pay": "^2.0"
+        "multilinguals/apollo-client": "^0.1.2"
     },
     "require-dev": {
         "fzaninotto/faker": "^1.4",