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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  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\model;
  14. use think\Model;
  15. use think\Db;
  16. use think\Page;
  17. use think\Config;
  18. use app\home\logic\AskLogic;
  19. /**
  20. * 模型
  21. */
  22. class Ask extends Model
  23. {
  24. //初始化
  25. protected function initialize()
  26. {
  27. // 需要调用`Model`的`initialize`方法
  28. parent::initialize();
  29. $this->users_db = Db::name('users'); // 会员表
  30. $this->ask_db = Db::name('ask'); // 问题表
  31. $this->ask_answer_db = Db::name('ask_answer'); // 答案表
  32. $this->ask_type_db = Db::name('ask_type'); // 问题栏目分类表
  33. $this->ask_answer_like_db = Db::name('ask_answer_like'); // 问题回答点赞表
  34. $this->AskLogic = new AskLogic;
  35. }
  36. // 用户信息及问题回答数据
  37. public function GetUsersAskCount($view_uid = null)
  38. {
  39. // 返回参数
  40. $result = [];
  41. // 查询会员信息
  42. $users = session('users');
  43. if (!empty($users) && $users['users_id'] == $view_uid) {
  44. $result = [
  45. 'NickName' => $users['nickname'],
  46. 'HeadPic' => $users['head_pic'],
  47. 'IsLogin' => 1,
  48. ];
  49. } else {
  50. $UsersInfo = $this->users_db->field('nickname,head_pic')->where('users_id', $view_uid)->find();
  51. if (!empty($UsersInfo)) {
  52. $result = [
  53. 'NickName' => $UsersInfo['nickname'],
  54. 'HeadPic' => $UsersInfo['head_pic'],
  55. 'IsLogin' => 0,
  56. ];
  57. }
  58. }
  59. // 问答数量
  60. if (!empty($result['NickName'])) {
  61. // 查询问题数量
  62. $result['AskCount'] = $this->ask_db->where('users_id', $view_uid)->count();
  63. // 查询回答数量
  64. $result['AnswerCount'] = $this->ask_answer_db->where('users_id', $view_uid)->count();
  65. }
  66. // 拼装URL
  67. $result['UsersAskUrl'] = askurl('home/Ask/ask_index', ['view_uid' => $view_uid]);
  68. // ROOT_DIR.'/index.php?m=home&c=Ask&a=ask_index&view_uid='.$view_uid;
  69. $result['UsersAnswerUrl'] = askurl('home/Ask/ask_index', ['view_uid' => $view_uid, 'method' => 'answer']);
  70. // ROOT_DIR.'/index.php?m=home&c=Ask&a=ask_index&view_uid='.$view_uid.'&method=answer';
  71. return $result;
  72. }
  73. // 会员问题回答数据
  74. public function GetUsersAskData($view_uid = null, $is_ask = true)
  75. {
  76. // 返回参数
  77. $result = [];
  78. $field = 'a.ask_id, a.type_id, a.ask_title, a.click, a.replies, a.add_time, a.is_review, b.users_id, b.nickname';
  79. if (!empty($is_ask)) {
  80. // 提问问题查询列表
  81. $where = [
  82. 'a.status' => ['IN', [0, 1]],
  83. 'a.users_id' => $view_uid,
  84. ];
  85. /* 分页 */
  86. $count = $this->ask_db->alias('a')->where($where)->count('ask_id');
  87. $pageObj = new Page($count, 10);
  88. $result['pageStr'] = $pageObj->show();
  89. /* END */
  90. /*问题表数据(问题表+会员表+问题分类表)*/
  91. $result['AskData'] = $this->ask_db->field($field)
  92. ->alias('a')
  93. ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
  94. ->where($where)
  95. ->order('a.add_time desc')
  96. ->limit($pageObj->firstRow . ',' . $pageObj->listRows)
  97. ->select();
  98. /* END */
  99. /*问题回答人查询*/
  100. $ask_id = get_arr_column($result['AskData'], 'ask_id');
  101. $RepliesData = $this->ask_answer_db->field('a.ask_id, a.users_id, b.nickname')
  102. ->alias('a')
  103. ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
  104. ->where('ask_id', 'IN', $ask_id)
  105. ->select();
  106. /* END */
  107. } else {
  108. // 回答问题查询列表
  109. /*问题回答人查询*/
  110. $RepliesData = $this->ask_answer_db->field('a.ask_id, a.users_id, b.nickname')
  111. ->alias('a')
  112. ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
  113. ->select();
  114. /* END */
  115. /* 查询条件 */
  116. $UsersIds = group_same_key($RepliesData, 'users_id');
  117. $ask_ids = get_arr_column($UsersIds[$view_uid], 'ask_id');
  118. // 按主键去重
  119. $ask_ids = array_unique($ask_ids, SORT_REGULAR);
  120. $where = [
  121. 'a.status' => ['IN', [0, 1]],
  122. 'a.ask_id' => ['IN', $ask_ids],
  123. ];
  124. /* END */
  125. /* 分页 */
  126. $count = $this->ask_db->alias('a')->where($where)->count('ask_id');
  127. $pageObj = new Page($count, 10);
  128. $result['pageStr'] = $pageObj->show();
  129. /* END */
  130. /*问题表数据(问题表+会员表+问题分类表)*/
  131. $result['AskData'] = $this->ask_db->field($field)
  132. ->alias('a')
  133. ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
  134. ->where($where)
  135. ->order('a.add_time desc')
  136. ->limit($pageObj->firstRow . ',' . $pageObj->listRows)
  137. ->select();
  138. /* END */
  139. }
  140. // 取出提问问题的ID作为主键
  141. $RepliesData = group_same_key($RepliesData, 'ask_id');
  142. /*数据处理*/
  143. foreach ($result['AskData'] as $key => $value) {
  144. // 时间友好显示处理
  145. $result['AskData'][$key]['add_time'] = friend_date($value['add_time']);
  146. // 问题内容Url
  147. $result['AskData'][$key]['AskUrl'] = askurl('home/Ask/details', ['ask_id' => $value['ask_id']]);
  148. // ROOT_DIR.'/index.php?m=home&c=Ask&a=details&ask_id='.$value['ask_id'];
  149. // 回复处理
  150. if (!empty($RepliesData[$value['ask_id']])) {
  151. $UsersConut = array_values(array_unique($RepliesData[$value['ask_id']], SORT_REGULAR));
  152. $result['AskData'][$key]['UsersConut'] = '等' . count($UsersConut) . '人参与讨论';
  153. } else {
  154. $UsersConut = ['0' => ['nickname' => $value['nickname']]];
  155. $result['AskData'][$key]['UsersConut'] = $value['nickname'];
  156. }
  157. // 处理参与讨论者的A标签跳转
  158. foreach ($UsersConut as $kkk => $vvv) {
  159. $nickname = $vvv['nickname'];
  160. if (($kkk + 1) == count($UsersConut) || 2 == $kkk) {
  161. $result['AskData'][$key]['NickName'] .= '<a href="javascript:void(0);">' . $nickname . '</a>';
  162. break;
  163. } else {
  164. $result['AskData'][$key]['NickName'] .= '<a href="javascript:void(0);">' . $nickname . '</a>、';
  165. }
  166. }
  167. }
  168. /* END */
  169. return $result;
  170. }
  171. // 问题数据
  172. public function GetNewAskData($where = array(), $limit = 20, $field = null)
  173. {
  174. // 返回参数
  175. $result = [];
  176. // 没有传入则默认查询这些字段
  177. if (empty($field)) {
  178. $field = 'a.*, b.nickname, b.head_pic, c.type_name';
  179. }
  180. $result['PendingAsk'] = '';
  181. if (isset($where['a.replies']) && 0 == $where['a.replies']) {
  182. $result['PendingAsk'] = '待回答';
  183. }
  184. // 提取搜索关键词
  185. if (!empty($where['SearchName']) || null == $where['SearchName']) {
  186. $SearchName = $where['SearchName'];
  187. unset($where['SearchName']);
  188. $result['SearchName'] = $SearchName;
  189. $result['SearchNameRed'] = "搜索 <font color='red'>" . $SearchName . "</font> 结果";
  190. }
  191. if (!empty($where['a.type_id'])) {
  192. // 存在栏目ID则执行
  193. $TypeData = $this->ask_type_db->where('parent_id', $where['a.type_id'])->field('type_id')->select();
  194. if (!empty($TypeData)) {
  195. // 将顶级栏目ID合并到新条件中
  196. $type_id = get_arr_column($TypeData, 'type_id');
  197. $type_id = array_merge($type_id, array(0 => $where['a.type_id']));
  198. $where['a.type_id'] = ['IN', $type_id];
  199. }
  200. // 查询满足要求的总记录数
  201. $count = $this->ask_db->alias('a')->where($where)->count('ask_id');
  202. // 实例化分页类 传入总记录数和每页显示的记录数
  203. $pageObj = new Page($count, $limit);
  204. /*问题表数据(问题表+会员表+问题分类表)*/
  205. $result['AskData'] = $this->ask_db->field($field)
  206. ->alias('a')
  207. ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
  208. ->join('ask_type c', 'a.type_id = c.type_id', 'LEFT')
  209. ->where($where)
  210. ->order('a.ask_id desc')
  211. ->limit($pageObj->firstRow . ',' . $pageObj->listRows)
  212. ->select();
  213. // 分页显示输出
  214. $result['pageStr'] = $pageObj->show();
  215. } else {
  216. /*问题表数据(问题表+会员表+问题分类表)*/
  217. $result['AskData'] = $this->ask_db->field($field)
  218. ->alias('a')
  219. ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
  220. ->join('ask_type c', 'a.type_id = c.type_id', 'LEFT')
  221. ->where($where)
  222. ->order('a.ask_id desc')
  223. ->limit($limit)
  224. ->select();
  225. /* END */
  226. $result['pageStr'] = '';
  227. }
  228. /*问题回答人查询*/
  229. $ask_id = get_arr_column($result['AskData'], 'ask_id');
  230. $RepliesData = $this->ask_answer_db->field('a.ask_id, a.users_id, b.head_pic, b.nickname')
  231. ->alias('a')
  232. ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
  233. ->where('ask_id', 'IN', $ask_id)
  234. ->select();
  235. $RepliesDataNew = [];
  236. foreach ($RepliesData as $key => $value) {
  237. /*头像处理*/
  238. $value['head_pic'] = get_head_pic($value['head_pic']);
  239. /*个人主页URL*/
  240. $value['usershomeurl'] = usershomeurl($value['users_id']);
  241. // 将二维数组以ask_id值作为键,并归类数组,效果同等group_same_key
  242. $RepliesDataNew[$value['ask_id']][] = $value;
  243. }
  244. /* END */
  245. /*数据处理*/
  246. foreach ($result['AskData'] as $key => $value) {
  247. /*头像处理*/
  248. $value['head_pic'] = get_head_pic($value['head_pic']);
  249. $result['AskData'][$key]['head_pic'] = $value['head_pic'];
  250. /* END */
  251. // 若存在搜索关键词则标红关键词
  252. if (isset($SearchName) && !empty($SearchName)) {
  253. $result['AskData'][$key]['ask_title'] = $this->AskLogic->GetRedKeyWord($SearchName, $value['ask_title']);
  254. }
  255. // 时间友好显示处理
  256. $result['AskData'][$key]['add_time'] = friend_date($value['add_time']);
  257. // 栏目分类Url
  258. $result['AskData'][$key]['TypeUrl'] = askurl('home/Ask/index', ['type_id' => $value['type_id']]);
  259. // 问题内容Url
  260. $result['AskData'][$key]['AskUrl'] = askurl('home/Ask/details', ['ask_id' => $value['ask_id']]);
  261. // 回复处理
  262. if (!empty($RepliesDataNew[$value['ask_id']])) {
  263. $UsersConut = array_unique($RepliesDataNew[$value['ask_id']], SORT_REGULAR);
  264. // 读取前三条数据
  265. $result['AskData'][$key]['HeadPic'] = array_slice($UsersConut, 0, 3);
  266. $result['AskData'][$key]['UsersConut'] = '等' . count($UsersConut) . '人参与讨论';
  267. } else {
  268. $result['AskData'][$key]['HeadPic'] = ['0' => ['head_pic' => $value['head_pic'], 'usershomeurl'=>usershomeurl($value['users_id'])]];
  269. $result['AskData'][$key]['UsersConut'] = $value['nickname'];
  270. }
  271. // 内容处理
  272. $preg = '/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i';
  273. $value['content'] = strip_tags(preg_replace($preg, '[图片]', htmlspecialchars_decode($value['content'])));
  274. $result['AskData'][$key]['content'] = $value['content'];
  275. // $result['AskData'][$key]['content'] = mb_strimwidth($value['content'], 0, 120, "...");
  276. }
  277. /* END */
  278. // 是否推荐
  279. if (!empty($where['a.is_recom'])) {
  280. $result['IsRecom'] = 1;
  281. } else {
  282. $result['IsRecom'] = 0;
  283. }
  284. if (!empty($where['a.money'])) {
  285. $result['IsRecom'] = 3;
  286. }
  287. return $result;
  288. }
  289. // 加急问题列表(默认10条数据)
  290. public function GetUrgentAskData($limit = 10)
  291. {
  292. $field = 'ask_id, type_id, ask_title, money';
  293. $where = [
  294. 'is_del' => 0,
  295. 'is_review' => 1,
  296. 'money' => ['>', 0]
  297. ];
  298. $result['UrgentAsk'] = $this->ask_db->field($field)->where($where)->order('money desc')->limit($limit)->select();
  299. foreach ($result['UrgentAsk'] as &$value) {
  300. $value['AskUrl'] = askurl('home/Ask/details', ['ask_id' => $value['ask_id']]);
  301. }
  302. return $result;
  303. }
  304. // 统计会员提问、回答、被评论、被点赞次数
  305. public function GetCountAskData($users_id = null)
  306. {
  307. // 查询提问数据
  308. $where = [
  309. 'is_del' => 0,
  310. 'users_id' => $users_id
  311. ];
  312. $AskListID = $this->ask_db->field('ask_id')->where($where)->column('ask_id');
  313. // 统计会员所有提问数
  314. $CountAsk = count($AskListID);
  315. // 查询回答、评论、回复数据
  316. $where = [
  317. 'is_del' => 0
  318. ];
  319. $answer_list = $this->ask_answer_db->where($where)->select();
  320. $CountAnswer = $CountBeLike = $CountBeAnswer = 0;
  321. $CountBeLikeID = $CountAnswerID = [];
  322. foreach ($answer_list as $key => $value) {
  323. // 统计数量
  324. if (($users_id == $value['users_id'])) {
  325. // 统计会员所有回答数
  326. if (empty($value['answer_pid'])) $CountAnswer += 1;
  327. // 统计会员所有回答、评论、回复被点赞ID,用于查询点赞数据表中符合的点赞数
  328. if (!empty($value['click_like'])) array_push($CountBeLikeID, $value['answer_id']);
  329. // 统计会员所有回答、评论、回复ID,用于下一个foreach做被评论、被@回复计算
  330. array_push($CountAnswerID, $value['answer_id']);
  331. }
  332. }
  333. // 统计会员所有提问、回答、评论、回复被点赞数
  334. $LikeWhere = '';
  335. if (!empty($AskListID) && empty($CountBeLikeID)) {
  336. $AskListID = implode(',', $AskListID);
  337. $LikeWhere = '(`like_source` = 1 AND `ask_id` IN ('.$AskListID.') AND `users_id` <> '.$users_id.')';
  338. } else if (empty($AskListID) && !empty($CountBeLikeID)) {
  339. $CountBeLikeID = implode(',', $CountBeLikeID);
  340. $LikeWhere = '(`users_id` <> '.$users_id.' AND `answer_id` IN ('.$CountBeLikeID.'))';
  341. } else if (!empty($AskListID) && !empty($CountBeLikeID)) {
  342. $AskListID = implode(',', $AskListID);
  343. $CountBeLikeID = implode(',', $CountBeLikeID);
  344. $LikeWhere = '(`like_source` IN (2,3) AND `answer_id` IN ('.$CountBeLikeID.') AND `users_id` <> '.$users_id.') OR (`like_source` = 1 AND `ask_id` IN ('.$AskListID.') AND `users_id` <> '.$users_id.')';
  345. }
  346. $CountBeLike = !empty($LikeWhere) ? $this->ask_answer_like_db->where($LikeWhere)->count() : $CountBeLike;
  347. // 统计会员所有回答被、评论、被@回复数量,仅针对会员回答的层级下
  348. if (!empty($CountAnswerID)) {
  349. foreach ($answer_list as $value) {
  350. if (in_array($value['answer_pid'], $CountAnswerID) && $users_id != $value['users_id']) {
  351. $CountBeAnswer += 1;
  352. }
  353. }
  354. }
  355. // 返回统计结果
  356. $result['CountUsersAsk'] = [
  357. 'CountAsk' => $CountAsk,
  358. 'CountAnswer' => $CountAnswer,
  359. 'CountBeLike' => $CountBeLike,
  360. 'CountBeAnswer' => $CountBeAnswer
  361. ];
  362. return $result;
  363. }
  364. // 问题栏目分类数据
  365. public function GetAskTypeData($param = array(), $is_add = null)
  366. {
  367. // 数据初始化
  368. $result = [
  369. 'IsTypeId' => 0,
  370. 'ParentId' => 0,
  371. 'TypeId' => 0,
  372. 'TypeName' => '',
  373. 'HtmlCode' => '<span style="color: red;">请先让管理员在问答栏目列表中添加分类!</span>',
  374. 'TypeData' => []
  375. ];
  376. /*栏目处理*/
  377. $TypeData = $this->ask_type_db->order('sort_order asc, type_id asc')->select();
  378. if (empty($TypeData)) return [];
  379. foreach ($TypeData as $key => $value) {
  380. /*若存在分类ID且和数据中的值相等则执行*/
  381. if (!empty($param['type_id']) && $value['type_id'] == $param['type_id']) {
  382. $result['TypeName'] = $value['type_name'];
  383. $result['TypeId'] = $value['type_id'];
  384. $result['ParentId'] = $value['parent_id'];
  385. // 描点标记焦点
  386. if (!empty($value['parent_id'])) {
  387. $result['IsTypeId'] = $value['parent_id'];
  388. } else {
  389. $result['IsTypeId'] = $value['type_id'];
  390. }
  391. }
  392. /* END */
  393. // 是否顶级栏目
  394. if ($value['parent_id'] == 0) {
  395. $value['SelectType'] = 0;
  396. $PidData[] = $value;
  397. } else {
  398. $value['SelectSubType'] = 0;
  399. $TidData[] = $value;
  400. }
  401. }
  402. // 调用来源
  403. if (isset($is_add) && 'add_ask' == $is_add) {
  404. // 问题发布调用
  405. $return['HtmlCode'] = $this->AskLogic->GetTypeHtmlCode($PidData, $TidData, $param['type_id']);
  406. return $return;
  407. } else if (isset($is_add) && 'edit_ask' == $is_add) {
  408. // 问题编辑调用
  409. $return['HtmlCode'] = $this->AskLogic->GetTypeHtmlCode($PidData, $TidData, $param['type_id']);
  410. return $return;
  411. } else {
  412. // 列表或内容页调用
  413. $TidData = !empty($TidData) ? group_same_key($TidData, 'parent_id') : [];
  414. // 一级栏目处理
  415. foreach ($PidData as $P_key => $PidValue) {
  416. $result['TypeData'][] = $PidValue;
  417. $result['TypeData'][$P_key]['Url'] = askurl('home/Ask/index', ['type_id' => $PidValue['type_id']]);
  418. // ROOT_DIR.'/index.php?m=home&c=Ask&a=index&type_id='.$PidValue['type_id'];
  419. $result['TypeData'][$P_key]['SubType'] = [];
  420. if (!empty($TidData[$PidValue['type_id']])) {
  421. // 所属子栏目处理
  422. foreach ($TidData[$PidValue['type_id']] as $T_key => $TidValue) {
  423. if ($TidValue['parent_id'] == $PidValue['type_id']) {
  424. array_push($result['TypeData'][$P_key]['SubType'], $TidValue);
  425. $result['TypeData'][$P_key]['SubType'][$T_key]['Url'] = askurl('home/Ask/index', ['type_id' => $TidValue['type_id']]);
  426. // ROOT_DIR.'/index.php?m=home&c=Ask&a=index&type_id='.$TidValue['type_id'];
  427. }
  428. }
  429. }
  430. }
  431. }
  432. /* END */
  433. return $result;
  434. }
  435. // 周榜
  436. public function GetAskWeekListData()
  437. {
  438. $time1 = strtotime(date('Y-m-d H:i:s', time()));
  439. $time2 = $time1 - (86400 * 7);
  440. $where = [
  441. 'a.add_time' => ['between time', [$time2, $time1]],
  442. 'a.is_review' => 1,
  443. ];
  444. $result['WeekList'] = [];
  445. $WeekList = $this->ask_db->field('a.ask_id, a.type_id, a.ask_title, a.click, a.replies, b.head_pic, c.type_name')
  446. ->alias('a')
  447. ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
  448. ->join('__ASK_TYPE__ c', 'c.type_id = a.type_id', 'LEFT')
  449. ->order('click desc, replies desc')
  450. ->where($where)
  451. ->limit('0, 10')
  452. ->select();
  453. if (empty($WeekList)) return $result;
  454. foreach ($WeekList as $key => $value) {
  455. // 内页详情URL
  456. $result['WeekList'][$key]['AskUrl'] = askurl('home/Ask/details', ['ask_id' => $value['ask_id']]);
  457. // 问题标题
  458. $result['WeekList'][$key]['ask_title'] = $value['ask_title'];
  459. // 问答栏目板块URL
  460. $result['WeekList'][$key]['type_url'] = askurl('home/Ask/index', ['type_id' => $value['type_id']]);
  461. // 问答栏目板块名称
  462. $result['WeekList'][$key]['type_name'] = $value['type_name'];
  463. // 回答数
  464. $result['WeekList'][$key]['replies'] = $value['replies'];
  465. }
  466. return $result;
  467. }
  468. // 总榜
  469. public function GetAskTotalListData()
  470. {
  471. $result['TotalList'] = [];
  472. $TotalList = $this->ask_db->field('a.ask_id, a.type_id, a.ask_title, a.click, a.replies, b.head_pic')
  473. ->alias('a')
  474. ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
  475. ->order('click desc, replies desc')
  476. ->where('a.is_review', 1)
  477. ->limit('0, 10')
  478. ->select();
  479. if (empty($TotalList)) {
  480. return $result;
  481. }
  482. foreach ($TotalList as $key => $value) {
  483. $result['TotalList'][$key]['AskUrl'] = askurl('home/Ask/details', ['ask_id' => $value['ask_id']]);
  484. $result['TotalList'][$key]['ask_title'] = $value['ask_title'];
  485. }
  486. return $result;
  487. }
  488. // 问题详情数据
  489. public function GetAskDetailsData($param = array(), $parent_id = null, $users_id = null)
  490. {
  491. $info = $this->ask_db->field('a.*, b.username, b.nickname, b.head_pic, c.type_name')
  492. ->alias('a')
  493. ->join('ask_type c', 'c.type_id = a.type_id', 'LEFT')
  494. ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
  495. ->where('ask_id', $param['ask_id'])
  496. ->find();
  497. if (empty($info)) return ['code' => 0, 'msg' => '浏览的问题不存在'];
  498. if (0 !== $parent_id && 0 == $info['is_review'] && $info['users_id'] !== $users_id) {
  499. return ['code' => 0, 'msg' => '问题未审核通过,暂时不可浏览'];
  500. }
  501. // 问答栏目板块URL
  502. $info['type_url'] = askurl('home/Ask/index', ['type_id' => $info['type_id']]);
  503. // 问题的回答数
  504. $info['ques_answer_count'] = $this->ask_answer_db->where(['answer_pid'=>0, 'ask_id'=>$info['ask_id']])->count();
  505. // 问题的点赞数
  506. $info['ques_like_count'] = $this->ask_answer_like_db->where(['ask_id'=>$info['ask_id'], 'like_source'=>1])->count();
  507. // 头像处理
  508. $info['head_pic'] = get_head_pic($info['head_pic']);
  509. // 时间友好显示处理
  510. $info['add_time'] = friend_date($info['add_time']);
  511. // 处理格式
  512. $info['content'] = htmlspecialchars_decode($info['content']);
  513. // seo信息
  514. $info['seo_title'] = $info['ask_title'].' - '.$info['type_name'];
  515. $info['seo_keywords'] = $info['ask_title'];
  516. $info['seo_description'] = @msubstr(checkStrHtml($info['content']), 0, config('global.arc_seo_description_length'), false);
  517. // 个人主页URL
  518. $info['usershomeurl'] = usershomeurl($info['users_id']);
  519. // 返回数据
  520. $ResultData = [
  521. 'code' => 1,
  522. 'info' => $info,
  523. // 搜索的内容
  524. 'SearchName' => null,
  525. // 登录者是否为问题的发布者
  526. 'IsUsers' => $info['users_id'] == session('users_id') ? 1 : 0
  527. ];
  528. return $ResultData;
  529. }
  530. // 问题回答数据
  531. public function GetAskReplyData($param = array(), $parent_id = null)
  532. {
  533. /*查询条件*/
  534. $WhereOr = [];
  535. $RepliesWhere = ['ask_id' => $param['ask_id'], 'is_review' => 1];
  536. $bestanswer_id = $this->ask_db->where('ask_id', $param['ask_id'])->getField('bestanswer_id');
  537. if (!empty($param['answer_id'])) {
  538. $WhereOr = ['answer_pid' => $param['answer_id']];
  539. $RepliesWhere = ['answer_id' => $param['answer_id'], 'is_review' => 1];
  540. }
  541. // 若为则创始人则去除仅查询已审核评论这个条件,$parent_id = 0 表示为创始人
  542. if (isset($parent_id) && 0 == $parent_id) unset($RepliesWhere['is_review']);
  543. /* END */
  544. /*评论读取条数*/
  545. $firstRow = !empty($param['firstRow']) ? $param['firstRow'] : 0;
  546. $listRows = !empty($param['listRows']) ? $param['listRows'] : 5;
  547. $result['firstRow'] = $firstRow;
  548. $result['listRows'] = $listRows;
  549. /* END */
  550. /*排序*/
  551. $OrderBy = !empty($param['click_like']) ? 'a.click_like ' . $param['click_like'] . ', a.add_time asc' : 'a.add_time asc';
  552. $click_like = isset($param['click_like']) ? $param['click_like'] : '';
  553. $result['SortOrder'] = 'desc' == $click_like ? 'asc' : 'desc';
  554. /* END */
  555. /*评论回答*/
  556. $RepliesData = $this->ask_answer_db->field('a.*, b.head_pic, b.nickname, c.nickname as `at_usersname`')
  557. ->alias('a')
  558. ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
  559. ->join('__USERS__ c', 'a.at_users_id = c.users_id', 'LEFT')
  560. ->order($OrderBy)
  561. ->where($RepliesWhere)
  562. ->whereOr($WhereOr)
  563. ->select();
  564. if (empty($RepliesData)) return [];
  565. /* END */
  566. /*点赞数据*/
  567. $AnswerIds = get_arr_column($RepliesData, 'answer_id');
  568. $AnswerLikeData = $this->ask_answer_like_db->field('a.*, b.nickname')
  569. ->alias('a')
  570. ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
  571. ->order('like_id desc')
  572. ->where('answer_id', 'IN', $AnswerIds)
  573. ->select();
  574. $AnswerLikeData = group_same_key($AnswerLikeData, 'answer_id');
  575. /* END */
  576. /*回答处理*/
  577. $PidData = $AnswerData = [];
  578. foreach ($RepliesData as $key => $value) {
  579. // 友好显示时间
  580. $value['add_time'] = friend_date($value['add_time']);
  581. // 处理格式
  582. $value['content'] = htmlspecialchars_decode($value['content']);
  583. // 头像处理
  584. $value['head_pic'] = get_head_pic($value['head_pic']);
  585. // 个人主页URL
  586. $value['usershomeurl'] = usershomeurl($value['users_id']);
  587. // 会员昵称
  588. $value['nickname'] = !empty($value['nickname']) ? $value['nickname'] : $value['username'];
  589. // 是否上一级回答
  590. if ($value['answer_pid'] == 0) {
  591. $PidData[] = $value;
  592. } else {
  593. $AnswerData[] = $value;
  594. }
  595. }
  596. /* END */
  597. /*一级回答*/
  598. foreach ($PidData as $key => $PidValue) {
  599. $result['AnswerData'][] = $PidValue;
  600. // 子回答
  601. $result['AnswerData'][$key]['AnswerSubData'] = [];
  602. // 点赞数据
  603. $result['AnswerData'][$key]['AnswerLike'] = [];
  604. /*所属子回答处理*/
  605. foreach ($AnswerData as $AnswerValue) {
  606. if ($AnswerValue['answer_pid'] == $PidValue['answer_id']) {
  607. array_push($result['AnswerData'][$key]['AnswerSubData'], $AnswerValue);
  608. }
  609. }
  610. // 回答的评论回复数
  611. $result['AnswerData'][$key]['CommentReplyNum'] = count($result['AnswerData'][$key]['AnswerSubData']);
  612. /* END */
  613. /*读取指定数据*/
  614. // 以是否审核排序,审核的优先
  615. array_multisort(get_arr_column($result['AnswerData'][$key]['AnswerSubData'], 'is_review'), SORT_DESC, $result['AnswerData'][$key]['AnswerSubData']);
  616. // 读取指定条数
  617. $result['AnswerData'][$key]['AnswerSubData'] = array_slice($result['AnswerData'][$key]['AnswerSubData'], $firstRow, $listRows);
  618. /* END */
  619. $result['AnswerData'][$key]['AnswerLike']['LikeNum'] = null;
  620. $result['AnswerData'][$key]['AnswerLike']['LikeName'] = null;
  621. /*点赞处理*/
  622. foreach ($AnswerLikeData as $LikeKey => $LikeValue) {
  623. if ($PidValue['answer_id'] == $LikeKey) {
  624. // 点赞总数
  625. $LikeNum = count($LikeValue);
  626. $result['AnswerData'][$key]['AnswerLike']['LikeNum'] = $LikeNum;
  627. for ($i = 0; $i < $LikeNum; $i++) {
  628. // 获取前三个点赞人处理后退出本次for
  629. if ($i > 2) break;
  630. // 点赞人用户名\昵称
  631. $LikeName = $LikeValue[$i]['nickname'];
  632. // 在第二个数据前加入顿号,拼装a链接
  633. if ($i != 0) {
  634. $LikeName = ' 、<a href="javascript:void(0);">' . $LikeName . '</a>';
  635. } else {
  636. $LikeName = '<a href="javascript:void(0);">' . $LikeName . '</a>';
  637. }
  638. $result['AnswerData'][$key]['AnswerLike']['LikeName'] .= $LikeName;
  639. }
  640. }
  641. }
  642. /* END */
  643. }
  644. /* END */
  645. /*最佳答案数据*/
  646. foreach ($result['AnswerData'] as $key => $value) {
  647. if ($bestanswer_id == $value['answer_id']) {
  648. $result['BestAnswer'][$key] = $value;
  649. unset($result['AnswerData'][$key]);
  650. }
  651. }
  652. /* NED */
  653. // 统计回答数
  654. $result['AnswerCount'] = count($RepliesData);
  655. return $result;
  656. }
  657. // 操作问题表回复数
  658. public function UpdateAskReplies($ask_id = null, $IsAdd = true, $DelNum = 0)
  659. {
  660. if (empty($ask_id)) return false;
  661. if (!empty($IsAdd)) {
  662. $Updata = [
  663. 'replies' => Db::raw('replies+1'),
  664. 'update_time' => getTime(),
  665. ];
  666. } else {
  667. $Updata = [
  668. 'replies' => Db::raw('replies-1'),
  669. 'update_time' => getTime(),
  670. ];
  671. if ($DelNum > 0) $Updata['replies'] = Db::raw('replies-' . $DelNum);
  672. }
  673. $this->ask_db->where('ask_id', $ask_id)->update($Updata);
  674. }
  675. // 增加问题浏览点击量
  676. public function UpdateAskClick($ask_id = null)
  677. {
  678. if (empty($ask_id)) return false;
  679. $Updata = [
  680. 'click' => Db::raw('click+1'),
  681. 'update_time' => getTime(),
  682. ];
  683. $this->ask_db->where('ask_id', $ask_id)->update($Updata);
  684. }
  685. // 会员中心--问题中心--我的问题\回复
  686. public function GetUsersAskDataNew($view_uid = null, $is_ask = true)
  687. {
  688. // 返回参数
  689. $result = [];
  690. if (!empty($is_ask)) {
  691. // 提问问题查询列表
  692. /*查询字段*/
  693. $field = 'a.ask_id, a.ask_title, a.click, a.replies, a.add_time, a.is_review, b.type_name';
  694. /* END */
  695. /*查询条件*/
  696. $where = [
  697. 'a.status' => ['IN', [0, 1]],
  698. 'a.users_id' => $view_uid,
  699. ];
  700. /* END */
  701. /* 分页 */
  702. $count = $this->ask_db->alias('a')->where($where)->count('ask_id');
  703. $pageObj = new Page($count, 10);
  704. $result['pageStr'] = $pageObj->show();
  705. /* END */
  706. /*问题表数据(问题表+会员表+问题分类表)*/
  707. $result['AskData'] = $this->ask_db->field($field)
  708. ->alias('a')
  709. ->join('ask_type b', 'a.type_id = b.type_id', 'LEFT')
  710. ->where($where)
  711. ->order('a.add_time desc')
  712. ->limit($pageObj->firstRow . ',' . $pageObj->listRows)
  713. ->select();
  714. /* END */
  715. } else {
  716. // 回答问题查询列表
  717. /*查询字段*/
  718. $field = 'a.*, b.ask_title';
  719. /* END */
  720. /*查询条件*/
  721. $where = [
  722. 'a.users_id' => $view_uid,
  723. ];
  724. /* END */
  725. /* 分页 */
  726. $count = $this->ask_answer_db->alias('a')->where($where)->count('answer_id');
  727. $pageObj = new Page($count, 5);
  728. $result['pageStr'] = $pageObj->show();
  729. /* END */
  730. /*问题回答人查询*/
  731. $result['AskData'] = $this->ask_answer_db->field($field)
  732. ->alias('a')
  733. ->join('ask b', 'a.ask_id = b.ask_id', 'LEFT')
  734. ->where($where)
  735. ->order('a.add_time desc')
  736. ->limit($pageObj->firstRow . ',' . $pageObj->listRows)
  737. ->select();
  738. /* END */
  739. }
  740. /*数据处理*/
  741. foreach ($result['AskData'] as $key => $value) {
  742. // 问题内容Url
  743. $result['AskData'][$key]['AskUrl'] = askurl('home/Ask/details', ['ask_id' => $value['ask_id']]);
  744. // ROOT_DIR.'/index.php?m=home&c=Ask&a=details&ask_id='.$value['ask_id'];
  745. if (empty($is_ask)) {
  746. $result['AskData'][$key]['AskUrl'] .= !empty($value['answer_pid']) ? '#ul_div_li_' . $value['answer_pid'] : '#ul_div_li_' . $value['answer_id'];
  747. }
  748. if (isset($value['answer_id']) && !empty($value['answer_id'])) {
  749. $preg = '/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i';
  750. $value['content'] = htmlspecialchars_decode($value['content']);
  751. $value['content'] = preg_replace($preg, '[图片]', $value['content']);
  752. $value['content'] = strip_tags($value['content']);
  753. $result['AskData'][$key]['content'] = $value['content'];
  754. }
  755. }
  756. /* END */
  757. return $result;
  758. }
  759. //贡献榜(积分)score 财富榜(金币)money
  760. public function GetOrderList($type = 'score')
  761. {
  762. if ($type == 'score') {
  763. $list = Db::name('users')
  764. ->field('nickname,head_pic,users_id,scores,devote')
  765. ->order('scores desc')
  766. ->limit(10)
  767. ->select();
  768. $level = Db::name('ask_score_level')->order('min', 'asc')->select();
  769. foreach ($list as $key => $val) {
  770. foreach ($level as $k => $v) {
  771. if ($val['scores'] >= $v['min'] && ( $val['scores'] <= $v['max'] || $v['max'] == 0)) {
  772. $val['level'] = $v['name'];
  773. } else {
  774. continue;
  775. }
  776. }
  777. if (!empty($val['head_pic'])) {
  778. $val['head_pic'] = handle_subdir_pic($val['head_pic']);
  779. } else {
  780. $val['head_pic'] = get_head_pic();
  781. }
  782. $val['usershomeurl'] = usershomeurl($val['users_id']); // 个人主页
  783. $list[$key] = $val;
  784. }
  785. } elseif ($type == 'money') {
  786. $list = Db::name('users_score')
  787. ->field('sum(money) as money,users_id')
  788. ->where('type', 3)
  789. ->group('users_id')
  790. ->order('money desc')
  791. ->limit(10)
  792. ->select();
  793. if (!empty($list)) {
  794. $ids = [];
  795. foreach ($list as $k => $v) {
  796. $ids[] = $v['users_id'];
  797. }
  798. $users_data = Db::name('users')
  799. ->field('nickname,head_pic,users_id,devote,scores')
  800. ->where('users_id', 'in', $ids)
  801. ->getAllWithIndex('users_id');
  802. $level = Db::name('ask_score_level')->order('min', 'asc')->select();
  803. if (!empty($users_data)) {
  804. foreach ($list as $key => $val) {
  805. $val = array_merge($val, $users_data[$val['users_id']]);
  806. if (!empty($v['head_pic'])) {
  807. $val['head_pic'] = handle_subdir_pic($val['head_pic']);
  808. } else {
  809. $val['head_pic'] = get_head_pic();
  810. }
  811. foreach ($level as $k => $v) {
  812. if ($val['scores'] >= $v['min'] && ( $val['scores'] <= $v['max'] || $v['max'] == 0)) {
  813. $val['level'] = $v['name'];
  814. } else {
  815. continue;
  816. }
  817. }
  818. $val['usershomeurl'] = usershomeurl($val['users_id']); // 个人主页
  819. $list[$key] = $val;
  820. }
  821. }
  822. }
  823. }
  824. return $list;
  825. }
  826. /**
  827. * 增加余额(设为最佳答案)$act='inc'
  828. */
  829. public function setMoney($users_id = 0, $money = 0, $ask_id = 0, $reply_id = 0)
  830. {
  831. Db::name('users')->where('users_id', $users_id)->setInc('users_money', $money);
  832. $data = [
  833. 'ask_id' => $ask_id,
  834. 'reply_id' => $reply_id,
  835. 'users_id' => $users_id,
  836. 'money' => $money,
  837. 'info' => '最佳答案',
  838. 'type' => 3,
  839. 'add_time' => getTime(),
  840. 'update_time' => getTime(),
  841. ];
  842. Db::name('users_score')->insert($data);
  843. }
  844. /**
  845. * 编辑问题增加悬赏金额
  846. */
  847. public function updateMoney($users_id = 0, $money = 0, $ori_money = 0, $ask_id = 0)
  848. {
  849. Db::name('users')->where('users_id', $users_id)->setDec('users_money', $money - $ori_money);
  850. $data = [
  851. 'money' => $money,
  852. 'update_time' => getTime(),
  853. ];
  854. Db::name('users_score')->where(['ask_id' => $ask_id, 'users_id' => $users_id, 'type' => 1])->update($data);
  855. }
  856. /**
  857. * 添加赠送积分记录
  858. * $type 1-提问 2-回答
  859. */
  860. public function setScore($users_id = 0, $ask_id = 0, $reply_id = 0, $type = 1, $money = 0)
  861. {
  862. //先判断是否开启问答送积分
  863. $data = getUsersConfigData('score');
  864. if (empty($data['score_ask_status']) && $money == 0) {
  865. //未开启问答送积分+没有悬赏
  866. return false;
  867. }
  868. if ($type == 1) {
  869. $score = $data['score_ask_score'];
  870. $count = $data['score_ask_count'];
  871. $info = '问答提问';
  872. } elseif ($type == 2) {
  873. $score = $data['score_reply_score'];
  874. $count = $data['score_reply_count'];
  875. $info = '问答回答';
  876. }
  877. if (empty($data['score_ask_status'])) {
  878. $score = 0;
  879. }
  880. if (!empty($count) && 0 < $count) {
  881. //有次数限制 判断当日次数是是否已用完
  882. $counts = Db::name('users_score')
  883. ->where('type', $type)
  884. ->where('users_id', $users_id)
  885. ->where('score', '>', 0)
  886. ->whereTime('add_time', 'today')
  887. ->count();
  888. if ($counts >= $count) {
  889. if ((1 == $type && $money == 0) || 2 == $type) {
  890. return false;
  891. } elseif (1 == $type && !empty($money)) {
  892. $score = 0;
  893. }
  894. }
  895. }
  896. $data = [
  897. 'ask_id' => $ask_id,
  898. 'reply_id' => $reply_id,
  899. 'users_id' => $users_id,
  900. 'money' => -$money,
  901. 'info' => $info,
  902. 'score' => !empty($score) ? '+' . $score : $score,
  903. 'devote' => $score,
  904. 'type' => $type,
  905. 'add_time' => getTime(),
  906. 'update_time' => getTime(),
  907. ];
  908. Db::name('users_score')->insert($data);
  909. //会员表更新
  910. $update = [];
  911. if (!empty($score)) {
  912. $update['scores'] = Db::raw('scores+' . $score);
  913. $update['devote'] = Db::raw('devote+' . $score);
  914. }
  915. if (!empty($money)) {
  916. $update['users_money'] = Db::raw('users_money-' . $money);
  917. }
  918. Db::name('users')->where('users_id', $users_id)->update($update);
  919. }
  920. public function RebackDel($ask_id = 0,$ask = [])
  921. {
  922. $money = $ask['money'];
  923. $users_id = $ask['users_id'];
  924. if ($money > 0) {
  925. //退钱
  926. Db::name('users')->where('users_id', $users_id)->setInc('users_money', $money);
  927. // adminLog($users_id . '删除问题,id:' . $ask_id . ',标题:' . $ask['ask_title']);
  928. }
  929. $count_score = Db::name('users_score')->where(['ask_id' => $ask_id, 'users_id' => $users_id])->sum('score');
  930. Db::name('users_score')->where(['ask_id' => $ask_id, 'users_id' => $users_id])->delete();
  931. $user_info = Db::name('users')->where('users_id', $users_id)->field('users_money,scores')->find();
  932. if (!empty($user_info)) {
  933. //更新
  934. $update['scores'] = ($user_info['scores'] - $count_score) > 0 ? $user_info['scores'] - $count_score : 0;
  935. $update['update_time'] = getTime();
  936. Db::name('users')->where('users_id', $users_id)->update($update);
  937. }
  938. }
  939. // 获取问答设置内容
  940. public function getAskSetting($action = null)
  941. {
  942. $result = [];
  943. $data = tpSetting('ask');
  944. if (!empty($data)) {
  945. $AskQuesSteps = in_array($action, ['add_ask', 'edit_ask']) ? str_replace("\n", "<br/>", $data['ask_ques_steps']) : '';
  946. $result = [
  947. // 一键入群设置
  948. 'Qq' => [
  949. 'ask_qq' => !empty($data['ask_qq']) ? $data['ask_qq'] : '',
  950. 'ask_intro' => !empty($data['ask_intro']) ? $data['ask_intro'] : '',
  951. 'ask_qq_link' => !empty($data['ask_qq_link']) ? $data['ask_qq_link'] : '',
  952. ],
  953. // 问答提问步骤
  954. 'ask_ques_steps' => $AskQuesSteps,
  955. // 积分管理设置的积分名称
  956. 'score_name' => getUsersConfigData('score.score_name'),
  957. ];
  958. }
  959. return $result;
  960. }
  961. }