暫無描述
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.

Ask.php 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. <?php
  2. /**
  3. * 易优CMS
  4. * ============================================================================
  5. * 版权所有 2016-2028 海南赞赞网络科技有限公司,并保留所有权利。
  6. * 网站地址: http://www.eyoucms.com
  7. * ----------------------------------------------------------------------------
  8. * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
  9. * ============================================================================
  10. * Author: 小虎哥 <1105415366@qq.com>
  11. * Date: 2018-4-3
  12. */
  13. namespace app\admin\controller;
  14. use think\Page;
  15. use think\Db;
  16. use app\common\logic\ArctypeLogic;
  17. /**
  18. * 插件的控制器
  19. */
  20. class Ask extends Base
  21. {
  22. private $arctypeLogic;
  23. /**
  24. * 构造方法
  25. */
  26. public function _initialize()
  27. {
  28. parent::_initialize();
  29. $functionLogic = new \app\common\logic\FunctionLogic;
  30. $functionLogic->check_authorfile(2);
  31. $this->arctypeLogic = new ArctypeLogic();
  32. // 问题表
  33. $this->ask_db = Db::name('ask');
  34. // 答案表
  35. $this->ask_answer_db = Db::name('ask_answer');
  36. // 点赞表
  37. $this->ask_answer_like_db = Db::name('ask_answer_like');
  38. // 问题分类表
  39. $this->ask_type_db = Db::name('ask_type');
  40. $score_name = getUsersConfigData('score.score_name');
  41. $this->assign('score_name', $score_name);
  42. }
  43. /**
  44. * 插件后台管理 - 栏目管理
  45. */
  46. public function index()
  47. {
  48. $list = $this->ask_type_db->order('sort_order asc, type_id asc')->select();
  49. foreach ($list as $key => $value) {
  50. // 是否顶级栏目
  51. if ($value['parent_id'] == 0) {
  52. $PidData[] = $value;
  53. } else {
  54. $TidData[] = $value;
  55. }
  56. }
  57. $list_new = [];
  58. foreach ($PidData as $P_key => $PidValue) {
  59. $type_name = $PidValue['type_name'];
  60. $PidValue['type_name_input'] = '<input type="text" name="type_name[]" value="' . $PidValue['type_name'] . '" class="w220">';
  61. $PidValue['parent_name'] = '顶级栏目';
  62. /*一级栏目*/
  63. $list_new[] = $PidValue;
  64. /* END */
  65. foreach ($TidData as $T_key => $TidValue) {
  66. /*二级栏目*/
  67. if ($TidValue['parent_id'] == $PidValue['type_id']) {
  68. $TidValue['type_name_input'] = '|— <input type="text" name="type_name[]" value="' . $TidValue['type_name'] . '" class="w200">';
  69. $TidValue['parent_name'] = $type_name;
  70. $list_new[] = $TidValue;
  71. }
  72. /* END */
  73. }
  74. }
  75. $this->assign('list', $list_new);
  76. /*栏目处理*/
  77. $PidDataNew[0] = [
  78. 'type_id' => 0,
  79. 'type_name' => '顶级栏目',
  80. 'parent_id' => 0,
  81. ];
  82. $PidData = !empty($PidData) ? array_merge($PidDataNew, $PidData) : $PidDataNew;
  83. $this->assign('PidData', $PidData);
  84. /* END */
  85. /*是否有数据*/
  86. $IsEmpty = empty($list_new) ? 0 : 1;
  87. $this->assign('IsEmpty', $IsEmpty);
  88. /* END */
  89. return $this->fetch();
  90. }
  91. /**
  92. * 插件后台管理 - 问题列表
  93. */
  94. public function ask_list()
  95. {
  96. $list = array();
  97. $keywords = input('keywords/s');
  98. $map = array();
  99. if (!empty($keywords)) {
  100. $map['a.ask_title'] = array('LIKE', "%{$keywords}%");
  101. }
  102. $map['a.is_del'] = 0;
  103. $count = $this->ask_db->alias('a')->where($map)->count('ask_id');// 查询满足要求的总记录数
  104. $pageObj = new Page($count, config('paginate.list_rows'));// 实例化分页类 传入总记录数和每页显示的记录数
  105. $list = $this->ask_db->field('a.*, b.type_name, b.parent_id')
  106. ->alias('a')
  107. ->join('__ASK_TYPE__ b', 'a.type_id = b.type_id', 'LEFT')
  108. ->where($map)
  109. ->order('a.is_review asc, a.ask_id desc')
  110. ->limit($pageObj->firstRow . ',' . $pageObj->listRows)
  111. ->select();
  112. // 分类处理
  113. if (!empty($list)) {
  114. // 用户ID
  115. $users_ids = [];
  116. // 总分类数据
  117. $TypeData = $this->ask_type_db->getField('type_id, type_name, parent_id');
  118. foreach ($list as $key => $value) {
  119. array_push($users_ids, $value['users_id']);
  120. /*分类处理*/
  121. if (!empty($value['parent_id'])) {
  122. $list[$key]['sub_type_name'] = $value['type_name'];
  123. $list[$key]['type_name'] = $TypeData[$value['parent_id']]['type_name'];
  124. } else {
  125. $list[$key]['type_name'] = $value['type_name'];
  126. $list[$key]['sub_type_name'] = '';
  127. }
  128. /* END */
  129. /*问题状态处理*/
  130. if (0 == $value['status']) {
  131. $list[$key]['status'] = '<font color="red">未解决</font>';
  132. } else if (1 == $value['status']) {
  133. $list[$key]['status'] = '已解决';
  134. } else if (2 == $value['status']) {
  135. $list[$key]['status'] = '<font color="#cccccc">已关闭</font>';
  136. }
  137. /* END */
  138. // 访问前台url
  139. $list[$key]['HomeUrl'] = get_askurl("home/Ask/details", ['ask_id'=>$value['ask_id']]);
  140. }
  141. // 用户信息
  142. $users_list = Db::name('users')->field('users_id, username, nickname, head_pic')->where(['users_id'=>['IN', $users_ids]])->getAllWithIndex('users_id');
  143. $this->assign('users_list', $users_list);
  144. }
  145. $pageStr = $pageObj->show(); // 分页显示输出
  146. $this->assign('list', $list); // 赋值数据集
  147. $this->assign('page', $pageStr); // 赋值分页输出
  148. $this->assign('pager', $pageObj); // 赋值分页对象
  149. return $this->fetch('ask_list');
  150. }
  151. /**
  152. * 插件后台管理 - 答案列表
  153. */
  154. public function answer()
  155. {
  156. $list = array();
  157. $keywords = input('keywords/s');
  158. $map = array();
  159. if (!empty($keywords)) {
  160. $map['a.content'] = array('LIKE', "%{$keywords}%");
  161. }
  162. $count = $this->ask_answer_db->alias('a')->where($map)->count('answer_id');// 查询满足要求的总记录数
  163. $pageObj = new Page($count, config('paginate.list_rows'));// 实例化分页类 传入总记录数和每页显示的记录数
  164. $list = $this->ask_answer_db->field('a.*, b.nickname, b.username, b.head_pic')
  165. ->alias('a')
  166. ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
  167. ->where($map)
  168. ->order('a.is_review asc, a.answer_id desc')
  169. ->limit($pageObj->firstRow . ',' . $pageObj->listRows)
  170. ->select();
  171. // 用户ID
  172. $users_ids = [];
  173. foreach ($list as $key => $value) {
  174. array_push($users_ids, $value['users_id']);
  175. // 访问前台url
  176. $HomeAskUrl = get_askurl("home/Ask/details", ['ask_id'=>$value['ask_id']]);
  177. $list[$key]['HomeUrl'] = $HomeAskUrl;
  178. $HomeAskUrl .= !empty($value['answer_pid']) ? '#ul_div_li_' . $value['answer_pid'] : '#ul_div_li_' . $value['answer_id'];
  179. $list[$key]['HomeAnswerUrl'] = $HomeAskUrl;
  180. // 内容处理
  181. $preg = '/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i';
  182. $value['content'] = htmlspecialchars_decode($value['content']);
  183. $value['content'] = preg_replace($preg, '[图片]', $value['content']);
  184. $value['content'] = strip_tags($value['content']);
  185. $list[$key]['content'] = mb_strimwidth($value['content'], 0, 120, "...");
  186. }
  187. $pageStr = $pageObj->show(); // 分页显示输出
  188. $this->assign('list', $list); // 赋值数据集
  189. $this->assign('page', $pageStr); // 赋值分页输出
  190. $this->assign('pager', $pageObj); // 赋值分页对象
  191. return $this->fetch('answer');
  192. }
  193. // 删除栏目
  194. public function ask_type_del()
  195. {
  196. $type_id = input('del_id/a');
  197. $type_id = eyIntval($type_id);
  198. if (!empty($type_id)) {
  199. $result = $this->ask_type_db->where("type_id", 'IN', $type_id)->select();
  200. $title_list = get_arr_column($result, 'type_name');
  201. $r = $this->ask_type_db->where("type_id", 'IN', $type_id)->delete();
  202. if ($r) {
  203. adminLog('删除问答栏目:' . implode(',', $title_list));
  204. // 同步删除顶级栏目下的子栏目
  205. if (empty($result[0]['parent_id'])) {
  206. $this->ask_type_db->where("parent_id", 'IN', $type_id)->delete();
  207. }
  208. $this->success("删除成功!");
  209. } else {
  210. $this->error("删除失败!");
  211. }
  212. } else {
  213. $this->error("参数有误!");
  214. }
  215. }
  216. /**
  217. * 插件后台管理 - 插件配置
  218. */
  219. public function conf()
  220. {
  221. if (IS_POST) {
  222. $post = input('post.');
  223. $inc_type = 'ask';
  224. tpSetting($inc_type, $post['ask']);
  225. $functionLogic = new \app\common\logic\FunctionLogic;
  226. $functionLogic->scoreConf($post['score']);
  227. $this->success("操作成功");
  228. }
  229. $askConf = tpSetting('ask');
  230. $this->assign('askConf', $askConf);
  231. $score = getUsersConfigData('score');
  232. $this->assign('score', $score);
  233. return $this->fetch('conf');
  234. }
  235. public function level_set()
  236. {
  237. $LevelData = model('UsersLevel')->getList();
  238. $this->assign('list', $LevelData);
  239. return $this->fetch('level_set');
  240. }
  241. /**
  242. * 栏目SEO配置
  243. * @return [type] [description]
  244. */
  245. public function ask_type_seo()
  246. {
  247. $type_id = input('param.type_id/d');
  248. if (IS_POST) {
  249. if (empty($type_id)) {
  250. $this->error('操作失败');
  251. }
  252. $data = input('post.');
  253. $data['type_name'] = !empty($data['type_name']) ? trim($data['type_name']) : '';
  254. $data['seo_title'] = !empty($data['seo_title']) ? trim($data['seo_title']) : '';
  255. $data['seo_keywords'] = !empty($data['seo_keywords']) ? trim($data['seo_keywords']) : '';
  256. $data['seo_description'] = !empty($data['seo_description']) ? trim($data['seo_description']) : '';
  257. $data['update_time'] = getTime();
  258. $r = $this->ask_type_db->where('type_id', $type_id)->update($data);
  259. if (false !== $r) {
  260. $this->success('操作成功!');
  261. } else {
  262. $this->error('操作失败!');
  263. }
  264. }
  265. $info = $this->ask_type_db->where('type_id', $type_id)->find();
  266. if (empty($info)) {
  267. $this->error('数据不存在,请联系管理员!');
  268. exit;
  269. }
  270. $this->assign('info', $info);
  271. return $this->fetch('ask_type_seo');
  272. }
  273. /**
  274. * 插件后台管理 - 删除问题
  275. */
  276. public function ask_del()
  277. {
  278. $ask_id = input('del_id/a');
  279. $ask_id = eyIntval($ask_id);
  280. if (!empty($ask_id)) {
  281. $ask = Db::name('ask')->where('ask_id', 'IN', $ask_id)->select();
  282. $result = $this->ask_db->where("ask_id", 'IN', $ask_id)->select();
  283. $title_list = get_arr_column($result, 'ask_title');
  284. $r = $this->ask_db->where("ask_id", 'IN', $ask_id)->update(['is_del'=>1]);
  285. if ($r) {
  286. adminLog('删除问题:' . implode(',', $title_list));
  287. // 同步删除答案表数据
  288. $this->ask_answer_db->where("ask_id", 'IN', $ask_id)->update(['is_del'=>1]);
  289. // 同步删除点赞表数据
  290. $this->ask_answer_like_db->where("ask_id", 'IN', $ask_id)->update(['is_del'=>1]);
  291. /*afterDel start*/
  292. foreach ($ask as $key => $val) {
  293. if (!empty($val['bestanswer_id'])){
  294. continue;
  295. }
  296. $users_id = $val['users_id'];
  297. $money = $val['money'];
  298. if ($money > 0) {
  299. //退钱
  300. Db::name('users')->where('users_id', $users_id)->setInc('users_money', $money);
  301. $data = [
  302. 'ask_id' => $val['ask_id'],
  303. 'users_id' => $users_id,
  304. 'type' => 4,//悬赏退回
  305. 'money' => $money,
  306. ];
  307. Db::name('users_score')->insert($data);
  308. }
  309. }
  310. /*afterDel end*/
  311. $this->success("删除成功!");
  312. } else {
  313. $this->error("删除失败!");
  314. }
  315. } else {
  316. $this->error("参数有误!");
  317. }
  318. }
  319. /**
  320. * 插件后台管理 - 批量审核问题
  321. */
  322. public function ask_review()
  323. {
  324. $ask_id = input('ask_id/a');
  325. $ask_id = eyIntval($ask_id);
  326. if (!empty($ask_id)) {
  327. $UpData = [
  328. 'is_review' => 1,
  329. 'update_time' => getTime(),
  330. ];
  331. $r = $this->ask_db->where("ask_id", 'IN', $ask_id)->update($UpData);
  332. if ($r) {
  333. $this->success("审核成功!");
  334. } else {
  335. $this->error("审核失败!");
  336. }
  337. } else {
  338. $this->error("参数有误!");
  339. }
  340. }
  341. /**
  342. * 插件后台管理 - 批量推荐问题
  343. */
  344. public function ask_recom()
  345. {
  346. $ask_id = input('ask_id/a');
  347. $ask_id = eyIntval($ask_id);
  348. if (!empty($ask_id)) {
  349. $UpData = [
  350. 'is_recom' => 1,
  351. 'update_time' => getTime(),
  352. ];
  353. $r = $this->ask_db->where("ask_id", 'IN', $ask_id)->update($UpData);
  354. if ($r) {
  355. $this->success("审核成功!");
  356. } else {
  357. $this->error("审核失败!");
  358. }
  359. } else {
  360. $this->error("参数有误!");
  361. }
  362. }
  363. /**
  364. * 插件后台管理 - 批量删除答案
  365. */
  366. public function answer_del()
  367. {
  368. $answer_id = input('del_id/a');
  369. $answer_id = eyIntval($answer_id);
  370. if (!empty($answer_id)) {
  371. $r = $this->ask_answer_db->where("answer_id", 'IN', $answer_id)->delete();
  372. if ($r) {
  373. // 同步删除点赞表数据
  374. $this->ask_answer_like_db->where("answer_id", 'IN', $answer_id)->delete();
  375. $this->success("删除成功!");
  376. } else {
  377. $this->error("删除失败!");
  378. }
  379. } else {
  380. $this->error("参数有误!");
  381. }
  382. }
  383. /**
  384. * 插件后台管理 - 批量审核答案
  385. */
  386. public function answer_review()
  387. {
  388. $answer_id = input('ask_id/a');
  389. $answer_id = eyIntval($answer_id);
  390. if (!empty($answer_id)) {
  391. $UpData = [
  392. 'is_review' => 1,
  393. 'update_time' => getTime(),
  394. ];
  395. $r = $this->ask_answer_db->where("answer_id", 'IN', $answer_id)->update($UpData);
  396. if ($r) {
  397. $this->success("审核成功!");
  398. } else {
  399. $this->error("审核失败!");
  400. }
  401. } else {
  402. $this->error("参数有误!");
  403. }
  404. }
  405. /**
  406. * 积分级别列表
  407. * @return mixed
  408. */
  409. public function score_level()
  410. {
  411. if (IS_AJAX_POST){
  412. $post = input('post.');
  413. if (empty($post['name'])) {
  414. $this->error('至少新增一个级别名称!');
  415. } else {
  416. $is_empty = true;
  417. foreach ($post['name'] as $key => $val) {
  418. $val = trim($val);
  419. if (!empty($val)) {
  420. $is_empty = false;
  421. break;
  422. }
  423. }
  424. if (true === $is_empty) {
  425. $this->error('级别名称不能为空!');
  426. }
  427. }
  428. // 处理新增数据
  429. $AddAskData = [];
  430. foreach ($post['name'] as $key => $value) {
  431. $name = trim($value);
  432. if (empty($name)) {
  433. continue;
  434. }
  435. $id = !empty($post['id'][$key]) ? intval($post['id'][$key]) : 0;
  436. $min = !empty($post['min'][$key]) ? intval($post['min'][$key]+1) : 0;
  437. $max = !empty($post['min'][$key+1]) ? intval($post['min'][$key+1]) : 0;
  438. $AddAskData[] = [
  439. 'id' => $id,
  440. 'name' => $name,
  441. 'min' => $min,
  442. 'max' => $max,
  443. ];
  444. if (empty($id)) {
  445. unset($AddAskData[$key]['id']);
  446. }
  447. }
  448. // 添加\更新
  449. $AskScoreLevelModel = new \app\common\model\AskScoreLevel;
  450. if (!empty($AddAskData)) $ReturnId = $AskScoreLevelModel->saveAll($AddAskData);
  451. if (!empty($ReturnId)) $this->success('保存成功');
  452. $this->error('保存失败');
  453. }
  454. $list = Db::name('ask_score_level')->select();
  455. $this->assign('list', $list);
  456. return $this->fetch();
  457. }
  458. /**
  459. * 删除积分级别
  460. */
  461. public function score_level_del()
  462. {
  463. $id = input('del_id/a');
  464. $id = eyIntval($id);
  465. if (!empty($id)) {
  466. $r = Db::name('ask_score_level')->where("id", 'IN', $id)->delete();
  467. if ($r) {
  468. adminLog('删除问答积分级别表,id:' . implode(',', $id));
  469. $this->success("删除成功!");
  470. } else {
  471. $this->error("删除失败!");
  472. }
  473. } else {
  474. $this->error("参数有误!");
  475. }
  476. }
  477. // 问答模型 - 保存栏目
  478. public function ajax_ask_type_save()
  479. {
  480. if (IS_AJAX_POST) {
  481. $post = input('post.');
  482. if (empty($post['type_name'])) {
  483. $this->error('至少新增一个栏目名称!');
  484. } else {
  485. $is_empty = true;
  486. foreach ($post['type_name'] as $key => $val) {
  487. $val = trim($val);
  488. if (!empty($val)) {
  489. $is_empty = false;
  490. break;
  491. }
  492. }
  493. if (true === $is_empty) {
  494. $this->error('栏目名称不能为空!');
  495. }
  496. }
  497. // 处理新增数据
  498. $AddAskData = [];
  499. foreach ($post['type_name'] as $key => $value) {
  500. $type_name = trim($value);
  501. if (empty($type_name)) {
  502. continue;
  503. }
  504. $type_id = $post['type_id'][$key];
  505. $parent_id = $post['parent_id'][$key];
  506. $sort_order = $post['sort_order'][$key];
  507. if (empty($parent_id) || $parent_id < 0) $parent_id = 0;
  508. $AddAskData[] = [
  509. 'type_id' => $type_id,
  510. 'type_name' => $type_name,
  511. 'parent_id' => $parent_id,
  512. 'sort_order' => $sort_order,
  513. 'update_time' => getTime(),
  514. ];
  515. if (empty($type_id)) {
  516. $AddAskData[$key]['seo_description'] = '';
  517. $AddAskData[$key]['lang'] = $this->admin_lang;
  518. $AddAskData[$key]['add_time'] = getTime();
  519. unset($AddAskData[$key]['type_id']);
  520. }
  521. }
  522. // 添加\更新
  523. $AskTypeModel = new \app\common\model\AskType;
  524. if (!empty($AddAskData)) $ReturnId = $AskTypeModel->saveAll($AddAskData);
  525. // 返回
  526. if (!empty($ReturnId)) $this->success('操作成功');
  527. $this->error('操作失败');
  528. }
  529. }
  530. }