CREMB Min Thinkphp6 热点统计功能

具体操作可以参考另外一个blog: https://blog.csdn.net/ZHOUYONGXYZ/article/details/118516421

1. 路由

// 统计商品访问热度
    Route::post('product/hit/:product_id', 'v1.redeemcode.RedeemCodeController/product_hit')->name('productHit');
// 统计商品访问热度
    Route::get('product/hit/query', 'v1.redeemcode.RedeemCodeController/product_hit_query')->name('productHitQuery');

2. Controller

<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace app\api\controller\v1\redeemcode;
 
use app\Request;
use app\services\redeemcode\RedeemCodeServices;
use app\services\redeemcode\ProductHitServices;
use crmeb\services\CacheService;
 
 
/**
 * 用户类
 * Class RedeemCodeController
 * @package app\api\controller\store
 */
class RedeemCodeController
{
    protected $services = NUll;
 
    /**
     * UserController constructor.
     * @param RedeemCodeServices $services
     */
    public function __construct(RedeemCodeServices $services)
    {
        $this->services = $services;
    }
 
 
    public function get_status(Request $request, $product_id)
    {
        $valid_ids = array(7,8,9,10,11,12,14,16);
        $data = array("valid" => 0);
        if(in_array($product_id, $valid_ids)) {
            $data["valid"] = 1;
        }
        return app('json')->success($data);
    }
 
    public function verify_redeemcode(Request $request, $product_id)
    {
        $data = $request->postMore([
            ['code', '']
        ]);
 
        $result = $this->services->getRedeemCodeList($product_id, $data['code']);
        //echo $result;
        
        return app('json')->success($result);
        
    }
 
    public function consume_redeemcode(Request $request, $product_id)
    {
        $data = $request->postMore([
            ['code', '']
        ]);
 
        $result = $this->services->consumeRedeemCode($product_id, $data['code']);
        $data = array("status" => $result);
        //echo $result;
        
        return app('json')->success($data);
        
    }

    public function product_hit(Request $request, ProductHitServices $services, $product_id)
    {
        $result = $services->productHit($product_id);
        return app('json')->success(['result' => $result]);
    }

    public function product_hit_query(Request $request, ProductHitServices $services)
    {
        return app('json')->success($services->getProductList());
    }
 
 
}

3. Services

<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
 
//declare (strict_types=1);
 
namespace app\services\redeemcode;
 
use app\services\BaseServices;
use app\dao\redeemcode\ProductHitDao;
use crmeb\exceptions\AdminException;
use crmeb\services\CacheService;
use crmeb\services\FormBuilder as Form;
use crmeb\services\FormBuilder;
use crmeb\services\WechatService;
use crmeb\utils\Queue;
use think\Exception;
use think\exception\ValidateException;
use think\facade\Route as Url;
 
/**
 *
 * Class UserServices
 * @package app\services\user
 * @method array getUserInfoArray(array $where, string $field, string $key) 根据条件查询对应的用户信息以数组形式返回
 * @method update($id, array $data, ?string $key = null) 修改数据
 * @method get($id, ?array $field = [], ?array $with = []) 获取一条数据
 * @method count(array $where) 获取指定条件下的数量
 * @method value(array $where, string $field) 获取指定的键值
 * @method bcInc($key, string $incField, string $inc, string $keyField = null, int $acc = 2) 高精度加法
 * @method bcDec($key, string $incField, string $inc, string $keyField = null, int $acc = 2) 高精度减法
 * @method getTrendData($time, $type, $timeType)
 */
class ProductHitServices extends BaseServices
{
 
    /**
     * UserServices constructor.
     * @param ProductHitDao $dao
     */
    public function __construct(ProductHitDao $dao)
    {
        $this->dao = $dao;
    }
 
 
    public function getProductList()
    {
        return $this->dao->getProductList();
    }

    public function productHit($product_id) {
        return $this->dao->productHit($product_id);
    }
}

4. Dao

<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
declare (strict_types=1);
 
namespace app\dao\redeemcode;
 
use app\dao\BaseDao;
use app\model\redeemcode\ProductHit;
 
/**
 * 用户
 * Class UserDao
 * @package app\dao\user
 */
class ProductHitDao extends BaseDao
{
 
    protected function setModel(): string
    {
        return ProductHit::class;
    }
 
    public function getProductList(): array
    {
        return $this->getModel()->select()->toArray();
    }

    public function productHit($product_id)
    {
        $product = $this->getModel()->where('product_id', $product_id)->findOrEmpty()->toArray();
        if(empty($product)) {
            return $this->getModel()->save(['product_id' => $product_id, 'hit' => 1]);
        } else {
            return $this->getModel()->where('product_id', $product_id)->inc('hit', 1)->update();
        }
    }
 
}

5. Model

<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
 
namespace app\model\redeemcode;
 
use crmeb\basic\BaseModel;
use think\Model;
 
/**
 * Class User
 * @package app\model\user
 */
class ProductHit extends BaseModel
{
    protected $table = 'sf_hit_count';
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值