Ingen beskrivning
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 38KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. <?php
  2. /**
  3. * 易优CMS
  4. * ============================================================================
  5. * 版权所有 2016-2028 海南赞赞网络科技有限公司,并保留所有权利。
  6. * 网站地址: http://www.eyoucms.com
  7. * ----------------------------------------------------------------------------
  8. * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
  9. * ============================================================================
  10. * Author: 陈风任 <491085389@qq.com>
  11. * Date: 2019-07-30
  12. */
  13. namespace weapp\Ask\controller;
  14. use think\Page;
  15. use think\Db;
  16. use app\common\controller\Weapp;
  17. use weapp\Ask\model\AskTypeModel;
  18. use weapp\Ask\logic\AskLogic;
  19. use app\common\logic\ArctypeLogic;
  20. /**
  21. * 插件的控制器
  22. */
  23. class Ask extends Weapp
  24. {
  25. private $arctypeLogic;
  26. /**
  27. * 插件基本信息
  28. */
  29. private $weappInfo;
  30. /**
  31. * 构造方法
  32. */
  33. public function __construct(){
  34. parent::__construct();
  35. $this->arctypeLogic = new ArctypeLogic();
  36. // 问题表
  37. $this->weapp_ask_db = Db::name('weapp_ask');
  38. // 答案表
  39. $this->weapp_ask_answer_db = Db::name('weapp_ask_answer');
  40. // 点赞表
  41. $this->weapp_ask_answer_like_db = Db::name('weapp_ask_answer_like');
  42. // 问题分类表
  43. $this->weapp_ask_type_db = Db::name('weapp_ask_type');
  44. // 会员级别表
  45. $this->users_level_db = Db::name('users_level');
  46. // 问答业务层
  47. $this->AskLogic = new AskLogic;
  48. // 问答数据层
  49. $this->AskTypeModel = new AskTypeModel;
  50. /*插件基本信息*/
  51. $this->weappInfo = $this->getWeappInfo();
  52. $this->assign('weappInfo', $this->weappInfo);
  53. /*--end*/
  54. // $this->ask_logic_1658220528();
  55. }
  56. //
  57. private function ask_logic_1658220528(){
  58. $syn_ask_logic_1658220528 = Db::name('setting')->where(['name'=>'syn_ask_logic_1658220528', 'inc_type'=>'syn', 'lang'=>'cn'])->value('value');
  59. if (empty($syn_ask_logic_1658220528)){
  60. $Prefix = config('database.prefix');
  61. $isTable = Db::query('SHOW TABLES LIKE \''.$Prefix.'weapp_ask\'');
  62. if (!empty($isTable)) {
  63. $weapp_ask_typeTableInfo = Db::query("SHOW COLUMNS FROM {$Prefix}weapp_ask_type");
  64. $weapp_ask_typeTableInfo = get_arr_column($weapp_ask_typeTableInfo, 'Field');
  65. if (!empty($weapp_ask_typeTableInfo) && !in_array('seo_title', $weapp_ask_typeTableInfo)){
  66. $sql = "ALTER TABLE `{$Prefix}weapp_ask_type` ADD COLUMN `seo_title` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' AFTER `parent_id`;";
  67. @Db::execute($sql);
  68. }
  69. if (!empty($weapp_ask_typeTableInfo) && !in_array('seo_keywords', $weapp_ask_typeTableInfo)){
  70. $sql = "ALTER TABLE `{$Prefix}weapp_ask_type` ADD COLUMN `seo_keywords` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' AFTER `seo_title`;";
  71. @Db::execute($sql);
  72. }
  73. if (!empty($weapp_ask_typeTableInfo) && !in_array('seo_description', $weapp_ask_typeTableInfo)){
  74. $sql = "ALTER TABLE `{$Prefix}weapp_ask_type` ADD COLUMN `seo_description` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' AFTER `seo_keywords`;";
  75. @Db::execute($sql);
  76. }
  77. $weapp_askTableInfo = Db::query("SHOW COLUMNS FROM {$Prefix}weapp_ask");
  78. $weapp_askTableInfo = get_arr_column($weapp_askTableInfo, 'Field');
  79. if (!empty($weapp_askTableInfo) && !in_array('money', $weapp_askTableInfo)){
  80. $sql = "ALTER TABLE `{$Prefix}weapp_ask` ADD COLUMN `money` decimal(8,2) NULL DEFAULT 0.00 COMMENT '悬赏金额' AFTER `update_time`;";
  81. @Db::execute($sql);
  82. }
  83. if (!empty($weapp_askTableInfo) && !in_array('is_top', $weapp_askTableInfo)){
  84. $sql = "ALTER TABLE `{$Prefix}weapp_ask` ADD COLUMN `is_top` tinyint(1) NULL DEFAULT 0 COMMENT '是否置顶' AFTER `money`;";
  85. @Db::execute($sql);
  86. }
  87. $this->schemaAskTable('weapp_ask_type');
  88. $this->schemaAskTable('weapp_ask');
  89. tpSetting('syn', ['syn_ask_logic_1658220528'=>1], 'cn');
  90. }
  91. }
  92. }
  93. /**
  94. * 插件安装前置操作
  95. */
  96. public function beforeInstall()
  97. {
  98. $channeltypeInfo = Db::name('channeltype')->where(['id'=>51, 'status'=>1, 'is_del'=>0])->find();
  99. if (!empty($channeltypeInfo)) {
  100. $this->error('检测到已启用【问答模型】,请先禁用!');
  101. }
  102. $name = 'd2ViX2lzX2F1dGhvcnRva2Vu';
  103. $isauthor = tpCache('web.'.base64_decode($name));
  104. $domain = request()->host();
  105. $server_ip = gethostbyname($_SERVER["SERVER_NAME"]);
  106. if (preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/i', $domain) || 'localhost' == $domain || '127.0.0.1' == $server_ip || -1 != $isauthor) {
  107. } else {
  108. $file = './'.WEAPP_DIR_NAME.'/Ask';
  109. if (file_exists($file)) {
  110. delFile($file, true);
  111. }
  112. $str1 = '6Z2e5o6I5p2D572R56uZ56aB5q2i5L2/55So77yM6K+36LSt5Lmw5ZWG5Lia5o6I5p2D77yB';
  113. $this->error(base64_decode($str1));
  114. }
  115. }
  116. /**
  117. * 插件安装后置操作
  118. */
  119. public function afterInstall()
  120. {
  121. Db::name('users_menu')->where([
  122. 'mca' => 'plugins/Ask/ask_index',
  123. 'lang' => $this->admin_lang,
  124. ])->delete();
  125. Db::name('users_menu')->add([
  126. 'title' => '我的问答',
  127. 'mca' => 'plugins/Ask/ask_index',
  128. 'is_userpage' => 0,
  129. 'sort_order' => 100,
  130. 'status' => 1,
  131. 'lang' => $this->admin_lang,
  132. 'add_time' => getTime(),
  133. 'update_time' => getTime(),
  134. ]);
  135. try {
  136. $this->schemaAskTable('arctype'); // 重新生成数据表字段缓存文件
  137. // 在栏目追加一个入口
  138. $dirname = $this->arctypeLogic->get_dirname($this->weappInfo['code'], $this->weappInfo['code']);
  139. $dirname = strtolower($dirname);
  140. $saveData = [];
  141. $saveData['channeltype'] = 6;
  142. $saveData['current_channel'] = 6;
  143. $saveData['typename'] = '问答';
  144. $saveData['dirname'] = $dirname;
  145. $saveData['dirpath'] = '/' . $saveData['dirname'];
  146. $saveData['typelink'] = htmlspecialchars('/index.php?m=plugins&c=Ask&a=index');
  147. $saveData['litpic'] = '';
  148. $saveData['seo_description'] = '';
  149. $saveData['sort_order'] = 100;
  150. $saveData['is_part'] = 1;
  151. $saveData['admin_id'] = session('?admin_id') ? session('admin_id') : 0;
  152. $saveData['status'] = 1;
  153. $saveData['is_release'] = 0;
  154. $saveData['lang'] = $this->admin_lang;
  155. $saveData['weapp_code'] = $this->weappInfo['code'];
  156. $saveData['add_time'] = getTime();
  157. $typeid = model('Arctype')->addData($saveData);
  158. Db::name('arctype')->where([
  159. 'id' => $typeid,
  160. ])
  161. ->cache(true,null,"arctype")
  162. ->update([
  163. 'update_time' => getTime(),
  164. ]);
  165. /*--end*/
  166. } catch (\Exception $e) {}
  167. $this->syn_users_level();
  168. }
  169. /**
  170. * 插件卸载后置操作
  171. */
  172. public function afterUninstall()
  173. {
  174. Db::name('users_menu')->where([
  175. 'mca' => 'plugins/Ask/ask_index',
  176. 'lang' => $this->admin_lang,
  177. ])->delete();
  178. try {
  179. $arctypeRow = Db::name('arctype')->field('id')
  180. ->where([
  181. 'weapp_code' => $this->weappInfo['code'],
  182. ])->find();
  183. if (!empty($arctypeRow)) {
  184. $r = Db::name('arctype')->where([
  185. 'id' => $arctypeRow['id'],
  186. ])
  187. ->cache(true,null,"arctype")
  188. ->delete(); // 删除栏目
  189. if ($r) {
  190. model('Archives')->del([$arctypeRow['id']]); // 删除文档
  191. }
  192. }
  193. } catch (\Exception $e) {}
  194. }
  195. /**
  196. * 插件启用前置操作
  197. */
  198. public function beforeEnable()
  199. {
  200. $channeltypeInfo = Db::name('channeltype')->where(['id'=>51, 'status'=>1, 'is_del'=>0])->find();
  201. if (!empty($channeltypeInfo)) {
  202. $this->error('检测到已启用【问答模型】,请先禁用!');
  203. }
  204. }
  205. /**
  206. * 插件启用的后置操作
  207. */
  208. public function afterEnable()
  209. {
  210. Db::name('users_menu')->where([
  211. 'mca' => 'plugins/Ask/ask_index',
  212. 'lang' => $this->admin_lang,
  213. ])->update([
  214. 'status' => 1,
  215. 'update_time' => getTime(),
  216. ]);
  217. try {
  218. Db::name('arctype')->where([
  219. 'weapp_code' => $this->weappInfo['code'],
  220. ])
  221. ->cache(true,null,"arctype")
  222. ->update([
  223. 'is_hidden' => 0,
  224. 'update_time' => getTime(),
  225. ]);
  226. } catch (\Exception $e) {}
  227. }
  228. /**
  229. * 插件禁用的后置操作
  230. */
  231. public function afterDisable()
  232. {
  233. Db::name('users_menu')->where([
  234. 'mca' => 'plugins/Ask/ask_index',
  235. 'lang' => $this->admin_lang,
  236. ])->update([
  237. 'status' => 0,
  238. 'update_time' => getTime(),
  239. ]);
  240. try {
  241. Db::name('arctype')->where([
  242. 'weapp_code' => $this->weappInfo['code'],
  243. ])
  244. ->cache(true,null,"arctype")
  245. ->update([
  246. 'is_hidden' => 1,
  247. 'update_time' => getTime(),
  248. ]);
  249. } catch (\Exception $e) {}
  250. }
  251. /**
  252. * 插件使用指南
  253. */
  254. public function doc()
  255. {
  256. return $this->fetch('doc');
  257. }
  258. /**
  259. * 插件后台管理 - 问题列表
  260. */
  261. public function ask_list()
  262. {
  263. $list = array();
  264. $keywords = input('keywords/s');
  265. $keywords = trim($keywords);
  266. $map = array();
  267. if (!empty($keywords)) {
  268. $map['a.ask_title'] = array('LIKE', "%{$keywords}%");
  269. }
  270. $count = $this->weapp_ask_db->alias('a')->where($map)->count('ask_id');// 查询满足要求的总记录数
  271. $pageObj = new Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数
  272. $list = $this->weapp_ask_db->field('a.*, b.type_name, b.parent_id, c.nickname')
  273. ->alias('a')
  274. ->join('__WEAPP_ASK_TYPE__ b', 'a.type_id = b.type_id', 'LEFT')
  275. ->join('__USERS__ c', 'a.users_id = c.users_id', 'LEFT')
  276. ->where($map)
  277. ->order('a.is_review asc, a.ask_id desc')
  278. ->limit($pageObj->firstRow.','.$pageObj->listRows)
  279. ->select();
  280. // 分类处理
  281. if (!empty($list)) {
  282. // 总分类数据
  283. $TypeData = $this->weapp_ask_type_db->getField('type_id, type_name, parent_id');
  284. foreach ($list as $key => $value) {
  285. /*分类处理*/
  286. if (!empty($value['parent_id'])) {
  287. $list[$key]['sub_type_name'] = $value['type_name'];
  288. $list[$key]['type_name'] = $TypeData[$value['parent_id']]['type_name'];
  289. }else{
  290. $list[$key]['type_name'] = $value['type_name'];
  291. $list[$key]['sub_type_name'] = '';
  292. }
  293. /* END */
  294. /*问题状态处理*/
  295. if (0 == $value['status']) {
  296. $list[$key]['status'] = '未解决';
  297. }else if (1 == $value['status']) {
  298. $list[$key]['status'] = '已解决';
  299. }else if (2 == $value['status']) {
  300. $list[$key]['status'] = '已关闭';
  301. }
  302. /* END */
  303. // 访问前台url
  304. $list[$key]['HomeUrl'] = $this->request->domain().ROOT_DIR."/index.php?m=plugins&c=Ask&a=details&ask_id={$value['ask_id']}";
  305. }
  306. }
  307. $pageStr = $pageObj->show(); // 分页显示输出
  308. $this->assign('list', $list); // 赋值数据集
  309. $this->assign('pageStr', $pageStr); // 赋值分页输出
  310. $this->assign('pageObj', $pageObj); // 赋值分页对象
  311. return $this->fetch('ask_list');
  312. }
  313. /*
  314. * 更新weapp_ask_type表的seo_title、seo_keywords、seo_description字段
  315. */
  316. private function add_seo_field(){
  317. $add_seo_field_156 = tpSetting('weapp.add_seo_fields_156', [], 'cn');
  318. if (empty($add_seo_field_156)){
  319. tpSetting('weapp', ['add_seo_fields_156'=>1], 'cn');
  320. try{
  321. $Prefix = config('database.prefix');
  322. $sql = "ALTER TABLE `{$Prefix}weapp_ask_type` ADD COLUMN `seo_title` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' AFTER `parent_id`;";
  323. @Db::execute($sql);
  324. $sql = "ALTER TABLE `{$Prefix}weapp_ask_type` ADD COLUMN `seo_keywords` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' AFTER `parent_id`;";
  325. @Db::execute($sql);
  326. $sql = "ALTER TABLE `{$Prefix}weapp_ask_type` ADD COLUMN `seo_description` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' AFTER `parent_id`;";
  327. @Db::execute($sql);
  328. @unlink('./data/schema/ey_weapp_ask_type.php');
  329. }catch (\Exception $e){
  330. }
  331. }
  332. }
  333. /**
  334. * 插件后台管理 - 栏目管理
  335. */
  336. public function index()
  337. {
  338. $this->add_seo_field();
  339. $list = $this->weapp_ask_type_db->order('sort_order asc, type_id asc')->select();
  340. foreach ($list as $key => $value) {
  341. // 是否顶级栏目
  342. if($value['parent_id'] == 0){
  343. $PidData[] = $value;
  344. }else{
  345. $TidData[] = $value;
  346. }
  347. }
  348. $list_new = [];
  349. foreach ($PidData as $P_key => $PidValue) {
  350. $type_name = $PidValue['type_name'];
  351. $PidValue['type_name'] = '<input type="text" name="type_name[]" value="'.$PidValue['type_name'].'">';
  352. $PidValue['parent_name'] = '顶级栏目';
  353. /*一级栏目*/
  354. $list_new[] = $PidValue;
  355. /* END */
  356. foreach ($TidData as $T_key => $TidValue) {
  357. /*二级栏目*/
  358. if ($TidValue['parent_id'] == $PidValue['type_id']) {
  359. $TidValue['type_name'] = '|—<input type="text" name="type_name[]" value="'.$TidValue['type_name'].'">';
  360. $TidValue['parent_name'] = $type_name;
  361. $list_new[] = $TidValue;
  362. }
  363. /* END */
  364. }
  365. }
  366. $this->assign('list', $list_new);
  367. /*栏目处理*/
  368. $PidDataNew[0] = [
  369. 'type_id' => 0,
  370. 'type_name' => '顶级栏目',
  371. 'parent_id' => 0,
  372. ];
  373. $PidData = !empty($PidData) ? array_merge($PidDataNew, $PidData) : $PidDataNew;
  374. $this->assign('PidData', $PidData);
  375. /* END */
  376. /*是否有数据*/
  377. $IsEmpty = empty($list_new) ? 0 : 1;
  378. $this->assign('IsEmpty', $IsEmpty);
  379. /* END */
  380. return $this->fetch('ask_type');
  381. }
  382. /**
  383. * 插件后台管理 - 答案列表
  384. */
  385. public function answer()
  386. {
  387. $list = array();
  388. $keywords = input('keywords/s');
  389. $map = array();
  390. if (!empty($keywords)) {
  391. $map['a.content'] = array('LIKE', "%{$keywords}%");
  392. }
  393. $count = $this->weapp_ask_answer_db->alias('a')->where($map)->count('answer_id');// 查询满足要求的总记录数
  394. $pageObj = new Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数
  395. $list = $this->weapp_ask_answer_db->field('a.*, b.nickname')
  396. ->alias('a')
  397. ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
  398. ->where($map)
  399. ->order('a.is_review asc, a.answer_id desc')
  400. ->limit($pageObj->firstRow.','.$pageObj->listRows)
  401. ->select();
  402. foreach ($list as $key => $value) {
  403. // 访问前台url
  404. $HomeAskUrl = $this->request->domain().ROOT_DIR."/index.php?m=plugins&c=Ask&a=details&ask_id={$value['ask_id']}";
  405. $HomeAskUrl .= !empty($value['answer_pid']) ? '#ul_div_li_'.$value['answer_pid'] : '#ul_div_li_'.$value['answer_id'];
  406. $list[$key]['HomeUrl'] = $HomeAskUrl;
  407. // 内容处理
  408. $preg = '/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i';
  409. $value['content'] = htmlspecialchars_decode($value['content']);
  410. $value['content'] = preg_replace($preg,'[图片]',$value['content']);
  411. $value['content'] = strip_tags($value['content']);
  412. $list[$key]['content'] = mb_strimwidth($value['content'], 0, 120, "...");
  413. }
  414. $pageStr = $pageObj->show(); // 分页显示输出
  415. $this->assign('list', $list); // 赋值数据集
  416. $this->assign('pageStr', $pageStr); // 赋值分页输出
  417. $this->assign('pageObj', $pageObj); // 赋值分页对象
  418. return $this->fetch('answer');
  419. }
  420. // 新增栏目
  421. public function ask_type_data_save()
  422. {
  423. if (IS_AJAX_POST) {
  424. $post = input('post.');
  425. // 处理新增数据
  426. $AddAskData = [];
  427. foreach ($post['type_name'] as $key => $value) {
  428. $type_id = $post['type_id'][$key];
  429. $type_name = trim($value);
  430. $parent_id = $post['parent_id'][$key];
  431. $sort_order = $post['sort_order'][$key];
  432. if (empty($parent_id) && $parent_id < 0) $this->error('请选择所属栏目!');
  433. if (empty($type_name)) $this->error('分类名称不可为空');
  434. $AddAskData[] = [
  435. 'type_id' => $type_id,
  436. 'type_name' => $type_name,
  437. 'parent_id' => $parent_id,
  438. 'sort_order' => $sort_order,
  439. 'update_time' => getTime(),
  440. ];
  441. if (empty($type_id)) {
  442. $AddAskData[$key]['lang'] = $this->admin_lang;
  443. $AddAskData[$key]['add_time'] = getTime();
  444. unset($AddAskData[$key]['type_id']);
  445. }
  446. }
  447. // 添加\更新
  448. if (!empty($AddAskData)) $ReturnId = $this->AskTypeModel->saveAll($AddAskData);
  449. // 返回
  450. if (!empty($ReturnId)) $this->success('保存成功');
  451. $this->error('保存失败');
  452. }
  453. $param = input('param.');
  454. if (!empty($param['type_id']) && !empty($param['type_name'])) {
  455. // 添加二级栏目
  456. $this->assign('type_name', $param['type_name']);
  457. $this->assign('type_id', $param['type_id']);
  458. }else{
  459. $TypeData = $this->weapp_ask_type_db->where('parent_id', 0)->select();
  460. $this->assign('TypeData', $TypeData);
  461. }
  462. return $this->fetch('ask_type_add');
  463. }
  464. // 删除栏目
  465. public function ask_type_del()
  466. {
  467. $type_id = input('del_id/a');
  468. $type_id = eyIntval($type_id);
  469. if(!empty($type_id)){
  470. $result = $this->weapp_ask_type_db->where("type_id", 'IN', $type_id)->select();
  471. $title_list = get_arr_column($result, 'type_name');
  472. $r = $this->weapp_ask_type_db->where("type_id", 'IN', $type_id)->delete();
  473. if($r){
  474. adminLog('删除'.$this->weappInfo['name'].':'.implode(',', $title_list));
  475. // 同步删除顶级栏目下的子栏目
  476. if (empty($result[0]['parent_id'])) {
  477. $this->weapp_ask_type_db->where("parent_id", 'IN', $type_id)->delete();
  478. }
  479. $this->success("删除成功!");
  480. }else{
  481. $this->error("删除失败!");
  482. }
  483. }else{
  484. $this->error("参数有误!");
  485. }
  486. }
  487. /**
  488. * 插件后台管理 - 插件配置
  489. */
  490. public function conf()
  491. {
  492. // 同步会员级别列表
  493. $this->syn_users_level();
  494. $LevelData = Db::name('weapp_ask_users_level')->select();
  495. $this->assign('list',$LevelData);
  496. /*问答URL模式*/
  497. $HomeAskUrl = '';
  498. $ask_seo_pseudo = 1; // 默认动态URL
  499. $askConfData = $this->AskTypeModel->getWeappData();
  500. if (!empty($askConfData['data']['seo_pseudo'])) {
  501. $ask_seo_pseudo = intval($askConfData['data']['seo_pseudo']);
  502. }
  503. // 问答首页URL
  504. if (1 == $ask_seo_pseudo) {
  505. $HomeAskUrl = $this->root_dir.'/index.php?m=plugins&c=Ask&a=index';
  506. } else {
  507. $HomeAskUrl = url('plugins/Ask/index', [], true, false, $ask_seo_pseudo, 1);
  508. }
  509. $HomeAskUrl = str_replace($this->request->baseFile(), $this->root_dir.'/index.php', $HomeAskUrl); // 支持子目录
  510. $this->assign('HomeAskUrl',$HomeAskUrl);
  511. $this->assign('askConfData',$askConfData);
  512. /*end*/
  513. /*同步更新问答首页的URL到栏目问答的外部链接字段里*/
  514. Db::name('arctype')->where([
  515. 'weapp_code' => 'Ask',
  516. ])->cache(true,null,"arctype")->update([
  517. 'typelink' => $HomeAskUrl,
  518. 'update_time' => getTime(),
  519. ]);
  520. /*end*/
  521. return $this->fetch('conf');
  522. }
  523. /**
  524. * 同步会员级别列表
  525. * @return [type] [description]
  526. */
  527. private function syn_users_level()
  528. {
  529. $level_list = Db::name('users_level')->field('level_id,level_name,level_value,ask_is_release,ask_is_review')->where('lang', $this->admin_lang)->select();
  530. $ask_level_list = Db::name('weapp_ask_users_level')->getAllWithIndex('level_id');
  531. $saveData = $level_ids = [];
  532. foreach ($level_list as $key => $val) {
  533. array_push($level_ids, $val['level_id']);
  534. $data = [
  535. 'level_name' => $val['level_name'],
  536. 'level_value' => $val['level_value'],
  537. // 'ask_is_release' => $val['ask_is_release'],
  538. // 'ask_is_review' => $val['ask_is_review'],
  539. ];
  540. if (!empty($ask_level_list[$val['level_id']])) {
  541. $data['level_id'] = $val['level_id'];
  542. }
  543. $saveData[] = $data;
  544. }
  545. if (!empty($saveData)) {
  546. $model = new \weapp\Ask\model\AskUsersLevelModel;
  547. $model->saveAll($saveData);
  548. }
  549. $del_level_ids = [];
  550. foreach ($ask_level_list as $key => $val) {
  551. if (!in_array($val['level_id'], $level_ids)) {
  552. array_push($del_level_ids, $val['level_id']);
  553. }
  554. }
  555. if (!empty($del_level_ids)) {
  556. Db::name('weapp_ask_users_level')->where(['level_id'=>['IN', $del_level_ids]])->delete();
  557. }
  558. }
  559. public function setData()
  560. {
  561. if (IS_AJAX_POST) {
  562. $data = Db::name('weapp')->where(['code'=>'Ask'])->getField('data');
  563. if (!empty($data)) {
  564. $data = unserialize($data);
  565. } else {
  566. $data = [];
  567. }
  568. $name = input('post.name/s');
  569. $value = input('post.value/d');
  570. $data[$name] = $value;
  571. $saveData = array(
  572. 'data' => serialize($data),
  573. 'update_time' => getTime(),
  574. );
  575. $r = Db::name('weapp')->where(array('code' => $this->weappInfo['code']))->update($saveData);
  576. if ($r) {
  577. delFile(HTML_ROOT); //清除页面缓存,让整站导航及时刷新问答链接
  578. adminLog('编辑' . $this->weappInfo['name'] . '成功'); // 写入操作日志
  579. $this->success("操作成功", weapp_url('Ask/Ask/conf'));
  580. }
  581. }
  582. $this->error('操作失败');
  583. }
  584. /**
  585. * 栏目SEO配置
  586. * @return [type] [description]
  587. */
  588. public function ask_type_seo()
  589. {
  590. $type_id = input('param.type_id/d');
  591. if (IS_POST) {
  592. if (empty($type_id)) {
  593. $this->error('操作失败');
  594. }
  595. $data = input('post.');
  596. $data['update_time'] = getTime();
  597. $r = Db::name('weapp_ask_type')->where(['type_id'=>$type_id])->update($data);
  598. if ($r !== false) {
  599. $this->success('操作成功');
  600. }
  601. $this->error('操作失败');
  602. }
  603. $data = Db::name('weapp_ask_type')->find($type_id);
  604. $this->assign('data', $data);
  605. return $this->fetch('ask_type_seo');
  606. }
  607. /**
  608. * 插件后台管理 - 删除问题
  609. */
  610. public function ask_del()
  611. {
  612. $ask_id = input('del_id/a');
  613. $ask_id = eyIntval($ask_id);
  614. if(!empty($ask_id)){
  615. $result = $this->weapp_ask_db->where("ask_id", 'IN', $ask_id)->select();
  616. $title_list = get_arr_column($result, 'ask_title');
  617. $r = $this->weapp_ask_db->where("ask_id", 'IN', $ask_id)->delete();
  618. if($r){
  619. adminLog('删除'.$this->weappInfo['name'].':'.implode(',', $title_list));
  620. // 同步删除答案表数据
  621. $this->weapp_ask_answer_db->where("ask_id", 'IN', $ask_id)->delete();
  622. // 同步删除点赞表数据
  623. $this->weapp_ask_answer_like_db->where("ask_id", 'IN', $ask_id)->delete();
  624. $this->success("删除成功!");
  625. }else{
  626. $this->error("删除失败!");
  627. }
  628. }else{
  629. $this->error("参数有误!");
  630. }
  631. }
  632. /**
  633. * 插件后台管理 - 批量审核问题
  634. */
  635. public function ask_review()
  636. {
  637. $ask_id = input('ask_id/a');
  638. $ask_id = eyIntval($ask_id);
  639. if(!empty($ask_id)){
  640. $UpData = [
  641. 'is_review' => 1,
  642. 'update_time' => getTime(),
  643. ];
  644. $r = $this->weapp_ask_db->where("ask_id", 'IN', $ask_id)->update($UpData);
  645. if($r){
  646. $this->success("审核成功!");
  647. }else{
  648. $this->error("审核失败!");
  649. }
  650. }else{
  651. $this->error("参数有误!");
  652. }
  653. }
  654. /**
  655. * 插件后台管理 - 批量推荐问题
  656. */
  657. public function ask_recom()
  658. {
  659. $ask_id = input('ask_id/a');
  660. $ask_id = eyIntval($ask_id);
  661. if(!empty($ask_id)){
  662. $UpData = [
  663. 'is_recom' => 1,
  664. 'update_time' => getTime(),
  665. ];
  666. $r = $this->weapp_ask_db->where("ask_id", 'IN', $ask_id)->update($UpData);
  667. if($r){
  668. $this->success("审核成功!");
  669. }else{
  670. $this->error("审核失败!");
  671. }
  672. }else{
  673. $this->error("参数有误!");
  674. }
  675. }
  676. /**
  677. * 插件后台管理 - 批量删除答案
  678. */
  679. public function answer_del()
  680. {
  681. $answer_id = input('del_id/a');
  682. $answer_id = eyIntval($answer_id);
  683. if(!empty($answer_id)){
  684. $r = $this->weapp_ask_answer_db->where("answer_id", 'IN', $answer_id)->delete();
  685. if($r){
  686. // 同步删除点赞表数据
  687. $this->weapp_ask_answer_like_db->where("answer_id", 'IN', $answer_id)->delete();
  688. $this->success("删除成功!");
  689. }else{
  690. $this->error("删除失败!");
  691. }
  692. }else{
  693. $this->error("参数有误!");
  694. }
  695. }
  696. /**
  697. * 插件后台管理 - 批量审核答案
  698. */
  699. public function answer_review()
  700. {
  701. $answer_id = input('ask_id/a');
  702. $answer_id = eyIntval($answer_id);
  703. if(!empty($answer_id)){
  704. $UpData = [
  705. 'is_review' => 1,
  706. 'update_time' => getTime(),
  707. ];
  708. $r = $this->weapp_ask_answer_db->where("answer_id", 'IN', $answer_id)->update($UpData);
  709. if($r){
  710. $this->success("审核成功!");
  711. }else{
  712. $this->error("审核失败!");
  713. }
  714. }else{
  715. $this->error("参数有误!");
  716. }
  717. }
  718. public function getasklist($vars = [])
  719. {
  720. /*问答本身的URL模式*/
  721. $askTypeModel = new \weapp\Ask\model\AskTypeModel;
  722. $askConfData = $askTypeModel->getWeappData();
  723. $ask_seo_pseudo = 1;
  724. if (!empty($askConfData['data']['seo_pseudo'])) {
  725. $ask_seo_pseudo = intval($askConfData['data']['seo_pseudo']);
  726. }
  727. /*end*/
  728. $param = $vars;
  729. /*查询条件*/
  730. $where = [
  731. 'is_review' => 1
  732. ];
  733. if (!empty($param['typeid'])) $where['a.type_id'] = ['IN', explode(',', $param['typeid'])];
  734. if (!empty($param['notypeid'])) $where['a.type_id'] = ['NOT IN', explode(',', $param['notypeid'])];
  735. /*END*/
  736. /*排序方式*/
  737. $order = 'a.sort_order asc, a.ask_id desc';
  738. if (!empty($param['orderby'])) {
  739. $orderby = explode(',', $param['orderby']);
  740. if (!empty($orderby[1])) {
  741. $orderby = $param['orderby'];
  742. } else {
  743. $orderby = 'a.'.$param['orderby'] . ' ' . $param['orderway'];
  744. }
  745. }
  746. /*END*/
  747. $limit = !empty($param['limit']) ? intval($param['limit']) : 0;
  748. /*查询数据*/
  749. $result = Db::name('weapp_ask')->alias('a')
  750. ->field('a.*, b.type_name')
  751. ->join('__WEAPP_ASK_TYPE__ b', 'a.type_id = b.type_id', 'LEFT')
  752. ->where($where)
  753. ->order($orderby)
  754. ->limit($limit)
  755. ->select();
  756. /*END*/
  757. /*处理数据*/
  758. if (!empty($result)) {
  759. $users_ids = [];
  760. foreach ($result as $key => $value) {
  761. array_push($users_ids, $value['users_id']);
  762. $result[$key]['ask_url'] = url('plugins/Ask/details', ['ask_id'=>$value['ask_id']], true, false, $ask_seo_pseudo);
  763. $result[$key]['type_url'] = url('plugins/Ask/index', ['type_id'=>$value['type_id']], true, false, $ask_seo_pseudo);
  764. $result[$key]['content'] = htmlspecialchars_decode($value['content']);
  765. }
  766. $usersList = [];
  767. $usersRow = Db::name('users')->field('a.*, b.level_name')
  768. ->alias('a')
  769. ->join('users_level b', 'a.level=b.level_id', 'LEFT')
  770. ->where(['a.users_id'=>['IN', $users_ids]])
  771. ->select();
  772. foreach ($usersRow as $key => $val) {
  773. // 会员头像
  774. $val['head_pic'] = get_head_pic(htmlspecialchars_decode($val['head_pic']));
  775. $val['head_pic'] = func_preg_replace(['http://thirdqq.qlogo.cn'], ['https://thirdqq.qlogo.cn'], $val['head_pic']);
  776. // 会员昵称
  777. $val['nickname'] = !empty($val['nickname']) ? $val['nickname'] : $val['username'];
  778. // 过滤密码
  779. unset($val['password']);
  780. unset($val['paypwd']);
  781. $usersList[$val['users_id']]['users'] = $val;
  782. }
  783. foreach ($result as $k => $v){
  784. !empty($usersList[$v['users_id']]) && $result[$k] += $usersList[$v['users_id']];
  785. }
  786. } else {
  787. return false;
  788. }
  789. /*END*/
  790. return $result;
  791. }
  792. //设置频率
  793. public function rate_edit(){
  794. if (IS_POST) {
  795. $post = input('post.');
  796. $data = Db::name('weapp')->where(['code'=>'Ask'])->getField('data');
  797. if (!empty($data)) {
  798. $data = unserialize($data);
  799. } else {
  800. $data = [];
  801. }
  802. $data = array_merge($data, $post);
  803. $saveData = [
  804. 'data' => serialize($data),
  805. 'update_time' => getTime(),
  806. ];
  807. $r = Db::name('weapp')->where(['code'=>'Ask'])->update($saveData);
  808. if ($r !== false) {
  809. $this->success('操作成功');
  810. }
  811. $this->error('操作失败');
  812. }
  813. $data = Db::name('weapp')->where(['code'=>'Ask'])->getField('data');
  814. if (!empty($data)) {
  815. $data = unserialize($data);
  816. }
  817. $this->assign('data', $data);
  818. return $this->fetch('rate_edit');
  819. }
  820. public function seo_edit()
  821. {
  822. if (IS_POST) {
  823. $post = input('post.');
  824. $data = Db::name('weapp')->where(['code'=>'Ask'])->getField('data');
  825. if (!empty($data)) {
  826. $data = unserialize($data);
  827. } else {
  828. $data = [];
  829. }
  830. $data = array_merge($data, $post);
  831. $saveData = [
  832. 'data' => serialize($data),
  833. 'update_time' => getTime(),
  834. ];
  835. $r = Db::name('weapp')->where(['code'=>'Ask'])->update($saveData);
  836. if ($r !== false) {
  837. $this->success('操作成功');
  838. }
  839. $this->error('操作失败');
  840. }
  841. $data = Db::name('weapp')->where(['code'=>'Ask'])->getField('data');
  842. if (!empty($data)) {
  843. $data = unserialize($data);
  844. }
  845. $this->assign('data', $data);
  846. return $this->fetch('seo_edit');
  847. }
  848. /**
  849. * ajax 修改指定表数据字段 一般修改状态 比如 是否推荐 是否开启 等 图标切换的
  850. * table,id_name,id_value,field,value
  851. */
  852. public function changeTableVal()
  853. {
  854. if (IS_AJAX_POST) {
  855. $url = null;
  856. $data = [
  857. 'refresh' => 0,
  858. ];
  859. $param = input('param.');
  860. $table = input('param.table/s'); // 表名
  861. $id_name = input('param.id_name/s'); // 表主键id名
  862. $id_value = input('param.id_value/d'); // 表主键id值
  863. $field = input('param.field/s'); // 修改哪个字段
  864. $value = input('param.value/s', '', null); // 修改字段值
  865. $value = eyPreventShell($value) ? $value : strip_sql($value);
  866. /*处理数据的安全性*/
  867. if (empty($id_value)) {
  868. $this->error('查询条件id不合法!');
  869. }
  870. foreach ($param as $key => $val) {
  871. if ('value' == $key) {
  872. continue;
  873. }
  874. if (!preg_match('/^([A-Za-z0-9_-]*)$/i', $val)) {
  875. $this->error('数据含有非法入侵字符!');
  876. }
  877. }
  878. /*end*/
  879. $savedata = [
  880. $field => $value,
  881. 'update_time' => getTime(),
  882. ];
  883. // 根据条件保存修改的数据
  884. $r = Db::name($table)->where([$id_name => $id_value])->cache(true,null,$table)->save($savedata);
  885. if ($r !== false) {
  886. $this->success('更新成功', $url, $data);
  887. }
  888. $this->error('更新失败', null, []);
  889. }
  890. }
  891. //设置悬赏
  892. public function reward_edit(){
  893. if (IS_POST) {
  894. $post = input('post.');
  895. $data = Db::name('weapp')->where(['code'=>'Ask'])->getField('data');
  896. if (!empty($data)) {
  897. $data = unserialize($data);
  898. } else {
  899. $data = [];
  900. }
  901. $data = array_merge($data, $post);
  902. $saveData = [
  903. 'data' => serialize($data),
  904. 'update_time' => getTime(),
  905. ];
  906. $r = Db::name('weapp')->where(['code'=>'Ask'])->update($saveData);
  907. if ($r !== false) {
  908. $this->success('操作成功');
  909. }
  910. $this->error('操作失败');
  911. }
  912. $data = Db::name('weapp')->where(['code'=>'Ask'])->getField('data');
  913. if (!empty($data)) {
  914. $data = unserialize($data);
  915. }
  916. $this->assign('data', $data);
  917. return $this->fetch('reward_edit');
  918. }
  919. //设置敏感词过滤
  920. public function sensitive_edit(){
  921. if (IS_POST) {
  922. $post = input('post.');
  923. $data = Db::name('weapp')->where(['code'=>'Ask'])->getField('data');
  924. if (!empty($data)) {
  925. $data = unserialize($data);
  926. } else {
  927. $data = [];
  928. }
  929. $data = array_merge($data, $post);
  930. $saveData = [
  931. 'data' => serialize($data),
  932. 'update_time' => getTime(),
  933. ];
  934. $r = Db::name('weapp')->where(['code'=>'Ask'])->update($saveData);
  935. if ($r !== false) {
  936. $this->success('操作成功');
  937. }
  938. $this->error('操作失败');
  939. }
  940. $data = Db::name('weapp')->where(['code'=>'Ask'])->getField('data');
  941. if (!empty($data)) {
  942. $data = unserialize($data);
  943. }
  944. $this->assign('data', $data);
  945. return $this->fetch('sensitive_edit');
  946. }
  947. /**
  948. * 重新生成单个数据表缓存字段文件
  949. */
  950. function schemaAskTable($name)
  951. {
  952. $table = $name;
  953. $prefix = \think\Config::get('database.prefix');
  954. if (!preg_match('/^'.$prefix.'/i', $name)) {
  955. $table = $prefix.$name;
  956. }
  957. try{
  958. /*调用命令行的指令*/
  959. \think\Console::call('optimize:schema', ['--table', $table]);
  960. /*--end*/
  961. }catch (\Exception $e){}
  962. }
  963. }