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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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-7-30
  12. */
  13. namespace app\home\controller;
  14. use think\Db;
  15. use think\Config;
  16. use app\home\logic\AskLogic;
  17. class Ask extends Base
  18. {
  19. public $users = [
  20. 'users_id' => 0,
  21. 'admin_id' => 0,
  22. 'nickname' => '游客',
  23. ];
  24. public $users_id = 0;
  25. public $nickname = '游客';
  26. public $parent_id = -1;
  27. public $users_money = 0;
  28. public $AskModel;
  29. public function _initialize()
  30. {
  31. parent::_initialize();
  32. // 问答数据层
  33. $this->AskModel = model('Ask');
  34. // 问答业务层
  35. $this->AskLogic = new AskLogic;
  36. // 用户头像处理
  37. $this->users['head_pic'] = get_head_pic();
  38. // 问题表
  39. $this->ask_db = Db::name('ask');
  40. // 答案表
  41. $this->ask_answer_db = Db::name('ask_answer');
  42. // 问题、回答、评论、回复点赞表
  43. $this->ask_answer_like_db = Db::name('ask_answer_like');
  44. /*获取最新的会员信息*/
  45. $LatestData = $this->GetUsersLatestData();
  46. if (!empty($LatestData)) {
  47. // 会员全部信息
  48. $this->users = $LatestData;
  49. // 会员ID
  50. $this->users_id = $LatestData['users_id'];
  51. // 会员昵称
  52. $this->nickname = $LatestData['nickname'];
  53. // 会员余额
  54. $this->users_money = $LatestData['users_money'];
  55. // 后台管理员信息
  56. $this->parent_id = session('admin_info.parent_id');
  57. } else {
  58. //过滤不需要登陆的行为
  59. $ctl_act = CONTROLLER_NAME . '@' . ACTION_NAME;
  60. $ctl_all = CONTROLLER_NAME . '@*';
  61. $filter_login_action = [
  62. 'Lists@index', // 问答内容列表
  63. 'Ask@index', // 问答内容列表
  64. 'Ask@details', // 问答详情
  65. ];
  66. if (!in_array($ctl_act, $filter_login_action) && !in_array($ctl_all, $filter_login_action)) {
  67. if (IS_AJAX) {
  68. if (empty($this->users)) $this->error('请先登录!');
  69. } else {
  70. if (isWeixin()) {
  71. //微信端
  72. $this->redirect('user/Users/users_select_login');
  73. } else {
  74. // 其他端
  75. $this->redirect('user/Users/login');
  76. }
  77. }
  78. }
  79. }
  80. /* END */
  81. /*加载到模板*/
  82. $this->assign('users', $this->users);
  83. $this->assign('users_id', $this->users_id);
  84. $this->assign('nickname', $this->nickname);
  85. $this->assign('AdminParentId', $this->parent_id);
  86. /* END */
  87. /*会员功能是否开启*/
  88. $msg = $logut_redirect_url = '';
  89. $this->usersConfig = getUsersConfigData('all');
  90. $web_users_switch = tpCache('web.web_users_switch');
  91. if (empty($web_users_switch) || isset($this->usersConfig['users_open_register']) && $this->usersConfig['users_open_register'] == 1) {
  92. // 前台会员中心已关闭
  93. $msg = '会员中心尚未开启!';
  94. $logut_redirect_url = ROOT_DIR . '/';
  95. } else if (session('?users_id') && empty($this->users)) {
  96. // 登录的会员被后台删除,立马退出会员中心
  97. $msg = '当前用户名不存在!';
  98. $logut_redirect_url = url('user/Users/centre');
  99. }
  100. if (!empty($logut_redirect_url)) {
  101. // 清理session并回到首页
  102. session('users_id', null);
  103. session('users', null);
  104. cookie('users_id', null);
  105. $this->error($msg, $logut_redirect_url);
  106. }
  107. /* END */
  108. }
  109. // 问答首页
  110. public function index()
  111. {
  112. $functionLogic = new \app\common\logic\FunctionLogic;
  113. $functionLogic->validate_authorfile(2);
  114. $param = input('param.');
  115. // 查询条件处理
  116. $Where = $this->AskLogic->GetAskWhere($param, $this->parent_id);
  117. // 最新问题,默认读取20条,可传入条数及字段名称进行获取
  118. $ResultAsk = $this->AskModel->GetNewAskData($Where);
  119. // Url处理
  120. $UrlData = $this->AskLogic->GetUrlData($param);
  121. // 栏目处理
  122. $TypeData = $this->AskModel->GetAskTypeData($param);
  123. // 主页SEO信息
  124. $type_id = !empty($param['type_id']) ? intval($param['type_id']) : 0;
  125. $SeoData = $this->AskLogic->GetSeoData($type_id);
  126. // 加急(悬赏)问题,默认读取10条,可传入数字进行获取
  127. $UrgentAsk = $this->AskModel->GetUrgentAskData(10);
  128. // 统计会员提问、回答、被评论、被点赞次数
  129. $CountUsersAsk = $this->AskModel->GetCountAskData($this->users_id);
  130. // 问答设置内容
  131. $AskSetting = $this->AskModel->getAskSetting($this->request->action());
  132. // 贡献榜(积分)score
  133. $ScoreList = $this->AskModel->GetOrderList();
  134. // 财富榜(金币)money
  135. // $MoneyList = $this->AskModel->GetOrderList('money');
  136. // 数组合并加载到模板
  137. $result = array_merge($ResultAsk, $UrlData, $TypeData, $SeoData, $UrgentAsk, $CountUsersAsk, $AskSetting);
  138. $result['ScoreList'] = $ScoreList;
  139. // $result['MoneyList'] = $MoneyList;
  140. // dump($result);exit;
  141. $eyou = array(
  142. 'field' => $result,
  143. );
  144. $this->assign('eyou', $eyou);
  145. return $this->fetch('ask/index');
  146. }
  147. // 问题详情页
  148. public function details()
  149. {
  150. $param = input('param.');
  151. if (empty($param['ask_id'])) $this->error('请选择浏览的问题');
  152. // 增加问题浏览点击量
  153. $this->AskModel->UpdateAskClick($param['ask_id']);
  154. // 问题详情数据
  155. $AskDetails = $this->AskModel->GetAskDetailsData($param, $this->parent_id, $this->users_id);
  156. if (0 == $AskDetails['code']) $this->error($AskDetails['msg']);
  157. // 问题回答数据,包含最佳答案
  158. $AskReplyData = $this->AskModel->GetAskReplyData($param, $this->parent_id);
  159. // 栏目处理
  160. $TypeData = $this->AskModel->GetAskTypeData($param);
  161. // 热门帖子,周榜
  162. $WeekList = $this->AskModel->GetAskWeekListData();
  163. // 热门帖子,总榜
  164. $TotalList = $this->AskModel->GetAskTotalListData();
  165. // Url处理
  166. $UrlData = $this->AskLogic->GetUrlData($param);
  167. // 加急(悬赏)问题,默认读取10条,可传入数字进行获取
  168. $UrgentAsk = $this->AskModel->GetUrgentAskData(10);
  169. // 统计会员提问、回答、被评论、被点赞次数
  170. $CountUsersAsk = $this->AskModel->GetCountAskData($this->users_id);
  171. //QQ群信息
  172. $AskSetting = $this->AskModel->getAskSetting($this->request->action());
  173. // 贡献榜(积分)score
  174. $ScoreList = $this->AskModel->GetOrderList();
  175. // 数组合并加载到模板
  176. $result = array_merge($AskDetails, $AskReplyData, $TypeData, $WeekList, $TotalList, $UrlData, $UrgentAsk, $CountUsersAsk, $AskSetting);
  177. $result['ScoreList'] = $ScoreList;
  178. $eyou = array(
  179. 'field' => $result,
  180. );
  181. $this->assign('eyou', $eyou);
  182. return $this->fetch('ask/details');
  183. }
  184. // 提交问题
  185. public function add_ask()
  186. {
  187. if (IS_AJAX_POST || IS_POST) {
  188. $param = input('param.');
  189. // 是否登录、是否允许发布问题、数据判断及处理,返回内容数据
  190. $content = $this->ParamDealWith($param);
  191. if ($param['money'] > 0 && $param['money'] > $this->users['users_money']) {
  192. $this->error('余额不足以抵扣悬赏金额,请充值!');
  193. }
  194. /*添加数据*/
  195. $AddAsk = [
  196. 'type_id' => $param['ask_type_id'],
  197. 'users_id' => $this->users_id,
  198. 'ask_title' => $param['title'],
  199. 'money' => $param['money'],
  200. 'content' => $content,
  201. 'users_ip' => clientIP(),
  202. 'add_time' => getTime(),
  203. 'update_time' => getTime(),
  204. ];
  205. // 如果这个会员组属于需要审核的,则追加
  206. if (1 == $this->users['ask_is_review']) $AddAsk['is_review'] = 0;
  207. /* END */
  208. $ResultId = $this->ask_db->add($AddAsk);
  209. if (!empty($ResultId)) {
  210. // 悬赏 积分奖励记录
  211. $this->AskModel->setScore($this->users_id, $ResultId,0,1,$param['money']);
  212. $url = $this->AskLogic->GetUrlData($param, 'NewDateUrl');
  213. if (1 == $this->users['ask_is_review']) {
  214. $this->success('发布成功,但你的问题需要管理员审核!', $url, ['review' => true]);
  215. } else {
  216. $this->success('发布成功!', $url);
  217. }
  218. } else {
  219. $this->error('发布的信息有误,请检查!');
  220. }
  221. }
  222. // 是否允许发布问题
  223. $this->IsRelease();
  224. // 栏目处理
  225. $result = $this->AskModel->GetAskTypeData(null, 'add_ask');
  226. $result['SubmitAddAsk'] = $this->AskLogic->GetUrlData(null, 'SubmitAddAsk');
  227. $result['users_money'] = $this->users_money;
  228. $result['AddAskUrl'] = $this->AskLogic->GetUrlData([], 'AddAskUrl');
  229. $result['NewDateUrl'] = $this->AskLogic->GetUrlData([], 'NewDateUrl');
  230. $result['SearchName'] = null;
  231. // 统计会员提问、回答、被评论、被点赞次数
  232. $CountUsersAsk = $this->AskModel->GetCountAskData($this->users_id);
  233. // 主页SEO信息
  234. $SeoData = $this->AskLogic->GetSeoData(0);
  235. // 问答设置内容
  236. $AskSetting = $this->AskModel->getAskSetting($this->request->action());
  237. // 数组合并加载到模板
  238. $result = array_merge($result, $CountUsersAsk, $SeoData, $AskSetting);
  239. $eyou = array(
  240. 'field' => $result,
  241. );
  242. $this->assign('eyou', $eyou);
  243. return $this->fetch('ask/add_ask');
  244. }
  245. // 编辑问题
  246. public function edit_ask()
  247. {
  248. if (IS_AJAX_POST || IS_POST) {
  249. $param = input('param.');
  250. $param['ask_id'] = intval($param['ask_id']);
  251. // 是否登录、是否允许发布问题、数据判断及处理,返回内容数据
  252. $content = $this->ParamDealWith($param, false);
  253. $ori_money = Db::name('ask')->where('ask_id', $param['ask_id'])->getField('money');
  254. if ($ori_money > $param['money']) {
  255. $this->error('悬赏金额不能小于' . $ori_money . '元!');
  256. }
  257. if ($param['money'] > 0 && $param['money'] > $this->users['users_money']) {
  258. $this->error('余额不足以抵扣悬赏金额,请充值!');
  259. }
  260. /*添加数据*/
  261. $UpAsk = [
  262. 'type_id' => $param['ask_type_id'],
  263. 'ask_title' => $param['title'],
  264. 'money' => $param['money'],
  265. 'content' => $content,
  266. 'users_ip' => clientIP(),
  267. 'update_time' => getTime(),
  268. ];
  269. // 如果这个会员组属于需要审核的,则追加
  270. if (1 == $this->users['ask_is_review']) $UpAsk['is_review'] = 0;
  271. /* END */
  272. /*条件处理*/
  273. $where['ask_id'] = $param['ask_id'];
  274. // 不是后台管理则只能修改自己的问题
  275. if (empty($this->users['admin_id'])) $where['users_id'] = $this->users_id;
  276. /* END */
  277. $ResultID = $this->ask_db->where($where)->update($UpAsk);
  278. if (!empty($ResultID)) {
  279. if ($ori_money < $param['money']) {
  280. $this->AskModel->updateMoney($this->users_id, $param['money'], $ori_money, $param['ask_id']);
  281. }
  282. $url = $this->AskLogic->GetUrlData($param, 'AskDetailsUrl');
  283. $this->success('编辑成功!', $url);
  284. } else {
  285. $this->error('编辑的信息有误,请检查!');
  286. }
  287. }
  288. // 是否允许发布问题
  289. $this->IsRelease(false);
  290. $ask_id = input('ask_id/d');
  291. $where['ask_id'] = $ask_id;
  292. // 不是后台管理则只能修改自己的问题
  293. if (empty($this->users['admin_id'])) $where['users_id'] = $this->users_id;
  294. $Info = $this->ask_db->where($where)->find();
  295. if (empty($Info)) $this->error('请选择编辑的问题!');
  296. // 栏目处理
  297. $result = $this->AskModel->GetAskTypeData($Info, 'edit_ask');
  298. $result['Info'] = $Info;
  299. $result['SubmitEditAsk'] = $this->AskLogic->GetUrlData(['ask_id' => $ask_id], 'SubmitEditAsk');
  300. $result['users_money'] = $this->users_money;
  301. $result['AddAskUrl'] = $this->AskLogic->GetUrlData([], 'AddAskUrl');
  302. $result['NewDateUrl'] = $this->AskLogic->GetUrlData([], 'NewDateUrl');
  303. $result['SearchName'] = null;
  304. // 统计会员提问、回答、被评论、被点赞次数
  305. $CountUsersAsk = $this->AskModel->GetCountAskData($this->users_id);
  306. // 主页SEO信息
  307. $SeoData = $this->AskLogic->GetSeoData(0);
  308. // 问答设置内容
  309. $AskSetting = $this->AskModel->getAskSetting($this->request->action());
  310. // 数组合并加载到模板
  311. $result = array_merge($result, $CountUsersAsk, $SeoData, $AskSetting);
  312. $eyou = array(
  313. 'field' => $result,
  314. );
  315. $this->assign('eyou', $eyou);
  316. return $this->fetch('ask/edit_ask');
  317. }
  318. // 采纳最佳答案
  319. public function ajax_best_answer()
  320. {
  321. if (IS_AJAX_POST) {
  322. $param = input('param.');
  323. // 数据判断处理
  324. if (empty($param['answer_id']) || empty($param['ask_id'])) $this->error('请选择采纳的回答');
  325. $param['ask_id'] = intval($param['ask_id']);
  326. $param['answer_id'] = intval($param['answer_id']);
  327. // 查询提问信息
  328. $AskInfo = $this->ask_db->field('users_id, status')->where('ask_id', $param['ask_id'])->find();
  329. if (!empty($this->users['admin_id']) || $AskInfo['users_id'] = input('post.users_id/d')) {
  330. // 查询问答状态
  331. $AskStatus = $this->ask_db->where('ask_id', $param['ask_id'])->getField('status');
  332. // 更新问题数据表
  333. $Updata = [
  334. 'ask_id' => $param['ask_id'],
  335. 'status' => 1,
  336. 'solve_time' => getTime(),
  337. 'bestanswer_id' => $param['answer_id'],
  338. 'update_time' => getTime()
  339. ];
  340. $ResultID = $this->ask_db->update($Updata);
  341. if (!empty($ResultID)) {
  342. // 将这个问题下的所有答案设置为非最佳答案
  343. $this->ask_answer_db->where('ask_id', $param['ask_id'])->update(['is_bestanswer' => 0]);
  344. // 设置当前问题为最佳答案
  345. $this->ask_answer_db->where('answer_id', $param['answer_id'])->update(['is_bestanswer' => 1]);
  346. // 问题未解决并存在悬赏金则执行
  347. $money = $this->ask_db->where('ask_id', $param['ask_id'])->value('money');
  348. if (isset($AskInfo['status']) && 0 == $AskInfo['status'] && 0 < $money) {
  349. $answer_user_id = $this->ask_answer_db->where('answer_id', $param['answer_id'])->value('users_id');
  350. // 悬赏金额插入记录
  351. $this->AskModel->setMoney($answer_user_id, $money, $param['ask_id'], $param['answer_id']);
  352. }
  353. $this->success('已采纳');
  354. } else {
  355. $this->error('请选择采纳的回答');
  356. }
  357. } else {
  358. $this->error('无操作权限');
  359. }
  360. }
  361. }
  362. // 添加回答
  363. public function ajax_add_answer()
  364. {
  365. if (IS_AJAX_POST || IS_POST) {
  366. $param = input('param.');
  367. // 是否登录、是否允许发布问题、数据判断及处理,返回内容数据
  368. $content = $this->AnswerDealWith($param, false);
  369. /*添加数据*/
  370. $AddAnswer = [
  371. 'ask_id' => $param['ask_id'],
  372. // 如果这个会员组属于需要审核的,则追加。 默认1为已审核
  373. 'is_review' => 1 == $this->users['ask_is_review'] ? 0 : 1,
  374. 'type_id' => $param['type_id'],
  375. 'users_id' => $this->users_id,
  376. 'username' => $this->users['username'],
  377. 'users_ip' => clientIP(),
  378. 'content' => $content,
  379. // 若是回答答案则追加数据
  380. 'answer_pid' => !empty($param['answer_id']) ? $param['answer_id'] : 0,
  381. // 用户则追加数据
  382. 'at_users_id' => !empty($param['at_users_id']) ? $param['at_users_id'] : 0,
  383. 'at_answer_id' => !empty($param['at_answer_id']) ? $param['at_answer_id'] : 0,
  384. 'add_time' => getTime(),
  385. 'update_time' => getTime(),
  386. ];
  387. $ResultID = $this->ask_answer_db->add($AddAnswer);
  388. /* END */
  389. if (!empty($ResultID)) {
  390. $ask_users_id = Db::name('ask')->where('ask_id',$param['ask_id'])->getField('users_id');
  391. if ($ask_users_id != $this->users_id){
  392. // 回答加积分 自问自答不加积分
  393. $this->AskModel->setScore($this->users_id, $param['ask_id'], $ResultID, 2);
  394. }
  395. // 增加问题回复数
  396. $this->AskModel->UpdateAskReplies($param['ask_id'], true);
  397. if (1 == $this->users['ask_is_review']) {
  398. $this->success('回答成功,但你的回答需要管理员审核!', null, ['review' => true]);
  399. } else {
  400. $AddAnswer['answer_id'] = $ResultID;
  401. $AddAnswer['head_pic'] = $this->users['head_pic'];
  402. $AddAnswer['at_usersname'] = '';
  403. if (!empty($AddAnswer['at_users_id'])) {
  404. $FindData = Db::name('users')->field('nickname, username')->where('users_id', $AddAnswer['at_users_id'])->find();
  405. $AddAnswer['at_usersname'] = empty($FindData['nickname']) ? $FindData['username'] : $FindData['nickname'];
  406. }
  407. $ResultData = $this->AskLogic->GetReplyHtml($AddAnswer);
  408. // 查询这个回答的评论回复数
  409. $ResultData['comment_reply_num'] = $this->ask_answer_db->where('answer_pid', $AddAnswer['answer_pid'])->count();
  410. $this->success('回答成功!', null, $ResultData);
  411. }
  412. } else {
  413. $this->error('提交信息有误,请刷新重试!');
  414. }
  415. }
  416. }
  417. // 编辑回答
  418. public function ajax_edit_answer()
  419. {
  420. if (IS_AJAX_POST || IS_POST) {
  421. $param = input('param.');
  422. $param['ask_id'] = intval($param['ask_id']);
  423. $param['answer_id'] = intval($param['answer_id']);
  424. // 是否登录、是否允许发布问题、数据判断及处理,返回内容数据
  425. $content = $this->AnswerDealWith($param, false);
  426. /*编辑数据*/
  427. $UpAnswerData = [
  428. 'content' => $content,
  429. 'users_ip' => clientIP(),
  430. 'update_time' => getTime(),
  431. ];
  432. // 如果这个会员组属于需要审核的,则追加
  433. if (1 == $this->users['ask_is_review']) $UpAnswerData['is_review'] = 0;
  434. /* END */
  435. // 更新条件
  436. $where = [
  437. 'answer_id' => $param['answer_id'],
  438. 'ask_id' => $param['ask_id'],
  439. ];
  440. if (empty($this->users['admin_id'])) $where['users_id'] = $this->users_id;
  441. // 更新数据
  442. $ResultId = $this->ask_answer_db->where($where)->update($UpAnswerData);
  443. if (!empty($ResultId)) {
  444. $url = $this->AskLogic->GetUrlData($param, 'AskDetailsUrl');
  445. if (1 == $this->users['ask_is_review']) {
  446. $this->success('编辑成功,但你的回答需要管理员审核!', $url, ['review' => true]);
  447. } else {
  448. $this->success('编辑成功!', $url);
  449. }
  450. } else {
  451. $this->error('编辑的信息有误,请检查!');
  452. }
  453. }
  454. // 是否允许发布问题
  455. $this->IsAnswer(false);
  456. $answer_id = input('param.answer_id/d');
  457. $where = [
  458. 'a.answer_id' => $answer_id,
  459. ];
  460. if (empty($this->users['admin_id'])) {
  461. $where['a.users_id'] = $this->users_id;
  462. }
  463. $AnswerData = $this->ask_answer_db->field('a.answer_id, a.ask_id, a.content, b.ask_title')
  464. ->alias('a')
  465. ->join('ask b', 'a.ask_id = b.ask_id', 'LEFT')
  466. ->where($where)
  467. ->find();
  468. if (empty($AnswerData)) $this->error('要修改的回答不存在!');
  469. // 更新人
  470. $AnswerData['nickname'] = $this->nickname;
  471. $result['Info'] = $AnswerData;
  472. $result['EditAnswerUrl'] = $this->AskLogic->GetUrlData(['answer_id' => $answer_id], 'EditAnswer');
  473. $result['AddAskUrl'] = $this->AskLogic->GetUrlData([], 'AddAskUrl');
  474. $result['NewDateUrl'] = $this->AskLogic->GetUrlData([], 'NewDateUrl');
  475. $result['SearchName'] = null;
  476. // 统计会员提问、回答、被评论、被点赞次数
  477. $CountUsersAsk = $this->AskModel->GetCountAskData($this->users_id);
  478. // 问答设置内容
  479. $AskSetting = $this->AskModel->getAskSetting($this->request->action());
  480. // 数组合并加载到模板
  481. $result = array_merge($result, $CountUsersAsk, $AskSetting);
  482. $eyou = array(
  483. 'field' => $result,
  484. );
  485. $this->assign('eyou', $eyou);
  486. return $this->fetch('ask/edit_answer');
  487. }
  488. // 删除指定的问题、回答、评论、回复、点赞
  489. public function ajax_del_ask()
  490. {
  491. if (IS_AJAX_POST) {
  492. // 是否登录
  493. $this->UsersIsLogin();
  494. // 数据判断
  495. $type = input('param.type/d');
  496. $ask_id = input('param.ask_id/d');
  497. if (empty($this->users['admin_id'])) $this->error('无操作权限');
  498. if (empty($ask_id) || 1 != $type) $this->error('请选择要删除的提问问题');
  499. // 执行删除
  500. $Where['ask_id'] = $ask_id;
  501. $askData = $this->ask_db->where($Where)->find();
  502. $ResultID = $this->ask_db->where($Where)->delete();
  503. if (!empty($ResultID)) {
  504. // 同步删除对应问题下所有回答
  505. $this->ask_answer_db->where($Where)->delete();
  506. // 同步删除对应问题下所有点赞
  507. $this->ask_answer_like_db->where($Where)->delete();
  508. // 更新会员提问及回答所得积分和余额
  509. $this->AskModel->RebackDel($ask_id,$askData);
  510. // 返回跳转链接
  511. $url = $this->AskLogic->GetUrlData([], 'NewDateUrl');
  512. $this->success('删除成功!', $url);
  513. } else {
  514. $this->error('删除信息错误,请刷新重试');
  515. }
  516. }
  517. }
  518. // 删除指定的回答、评论、回复、点赞
  519. public function ajax_del_answer()
  520. {
  521. if (IS_AJAX_POST) {
  522. // 是否登录
  523. $this->UsersIsLogin();
  524. // 数据判断
  525. $type = input('param.type/d');
  526. $ask_id = input('param.ask_id/d');
  527. $answer_id = input('param.answer_id/d');
  528. if (empty($ask_id) || empty($answer_id) || 2 != $type) $this->error('请选择删除内容');
  529. // 执行删除
  530. $Where = [
  531. 'ask_id' => $ask_id,
  532. 'answer_id' => $answer_id,
  533. ];
  534. // 若操作人为后台管理员则允许删除其他会员的回答、评论
  535. if (empty($this->users['admin_id'])) $Where['users_id'] = $this->users_id;
  536. // 查询整个回答评论回复共有条数的ID
  537. $CountWhere['answer_pid'] = $answer_id;
  538. $AskAnswerID = $this->ask_answer_db->where($Where)->whereOr($CountWhere)->column('answer_id');
  539. // 执行删除
  540. $ResultID = $this->ask_answer_db->where($Where)->whereOr($CountWhere)->delete();
  541. if (!empty($ResultID)) {
  542. // 同步删除对应问题下所有点赞
  543. $this->ask_answer_like_db->where('answer_id', 'IN', $AskAnswerID)->delete();
  544. // 减少问题回复数
  545. $this->AskModel->UpdateAskReplies($ask_id, false, count($AskAnswerID));
  546. $this->success('删除成功!');
  547. } else {
  548. $this->error('删除信息错误,请刷新重试');
  549. }
  550. }
  551. }
  552. // 删除问题、回答
  553. public function ajax_del_comment()
  554. {
  555. if (IS_AJAX_POST) {
  556. // 是否登录
  557. $this->UsersIsLogin();
  558. // 数据判断
  559. $type = input('param.type/d');
  560. $ask_id = input('param.ask_id/d');
  561. $answer_id = input('param.answer_id/d');
  562. if (empty($ask_id) || empty($answer_id) || 3 != $type) $this->error('请选择删除内容');
  563. // 执行删除
  564. $Where = [
  565. 'ask_id' => $ask_id,
  566. 'answer_id' => $answer_id,
  567. ];
  568. // 若操作人为后台管理员则允许删除其他会员的回答、评论
  569. if (empty($this->users['admin_id'])) $Where['users_id'] = $this->users_id;
  570. // 执行删除
  571. $ResultID = $this->ask_answer_db->where($Where)->delete();
  572. if (!empty($ResultID)) {
  573. // 同步删除对应问题下所有点赞
  574. $this->ask_answer_like_db->where('answer_id', $answer_id)->delete();
  575. // 减少问题回复数
  576. $this->AskModel->UpdateAskReplies($ask_id, false, 1);
  577. $this->success('删除成功!');
  578. } else {
  579. $this->error('删除信息错误,请刷新重试');
  580. }
  581. }
  582. }
  583. // 点赞
  584. public function ajax_click_like()
  585. {
  586. if (IS_AJAX_POST) {
  587. // 是否登录
  588. $this->UsersIsLogin();
  589. $post = input('post.');
  590. $ask_id = !empty($post['ask_id']) ? intval($post['ask_id']) : 0;
  591. $answer_id = !empty($post['answer_id']) ? intval($post['answer_id']) : 0;
  592. $like_source = !empty($post['like_source']) ? intval($post['like_source']) : 0;
  593. if (empty($like_source) || (1 == $like_source && empty($ask_id))) {
  594. $this->error('请选择点赞信息');
  595. } else if (in_array($like_source, [2, 3]) && (empty($ask_id) || empty($answer_id))) {
  596. $this->error('请选择点赞信息');
  597. }
  598. $Where = [
  599. 'ask_id' => $ask_id,
  600. 'answer_id' => $answer_id,
  601. 'users_id' => $this->users_id,
  602. 'like_source' => $like_source
  603. ];
  604. $IsCount = $this->ask_answer_like_db->where($Where)->count();
  605. if (!empty($IsCount)) {
  606. $this->error('您已赞过!');
  607. } else {
  608. // 添加新的点赞记录
  609. $AddData = [
  610. 'click_like' => 1,
  611. 'like_source' => $like_source,
  612. 'users_ip' => clientIP(),
  613. 'add_time' => getTime(),
  614. 'update_time' => getTime(),
  615. ];
  616. $AddData = array_merge($Where, $AddData);
  617. $ResultID = $this->ask_answer_like_db->add($AddData);
  618. if (!empty($ResultID)) {
  619. if (1 == $like_source) {
  620. unset($Where['users_id']);
  621. } else if (in_array($like_source, [2, 3])) {
  622. $Where = [
  623. 'answer_id' => $answer_id,
  624. 'like_source' => $like_source
  625. ];
  626. }
  627. $LikeCount = $this->ask_answer_like_db->where($Where)->count();
  628. if (1 == $LikeCount) {
  629. $LikeName = '<a href="javascript:void(0);">' . $this->nickname . '</a>';
  630. } else {
  631. $LikeName = '<a href="javascript:void(0);">' . $this->nickname . '</a>、 ';
  632. }
  633. $data = [
  634. // 点赞数
  635. 'LikeCount' => $LikeCount,
  636. // 点赞人
  637. 'LikeName' => $LikeName,
  638. ];
  639. // 同步点赞次数到答案表
  640. $Where = [
  641. 'ask_id' => $ask_id,
  642. 'answer_id' => $answer_id
  643. ];
  644. $UpdataNew = [
  645. 'click_like' => $LikeCount,
  646. 'update_time' => getTime(),
  647. ];
  648. $this->ask_answer_db->where($Where)->update($UpdataNew);
  649. $this->success('点赞成功!', null, $data);
  650. }
  651. }
  652. }
  653. }
  654. // 审核问题,仅创始人有权限
  655. public function ajax_review_ask()
  656. {
  657. if (IS_AJAX_POST) {
  658. // 创始人才有权限在前台审核评论
  659. if (0 == $this->parent_id) {
  660. $this->UsersIsLogin();
  661. $param = input('param.');
  662. if (empty($param['ask_id'])) $this->error('请选择需要审核的问题!');
  663. // 更新审核问题
  664. $UpAakData = [
  665. 'is_review' => 1,
  666. 'update_time' => getTime(),
  667. ];
  668. $ResultId = $this->ask_db->where('ask_id', intval($param['ask_id']))->update($UpAakData);
  669. if (!empty($ResultId)) $this->success('审核成功!');
  670. $this->error('审核失败!');
  671. } else {
  672. $this->error('没有操作权限!');
  673. }
  674. }
  675. }
  676. // 审核评论,仅管理员有权限
  677. public function ajax_review_comment()
  678. {
  679. if (IS_AJAX_POST) {
  680. // 创始人才有权限在前台审核评论
  681. if (0 == $this->parent_id) {
  682. $this->UsersIsLogin();
  683. $param = input('param.');
  684. if (empty($param['ask_id']) || empty($param['answer_id'])) $this->error('提交信息有误,请刷新重试!');
  685. // 更新审核评论
  686. $where = [
  687. 'ask_id' => intval($param['ask_id']),
  688. 'answer_id' => intval($param['answer_id']),
  689. ];
  690. $UpAnswerData = [
  691. 'is_review' => 1,
  692. 'update_time' => getTime(),
  693. ];
  694. $ResultId = $this->ask_answer_db->where($where)->update($UpAnswerData);
  695. if (!empty($ResultId)) $this->success('审核成功!');
  696. $this->error('审核失败!');
  697. } else {
  698. $this->error('没有操作权限!');
  699. }
  700. }
  701. }
  702. // 获取指定数量的评论数据(分页)
  703. public function ajax_show_comment()
  704. {
  705. if (IS_AJAX_POST) {
  706. $param = input('param.');
  707. $Comment = $this->AskModel->GetAskReplyData($param, $this->parent_id);
  708. $Data = !empty($param['is_comment']) ? $Comment['AnswerData'][0]['AnswerSubData'] : $Comment['BestAnswer'][0]['AnswerSubData'];
  709. if (!empty($Data)) {
  710. $ResultData = $this->AskLogic->ForeachReplyHtml($Data, $this->parent_id, $param['is_comment'], $this->users_id);
  711. if (empty($ResultData['htmlcode'])) $this->error('没有更多数据');
  712. $this->success('加载完成', null, $ResultData);
  713. } else {
  714. $this->error('没有更多数据');
  715. }
  716. }
  717. }
  718. // 加载会员中心所需数据
  719. private function AssignData($param = array())
  720. {
  721. // 是否登录
  722. $this->UsersIsLogin();
  723. // 主题颜色
  724. $this->assign('usersConfig', $this->usersConfig);
  725. $this->usersConfig['theme_color'] = $theme_color = !empty($this->usersConfig['theme_color']) ? $this->usersConfig['theme_color'] : '#ff6565'; // 默认主题颜色
  726. $this->assign('theme_color', $theme_color);
  727. // 是否为手机端
  728. $is_mobile = isMobile() ? 1 : 2;
  729. $this->assign('is_mobile', $is_mobile);
  730. // 是否为端微信
  731. $is_wechat = isWeixin() ? 1 : 2;
  732. $this->assign('is_wechat', $is_wechat);
  733. // 是否为微信端小程序
  734. $is_wechat_applets = isWeixinApplets() ? 1 : 0;
  735. $this->assign('is_wechat_applets', $is_wechat_applets);
  736. // 焦点
  737. $Focus = empty($param['method']) ? 1 : 2;
  738. $this->assign('Focus', $Focus);
  739. // 菜单名称
  740. $this->MenuTitle = Db::name('users_menu')->where([
  741. 'mca' => 'home/Ask/ask_index',
  742. 'lang' => $this->home_lang,
  743. ])->getField('title');
  744. $this->assign('MenuTitle', $this->MenuTitle);
  745. }
  746. // 是否登录
  747. private function UsersIsLogin()
  748. {
  749. if (empty($this->users) || empty($this->users_id)) $this->error('请先登录!');
  750. }
  751. // 是否允许发布、编辑问题
  752. private function IsRelease($is_add = true)
  753. {
  754. if (empty($this->users['ask_is_release'])) {
  755. if (!empty($is_add)) {
  756. $this->error($this->users['level_name'] . '不允许发布问题!');
  757. } else {
  758. $this->error($this->users['level_name'] . '不允许编辑问题!');
  759. }
  760. }
  761. }
  762. // 问题数据判断及处理,返回问题内容数据
  763. private function ParamDealWith($param = [], $is_add = true)
  764. {
  765. // 是否登录
  766. $this->UsersIsLogin();
  767. // 是否允许发布、编辑
  768. $this->IsRelease($is_add);
  769. /*数据判断*/
  770. $content = '';
  771. if (empty($param)) $this->error('请提交完整信息!');
  772. if (empty($param['title'])) $this->error('请填写问题标题!');
  773. if (empty($param['ask_type_id'])) $this->error('请选择问题分类!');
  774. $content = $this->AskLogic->ContentDealWith($param);
  775. if (empty($content)) $this->error('请填写问题描述!');
  776. // 编辑时执行判断
  777. if (empty($is_add) && empty($param['ask_id'])) $this->error('请确认编辑问题!');
  778. /* END */
  779. return $content;
  780. }
  781. // 是否允许发布、编辑评论回复
  782. private function IsAnswer($is_add = true)
  783. {
  784. if (empty($this->users['ask_is_release'])) {
  785. if (!empty($is_add)) {
  786. $this->error($this->users['level_name'] . '不允许回复答案!');
  787. } else {
  788. $this->error($this->users['level_name'] . '不允许编辑答案!');
  789. }
  790. }
  791. }
  792. // 评论回复数据处理,返回评论回复内容数据
  793. private function AnswerDealWith($param = [], $is_add = true)
  794. {
  795. // 是否登录
  796. $this->UsersIsLogin();
  797. // 是否允许发布、编辑
  798. $this->IsAnswer($is_add);
  799. /*数据判断*/
  800. if (!empty($is_add)) {
  801. // 添加时执行判断
  802. if (empty($param) || empty($param['ask_id'])) $this->error('提交信息有误,请刷新重试!');
  803. } else {
  804. // 编辑时执行判断
  805. if (empty($is_add) && empty($param['ask_id'])) $this->error('请确认编辑问题!');
  806. }
  807. $content = '';
  808. $content = $this->AskLogic->ContentDealWith($param);
  809. if (empty($content)) $this->error('请写下你的回答!');
  810. /* END */
  811. return $content;
  812. }
  813. /**
  814. * 获取登录的会员最新数据
  815. */
  816. private function GetUsersLatestData($users_id = null)
  817. {
  818. $users_id = empty($users_id) ? session('users_id') : $users_id;
  819. if (!empty($users_id)) {
  820. /*读取的字段*/
  821. $field = 'a.*, b.*, b.discount as level_discount';
  822. /* END */
  823. /*查询数据*/
  824. $users = \think\Db::name('users')->field($field)
  825. ->alias('a')
  826. ->join('__USERS_LEVEL__ b', 'a.level = b.level_id', 'LEFT')
  827. ->where([
  828. 'a.users_id' => $users_id,
  829. 'a.lang' => get_home_lang(),
  830. 'a.is_activation' => 1,
  831. 'a.is_del' => 0,
  832. ])->find();
  833. // 会员不存在则返回空
  834. if (empty($users)) return false;
  835. /* END */
  836. /*会员数据处理*/
  837. // 头像处理
  838. $users['head_pic'] = get_head_pic($users['head_pic']);
  839. // 昵称处理
  840. $users['nickname'] = empty($users['nickname']) ? $users['username'] : $users['nickname'];
  841. // 密码为空并且存在openid则表示微信注册登录,密码字段更新为0,可重置密码一次。
  842. $users['password'] = empty($users['password']) && !empty($users['open_id']) ? 0 : 1;
  843. // 删除登录密码及支付密码
  844. unset($users['paypwd']);
  845. // 级别处理
  846. $LevelData = [];
  847. if (intval($users['level_maturity_days']) >= 36600) {
  848. $users['maturity_code'] = 1;
  849. $users['maturity_date'] = '终身';
  850. } else if (0 == $users['open_level_time'] && 0 == $users['level_maturity_days']) {
  851. $users['maturity_code'] = 0;
  852. $users['maturity_date'] = '';// 没有升级会员,置空
  853. } else {
  854. /*计算剩余天数*/
  855. $days = $users['open_level_time'] + ($users['level_maturity_days'] * 86400);
  856. // 取整
  857. $days = ceil(($days - getTime()) / 86400);
  858. if (0 >= $days) {
  859. /*更新会员的级别*/
  860. $LevelData = model('EyouUsers')->UpUsersLevelData($users_id);
  861. /* END */
  862. $users['maturity_code'] = 2;
  863. $users['maturity_date'] = '';// 会员过期,置空
  864. } else {
  865. $users['maturity_code'] = 3;
  866. $users['maturity_date'] = $days . ' 天';
  867. }
  868. /* END */
  869. }
  870. /* END */
  871. // 合并数据
  872. $LatestData = array_merge($users, $LevelData);
  873. /*更新session*/
  874. session('users', $LatestData);
  875. // session('open_id', $LatestData['open_id']);
  876. session('users_id', $LatestData['users_id']);
  877. cookie('users_id', $LatestData['users_id']);
  878. /* END */
  879. // 返回数据
  880. return $LatestData;
  881. } else {
  882. // session中不存在会员ID则返回空
  883. session('users_id', null);
  884. session('users', null);
  885. cookie('users_id', null);
  886. return false;
  887. }
  888. }
  889. }