Teacher.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller\educational;
  12. use app\admin\controller\AuthController;
  13. use service\JsonService as Json;
  14. use app\admin\model\educational\Teacher as TeacherModel;
  15. use app\admin\model\educational\TeacherCategpry;
  16. use app\admin\model\user\User;
  17. /**
  18. * 老师控制器
  19. * Class Teacher
  20. */
  21. class Teacher extends AuthController
  22. {
  23. /**
  24. * 老师列表
  25. */
  26. public function index()
  27. {
  28. $this->assign(['category' => TeacherCategpry::taskCategoryAll(2)]);
  29. return $this->fetch();
  30. }
  31. /**
  32. * 获取老师列表
  33. */
  34. public function getTeacherList()
  35. {
  36. $where = parent::getMore([
  37. ['page', 1],
  38. ['limit', 20],
  39. ['pid', 0],
  40. ['title', ''],
  41. ]);
  42. return Json::successlayui(TeacherModel::getTeacherLists($where));
  43. }
  44. /**
  45. * 快速编辑
  46. * @param string $field 字段名
  47. * @param int $id 修改的主键
  48. * @param string value 修改后的值
  49. * @return json
  50. */
  51. public function set_value($field = '', $id = '', $value = '')
  52. {
  53. if (!$field || !$id || $value == '') Json::fail('缺少参数3');
  54. if ($field == 'sort' && bcsub($value, 0, 0) < 0) return Json::fail('排序不能为负数');
  55. $res = parent::getDataModification('teacher', $id, $field, $value);
  56. if ($res)
  57. return Json::successful('保存成功');
  58. else
  59. return Json::fail('保存失败');
  60. }
  61. /**添加/编辑
  62. * @param int $id
  63. * @return mixed
  64. */
  65. public function create($id = 0, $uid = 0)
  66. {
  67. $teacher = $id > 0 ? TeacherModel::get($id) : [];
  68. $this->assign(['id' => $id, 'uid' => $uid, 'teacher' => json_encode($teacher)]);
  69. return $this->fetch();
  70. }
  71. /**
  72. * 获取老师分类
  73. */
  74. public function get_teacher_cate()
  75. {
  76. $category = TeacherCategpry::taskCategoryAll(2);
  77. return Json::successful($category);
  78. }
  79. /**添加/编辑老师
  80. * @param int $id
  81. */
  82. public function save_add($id = 0)
  83. {
  84. $data = parent::postMore([
  85. ['name', ''],
  86. ['uid', 0],
  87. ['pid', 0],
  88. ['image', ''],
  89. ['position', ''],
  90. ['phone', ''],
  91. ['sort', 0]
  92. ]);
  93. if ($data['pid'] <= 0) return Json::fail('请选择老师分类');
  94. if (!$data['name']) return Json::fail('请输入老师名称');
  95. if (!$data['image']) return Json::fail('请选择老师头像');
  96. if (!$data['position']) return Json::fail('请输入老师职位');
  97. if (!$data['phone']) return Json::fail('请输入老师手机号');
  98. if (!check_phone($data['phone'])) return Json::fail('手机号不正确');
  99. TeacherModel::beginTrans();
  100. if ($id) {
  101. $res = TeacherModel::edit($data, $id);
  102. } else {
  103. $user = User::where('uid', $data['uid'])->field('identitys,nickname')->find();
  104. if ($user['identitys'] == 2) return Json::fail('该用户已是老师');
  105. if ($user['identitys'] == 1) return Json::fail('该用户已是学员');
  106. $data['nickname'] = $user['nickname'];
  107. $data['add_time'] = time();
  108. if (!TeacherModel::be(['uid' => $data['uid'], 'is_del' => 0])) {
  109. $res = TeacherModel::set($data);
  110. if ($res) User::where('uid', $data['uid'])->update(['identitys' => 2]);
  111. } else {
  112. TeacherModel::rollbackTrans();
  113. return Json::fail('该用户已是老师');
  114. }
  115. }
  116. if ($res) {
  117. TeacherModel::commitTrans();
  118. return Json::successful('添加/编辑成功');
  119. } else {
  120. TeacherModel::rollbackTrans();
  121. return Json::fail('添加/编辑失败');
  122. }
  123. }
  124. /**删除老师
  125. * @param int $id
  126. */
  127. public function delete($id = 0)
  128. {
  129. if (!$id) return Json::fail('参数错误');
  130. $teacher = TeacherModel::get($id);
  131. if (!$teacher) return Json::fail('要删除的老师不存在');
  132. $res = parent::getDataModification('teacher', $id, 'is_del', 1);
  133. if ($res) {
  134. User::where('uid', $teacher['uid'])->update(['identitys' => 0]);
  135. return Json::successful('删除成功');
  136. } else {
  137. return Json::fail('删除失败');
  138. }
  139. }
  140. /**获取用户
  141. * @return mixed
  142. */
  143. public function user()
  144. {
  145. return $this->fetch();
  146. }
  147. /**
  148. * 获取用户列表
  149. */
  150. public function user_list()
  151. {
  152. $where = parent::getMore([
  153. ['page', 1],
  154. ['limit', 20],
  155. ['nickname', ''],
  156. ['identitys', 0],
  157. ['order', '']
  158. ]);
  159. return Json::successlayui(User::add_teacher_user_list($where));
  160. }
  161. }