You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
zhishifufei_php/application/institution/controller/special/Lecturer.php

474 lines
18 KiB

<?php
// +----------------------------------------------------------------------
// | 天诚科技 [ 刘海东 17600099397赋能开发者,助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.tczxkj.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 该系统并不是自由软件,未经许可不能去掉相关版权
// +----------------------------------------------------------------------
// | Author:甘肃天诚志信电子商务有限公司 刘海东 联系电话维系17600099397
// +----------------------------------------------------------------------
namespace app\institution\controller\special;
use app\institution\controller\AuthController;
use app\admin\model\special\Special;
use app\admin\model\system\WebRecommend;
use app\admin\model\system\WebRecommendRelation;
use app\admin\model\system\Recommend;
use app\admin\model\system\RecommendRelation;
use app\admin\model\special\Lecturer as LecturerModel;
use app\admin\model\institution\Institution as InstitutionModel;
use app\admin\model\institution\InstitutionAdmin as InstitutionAdminModel;
use app\institution\model\institution\InstitutionMenus;
use app\admin\model\user\User;
use service\JsonService;
use service\FormBuilder as Form;
use think\Db;
use think\Url;
/**
* 机构控制器
*/
class Lecturer extends AuthController
{
/**
* 机构列表展示
* @return
* */
public function index()
{
return $this->fetch();
}
/**
* 机构列表获取
* @return
* */
public function lecturer_list()
{
$where = parent::getMore([
['page', 1],
['is_show', ''],
['limit', 20],
['title', ''],
['entry_type',1]
]);
$where['mer_id'] = $this->merchantId;
return JsonService::successlayui(LecturerModel::getLecturerList($where));
}
/**添加/编辑
* @param int $id
* @return mixed|void
* @throws \think\exception\DbException
*/
public function create($id = 0)
{
if ($id) {
$lecturer = LecturerModel::get($id);
$lecturer['label'] = json_decode($lecturer['label']);
$lecturer['introduction'] = htmlspecialchars_decode($lecturer['introduction']);
if (!$lecturer) return JsonService::fail('机构信息不存在!');
} else {
$lecturer = [];
}
$this->assign(['lecturer' => json_encode($lecturer), 'id' => $id]);
return $this->fetch();
}
/**
* 添加和修改机构
* @param int $id 修改
* @return JsonService
* */
public function save_lecturer($id = 0)
{
$data = parent::postMore([
['lecturer_name', ''],
['lecturer_head', ''],
['label', []],
['phone', ''],
['explain', ''],
['introduction', ''],
['sort', 0],
['is_show', 1],
]);
$data['lecturer_name'] = preg_replace("#(^( |\s)+|( |\s)+$)#", "", $data['lecturer_name']);
if (!$data['lecturer_name']) return JsonService::fail('请输入机构名称');
if (mb_strlen($data['lecturer_name']) > 8) return JsonService::fail('机构名称不能超过8个字');
if (!$data['lecturer_head']) return JsonService::fail('请输入机构头像');
if (!count($data['label'])) return JsonService::fail('请输入标签');
if (!$data['explain']) return JsonService::fail('请编辑机构说明');
if (!$data['introduction']) return JsonService::fail('请编辑机构介绍');
$data['label'] = json_encode($data['label']);
$data['introduction'] = htmlspecialchars($data['introduction']);
$data['agent'] = $this->merchantId;
if ($id) {
LecturerModel::edit($data, $id);
return JsonService::successful('修改成功');
} else {
$data['add_time'] = time();
if (!LecturerModel::be(['lecturer_name' => $data['lecturer_name'], 'lecturer_head' => $data['lecturer_head'], 'label' => $data['label'], 'phone' => $data['phone'], 'is_del' => 0])) {
$res = LecturerModel::set($data);
} else {
return JsonService::fail('机构已存在');
}
if ($res)
return JsonService::successful('添加成功');
else
return JsonService::fail('添加失败');
}
}
/**
* 设置单个产品上架|下架
* @param int $is_show 是否显示
* @param int $id 修改的主键
* @return JsonService
*/
public function set_show($is_show = '', $id = '')
{
($is_show == '' || $id == '') && JsonService::fail('缺少参数');
$res = parent::getDataModification('lecturer', $id, 'is_show', (int)$is_show);
if ($res) {
$mer_id = LecturerModel::where('id', $id)->value('mer_id');
if ($mer_id) {
$data['estate'] = $is_show;
InstitutionModel::edit($data, $mer_id, 'id');
}
return JsonService::successful($is_show == 1 ? '显示成功' : '隐藏成功');
} else {
return JsonService::fail($is_show == 1 ? '显示失败' : '隐藏失败');
}
}
/**
* 快速编辑
* @param string $field 字段名
* @param int $id 修改的主键
* @param string value 修改后的值
* @return JsonService
*/
public function set_value($field = '', $id = '', $value = '')
{
($field == '' || $id == '' || $value == '') && JsonService::fail('缺少参数');
$res = parent::getDataModification('lecturer', $id, $field, $value);
if ($res)
return JsonService::successful('保存成功');
else
return JsonService::fail('保存失败');
}
/**
* 删除机构
* @param int $id 修改的主键
* @return json
* */
public function delete($id = 0)
{
if (!$id) return JsonService::fail('缺少参数');
if (LecturerModel::delLecturer($id))
return JsonService::successful('删除成功');
else
return JsonService::fail(LecturerModel::getErrorInfo('删除失败'));
}
/**
* 机构课程订单
*/
public function lecturer_order($id = 0)
{
$this->assign([
'year' => getMonth('h'),
'lecturer_id' => $id,
]);
return $this->fetch();
}
/**机构课程购买记录
* @throws \think\exception\DbException
*/
public function lecturer_order_list()
{
$where = parent::getMore([
['lecturer_id', 0],
['page', 1],
['limit', 10],
['data', ''],
]);
if (!$where['lecturer_id']) return JsonService::fail('缺少参数!');
$lecturer = LecturerModel::get($where['lecturer_id']);
if (!$lecturer) return JsonService::fail('机构不存在!');
$list = LecturerModel::lecturerOrderList($where);
return JsonService::successlayui($list);
}
/**
* 机构盈利
*/
public function getBadge()
{
$where = parent::postMore([
['lecturer_id', 0],
['data', ''],
]);
if (!$where['lecturer_id']) return JsonService::fail('缺少参数!');
$lecturer = LecturerModel::get($where['lecturer_id']);
if (!$lecturer) return JsonService::fail('机构不存在!');
$list = LecturerModel::getBadge($where);
return JsonService::successful($list);
}
/**
* 添加推荐
* @param int $special_id
* @return mixed
* @throws \think\exception\DbException
*/
public function web_recommend($lecturer_id = 0)
{
if (!$lecturer_id) $this->failed('缺少参数');
$lecturer = LecturerModel::get($lecturer_id);
if (!$lecturer) $this->failed('没有查到此机构');
if ($lecturer->is_del) $this->failed('此机构已删除');
$form = Form::create(Url::build('save_web_recommend', ['lecturer_id' => $lecturer_id]), [
Form::select('recommend_id', '推荐')->setOptions(function () {
$model = WebRecommend::where(['is_show' => 1, 'type' => 2]);
$list = $model->field('title,id')->order('sort desc,add_time desc')->select();
$menus = [];
foreach ($list as $menu) {
$menus[] = ['value' => $menu['id'], 'label' => $menu['title']];
}
return $menus;
})->filterable(1),
Form::number('sort', '排序'),
]);
$form->setMethod('post')->setTitle('推荐设置')->setSuccessScript('parent.$(".J_iframe:visible")[0].contentWindow.location.reload(); setTimeout(function(){parent.layer.close(parent.layer.getFrameIndex(window.name));},800);');
$this->assign(compact('form'));
return $this->fetch('public/form-builder');
}
/**
* 保存推荐
* @param int $special_id
* @throws \think\exception\DbException
*/
public function save_web_recommend($lecturer_id = 0)
{
if (!$lecturer_id) $this->failed('缺少参数');
$data = parent::postMore([
['recommend_id', 0],
['sort', 0],
]);
if (!$data['recommend_id']) return JsonService::fail('请选择推荐');
$recommend = WebRecommend::get($data['recommend_id']);
if (!$recommend) return JsonService::fail('导航菜单不存在');
$data['add_time'] = time();
$data['type'] = $recommend->type;
$data['link_id'] = $lecturer_id;
if (WebRecommendRelation::be(['type' => $recommend->type, 'link_id' => $lecturer_id, 'recommend_id' => $data['recommend_id']])) return JsonService::fail('已推荐,请勿重复推荐');
if (WebRecommendRelation::set($data))
return JsonService::successful('推荐成功');
else
return JsonService::fail('推荐失败');
}
/**取消推荐
* @param int $id
*/
public function cancel_web_recommendation($id = 0, $lecturer_id = 0)
{
if (!$id || !$lecturer_id) return JsonService::fail('缺少参数');
if (WebRecommendRelation::be(['id' => $id, 'link_id' => $lecturer_id])) {
$res = WebRecommendRelation::where(['id' => $id, 'link_id' => $lecturer_id])->delete();
if ($res)
return JsonService::successful('取消推荐成功');
else
return JsonService::fail('取消推荐失败');
} else {
return JsonService::fail('推荐不存在');
}
}
/**
* 添加推荐
* @param int $special_id
* @return mixed
* @throws \think\exception\DbException
*/
public function recommend($lecturer_id = 0)
{
if (!$lecturer_id) $this->failed('缺少参数');
$lecturer = LecturerModel::get($lecturer_id);
if (!$lecturer) $this->failed('没有查到此机构');
if ($lecturer->is_del) $this->failed('此机构已删除');
$form = Form::create(Url::build('save_recommend', ['lecturer_id' => $lecturer_id]), [
Form::select('recommend_id', '推荐')->setOptions(function () {
$model = Recommend::where(['is_show' => 1, 'type' => 6]);
$list = $model->field('title,id')->order('sort desc,add_time desc')->select();
$menus = [];
foreach ($list as $menu) {
$menus[] = ['value' => $menu['id'], 'label' => $menu['title']];
}
return $menus;
})->filterable(1),
Form::number('sort', '排序'),
]);
$form->setMethod('post')->setTitle('推荐设置')->setSuccessScript('parent.$(".J_iframe:visible")[0].contentWindow.location.reload(); setTimeout(function(){parent.layer.close(parent.layer.getFrameIndex(window.name));},800);');
$this->assign(compact('form'));
return $this->fetch('public/form-builder');
}
/**
* 保存推荐
* @param int $special_id
* @throws \think\exception\DbException
*/
public function save_recommend($lecturer_id = 0)
{
if (!$lecturer_id) $this->failed('缺少参数');
$data = parent::postMore([
['recommend_id', 0],
['sort', 0],
]);
if (!$data['recommend_id']) return JsonService::fail('请选择推荐');
$recommend = Recommend::get($data['recommend_id']);
if (!$recommend) return JsonService::fail('导航菜单不存在');
$data['add_time'] = time();
$data['type'] = $recommend->type;
$data['link_id'] = $lecturer_id;
if (RecommendRelation::be(['type' => $recommend->type, 'link_id' => $lecturer_id, 'recommend_id' => $data['recommend_id']])) return JsonService::fail('已推荐,请勿重复推荐');
if (RecommendRelation::set($data))
return JsonService::successful('推荐成功');
else
return JsonService::fail('推荐失败');
}
/**取消推荐
* @param int $id
*/
public function cancel_recommendation($id = 0, $lecturer_id = 0)
{
if (!$id || !$lecturer_id) return JsonService::fail('缺少参数');
if (RecommendRelation::be(['id' => $id, 'link_id' => $lecturer_id])) {
$res = RecommendRelation::where(['id' => $id, 'link_id' => $lecturer_id])->delete();
if ($res)
return JsonService::successful('取消推荐成功');
else
return JsonService::fail('取消推荐失败');
} else {
return JsonService::fail('推荐不存在');
}
}
/**生成机构后台
* @return mixed
*/
public function mercreate($id)
{
$lecturer = LecturerModel::get($id);
$this->assign([
'title' => '添加机构后台',
'lecturer' => json_encode($lecturer),
'action' => Url::build('save'),
'menus' => json(InstitutionMenus::ruleList())->getContent()
]);
return $this->fetch();
}
/**
* 添加机构商户
*/
public function save()
{
$data = parent::postMore([
'account',
['id', 0],
['uid', 0],
'conf_pwd',
'pwd',
'mer_name',
'real_name',
'mer_phone',
'mer_avatar',
'mer_special_divide',
'mer_store_divide',
'mer_event_divide',
'mer_data_divide',
'mer_test_divide',
'gold_divide',
'mark',
'mer_address',
['checked_menus', [], '', 'rules'],
['is_source', 0],
['is_audit', 0],
['status', 0]
]);
if (!is_array($data['rules']) || !count($data['rules'])) return JsonService::fail('请选择最少一个权限');
$data['rules'] = implode(',', $data['rules']);
if (!$data['account']) return JsonService::fail('请输入机构后台账号');
if (InstitutionAdminModel::where('account', trim($data['account']))->where('is_del', 0)->count()) return JsonService::fail('商户账号已存在,请使用别的商户账号注册');
if (!$data['pwd']) return JsonService::fail('请输入机构后台登陆密码');
if ($data['pwd'] != $data['conf_pwd']) return JsonService::fail('两次输入密码不想同');
if (!$data['mer_name']) return JsonService::fail('请输入机构后台名称');
if (!$data['uid']) return JsonService::fail('请输入绑定的用户ID');
$user = User::where('uid', $data['uid'])->find();
if (!$user) {
return JsonService::fail('绑定的用户不存在');
} else {
if ($user['business'] == 1) {
return JsonService::fail('该用户已是机构');
}
}
$id = $data['id'];
$data['pwd'] = trim(md5($data['pwd']));
$data['reg_time'] = time();
$data['add_time'] = time();
$data['reg_admin_id'] = $this->adminId;
$data['lecturer_id'] = $id;
$data['estate'] = 1;
$admin = array();
$admin['account'] = trim($data['account']);
$admin['pwd'] = $data['pwd'];
unset($data['id']);
unset($data['conf_pwd']);
unset($data['account']);
unset($data['pwd']);
InstitutionModel::beginTrans();
$res = InstitutionModel::set($data);
$res1 = false;
if ($res) {
$admin['uid'] = $data['uid'];
$admin['mer_id'] = $res->id;
$admin['real_name'] = $data['mer_name'];
$admin['rules'] = $data['rules'];
$admin['phone'] = $data['mer_phone'];
$admin['add_time'] = time();
$admin['status'] = 1;
$admin['level'] = 0;
$res1 = InstitutionAdminModel::set($admin);
}
$bool = false;
if ($res1 && $res) $bool = true;
InstitutionModel::checkTrans($bool);
if ($bool) {
LecturerModel::where('id', $id)->update(['mer_id' => $res->id, 'lecturer_name' => $data['mer_name'], 'phone' => $data['mer_phone']]);
Special::where('lecturer_id', $id)->update(['mer_id' => $res->id]);
User::where('uid', $data['uid'])->update(['business' => 1]);
Db::name('cache_merchant')->insert(
[
'mer_id' => $res->id,
'add_time' => time(),
'key'=> 'wechat_menus',
'result' => '[{"type":"view","name":"\u8bb2\u5e08\u9996\u9875","sub_button":[],"url":"https:\/\/www.ideaketang.com\/\/h5"},{"type":"click","name":"\u5e73\u53f0\u9996\u9875","sub_button":[],"key":"https:\/\/www.ideaketang.com\/h5"},{"type":"view","name":"\u670d\u52a1\u4e2d\u5fc3","sub_button":[],"key":"https:\/\/www.ideaketang.com\/h5","url":"https:\/\/www.ideaketang.com\/h5"}]'
]
);
return JsonService::successful('添加机构后台成功!');
} else {
return JsonService::successful('添加机构后台失败!');
}
}
}