1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
<?php


namespace app\api\controller;

use app\api\model\Deposit;
use app\api\model\DepositOrder;
use app\api\model\Industry;
use app\api\model\Message;
use app\api\model\StoreComment;
use app\api\model\StoreInform;
use app\api\model\StoreInformGood;
use app\api\model\StoreInformLog;
use app\api\model\StoreInformView;
use app\api\model\UserHouse;
use app\api\model\UserMoneyLog;
use app\api\validate\HotValidate;
use app\common\controller\Api;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\Request;

/**
 * 周边热点接口
 */
class Hot extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    protected $store_model;
    protected $store_inform_model;
    protected $store_inform_good_model;
    protected $user_house_model;
    protected $industry_model;
    protected $favorite_model;
    protected $comment_model;
    protected $good_model;
    protected $user_id;

    public function __construct(Request $request,\app\api\model\Store $store,Industry $industry,StoreInform $store_inform,UserHouse $user_house,StoreInformGood $store_inform_good)
    {
        parent::__construct($request);
        $this->industry_model = $industry;
        $this->store_model = $store;
        $this->store_inform_model = $store_inform;
        $this->store_inform_good_model = $store_inform_good;
        $this->user_house_model = $user_house;
        $this->user_id = $this->auth->id;
    }

    /**
     * 商圈信息列表
     * @ApiWeigh    (80)
     *
     * @ApiTitle    (商圈信息列表)
     * @ApiSummary  (商圈信息列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/hot/store_list)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="store_id", type="integer", required=false, description="店铺id")
     * @ApiParams   (name="page", type="integer", required=true, description="页数")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
        "list": [
            {
            "id": 5,
            "user_id": 1,
            "store_id": 0,
            "content": "内容",
            "images": "http://cloud.caiyunpan.brotop.cn/assets/img/qrcode.png",
            "house_ids": ",1,",
            "type": 类型1=红包2=一般信息,
            "score": "5800",
            "red_package": "5000",
            "number": 红包数量,
            "single": "单人领取金额",
            "get": 已领数量,
            "surplus": 剩余数量,
            "views": 浏览量,
            "good_count": 点赞总数,
            "is_good": 是否点赞0=否1=是
            "image_arr"图片: [
                "http://cloud.caiyunpan.brotop.cn/assets/img/qrcode.png"
            ],
            "store": {
                "id": 1,
                "store_name": "店铺名称",
                "store_icon": "图标",
                "industry_id": "行业标签",
                },
            },
        ],
        "this_page": 当前页数,
        "total_page": 总页数
    }
    })
     */
    public function store_list()
    {
        if($this->request->isPost()){
            $page = $this->request->param('page',1,'intval');
            $store_id = $this->request->param('store_id',0,'intval');
//            $house_ids = $this->user_house_model->where('user_id',$this->user_id)->column('house_id');
//            $query = function ($query) use ($house_ids){
//                if($this->store_id) {
//                    $query->where('store_id',$this->store_id);
//                }
//                foreach ($house_ids as $k=>$v){
//                    $w = ['house_ids'=>['like','%,'.$v.',%']];
//                    if($k == 0) {
//                        $query->where($w);
//                    } else {
//                        $query->whereOr($w);
//                    }
//                }
//            };
            $where_h = [
                'status'=>2,
            ];
            if($store_id) {
                $where_h['store_id'] = $store_id;
            } else {
                $where_h['house_ids'] = ['like',['%'.$this->auth->house_id.'%']];
            }
            $where = [
                'where' => $where_h,
                'with' => ['store']
            ];
            $order = ['number'=>'DESC','createtime'=>'DESC'];
            $inform = $this->store_inform_model->pageSelect($page,$where,'*',$order,config('option.num'));
            $list = $inform->items();
            $view_model = new StoreInformView();
            foreach ($list as &$v) {
                // 发布时间调整
                $v['createtime'] = tranTime($v['createtime']);
                // 获取点赞数及是否点赞
                $where_g = [
                    'object_id' => $v['id']
                ];
                $v['good_count'] = $this->store_inform_good_model->getCount($where_g);
                $where_g['user_id'] = $this->user_id;
                $v['is_good'] = $this->store_inform_good_model->getCount($where_g);
                // 记录浏览量
                $where_v = [
                    'user_id' => $this->auth->id,
                    'object_id' => $v['id']
                ];
                $view = $view_model->where($where_v)->find();
                if($view) {
                    $where_v = [
                        'id' => $view['id']
                    ];
                    $view_model->where($where_v)->setInc('count');
                } else {
                    $this->store_inform_model->where('id',$v['id'])->setInc('views');
                    $insert = [
                        'user_id' => $this->auth->id,
                        'object_id' => $v['id'],
                        'createtime' => time()
                    ];
                    $view_model->insertGetId($insert);
                }
            }
            $return = [
                'list' => $list,
                'this_page' => $inform->currentPage(),
                'total_page' => $inform->lastPage()
            ];
            $this->success('请求成功',$return);
        }
    }

    /**
     * 商圈店铺主页
     * @ApiWeigh    (75)
     *
     * @ApiTitle    (商圈店铺主页)
     * @ApiSummary  (商圈店铺主页)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/hot/store_detail)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="store_id", type="integer", required=true, description="店铺id")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
        "store": {
            "id": 1,
            "user_id": 1,
            "store_name": "店铺名称",
            "industry_id": "行业标签",
            "name": "测试11",
            "mobile": "联系电话",
            "store_icon": "图标",
            "province": "地址省",
            "city": "市",
            "region": "区",
            "images": "/assets/img/qrcode.png,/assets/img/qrcode.png",
            "content": "店铺详情",
            "image_arr"店铺轮播: [
                "http://cloud.caiyunpan.brotop.cn/assets/img/qrcode.png",
                "http://cloud.caiyunpan.brotop.cn/assets/img/qrcode.png"
            ]
        }
    }
    })
     */
    public function store_detail()
    {
        if($this->request->isPost()){
            $store_id = $this->request->param('store_id',0,'intval');
            $where = [
                'where' => ['id'=>$store_id]
            ];
            $store = $this->store_model->findOrFail($where);
            $return = [
                'store' => $store
            ];
            $this->success('请求成功',$return);
        }
    }

    /**
     * 点赞
     * @ApiWeigh    (61)
     *
     * @ApiTitle    (点赞)
     * @ApiSummary  (点赞)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/hot/good)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="object_id", type="int", required=true, description="点赞id")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {}
    })
     */
    public function good()
    {
        if($this->request->isPost()) {
            $param = (new HotValidate())->goCheck('good');
            // 判断是否点赞
            $this->good_model = new StoreInformGood();
            $where = [
                'where' => [
                    'user_id' => $this->auth->id,
                    'object_id' => $param['object_id'],
                ]
            ];
            $cancel = '';
            $user_good = $this->good_model->findOrFail($where,false);
            // 执行点赞/取消点赞操作
            Db::startTrans();
            if($user_good) {
                $cancel = '取消';
                $result = $this->good_model->where('id',$user_good['id'])->delete();
            } else {
                // 判断要点赞的数据是否存在
                $data = $this->store_inform_model->where('id',$param['object_id'])->find();
                if(!$data) {
                    Db::rollback();
                    $this->error('点赞的商圈信息不存在');
                }
                $insert = [
                    'user_id' => $this->auth->id,
                    'object_id' => $param['object_id'],
                ];
                $result = $this->good_model->save($insert);
            }
            if(!$result) {
                Db::rollback();
                $this->error($cancel.'点赞失败');
            }
            Db::commit();
            $this->success($cancel.'点赞成功');
        }
    }

    /**
     * 领取红包
     * @ApiWeigh    (60)
     *
     * @ApiTitle    (领取红包)
     * @ApiSummary  (领取红包)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/hot/get_red_package)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="store_inform_id", type="integer", required=true, description="商家信息id")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    "data": {
    }
    })
     */
    public function get_red_package()
    {
        if($this->request->isPost()){
            $param = (new HotValidate())->goCheck('get_red_package');
            $where = [
                'where' => ['id'=>$param['store_inform_id'],'status'=>2]
            ];
            $inform = $this->store_inform_model->findOrFail($where);
            if($inform['type'] == 2) {
                $this->error('一般信息无法领取红包');
            }
            if($inform['surplus'] == 0) {
                $this->error('红包已抢完');
            }
            $log_model = new StoreInformLog();
            $user_model = new \app\api\model\User();
            $money_log_model = new UserMoneyLog();
            $where_l = [
                'where' => [
                    'user_id' => $this->auth->id,
                    'inform_id' => $inform['id'],
                ]
            ];
            $log = $log_model->findOrFail($where_l,false);
            if($log) {
                $this->error('已领取过该商圈红包');
            }
            Db::startTrans();
            // 录入领取记录
            $log_data = [
                'user_id' => $this->auth->id,
                'inform_id' => $inform['id'],
                'money' => $inform['single']
            ];
            $res_log = $log_model->add($log_data);
            // 修改已领取、剩余红包数量
            $inform_edit = [
                'id' => $inform['id'],
                'get' => $inform['get'] + 1,
                'surplus' => $inform['number'] - $inform['get'] - 1,
            ];
            $res_inform = $this->store_inform_model->edit($inform_edit);
            // 新增用户余额
            $res_user = $user_model->where('id',$this->auth->id)->setInc('money',$inform['single']);
            // 新增余额记录
            $money_log_data = [
                'user_id' => $this->auth->id,
                'money' => $inform['single'],
                'before' => $this->auth->money,
                'after' => bcadd($this->auth->money,$inform['single'],2),
                'memo' => '领取红包'
            ];
            $res_moeny_log = $money_log_model->add($money_log_data);
            if(!$res_log || !$res_inform || !$res_user || !$res_moeny_log) {
                Db::rollback();
                $this->error('红包领取失败');
            }
            Db::commit();
            $this->success('领取成功');
        }
    }

    /**
     * 留言列表
     * @ApiWeigh    (50)
     *
     * @ApiTitle    (留言列表)
     * @ApiSummary  (留言列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/hot/comment_list)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="store_id", type="integer", required=true, description="店铺id")
     * @ApiParams   (name="page", type="integer", required=true, description="页数")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function comment_list()
    {
        $param = (new HotValidate())->goCheck('common');
        $where = [
            'where' => ['user_id'=>$this->auth->id,'store_id' => $param['store_id']]
        ];
        $model = new StoreComment();
        $order = ['createtime'=>'DESC'];
        $comment = $model->pageSelect($param['page'],$where,'*',$order,config('option.num'));
        $list = $comment->items();
        if($list) {
            foreach ($list as &$v) {
                $v['is_self'] = $v['type'] == 1 ? 1 : 0;
                if($v['is_self'] == 1) {
                    $user = $this->auth->getUserinfo();
                    $v['nickname'] = $user['nickname'];
                    $v['avatar'] = $user['avatar'];
                } else {
                    $where_s = [
                        'where' => [
                            'id' => $v['store_id']
                        ]
                    ];
                    $store = $this->store_model->findOrFail($where_s,false,'id,store_name as nickname,store_icon as avatar');
                    $v['nickname'] = $store['nickname'];
                    $v['avatar'] = cdnurl($store['avatar']);
                }
            }
//            $list = array_sort($list,'id',SORT_ASC);
            array_multisort(array_column($list,'createtime'),SORT_ASC,$list);
        }
        $return = [
            'list' => $list,
            'this_page' => $comment->currentPage(),
            'total_page' => $comment->lastPage()
        ];
        $this->success('成功',$return);
    }

    /**
     * 留言提交
     * @ApiWeigh    (50)
     *
     * @ApiTitle    (留言提交)
     * @ApiSummary  (留言提交)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/hot/comment_add)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="store_id", type="integer", required=true, description="店铺id")
     * @ApiParams   (name="content", type="string", required=true, description="留言内容")
     * @ApiParams   (name="image", type="string", required=true, description="图片")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function comment_add()
    {
        $param = (new HotValidate())->goCheck('comment_add');
        if(empty($param['content']) && empty($param['image'])) {
            $this->error('请填写内容或上传图片');
        }
        $where = [
            'where' => ['id' => $param['store_id']]
        ];
        $store = $this->store_model->findOrFail($where);
        Db::startTrans();
        $result = false;
        try{
            $param['user_id'] = $this->auth->id;
            $param['store_user_id'] = $store['user_id'];
            $model = new StoreComment();
            $result = $model->add($param);
            // 记录最新内容
            $msg_model = new Message();
            $where_m = [
                'user_id' => $this->auth->id,
                'object_id' => $store['id'],
                'object_user_id' => $store['user_id'],
                'type' => 2
            ];
            $msg = $msg_model->where($where_m)->find();
            if($msg) {
                $update = [
                    'id' => $msg['id'],
                    'content' => $param['content'],
                    'image' => $param['image'],
                    'is_read_user' => 1,
                    'is_read_object' => 2,
                ];
                $res_msg = $msg_model->edit($update);
                $msg_id = $msg['id'];
            } else {
                $insert = $where_m;
                $insert['type'] = 2;
                $insert['content'] = $param['content'];
                $insert['image'] = $param['image'];
                $insert['is_read_user'] = 1;
                $res_msg = $msg_model->add($insert);
                $msg_id = $msg_model->id;
            }
            // 发送模板消息
            $send_data = array(
                "first"  => '您有一条新的消息',
                'keyword1' => $param['content'] ? $param['content'] : '【图片消息】',
                "keyword2"  => date('Y-m-s H:i:s',time()),
                "remark" => '',
            );
            $openid = Db::name('third')->where('user_id',$store['user_id'])->value('openid');
            $url = config('option.vue_url') . '/messageto?is_template=1&id='.$msg_id;
            $this->wxsendmessage($openid,$send_data,config('option.template')['msg'],$url);
            Db::commit();
        } catch (PDOException $e) {
            Db::rollback();
            $this->error($e->getMessage());
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if(!$result || !$res_msg) {
            $this->error('留言失败');
        }
        $this->success('留言成功');
    }
}