BaseLogic.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. <?php
  2. abstract class BaseLogic {
  3. public function db() {
  4. return Doo::db ();
  5. }
  6. /**
  7. * 获得分页数据
  8. * @param unknown_type $table
  9. * @param unknown_type $condition
  10. * @param unknown_type $on_page
  11. * @param unknown_type $page_size
  12. */
  13. function get_page($table = "", $condition = "", $on_page = 1, $page_size = 20, $action = "", $get = "", $other = "page") {
  14. $page_c = "";
  15. $page ['previous'] = get_previous ( $on_page );
  16. $page ['on_page'] = $on_page;
  17. $total_count = $this->get_table_count ( $table, $condition );
  18. $total = intval ( $total_count / $page_size );
  19. $page ['total_page'] = ($total_count % $page_size) == 0 ? $total : $total + 1;
  20. $page ['total_data'] = $total_count;
  21. $page ['next'] = $on_page == $page ['total_page'] ? $page ['total_page'] : $on_page + 1;
  22. $i = 1;
  23. $page_max = 1;
  24. if ($on_page > 10) {
  25. $page_max = intval ( $on_page / 10 ) + 1;
  26. $i = intval ( $on_page / 10 ) * 10 - 1;
  27. }
  28. for(; $i <= $page ['total_page']; $i ++) {
  29. if ($i == $on_page) {
  30. if ($other == "page")
  31. $page_c .= '<a href="javascript:void(0);" class="current">' . $i . '</a>';
  32. else
  33. $page_c .= '&nbsp;<span class="current">' . $i . '</span>&nbsp;';
  34. } else if ($other == "page")
  35. $page_c .= '<a href="' . $action . $i . $get . '" class="paginate">' . $i . '</a>';
  36. else
  37. $page_c .= '&nbsp;<a href="' . $action . $i . $get . '" class="paginate">' . $i . '</a>&nbsp;';
  38. if ($i == (10 * $page_max))
  39. break;
  40. }
  41. $page ['page'] = $page_c;
  42. $page ['lower'] = (-- $on_page) * $page_size;
  43. return $page;
  44. }
  45. /**
  46. * 获取总页数
  47. * @param unknown_type $table
  48. * @param unknown_type $condition
  49. */
  50. public function get_table_count($table = "", $condition = "") {
  51. $sql = "select count(*) as count from " . $table . " where 1 " . $condition;
  52. $query = Doo::db ()->query ( $sql );
  53. $result = $query->fetch ();
  54. return $result ['count'];
  55. }
  56. /**
  57. * 获取数据
  58. * @param unknown_type $data 一般选择条件
  59. * @param unknown_type $limit
  60. * @param unknown_type $condition
  61. * @param unknown_type $table
  62. */
  63. function get_list($data = array(), $condition = "", $limit = "", $table = "") {
  64. foreach ( $data as $key => $value ) {
  65. if (is_numeric ( $value ))
  66. $condition .= " and " . $key . " = " . $value;
  67. else
  68. $condition .= " and " . $key . " like '%" . $value . "%' ";
  69. }
  70. $condition = ' where 1 ' . $condition . $limit;
  71. $sql = "select * from " . $table . $condition;
  72. $query = Doo::db ()->query ( $sql );
  73. $result = $query->fetchAll ();
  74. return $result;
  75. }
  76. /**
  77. * 批量更新数据
  78. * @param unknown_type $params 一个字段为一个数组
  79. * @param unknown_type $table
  80. * @param unknown_type $id
  81. */
  82. function update_list($params = array(), $table = "", $id = "") {
  83. $pa = array ();
  84. $co = array ();
  85. $wh = array ();
  86. $count = count ( $params [$id] );
  87. foreach ( $params as $key => $value ) {
  88. array_push ( $pa, $key );
  89. if ($key != $id) {
  90. array_push ( $wh, $key . "=VALUES(" . $key . ")" );
  91. }
  92. }
  93. //获取更新内容
  94. for($i = 0; $i < $count; $i ++) {
  95. $tm = array ();
  96. foreach ( $pa as $k ) {
  97. array_push ( $tm, "'" . $params [$k] [$i] . "'" );
  98. }
  99. $tm = implode ( ",", $tm );
  100. array_push ( $co, "(" . $tm . ")" );
  101. }
  102. $pa = implode ( ",", $pa );
  103. $wh = implode ( ",", $wh );
  104. $co = implode ( ",", $co );
  105. $sql = "INSERT INTO " . $table . " (" . $pa . ") VALUES " . $co . " ON DUPLICATE KEY UPDATE " . $wh;
  106. $query = Doo::db ()->query ( $sql );
  107. }
  108. /**
  109. * 自动设置问题状态
  110. * @param unknown_type $data 一条问题数据
  111. */
  112. function auto_set_status_question($result = array()) {
  113. $day_7 = get_last_close_day ( $result ['time'], 7 );
  114. $day_15 = get_last_close_day ( $result ['time'], 15 );
  115. $endtime = get_date ();
  116. $answer = isset ( $result ['qanswers'] ) ? $result ['qanswers'] : $result ['answers'];
  117. //条件满足-关闭问题
  118. if ($day_7 <= 0) {
  119. //必须是未解决问题
  120. if ($result ['status'] == 1) {
  121. //是否有回答
  122. if ($answer > 0) {
  123. //有无赞-15天关闭,最佳
  124. if ($day_15 <= 0 && $result ['goods'] == 0) {
  125. $this->_set_question ( $endtime, "endtime", $result ['id'] );
  126. $result ['status'] = format_question_status ( 9, 'page' );
  127. $result ['status_num'] = 9;
  128. //扣除RMB到基金中
  129. if ($result ['price'] > 0)
  130. $this->_set_fund ( $result ['authorid'], $result ['id'], $result ['price'] );
  131. } else { //系统设置最佳
  132. //获取赞最多
  133. $sql = "select * from " . t_answer . " where qid = '" . $result ['id'] . "' order by support desc limit 1";
  134. $query = Doo::db ()->query ( $sql );
  135. $answerinfo = $query->fetch ();
  136. $this->set_best_answer_sys ( $result ['id'], $answerinfo ['id'], $answerinfo ['authorid'], $result ['title'], $answerinfo );
  137. $result ['status'] = format_question_status ( 2, 'page' );
  138. $result ['status_num'] = 2;
  139. }
  140. } else { //无回答
  141. Doo::loadModel ( 'MessageDao' );
  142. $messagedao = new MessageDao ();
  143. $subject = "问题&nbsp;<a href=/askpage/" . $result ['id'] . " >" . $result ['title'] . "</a>&nbsp;<span class=colRed>过期,自动关闭</span>";
  144. $content = "问题&nbsp;<a href=/askpage/" . $result ['id'] . " >" . $result ['title'] . "</a>&nbsp;<span class=colRed>过期,自动关闭</span>";
  145. $messagedao->send_message ( ADMIN_NAME, 0, $result ['authorid'], $subject, $content );
  146. $this->_set_question ( $endtime, "endtime", $result ['id'] );
  147. $result ['status'] = format_question_status ( 9, 'page' );
  148. $result ['status_num'] = 9;
  149. //扣除RMB到基金中
  150. if ($result ['price'] > 0)
  151. $this->_set_fund ( $result ['authorid'], $result ['id'], $result ['price'] );
  152. }
  153. }
  154. }
  155. return $result;
  156. }
  157. /**
  158. * 添加数据到检索索引
  159. * @param unknown_type $data
  160. */
  161. function add_search_index($data = array(), $DB = SEARCH_WD_DB, $CONFIG = SEARCH_INI) {
  162. require_once (XUNSEARCH_URL);
  163. $xs = new XS ( $CONFIG );
  164. $index = $xs->index;
  165. $index->setDb ( $DB );
  166. //$index->clean();die;
  167. // 创建文档对象
  168. $doc = new XSDocument ();
  169. $doc->setFields ( $data );
  170. // 添加到索引数据库中
  171. $index->add ( $doc );
  172. }
  173. /**
  174. * 更新检索库中的索引
  175. * @param unknown_type $data
  176. */
  177. function update_search_index($data = array(), $DB = SEARCH_WD_DB, $CONFIG = SEARCH_INI) {
  178. require_once (XUNSEARCH_URL);
  179. $xs = new XS ( $CONFIG );
  180. $index = $xs->index;
  181. $index->setDb ( $DB );
  182. // 创建文档对象
  183. $doc = new XSDocument ();
  184. $doc->setFields ( $data );
  185. // 添加到索引数据库中
  186. $index->update ( $doc );
  187. }
  188. /**
  189. * 删除索引
  190. * @param unknown_type $data
  191. * @param unknown_type $DB
  192. * @param unknown_type $CONFIG
  193. */
  194. function delete_search_index($data = array(), $DB = SEARCH_WD_DB, $CONFIG = SEARCH_INI) {
  195. require_once (XUNSEARCH_URL);
  196. $xs = new XS ( $CONFIG );
  197. $index = $xs->index;
  198. $index->setDb ( $DB );
  199. $index->del ( $data );
  200. }
  201. /**
  202. * 扣除金额
  203. * @param unknown_type $price
  204. * @param unknown_type $uid
  205. */
  206. function offer_price_by_sso($sso_price = 0, $sso_uid = 0) {
  207. require_once (SITE_PATH . '/protected/plugin/client.php');
  208. $client = new client ( ZHSSO );
  209. $is_succes = $client->zhsso_member_msub ( $sso_uid, $sso_price, "ask" );
  210. $is_succes = explode ( "\r", $is_succes );
  211. $ts = json_decode ( $is_succes ['0'], true );
  212. return $ts;
  213. }
  214. /**
  215. * 增加金额
  216. * @param unknown_type $sso_price
  217. * @param unknown_type $sso_uid
  218. */
  219. function add_price_by_sso($sso_price = 0, $sso_uid = 0) {
  220. require_once (SITE_PATH . '/protected/plugin/client.php');
  221. $client = new client ( ZHSSO );
  222. $is_succes = $client->zhsso_member_madd ( $sso_uid, $sso_price, "ask" );
  223. $is_succes = explode ( "\r", $is_succes );
  224. $ts = json_decode ( $is_succes ['0'], true );
  225. return $ts;
  226. }
  227. /**
  228. * 获取通行证用户信息
  229. * @param unknown_type $username
  230. */
  231. function get_sso_user_info_by_name($username = "") {
  232. require_once (SITE_PATH . '/protected/plugin/client.php');
  233. $client = new client ( ZHSSO );
  234. $userinfo = $client->zhsso_getUserbyName ( $username );
  235. $userinfo = explode ( "\r", $userinfo );
  236. $ts = json_decode ( $userinfo ['0'], true );
  237. return $ts;
  238. }
  239. /**
  240. * 将财富添加到基金库
  241. * @param unknown_type $uid
  242. * @param unknown_type $qid
  243. * @param unknown_type $amount
  244. */
  245. function _set_fund($uid = 0, $qid = 0, $amount = 0) {
  246. $sql = "INSERT INTO " . t_fund . " (uid,qid,amount,time) VALUES (" . $uid . "," . $qid . "," . $amount . "," . get_date () . ")";
  247. $query = Doo::db ()->query ( $sql );
  248. }
  249. /**
  250. * 更改问题状态
  251. */
  252. private function _set_question($endtime = 0, $parsom = "endtime", $id = 0) {
  253. $sql = "UPDATE `" . t_question . "` SET `status` = " . QUESTOIN_STATUS_CLOSE . " , endtime =" . $endtime . " WHERE `id` = " . $id;
  254. $query = Doo::db ()->query ( $sql );
  255. }
  256. /**
  257. * 管理员设置最佳答案--整理
  258. * @param unknown_type $qid
  259. */
  260. function set_best_answer_sys($qid = 0, $aid = 0, $auid = 0, $title = "", $data = array()) {
  261. Doo::loadModel ( 'QuestionDao' );
  262. Doo::loadModel ( 'AnswerDao' );
  263. Doo::loadModel ( 'UserDao' );
  264. Doo::loadModel ( 'MessageDao' );
  265. Doo::loadModel ( 'Credit3logDao' );
  266. $questiondao = new QuestionDao ();
  267. $answerdao = new AnswerDao ();
  268. $userdao = new UserDao ();
  269. $messagedao = new MessageDao ();
  270. $credit3logdao = new Credit3logDao ();
  271. //获取问题
  272. $question = $questiondao->get_question_by_id ( $qid );
  273. if ($question ['price'] > 0) {
  274. //通行证
  275. $info = $userdao->get_userinfo_by_uid ( $auid );
  276. $sso_info = $this->get_sso_user_info_by_name ( $info ['username'] );
  277. //,
  278. $is = $this->add_price_by_sso ( $question ['price'], $sso_info [0] ['id'] );
  279. if ($is <= 0)
  280. return false;
  281. }
  282. //答案最佳者获得积分
  283. $acd1 = POINT_ADOPT;
  284. //更新问题
  285. $data_question ['endtime'] = get_date ();
  286. $data_question ['status'] = ADOPT;
  287. $condition = " where id = " . $qid;
  288. $questiondao->update_question ( $data_question, $condition );
  289. //更新答案
  290. $data_answer ['adopttime'] = $data_question ['endtime'];
  291. $data_answer ['comment'] = "谢谢您的回答";
  292. $data_answer ['status'] = ADOPT;
  293. $condition = " where id = " . $aid;
  294. $answerdao->update_answer ( $data_answer, $condition );
  295. //更新回答用户信息积分信息
  296. $userdao->add_credit1 ( $acd1, $auid, ACTION_ADOPT );
  297. //更新该用户的采纳数
  298. $userdao->add_adopts ( $auid, 1 );
  299. if ($question ['price'] > 0) {
  300. //财富-流向问题
  301. $userdao->add_credit3 ( + $question ['price'], $auid, RICH_ACTION_ADOPT );
  302. //财富流向表
  303. $ip = client_ip ();
  304. $action = $question ['author'] . "用户在" . format_date ( get_date () ) . "时间" . $ip . "IP回答被采纳,获得财富为" . $question ['price'] . ";问题ID为" . $qid;
  305. $credit3logdao->add_credit3_log ( $auid, $data ['author'], $qid, $action, $question ['price'], $ip );
  306. }
  307. //发送系统信息-发送给发回答用户
  308. $subject = "问题&nbsp;<a href=/askpage/" . $qid . " >" . $title . "</a>&nbsp;<span class=colRed >自动设置最佳答案</span>";
  309. $content = "问题&nbsp;<a href=/askpage/" . $qid . " >" . $title . "</a>&nbsp;<span class=colRed >自动设置最佳答案</span>";
  310. $messagedao->send_message ( ADMIN_NAME, 0, $auid, $subject, $content );
  311. }
  312. /**
  313. * 发送email
  314. */
  315. function send_email($uid = 0, $templete_name = "",$qid=0,$aid=0) {
  316. Doo::loadModel ( 'EmailconfigDao' );
  317. $emailconfigDao = new EmailconfigDao ();
  318. require_once SITE_PATH . '/protected/class/class.phpmailer.php';
  319. require_once SITE_PATH . '/protected/class/email_templete.file.php';
  320. //检测是否配置了发送
  321. $is_send_email = $emailconfigDao->get_emailconfig_by_uid ( $uid );
  322. if (empty ( $is_send_email ))
  323. return false;
  324. //检测需要发送的email和email_templete
  325. foreach ($is_send_email as $key=>$value){
  326. //是否有设置
  327. if($value['action_id']==$templete_name){
  328. //是否有模板
  329. if(isset($email_file[$templete_name])){
  330. $html_templete=$this->format_email_content($email_file[$templete_name],$templete_name,$qid,$aid);
  331. //检测邮件地址的正确性 &&ereg("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+",$email)
  332. if(!empty($html_templete['toemail']))
  333. $this->_mail($html_templete['templete'],$html_templete['subject'],$html_templete['toemail']);
  334. }
  335. }
  336. }
  337. }
  338. /**
  339. * 抽象格式化email内容
  340. * @param unknown_type $html_templete
  341. */
  342. abstract protected function format_email_content($html_templete,$templete_name,$qid,$aid);
  343. /**
  344. * 发送email
  345. * @param unknown_type $html_templete
  346. * @param unknown_type $subject
  347. * @param unknown_type $toemail
  348. */
  349. function _mail( $html_templete = "",$subject="",$toemail=""){
  350. require_once SITE_PATH . '/protected/class/class.pop3.php';
  351. require_once SITE_PATH . '/protected/class/class.smtp.php';
  352. require_once SITE_PATH . '/protected/class/class.phpmailer.php';
  353. $mail = new PHPMailer();
  354. $body = $html_templete;
  355. $mail->IsSMTP(); // telling the class to use SMTP
  356. $mail->SMTPAuth = true; // enable SMTP authentication
  357. //$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
  358. $mail->Host = "smtp.exmail.qq.com"; // sets GMAIL as the SMTP server
  359. //$mail->Port = 465; // set the SMTP port for the GMAIL server
  360. //$mail->CharSet = "UTF8";
  361. $mail->Username = "zhzdwd@zhzdwd.com"; // GMAIL username
  362. $mail->Password = "smartcost3850"; // GMAIL password
  363. $mail->From = "zhzdwd@zhzdwd.com";
  364. $mail->FromName = "=?UTF-8?B?" . base64_encode('纵横知道·问答') . "?=";
  365. $mail->Subject = $subject;
  366. $mail->MsgHTML($body);
  367. $mail->AddAddress($toemail);
  368. $mail->Send();
  369. // if(!$mail->Send()) {
  370. // echo "Mailer Error: " . $mail->ErrorInfo;
  371. // } else {
  372. // echo "Message sent!";
  373. // }
  374. // die;
  375. }
  376. }
  377. ?>