CardController.php 15.9 KB
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
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/26
 * Time: 8:08
 */

namespace api\wxapp\controller;

use cmf\controller\RestBaseController;
use think\Db;
use think\Validate;
/**
 * @title 名片模块 11 2
 * @description 名片模块
 */
class CardController extends RestBaseController
{
    /**
     * @title 获取搜索关键字(排名前三)
     * @description 名片
     * @url /wxapp/card/getKeyWordList
     * @method POST
     *
     * @header name:XX-Token type:string require:1 default:abc other: desc:登录标识
     * @header name:XX-Device-Type type:string require:0 default:wxapp other: desc:设备类型
     *
     * @return keyword:搜索关键字
     * @return num:搜索次数
     */
    public function getKeyWordList() {
        $result = Db::name('search')->field('keyword,num')->order('num desc')->limit(3)->select();
        if ($result === false) {
            $this->error(['code'=>'40000','msg'=>'获取失败']);
        }
        $this->success('获取成功',$result);
    }

    /**
     * @title 获取名片列表(筛选)
     * @description 名片
     * @url /wxapp/card/getList
     * @method POST
     *
     * @header name:XX-Token type:string require:1 default:abc other: desc:登录标识
     * @header name:XX-Device-Type type:string require:0 default:wxapp other: desc:设备类型
     *
     * @param name:keyword type:string require:0 other: desc:关键字搜索(姓名/公司/手机号)
     * @param name:page type:string require:0 other: desc:页码
     * @param name:per_page type:string require:0 other: desc:每页数据量
     *
     * @return total:总数据量
     * @return per_page:每页数据量
     * @return current_page:当前页
     * @return last_page:尾页
     * @return data:对象信息@!
     * @data id:名片id id avatar:头像 name:姓名 position:职称 tel:电话 home_tel:座机 email:邮箱 company:公司
     */
    public function getList() {
        $data = $this->request->param();
        $per_page = $data['per_page'] ?  : config('per_page');
        $condition['c.is_recommend'] = 1;//后台推荐
        //加分享过的名片
        $ids = Db::name('card_log')->where(['user_id' => $this->userId])->column('card_id');
        if($data['keyword']) {
            $condition['c.name|company|tel'] = ['like','%'.$data['keyword'].'%'];
            //搜索日志
            $info = Db::name('search')->where(['keyword' => $data['keyword']])->find();
            if ($info) {
                Db::name('search')->where(['keyword' => $data['keyword']])->setInc('num');
            } else {
                Db::name('search')->insert(['keyword' => $data['keyword'],'num' => 1]);
            }
        } else {
            //加业务员自己的名片id
            if ($this->userType == 3) {
                $my_card = Db::name('card')->where(['user_id' => $this->userId])->value('id');
                if ($my_card) {
                    array_push($ids,$my_card);
                }
            }
        }
        $condition = $condition == '' ? '1=1' : $condition;
        $condition2['c.id'] = ['in',$ids];
        $result = Db::name('card')
            ->alias('c')
            ->join('user u','c.user_id=u.id','left')
            ->where($condition)
            ->whereOr($condition2)
            ->field('c.id,c.name,c.position,c.tel,c.home_tel,c.email,c.company,u.avatar,IF(c.user_id='.$this->userId.',c.user_id,0) top')
            ->order('top desc,c.create_time desc')
            ->paginate($per_page);
        if ($result === false) {
            $this->error(['code'=>'40000','msg'=>'获取失败']);
        }
        $this->success('获取成功',$result);
    }

    /**
     * @title 获取名片详情
     * @description 名片
     * @url /wxapp/card/getDetails
     * @method POST
     *
     * @header name:XX-Token type:string require:1 default:abc other: desc:登录标识
     * @header name:XX-Device-Type type:string require:0 default:wxapp other: desc:设备类型
     *
     * @param name:id type:string require:1 other: desc:名片id
     *
     * @return id:名片id
     * @return user_id:业务员id
     * @return name:姓名
     * @return position:职称
     * @return tel:电话
     * @return home_tel:座机
     * @return email:邮箱
     * @return wechat:微信号
     * @return company:公司
     * @return avatar:头像
     * @return address:地址
     * @return description:个人简介
     * @return pics:我的照片(英文,相隔的图片地址)
     * @return browse_num:浏览数
     * @return like_num:点赞数(靠谱)
     * @return is_like:是否点赞1已点赞0未点赞
     */
    public function getDetails() {
        $validate = new Validate([
            'id'           => 'require',
        ]);
        $validate->message([
            'id.require'           => '缺少参数id!',
        ]);
        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error(['code'=>'40003','msg'=>$validate->getError()]);
        }
        $result = Db::name('card')
            ->alias('c')
            ->join('user u','c.user_id=u.id')
            ->where(['c.id' => $data['id']])
            ->field('c.id,c.user_id,c.name,c.position,c.tel,c.home_tel,c.email,c.wechat,c.company,u.avatar,c.address,c.description,c.pics,c.browse_num,c.like_num')
            ->find();
        //校验该用户是否 已对此名片点赞
        $check = Db::name('like_log')->where(['user_id' => $this->userId,'topic_id' => $data['id']])->field('id')->find();
        if ($check) {
            $result['is_like'] = 1;
        } else {
            $result['is_like'] = 0;
        }
        $this->success('获取成功',$result);
        //添加该名片的被浏览记录 名片入口
        Db::startTrans();
        $is_exist = Db::name('browse_log')
            ->where(['topic_id' => $data['id'],'user_id' => $this->userId,'type' =>1])
            ->field('id')
            ->find();
        if (!$is_exist) {
            //排除业务员自己
            if ($this->userType == 3) {
                $card_id = Db::name('card')->where(['user_id' => $this->userId])->value('id');
            }
            if (isset($card_id) && $card_id != $data['id']) {
                $do1 = Db::name('browse_log')->insert(['topic_id' => $data['id'],'user_id' => $this->userId]);
            }
        }
        //浏览数+1
        $do2 = Db::name('card')->where(['id' => $data['id']])->setInc('browse_num');
        if ($do1 === false || $do2 === false) {
            Db::rollback();
        }
        Db::commit();
        if ($result === false) {
            $this->error(['code'=>'40000','msg'=>'获取失败']);
        }
        $this->success('获取成功',$result);
    }

    /**
     * @title 获取我的名片详情
     * @description 名片
     * @url /wxapp/card/getMyInfo
     * @method POST
     *
     * @header name:XX-Token type:string require:1 default:abc other: desc:登录标识
     * @header name:XX-Device-Type type:string require:0 default:wxapp other: desc:设备类型
     *
     * @return id:名片id
     * @return user_id:业务员id
     * @return name:姓名
     * @return position:职称
     * @return tel:电话
     * @return home_tel:座机
     * @return email:邮箱
     * @return wechat:微信号
     * @return company:公司
     * @return avatar:头像
     * @return address:地址
     * @return description:个人简介
     * @return pics:我的照片(英文,相隔的图片地址)
     * @return browse_num:浏览数
     * @return like_num:点赞数(靠谱)
     * @return is_like:是否点赞1已点赞0未点赞
     */
    public function getMyInfo() {
        if ($this->userType != 3) {
            $this->error(['code'=>'40007','msg'=>'非信贷员身份']);
        }
        $result = Db::name('card')
            ->alias('c')
            ->join('user u','c.user_id=u.id')
            ->where(['c.user_id' => $this->userId])
            ->field('c.id,c.user_id,c.name,c.position,c.tel,c.home_tel,c.email,c.wechat,c.company,u.avatar,c.address,c.description,c.pics,c.browse_num,c.like_num')
            ->find();
        if ($result === false) {
            $this->error(['code'=>'40000','msg'=>'获取失败']);
        }
        $this->success('获取成功',$result);
    }

    /**
     * @title 编辑业务员资料
     * @description 名片
     * @url /wxapp/card/addCard
     * @method POST
     *
     * @header name:XX-Token type:string require:1 default:abc other: desc:登录标识
     * @header name:XX-Device-Type type:string require:0 default:wxapp other: desc:设备类型
     *
     * @param name:avatar type:string require:0 other: desc:头像
     * @param name:company type:string require:0 other: desc:公司
     * @param name:name type:string require:0 other: desc:姓名
     * @param name:position type:string require:0 other: desc:职称
     * @param name:tel type:string require:0 other: desc:电话
     * @param name:home_tel type:string require:0 other: desc:座机
     * @param name:email type:string require:0 other: desc:邮箱
     * @param name:wechat type:string require:0 other: desc:微信号
     * @param name:address type:string require:0 other: desc:地址
     * @param name:description type:string require:0 other: desc:个人简介
     * @param name:pics type:string require:0 other: desc:照片(图集为以英文,相隔的图片地址)
     */
    public function addCard() {
        Db::startTrans();
        $validate = new Validate([
            'avatar'           => 'require',
            'company'           => 'require',
            'name'           => 'require',
            'position'           => 'require',
            'tel'           => 'require',
            'home_tel'           => 'require',
            'email'           => 'require',
            'wechat'           => 'require',
            'address'           => 'require',
            'description'           => 'require',
            'pics'           => 'require',
        ]);

        $validate->message([
            'avatar.require'           => '缺少参数avatar!',
            'company.require'           => '缺少参数company!',
            'name.require'           => '缺少参数name!',
            'position.require'           => '缺少参数position!',
            'tel.require'           => '缺少参数tel!',
            'home_tel.require'           => '缺少参数home_tel!',
            'email.require'           => '缺少参数email',
            'wechat.require'           => '缺少参数wechat!',
            'address.require'           => '缺少参数address!',
            'description.require'           => '缺少参数description!',
            'pics.require'           => '缺少参数pics!',
        ]);

        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error(['code'=>'40003','msg'=>$validate->getError()]);
        }
        if ($this->userType != 3) {
            $this->error(['code'=>'40007','msg'=>'非信贷员身份']);
        }
        if ($data['avatar']) {//头像单独存储到用户信息表
            $arr['id'] = $this->userId;
            $arr['avatar'] = $data['avatar'];
            $res = Db::name('user')->update($arr);
            if ($res === false) {
                Db::rollback();
                $this->error(['code'=>'40000','msg'=>'操作失败']);
            }
        }
        unset($data['avatar']);
        //查询名片表 是否存在该用户编辑数据,存在则更新,反之添加
        $check = Db::name('card')->where(['user_id' => $this->userId])->field('id')->find();
        if ($check) {
            $data['id'] = $check['id'];
            $data['user_id'] = $this->userId;
            $data['company'] = $data['company'];
            $data['name'] = $data['name'];
            $data['position'] = $data['position'];
            $data['tel'] = $data['tel'];
            $data['home_tel'] = $data['home_tel'];
            $data['email'] = $data['email'];
            $data['wechat'] = $data['wechat'];
            $data['address'] = $data['address'];
            $data['description'] = $data['description'];
            $data['pics'] = $data['pics'];
            $result = Db::name('card')->update($data);
        } else {
            $data['user_id'] = $this->userId;
            $data['company'] = $data['company'];
            $data['name'] = $data['name'];
            $data['position'] = $data['position'];
            $data['tel'] = $data['tel'];
            $data['home_tel'] = $data['home_tel'];
            $data['email'] = $data['email'];
            $data['wechat'] = $data['wechat'];
            $data['address'] = $data['address'];
            $data['description'] = $data['description'];
            $data['pics'] = $data['pics'];
            $result = Db::name('card')->insert($data);
        }

        if ($result === false) {
            Db::rollback();
            $this->error(['code'=>'40000','msg'=>'操作失败']);
        }
        Db::commit();
        $this->success('操作成功');
    }

    /**
     * @title 名片点赞/取消点赞
     * @description 名片
     * @url /wxapp/card/like
     * @method POST
     *
     * @header name:XX-Token type:string require:1 default:abc other: desc:登录标识
     * @header name:XX-Device-Type type:string require:0 default:wxapp other: desc:设备类型
     *
     * @param name:id type:string require:1 other: desc:名片id
     *
     */
    public function like() {
        $validate = new Validate([
            'id'           => 'require',
        ]);
        $validate->message([
            'id.require'           => '缺少参数id!',
        ]);
        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error(['code'=>'40003','msg'=>$validate->getError()]);
        }
        //校验该名片是否存在
        $info = Db::name('card')->where(['id' => $data['id']])->field('id')->find();
        if (!$info) {
            $this->error(['code'=>'40005','msg'=>'信息不存在']);
        }
        //校验该用户是否 已对此名片点赞
        $check = Db::name('like_log')->where(['user_id' => $this->userId,'topic_id' => $data['id']])->field('id')->find();
        if ($check) {
            Db::name('card')->where(['id' => $data['id']])->setDec('like_num');
            $result = Db::name('like_log')->where(['user_id' => $this->userId,'topic_id' => $data['id']])->delete();
        } else {
            Db::name('card')->where(['id' => $data['id']])->setInc('like_num');
            $result = Db::name('like_log')->insert(['user_id' => $this->userId,'topic_id' => $data['id']]);
        }
        if ($result === false) {
            $this->error(['code'=>'40000','msg'=>'操作失败']);
        }
        $this->success('操作成功');
    }

    /**
     * @title 通过名片id获取最近浏览用户(最多6个)
     * @description 位置:名片详情
     * @url /wxapp/card/getUserListByID
     * @method POST
     *
     * @header name:XX-Token type:string require:1 default:abc other: desc:登录标识
     * @header name:XX-Device-Type type:string require:0 default:wxapp other: desc:设备类型
     *
     * @param name:id type:string require:1 other: desc:名片id
     *
     * @return avatar:用户头像
     */
    public function getUserListByID() {
        $validate = new Validate([
            'id'           => 'require',
        ]);
        $validate->message([
            'id.require'           => '缺少参数id!',
        ]);
        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error(['code'=>'40003','msg'=>$validate->getError()]);
        }
        $result = Db::name('browse_log')
            ->alias('b')
            ->join('user u','b.user_id = u.id')
            ->where(['b.topic_id' => $data['id']])
            ->field('u.avatar')
            ->order('b.create_time desc')
            ->limit(6)
            ->select();
        if ($result === false) {
            $this->error(['code'=>'40000','msg'=>'获取失败']);
        }
        $this->success('获取成功',$result);
    }
}