LeaveController.php
4.0 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
<?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';
}
$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);
}
}