Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Ask.php 38KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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\plugins\model;
  14. use think\Model;
  15. use think\Db;
  16. use think\Page;
  17. use think\Config;
  18. use app\plugins\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->weapp_ask_db = Db::name('weapp_ask'); // 问题表
  31. $this->weapp_ask_answer_db = Db::name('weapp_ask_answer'); // 答案表
  32. $this->weapp_ask_type_db = Db::name('weapp_ask_type'); // 问题栏目分类表
  33. $this->weapp_ask_answer_like_db = Db::name('weapp_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->weapp_ask_db->where('users_id', $view_uid)->count();
  63. // 查询回答数量
  64. $result['AnswerCount'] = $this->weapp_ask_answer_db->where('users_id', $view_uid)->count();
  65. }
  66. // 拼装URL
  67. $result['UsersAskUrl'] = url('plugins/Ask/ask_index', ['view_uid'=>$view_uid]);
  68. // ROOT_DIR.'/index.php?m=plugins&c=Ask&a=ask_index&view_uid='.$view_uid;
  69. $result['UsersAnswerUrl'] = url('plugins/Ask/ask_index', ['view_uid'=>$view_uid, 'method'=>'answer']);
  70. // ROOT_DIR.'/index.php?m=plugins&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,$limit = 10)
  75. {
  76. // 返回参数
  77. $result = [];
  78. $field = 'a.ask_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->weapp_ask_db->alias('a')->where($where)->count('ask_id');
  87. $pageObj = new \weapp\Ask\logic\PageLogic($count, $limit);
  88. $result['pageStr'] = $pageObj->show();
  89. /* END */
  90. /*问题表数据(问题表+会员表+问题分类表)*/
  91. $result['AskData'] = $this->weapp_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->weapp_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->weapp_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->weapp_ask_db->alias('a')->where($where)->count('ask_id');
  127. $pageObj = new \weapp\Ask\logic\PageLogic($count, $limit);
  128. $result['pageStr'] = $pageObj->show();
  129. /* END */
  130. /*问题表数据(问题表+会员表+问题分类表)*/
  131. $result['AskData'] = $this->weapp_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'] = url('plugins/Ask/details', ['ask_id'=>$value['ask_id']]);
  148. // ROOT_DIR.'/index.php?m=plugins&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,b.level';
  179. }
  180. $result['PendingAsk'] = '';
  181. if (isset($where['a.replies']) && 0 == $where['a.replies']) {
  182. $result['PendingAsk'] = '待回答';
  183. }
  184. $result['SearchName'] = '';
  185. // 提取搜索关键词
  186. if (!empty($where['a.ask_title'])) {
  187. $SearchName = str_replace("%","",$where['a.ask_title'][1]);
  188. $result['SearchName'] = $SearchName;
  189. $result['SearchNameRed'] = "搜索 <font color='red'>".$SearchName."</font> 结果";
  190. }else{
  191. $result['SearchName'] = "";
  192. }
  193. if (!empty($where['a.type_id'])) {
  194. // 存在栏目ID则执行
  195. $TypeData = $this->weapp_ask_type_db->where('parent_id',$where['a.type_id'])->field('type_id')->select();
  196. if (!empty($TypeData)) {
  197. // 将顶级栏目ID合并到新条件中
  198. $type_id = get_arr_column($TypeData, 'type_id');
  199. $type_id = array_merge($type_id, array(0 => $where['a.type_id']));
  200. $where['a.type_id'] = ['IN', $type_id];
  201. }
  202. // 查询满足要求的总记录数
  203. $count = $this->weapp_ask_db->alias('a')->where($where)->count('ask_id');
  204. // 实例化分页类 传入总记录数和每页显示的记录数
  205. $pageObj = new \weapp\Ask\logic\PageLogic($count, $limit);
  206. /*问题表数据(问题表+会员表+问题分类表)*/
  207. $result['AskData'] = $this->weapp_ask_db->field($field)
  208. ->alias('a')
  209. ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
  210. ->join('__WEAPP_ASK_TYPE__ c', 'a.type_id = c.type_id', 'LEFT')
  211. ->where($where)
  212. ->order('a.ask_id desc')
  213. ->limit($pageObj->firstRow.','.$pageObj->listRows)
  214. ->select();
  215. // 分页显示输出
  216. $result['pageStr'] = $pageObj->show();
  217. }else{
  218. /*问题表数据(问题表+会员表+问题分类表)*/
  219. $result['AskData'] = $this->weapp_ask_db->field($field)
  220. ->alias('a')
  221. ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
  222. ->join('__WEAPP_ASK_TYPE__ c', 'a.type_id = c.type_id', 'LEFT')
  223. ->where($where)
  224. ->order('a.ask_id desc')
  225. ->limit($limit)
  226. ->select();
  227. /* END */
  228. $result['pageStr'] = '';
  229. }
  230. /*问题回答人查询*/
  231. $ask_id = get_arr_column($result['AskData'], 'ask_id');
  232. $RepliesData = $this->weapp_ask_answer_db->field('a.ask_id, a.users_id, b.head_pic, b.nickname')
  233. ->alias('a')
  234. ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
  235. ->where('ask_id', 'IN', $ask_id)
  236. ->select();
  237. $RepliesDataNew = [];
  238. foreach ($RepliesData as $key => $value) {
  239. /*头像处理*/
  240. $value['head_pic'] = get_head_pic($value['head_pic']);
  241. /* END */
  242. // 将二维数组以ask_id值作为键,并归类数组,效果同等group_same_key
  243. $RepliesDataNew[$value['ask_id']][] = $value;
  244. }
  245. /* END */
  246. /*数据处理*/
  247. foreach ($result['AskData'] as $key => $value) {
  248. /*头像处理*/
  249. $value['head_pic'] = get_head_pic($value['head_pic']);
  250. $result['AskData'][$key]['head_pic'] = $value['head_pic'];
  251. /* END */
  252. // 若存在搜索关键词则标红关键词
  253. if (isset($SearchName) && !empty($SearchName)) {
  254. $result['AskData'][$key]['ask_title'] = $this->AskLogic->GetRedKeyWord($SearchName, $value['ask_title']);
  255. }
  256. // 时间友好显示处理
  257. $result['AskData'][$key]['add_time'] = friend_date($value['add_time']);
  258. // 栏目分类Url
  259. $result['AskData'][$key]['TypeUrl'] = url('plugins/Ask/index', ['type_id'=>$value['type_id']]);
  260. // ROOT_DIR.'/index.php?m=plugins&c=Ask&a=index&type_id='.$value['type_id'];
  261. // 问题内容Url
  262. $result['AskData'][$key]['AskUrl'] = url('plugins/Ask/details', ['ask_id'=>$value['ask_id']]);
  263. // ROOT_DIR.'/index.php?m=plugins&c=Ask&a=details&ask_id='.$value['ask_id'];
  264. // 回复处理
  265. if (!empty($RepliesDataNew[$value['ask_id']])) {
  266. $UsersConut = array_unique($RepliesDataNew[$value['ask_id']], SORT_REGULAR);
  267. // 读取前三条数据
  268. $result['AskData'][$key]['HeadPic'] = array_slice($UsersConut, 0, 3);
  269. $result['AskData'][$key]['UsersConut'] = '等'.count($UsersConut).'人参与讨论';
  270. }else{
  271. $result['AskData'][$key]['HeadPic'] = ['0'=>['head_pic'=>$value['head_pic']]];
  272. $result['AskData'][$key]['UsersConut'] = $value['nickname'];
  273. }
  274. }
  275. /* END */
  276. // 是否推荐
  277. if (!empty($where['a.is_recom'])) {
  278. $result['IsRecom'] = 1;
  279. }else{
  280. $result['IsRecom'] = 0;
  281. }
  282. if (!empty($where['a.money'])) {
  283. $result['IsRecom'] = 3;
  284. }
  285. return $result;
  286. }
  287. // 问题数据
  288. public function GetAjaxAskPage($page = 1,$where = array(), $limit = 10, $field = null)
  289. {
  290. // 返回参数
  291. $result = [];
  292. // 没有传入则默认查询这些字段
  293. if (empty($field)) {
  294. $field = 'a.*, b.nickname, b.head_pic, c.type_name';
  295. }
  296. $start = ($page-1) * $limit;
  297. $count = $this->weapp_ask_db->alias('a')->where($where)->count('ask_id');
  298. $total_page = intval($count/$limit);
  299. /*问题表数据(问题表+会员表+问题分类表)*/
  300. $result['AskData'] = $this->weapp_ask_db->field($field)
  301. ->alias('a')
  302. ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
  303. ->join('__WEAPP_ASK_TYPE__ c', 'a.type_id = c.type_id', 'LEFT')
  304. ->where($where)
  305. ->order('a.ask_id desc')
  306. ->limit($start,$limit)
  307. ->select();
  308. /* END */
  309. $result['pageStr'] = '';
  310. if (!empty($where['a.ask_title'])) {
  311. $SearchName = str_replace("%","",$where['a.ask_title'][1]);
  312. }
  313. /*问题回答人查询*/
  314. $ask_id = get_arr_column($result['AskData'], 'ask_id');
  315. $RepliesData = $this->weapp_ask_answer_db->field('a.ask_id, a.users_id, b.head_pic, b.nickname')
  316. ->alias('a')
  317. ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
  318. ->where('ask_id', 'IN', $ask_id)
  319. ->select();
  320. $RepliesDataNew = [];
  321. foreach ($RepliesData as $key => $value) {
  322. /*头像处理*/
  323. $value['head_pic'] = get_head_pic($value['head_pic']);
  324. /* END */
  325. // 将二维数组以ask_id值作为键,并归类数组,效果同等group_same_key
  326. $RepliesDataNew[$value['ask_id']][] = $value;
  327. }
  328. /* END */
  329. /*数据处理*/
  330. foreach ($result['AskData'] as $key => $value) {
  331. /*头像处理*/
  332. $value['head_pic'] = get_head_pic($value['head_pic']);
  333. $result['AskData'][$key]['head_pic'] = $value['head_pic'];
  334. /* END */
  335. // 若存在搜索关键词则标红关键词
  336. if (isset($SearchName) && !empty($SearchName)) {
  337. $result['AskData'][$key]['ask_title'] = $this->AskLogic->GetRedKeyWord($SearchName, $value['ask_title']);
  338. }
  339. // 时间友好显示处理
  340. $result['AskData'][$key]['add_time'] = friend_date($value['add_time']);
  341. // 栏目分类Url
  342. $result['AskData'][$key]['TypeUrl'] = url('plugins/Ask/index', ['type_id'=>$value['type_id']]);
  343. // ROOT_DIR.'/index.php?m=plugins&c=Ask&a=index&type_id='.$value['type_id'];
  344. // 问题内容Url
  345. $result['AskData'][$key]['AskUrl'] = url('plugins/Ask/details', ['ask_id'=>$value['ask_id']]);
  346. // ROOT_DIR.'/index.php?m=plugins&c=Ask&a=details&ask_id='.$value['ask_id'];
  347. // 回复处理
  348. if (!empty($RepliesDataNew[$value['ask_id']])) {
  349. $UsersConut = array_unique($RepliesDataNew[$value['ask_id']], SORT_REGULAR);
  350. // 读取前三条数据
  351. $result['AskData'][$key]['HeadPic'] = array_slice($UsersConut, 0, 3);
  352. $result['AskData'][$key]['UsersConut'] = '等'.count($UsersConut).'人参与讨论';
  353. }else{
  354. $result['AskData'][$key]['HeadPic'] = ['0'=>['head_pic'=>$value['head_pic']]];
  355. $result['AskData'][$key]['UsersConut'] = $value['nickname'];
  356. }
  357. }
  358. /* END */
  359. // 是否推荐
  360. $result['IsRecom'] = 0;
  361. if (!empty($where['a.is_recom'])) {
  362. $result['IsRecom'] = 1;
  363. }
  364. return ['total'=>$total_page,'list'=>$result['AskData'],'IsRecom'=>$result['IsRecom']];
  365. }
  366. // 问题栏目分类数据
  367. public function GetAskTypeData($param = array(), $is_add = null)
  368. {
  369. // 数据初始化
  370. $result = [
  371. 'IsTypeId' => 0,
  372. 'ParentId' => 0,
  373. 'TypeId' => 0,
  374. 'TypeName' => '',
  375. 'HtmlCode' => '<span style="color: red;">请先让管理员在插件栏目列表中添加分类!</span>',
  376. 'TypeData' => []
  377. ];
  378. /*栏目处理*/
  379. $TypeData = $this->weapp_ask_type_db->order('sort_order asc, type_id asc')->select();
  380. if (empty($TypeData)) return [];
  381. foreach ($TypeData as $key => $value) {
  382. /*若存在分类ID且和数据中的值相等则执行*/
  383. if (!empty($param['type_id']) && $value['type_id'] == $param['type_id']) {
  384. $result['TypeName'] = $value['type_name'];
  385. $result['TypeId'] = $value['type_id'];
  386. $result['ParentId'] = $value['parent_id'];
  387. // 描点标记焦点
  388. if (!empty($value['parent_id'])) {
  389. $result['IsTypeId'] = $value['parent_id'];
  390. }else{
  391. $result['IsTypeId'] = $value['type_id'];
  392. }
  393. }
  394. /* END */
  395. // 是否顶级栏目
  396. if($value['parent_id'] == 0){
  397. $value['SelectType'] = 0;
  398. $PidData[] = $value;
  399. }else{
  400. $value['SelectSubType'] = 0;
  401. $TidData[] = $value;
  402. }
  403. }
  404. // 调用来源
  405. if (isset($is_add) && 'add_ask' == $is_add) {
  406. // 问题发布调用
  407. $return['HtmlCode'] = $this->AskLogic->GetTypeHtmlCode($PidData, $TidData, $param['type_id']);
  408. return $return;
  409. }else if (isset($is_add) && 'edit_ask' == $is_add) {
  410. // 问题编辑调用
  411. $return['HtmlCode'] = $this->AskLogic->GetTypeHtmlCode($PidData, $TidData, $param['type_id']);
  412. return $return;
  413. }else{
  414. // 列表或内容页调用
  415. $TidData = group_same_key($TidData, 'parent_id');
  416. // 一级栏目处理
  417. foreach($PidData as $P_key => $PidValue){
  418. $result['TypeData'][] = $PidValue;
  419. $result['TypeData'][$P_key]['Url'] = url('plugins/Ask/index', ['type_id'=>$PidValue['type_id']]);
  420. // ROOT_DIR.'/index.php?m=plugins&c=Ask&a=index&type_id='.$PidValue['type_id'];
  421. $result['TypeData'][$P_key]['SubType'] = [];
  422. if (!empty($TidData[$PidValue['type_id']])) {
  423. // 所属子栏目处理
  424. foreach($TidData[$PidValue['type_id']] as $T_key => $TidValue){
  425. if($TidValue['parent_id'] == $PidValue['type_id']){
  426. array_push($result['TypeData'][$P_key]['SubType'], $TidValue);
  427. $result['TypeData'][$P_key]['SubType'][$T_key]['Url'] = url('plugins/Ask/index', ['type_id'=>$TidValue['type_id']]);
  428. // ROOT_DIR.'/index.php?m=plugins&c=Ask&a=index&type_id='.$TidValue['type_id'];
  429. }
  430. }
  431. }
  432. }
  433. }
  434. /* END */
  435. return $result;
  436. }
  437. // 周榜
  438. public function GetAskWeekListData()
  439. {
  440. $time1 = strtotime(date('Y-m-d H:i:s', time()));
  441. $time2 = $time1 - (86400 * 7);
  442. $where = [
  443. 'a.add_time' => ['between time', [$time2, $time1]],
  444. 'a.is_review'=> 1,
  445. ];
  446. $result['WeekList'] = [];
  447. $WeekList = $this->weapp_ask_db->field('a.ask_id, a.ask_title, a.click, a.replies, b.head_pic')
  448. ->alias('a')
  449. ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
  450. ->order('click desc, replies desc')
  451. ->where($where)
  452. ->limit('0, 10')
  453. ->select();
  454. if (empty($WeekList)) {
  455. return $result;
  456. }
  457. foreach ($WeekList as $key => $value) {
  458. $result['WeekList'][$key]['AskUrl'] = url('plugins/Ask/details', ['ask_id'=>$value['ask_id']]);
  459. // ROOT_DIR.'/index.php?m=plugins&c=Ask&a=details&ask_id='.$value['ask_id'];
  460. // $result['WeekList'][$key]['ask_title'] = mb_strimwidth($value['ask_title'], 0, 30, "...");
  461. $result['WeekList'][$key]['ask_title'] = $value['ask_title'];
  462. }
  463. return $result;
  464. }
  465. // 总榜
  466. public function GetAskTotalListData()
  467. {
  468. $result['TotalList'] = [];
  469. $TotalList = $this->weapp_ask_db->field('a.ask_id, a.ask_title, a.click, a.replies, b.head_pic')
  470. ->alias('a')
  471. ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
  472. ->order('click desc, replies desc')
  473. ->where('a.is_review', 1)
  474. ->limit('0, 10')
  475. ->select();
  476. if (empty($TotalList)) {
  477. return $result;
  478. }
  479. foreach ($TotalList as $key => $value) {
  480. $result['TotalList'][$key]['AskUrl'] = url('plugins/Ask/details', ['ask_id'=>$value['ask_id']]);
  481. // ROOT_DIR.'/index.php?m=plugins&c=Ask&a=details&ask_id='.$value['ask_id'];
  482. // $result['TotalList'][$key]['ask_title'] = mb_strimwidth($value['ask_title'], 0, 30, "...");
  483. $result['TotalList'][$key]['ask_title'] = $value['ask_title'];
  484. }
  485. return $result;
  486. }
  487. // 问题详情数据
  488. public function GetAskDetailsData($param = array(), $parent_id = null, $users_id = null)
  489. {
  490. $ResultData['code'] = 1;
  491. $ResultData['info'] = $this->weapp_ask_db->field('a.*, b.username, b.nickname, b.head_pic, c.type_name')
  492. ->alias('a')
  493. ->join('__WEAPP_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($ResultData['info'])) return ['code'=>0,'msg'=>'浏览的问题不存在!'];
  498. if (0 !== $parent_id) {
  499. if (0 == $ResultData['info']['is_review'] && $ResultData['info']['users_id'] !== $users_id){
  500. return ['code'=>0,'msg'=>'问题未审核通过,暂时不可浏览!'];
  501. }
  502. }
  503. // 头像处理
  504. $ResultData['info']['head_pic'] = get_head_pic($ResultData['info']['head_pic']);
  505. // 时间友好显示处理
  506. $ResultData['info']['add_time'] = friend_date($ResultData['info']['add_time']);
  507. $ResultData['IsUsers'] = 0;
  508. if ($ResultData['info']['users_id'] == session('users_id')) $ResultData['IsUsers'] = 1;
  509. // 处理格式
  510. $ResultData['info']['content'] = htmlspecialchars_decode($ResultData['info']['content']);
  511. $ResultData['SearchName'] = null;
  512. // seo信息
  513. $ResultData['info']['seo_title'] = $ResultData['info']['ask_title'] . ' - ' . $ResultData['info']['type_name'];
  514. $ResultData['info']['seo_keywords'] = $ResultData['info']['ask_title'];
  515. $ResultData['info']['seo_description'] = @msubstr(checkStrHtml($ResultData['info']['content']), 0, config('global.arc_seo_description_length'), false);
  516. return $ResultData;
  517. }
  518. // 问题回答数据
  519. public function GetAskReplyData($param = array(), $parent_id = null)
  520. {
  521. /*查询条件*/
  522. $bestanswer_id = $this->weapp_ask_db->where('ask_id', $param['ask_id'])->getField('bestanswer_id');
  523. $RepliesWhere = ['ask_id' => $param['ask_id'], 'is_review' => 1];
  524. $WhereOr = [];
  525. if (!empty($param['answer_id'])) {
  526. $RepliesWhere = ['answer_id' => $param['answer_id'], 'is_review' => 1];
  527. $WhereOr = ['answer_pid' => $param['answer_id']];
  528. }
  529. // 若为则创始人则去除仅查询已审核评论这个条件,$parent_id = 0 表示为创始人
  530. if (0 == $parent_id) unset($RepliesWhere['is_review']);
  531. /* END */
  532. /*评论读取条数*/
  533. $firstRow = !empty($param['firstRow']) ? $param['firstRow'] : 0;
  534. $listRows = !empty($param['listRows']) ? $param['listRows'] : 5;
  535. $result['firstRow'] = $firstRow;
  536. $result['listRows'] = $listRows;
  537. /* END */
  538. /*排序*/
  539. $OrderBy = !empty($param['click_like']) ? 'a.click_like '.$param['click_like'].', a.add_time asc' : 'a.add_time asc';
  540. $click_like = isset($param['click_like']) ? $param['click_like'] : '';
  541. $result['SortOrder'] = 'desc' == $click_like ? 'asc' : 'desc';
  542. /* END */
  543. /*评论回答*/
  544. $RepliesData = $this->weapp_ask_answer_db->field('a.*, b.head_pic, b.nickname, c.nickname as `at_usersname`')
  545. ->alias('a')
  546. ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
  547. ->join('__USERS__ c', 'a.at_users_id = c.users_id', 'LEFT')
  548. ->order($OrderBy)
  549. ->where($RepliesWhere)
  550. ->WhereOr($WhereOr)
  551. ->select();
  552. if (empty($RepliesData)) return [];
  553. /* END */
  554. /*点赞数据*/
  555. $AnswerIds = get_arr_column($RepliesData, 'answer_id');
  556. $AnswerLikeData = $this->weapp_ask_answer_like_db->field('a.*, b.nickname')
  557. ->alias('a')
  558. ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
  559. ->order('like_id desc')
  560. ->where('answer_id','IN',$AnswerIds)
  561. ->select();
  562. $AnswerLikeData = group_same_key($AnswerLikeData, 'answer_id');
  563. /* END */
  564. /*回答处理*/
  565. $PidData = $AnswerData = [];
  566. foreach ($RepliesData as $key => $value) {
  567. // 友好显示时间
  568. $value['add_time'] = friend_date($value['add_time']);
  569. // 处理格式
  570. $value['content'] = htmlspecialchars_decode($value['content']);
  571. // 头像处理
  572. $value['head_pic'] = get_head_pic($value['head_pic']);
  573. // 会员昵称
  574. $value['nickname'] = !empty($value['nickname']) ? $value['nickname'] : $value['username'];
  575. // 是否上一级回答
  576. if($value['answer_pid'] == 0){
  577. $PidData[] = $value;
  578. }else{
  579. $AnswerData[] = $value;
  580. }
  581. }
  582. /* END */
  583. /*一级回答*/
  584. foreach($PidData as $key => $PidValue){
  585. $result['AnswerData'][] = $PidValue;
  586. // 子回答
  587. $result['AnswerData'][$key]['AnswerSubData'] = [];
  588. // 点赞数据
  589. $result['AnswerData'][$key]['AnswerLike'] = [];
  590. /*所属子回答处理*/
  591. foreach($AnswerData as $AnswerValue){
  592. if($AnswerValue['answer_pid'] == $PidValue['answer_id']){
  593. array_push($result['AnswerData'][$key]['AnswerSubData'], $AnswerValue);
  594. }
  595. }
  596. /* END */
  597. /*读取指定数据*/
  598. // 以是否审核排序,审核的优先
  599. array_multisort(get_arr_column($result['AnswerData'][$key]['AnswerSubData'],'is_review'), SORT_DESC, $result['AnswerData'][$key]['AnswerSubData']);
  600. // 读取指定条数
  601. $result['AnswerData'][$key]['AnswerSubData'] = array_slice($result['AnswerData'][$key]['AnswerSubData'], $firstRow, $listRows);
  602. /* END */
  603. $result['AnswerData'][$key]['AnswerLike']['LikeNum'] = null;
  604. $result['AnswerData'][$key]['AnswerLike']['LikeName'] = null;
  605. /*点赞处理*/
  606. foreach ($AnswerLikeData as $LikeKey => $LikeValue) {
  607. if($PidValue['answer_id'] == $LikeKey){
  608. // 点赞总数
  609. $LikeNum = count($LikeValue);
  610. $result['AnswerData'][$key]['AnswerLike']['LikeNum'] = $LikeNum;
  611. for ($i=0; $i < $LikeNum; $i++) {
  612. // 获取前三个点赞人处理后退出本次for
  613. if ($i > 2) break;
  614. // 点赞人用户名\昵称
  615. $LikeName = $LikeValue[$i]['nickname'];
  616. // 在第二个数据前加入顿号,拼装a链接
  617. if ($i != 0) {
  618. $LikeName = ' 、<a href="javascript:void(0);">'. $LikeName .'</a>';
  619. }else{
  620. $LikeName = '<a href="javascript:void(0);">'. $LikeName .'</a>';
  621. }
  622. $result['AnswerData'][$key]['AnswerLike']['LikeName'] .= $LikeName;
  623. }
  624. }
  625. }
  626. /* END */
  627. }
  628. /* END */
  629. /*最佳答案数据*/
  630. foreach ($result['AnswerData'] as $key => $value) {
  631. if ($bestanswer_id == $value['answer_id']) {
  632. $result['BestAnswer'][$key] = $value;
  633. unset($result['AnswerData'][$key]);
  634. }
  635. }
  636. /* NED */
  637. // 统计回答数
  638. $result['AnswerCount'] = count($RepliesData);
  639. return $result;
  640. }
  641. // 操作问题表回复数
  642. public function UpdateAskReplies($ask_id = null, $IsAdd = true, $DelNum = 0)
  643. {
  644. if (empty($ask_id)) return false;
  645. if (!empty($IsAdd)) {
  646. $Updata = [
  647. 'replies' => Db::raw('replies+1'),
  648. 'update_time' => getTime(),
  649. ];
  650. }else{
  651. $Updata = [
  652. 'replies' => Db::raw('replies-1'),
  653. 'update_time' => getTime(),
  654. ];
  655. if ($DelNum > 0) $Updata['replies'] = Db::raw('replies-'.$DelNum);
  656. }
  657. $this->weapp_ask_db->where('ask_id', $ask_id)->update($Updata);
  658. }
  659. // 增加问题浏览点击量
  660. public function UpdateAskClick($ask_id = null)
  661. {
  662. if (empty($ask_id)) return false;
  663. $Updata = [
  664. 'click' => Db::raw('click+1'),
  665. 'update_time' => getTime(),
  666. ];
  667. $this->weapp_ask_db->where('ask_id', $ask_id)->update($Updata);
  668. }
  669. /*
  670. *会员中心--问题中心--我的问题\回复
  671. * $view_uid 用户id
  672. * $is_ask true:获取问题列表,false:获取回答列表
  673. * $limit 煤业显示条数
  674. */
  675. public function GetUsersAskDataNew($view_uid = null, $is_ask = true,$limit = 5)
  676. {
  677. // 返回参数
  678. $result = [];
  679. if (!empty($is_ask)) {
  680. // 提问问题查询列表
  681. /*查询字段*/
  682. $field = 'a.ask_id, a.ask_title, a.click, a.replies, a.add_time, a.is_review, b.type_name';
  683. /* END */
  684. /*查询条件*/
  685. $where = [
  686. 'a.status' => ['IN', [0, 1]],
  687. 'a.users_id' => $view_uid,
  688. ];
  689. /* END */
  690. /* 分页 */
  691. $count = $this->weapp_ask_db->alias('a')->where($where)->count('ask_id');
  692. $pageObj = new \weapp\Ask\logic\PageLogic($count, $limit);
  693. $result['pageStr'] = $pageObj->show();
  694. /* END */
  695. /*问题表数据(问题表+会员表+问题分类表)*/
  696. $result['AskData'] = $this->weapp_ask_db->field($field)
  697. ->alias('a')
  698. ->join('__WEAPP_ASK_TYPE__ b', 'a.type_id = b.type_id', 'LEFT')
  699. ->where($where)
  700. ->order('a.add_time desc')
  701. ->limit($pageObj->firstRow.','.$pageObj->listRows)
  702. ->select();
  703. /* END */
  704. }else{
  705. // 回答问题查询列表
  706. /*查询字段*/
  707. $field = 'a.*, b.ask_title';
  708. /* END */
  709. /*查询条件*/
  710. $where = [
  711. 'a.users_id' => $view_uid,
  712. ];
  713. /* END */
  714. /* 分页 */
  715. $count = $this->weapp_ask_answer_db->alias('a')->where($where)->count('answer_id');
  716. $pageObj = new \weapp\Ask\logic\PageLogic($count, $limit);
  717. $result['pageStr'] = $pageObj->show();
  718. /* END */
  719. /*问题回答人查询*/
  720. $result['AskData'] = $this->weapp_ask_answer_db->field($field)
  721. ->alias('a')
  722. ->join('__WEAPP_ASK__ b', 'a.ask_id = b.ask_id', 'LEFT')
  723. ->where($where)
  724. ->order('a.add_time desc')
  725. ->limit($pageObj->firstRow.','.$pageObj->listRows)
  726. ->select();
  727. /* END */
  728. }
  729. /*数据处理*/
  730. foreach ($result['AskData'] as $key => $value) {
  731. // 问题内容Url
  732. $result['AskData'][$key]['AskUrl'] = url('plugins/Ask/details', ['ask_id'=>$value['ask_id']]);
  733. // ROOT_DIR.'/index.php?m=plugins&c=Ask&a=details&ask_id='.$value['ask_id'];
  734. if (empty($is_ask)) {
  735. $result['AskData'][$key]['AskUrl'] .= !empty($value['answer_pid']) ? '#ul_div_li_'.$value['answer_pid'] : '#ul_div_li_'.$value['answer_id'];
  736. }
  737. if (isset($value['answer_id']) && !empty($value['answer_id'])) {
  738. $preg = '/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i';
  739. $value['content'] = htmlspecialchars_decode($value['content']);
  740. $value['content'] = preg_replace($preg,'[图片]',$value['content']);
  741. $value['content'] = strip_tags($value['content']);
  742. $result['AskData'][$key]['content'] = mb_strimwidth($value['content'], 0, 120, "...");
  743. }
  744. }
  745. /* END */
  746. return $result;
  747. }
  748. /**
  749. * 悬赏余额/积分记录
  750. */
  751. public function setUb($reward_type = 'scores',$users_id = 0, $ask_id = 0, $money = 0, $act = 'add',$ori_money = 0,$answer_id = 0)
  752. {
  753. $users = [];
  754. if ($reward_type == 'users_money'){
  755. $users = Db::name('users')->where('users_id', $users_id)->field('users_money')->find();
  756. }
  757. if ($act == 'add'){
  758. $info = '问答悬赏';
  759. //会员表更新
  760. if ($reward_type == 'scores'){
  761. $update['scores'] = Db::raw('scores-' . $money);
  762. }else{
  763. $update['users_money'] = Db::raw('users_money-' . $money);
  764. }
  765. $update['update_time'] = getTime();
  766. Db::name('users')->where('users_id', $users_id)->update($update);
  767. //日志记录
  768. if ($reward_type == 'scores'){
  769. $users = Db::name('users')->where('users_id', $users_id)->field('scores,devote')->find();
  770. $data = [
  771. 'ask_id' => $ask_id,
  772. 'users_id' => $users_id,
  773. 'info' => $info,
  774. 'score' => -$money,
  775. 'type' => 7,
  776. 'add_time' => getTime(),
  777. 'update_time' => getTime(),
  778. 'current_score' => $users['scores'],
  779. 'current_devote' => $users['devote'],
  780. ];
  781. Db::name('users_score')->insert($data);
  782. }else{
  783. $data = [
  784. 'users_id' => $users_id,
  785. 'money' => $money,
  786. 'users_money' => $users['users_money'],
  787. 'cause' => $info,
  788. 'cause_type' => 6,
  789. 'status' => 3,
  790. 'level_id' => $ask_id,//cause_type=6时为ask_id
  791. 'add_time' => getTime(),
  792. 'update_time' => getTime(),
  793. ];
  794. Db::name('users_money')->insert($data);
  795. }
  796. }elseif ($act == 'edit'){
  797. $difference = $money - $ori_money;
  798. $info = '问答悬赏';
  799. //会员表更新
  800. if ($reward_type == 'scores'){
  801. $update['scores'] = Db::raw('scores-' . $difference);
  802. }else{
  803. $update['users_money'] = Db::raw('users_money-' . $difference);
  804. }
  805. $update['update_time'] = getTime();
  806. Db::name('users')->where('users_id', $users_id)->update($update);
  807. //日志记录
  808. if ($reward_type == 'scores'){
  809. $users = Db::name('users')->where('users_id', $users_id)->field('scores,devote')->find();
  810. $data = [
  811. 'ask_id' => $ask_id,
  812. 'users_id' => $users_id,
  813. 'info' => $info,
  814. 'score' => -$difference,
  815. 'type' => 7,
  816. 'add_time' => getTime(),
  817. 'update_time' => getTime(),
  818. 'current_score' => $users['scores'],
  819. 'current_devote' => $users['devote'],
  820. ];
  821. Db::name('users_score')->insert($data);
  822. }else{
  823. $data = [
  824. 'users_id' => $users_id,
  825. 'money' => $difference,
  826. 'users_money' => $users['users_money'],
  827. 'cause' => $info,
  828. 'cause_type' => 6,
  829. 'status' => 3,
  830. 'level_id' => $ask_id,//cause_type=6时为ask_id
  831. 'add_time' => getTime(),
  832. 'update_time' => getTime(),
  833. ];
  834. Db::name('users_money')->insert($data);
  835. }
  836. }elseif ($act == 'answer'){
  837. $info = '问答获得悬赏';
  838. //会员表更新
  839. if ($reward_type == 'scores'){
  840. $update['scores'] = Db::raw('scores+' . $money);
  841. }else{
  842. $update['users_money'] = Db::raw('users_money+' . $money);
  843. }
  844. $update['update_time'] = getTime();
  845. Db::name('users')->where('users_id', $users_id)->update($update);
  846. //日志记录
  847. if ($reward_type == 'scores'){
  848. $users = Db::name('users')->where('users_id', $users_id)->field('scores,devote')->find();
  849. $data = [
  850. 'ask_id' => $ask_id,
  851. 'reply_id' => $answer_id,
  852. 'users_id' => $users_id,
  853. 'info' => $info,
  854. 'score' => $money,
  855. 'type' => 7,
  856. 'add_time' => getTime(),
  857. 'update_time' => getTime(),
  858. 'current_score' => $users['scores'],
  859. 'current_devote' => $users['devote'],
  860. ];
  861. Db::name('users_score')->insert($data);
  862. }else{
  863. $data = [
  864. 'users_id' => $users_id,
  865. 'money' => $money,
  866. 'users_money' => $users['users_money'],
  867. 'cause' => $info,
  868. 'cause_type' => 7,
  869. 'status' => 3,
  870. 'level_id' => $answer_id,//cause_type=7时为answer_id
  871. 'add_time' => getTime(),
  872. 'update_time' => getTime(),
  873. ];
  874. Db::name('users_money')->insert($data);
  875. }
  876. }
  877. }
  878. }