fetch(); } public function get_kefu_list() { $data = KefuModel::field("password", true)->select(); $count = KefuModel::count(); return Json::successlayui(compact('data', 'count')); } /** * 快速编辑 * @param string $field 字段名 * @param int $id 修改的主键 * @param string value 修改后的值 * @return json */ public function set_value($field = '', $id = '', $value = '') { if (!$field || !$id || $value == '') Json::fail('缺少参数3'); if ($field == 'sort' && bcsub($value, 0, 0) < 0) return Json::fail('排序不能为负数'); $res = KefuModel::where('id', $id)->update([$field => $value]); if ($res) return Json::successful('保存成功'); else return Json::fail('保存失败'); } public function create() { return $this->fetch(); } /** * 获取用户列表 */ public function user_list() { $where = parent::getMore([ ['page', 1], ['limit', 20], ['nickname', ''], ['identitys', 1], ['order', ''] ]); return Json::successlayui(User::add_teacher_user_list($where)); } public function save() { $data = parent::postMore([ ['uid', 0] ]); if (!$data['uid']) return Json::fail('请选择要添加的用户!'); if (KefuModel::where(['mer_id' => 0, 'uid' => $data['uid']])->count()) return Json::fail('用户中存在已有的客服!'); $now_user = User::where('uid', $data['uid'])->field('uid,avatar,nickname,phone')->find(); if (!$now_user['phone']) return Json::fail('添加用户没有绑定手机号!'); $data["avatar"] = $now_user["avatar"]; $data["nickname"] = $now_user["nickname"]; $data["account"] = $now_user["phone"]; $data["phone"] = $now_user["phone"]; $data["add_time"] = time(); $res = KefuModel::set($data); if ($res) { return Json::successful('添加成功!'); } else { return Json::fail('添加失败!'); } } /** * 删除指定资源 * * @param int $id * @return \think\Response */ public function delete($id) { if (!KefuModel::del($id)) return Json::fail(KefuModel::getErrorInfo('删除失败,请稍候再试!')); else return Json::successful('删除成功!'); } /** * 显示编辑资源表单页. * * @param int $id * @return \think\Response */ public function edit($id) { $service = KefuModel::get($id); if (!$service) return Json::fail('数据不存在!'); $f = array(); $f[] = Form::frameImageOne('avatar', '客服头像', Url::build('admin/widget.images/index', array('fodder' => 'avatar')), $service['avatar'])->icon('image')->width('100%')->height('500px')->spin(0); $f[] = Form::input('nickname', '客服名称', $service["nickname"]); $f[] = Form::input('phone', '手机号码', $service["phone"]); $f[] = Form::input('account', '登录账号', $service["account"]); $f[] = Form::password('password', '登录密码', ''); $f[] = Form::password('re_password', '确认密码', ''); $f[] = Form::radio('status', '客服状态', $service['status'])->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]); $f[] = Form::radio('notify', '订单通知', $service['notify'])->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]); $form = Form::make_post_form('修改数据', $f, Url::build('update', compact('id')), 2); $this->assign(compact('form')); return $this->fetch('public/form-builder'); } /** * 保存新建的资源 * * @param \think\Request $request * @return \think\Response */ public function update(Request $request, $id) { $params = $request->post(); if (empty($params["avatar"])) return Json::fail("客服头像不能为空!"); if (empty($params["nickname"])) return Json::fail("客服名称不能为空!"); if (empty($params["phone"])) return Json::fail("手机号不能为空!"); if (empty($params["account"])) return Json::fail("登录账号不能为空!"); $data = [ "avatar" => $params["avatar"], "nickname" => $params["nickname"], 'phone' => $params['phone'], 'account' => $params['account'], 'status' => $params['status'], 'notify' => $params['notify'] ]; if (strlen($params['password']) > 0 || strlen($params['re_password']) > 0) { if ($params['password'] != $params['re_password']) return Json::fail("两次输入的密码不一致!"); if (strlen($params['password']) < 6) return Json::fail("密码长度不能少于六位!"); $data['password'] = md5($params['password']); } KefuModel::edit($data, $id); return Json::successful('修改成功!'); } public function into_kefu_dashboard($kefu_id) { $kefuInfo = KefuModel::find($kefu_id); if (!$kefuInfo) return $this->fetch("public/error", [ 'msg' => "此客服不存在!", 'url' => Url::build("/admin/index/index") ]); if (!$kefuInfo['status']) return $this->fetch("public/error", [ 'msg' => "此客服处于禁用状态!", 'url' => Url::build("/admin/index/index") ]); $tokenService = new JwtService("kefu"); $token = $tokenService->getToken((int)$kefu_id); $redirectUrl = "/kefu/dashboard/index?token_type=kefu&token=$token"; return $this->redirect($redirectUrl, 302); } }