LeaveController.php 5.2 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/29
 * Time: 2:40
 */

namespace api\wxapp\controller;

use cmf\controller\RestBaseController;
use think\Db;
use think\Validate;
/**
 * @title 留言板模块
 * @description 留言板模块
 */
class LeaveController extends RestBaseController
{
    /**
     * @title 留言/回复
     * @description 位置:名片详情
     * @url /wxapp/leave/leave
     * @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:user_id type:string require:1 other: desc:业务员id(名片所属人)
     * @param name:content type:string require:1 other: desc:留言内容
     * @param name:id type:string require:0 other: desc:留言id(留言为0,回复为留言id)
     *
     */
    public function leave() {
        $validate = new Validate([
            'user_id'           => 'require',
            'content'           => 'require',
        ]);
        $validate->message([
            'user_id.require'           => '缺少参数user_id!',
            'content.require'           => '缺少参数content!',
        ]);
        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error(['code'=>'40003','msg'=>$validate->getError()]);
        }
        if ($data['id']) {
            $to_uid = Db::name('leave')->where(['id' => $data['id']])->value('from_uid');
        } else {
            $user_id = Db::name('card')->where(['id' => $data['user_id']])->value('user_id');
            $to_uid = $user_id;
        }
        $arr = [
            'parent_id' => $data['id'] ? : 0,
            'to_uid' => $to_uid,
            'from_uid' => $this->userId,
            'content' => $data['content']
        ];
        $result = Db::name('leave')->insert($arr);
        if ($result === false) {
            $this->error(['code'=>'40000','msg'=>'操作失败']);
        }
        $this->success('操作成功');
    }
    /**
     * @title 留言板(通用)
     * @description 位置:名片详情
     * @url /wxapp/leave/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:设备类型
     *
     * @return id:留言id
     * @return avatar:被留言人头像
     * @return to_uid:被留言人id(业务员id)
     * @return name:被留言人姓名
     * @return content:留言内容
     * @return create_time:留言时间
     * @return reply_info:回复内容(空为未回复)
     */
    public function getList() {
        $field = 'l.id,l.create_time';
        $condition['l.parent_id'] = 0;
        if ($this->userId && $this->userType == 2) {//用户
            $condition['l.from_uid'] = $this->userId;
            $field = 'l.id,l.to_uid,to.avatar,c.name,content,l.create_time';
        } elseif ($this->userId && $this->userType == 3) {//信贷员
            $condition['l.to_uid|l.from_uid'] = $this->userId;
            $field = 'l.id,l.to_uid,from.avatar,from.user_nickname name,content,l.create_time';
            //将该业务员的 留言都改为已读状态
            Db::name('leave')->where(['to_uid|from_uid' => $this->userId])->update(['is_read' => 1]);
        }
        $result = Db::name('leave')
            ->alias('l')
            ->join('user to','l.to_uid = to.id')
            ->join('card c','c.user_id = l.to_uid')
            ->join('user from','l.from_uid = from.id')
            ->where($condition)
            ->field($field)
            ->select()
            ->toArray();
        if ($result === false) {
            $this->error(['code'=>'40000','msg'=>'获取失败']);
        }
        if (count($result) > 0) {
            foreach ($result as $k => $v) {
                $reply = Db::name('leave')->where(['parent_id' => $v['id']])->column('content');
                $result[$k]['reply_info'] = $reply;
                $result[$k]['create_time'] = date('Y-m-d',strtotime($v['create_time']));
            }
        }
        
        $this->success('获取成功',$result);
    }
    
    /**
     * @title 留言板(是否有红点)
     * @description 位置:名片详情
     * @url /wxapp/leave/getUnread
     * @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 is_unread:1未读 0已读 
     */
    public function getUnread() {
        $condition['parent_id'] = 0;
        $condition['to_uid|from_uid'] = $this->userId;
        $check = Db::name('leave')->where($condition)->field('is_read')->find();
        if($check) {
            $condition['is_read'] = 0;
            $result = Db::name('leave')->where($condition)->field('is_read')->find();
            if($result) {
                $is_unread = 1; 
            } else {
                $is_unread = 0;
            }
        } else {
            $is_unread = 0;
        }
        $this->success('获取成功',['is_unread'=>$is_unread]);
    }
}