PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/testSource/phpcms/content/application/models/api/pingjia/morecommentary.php

https://gitlab.com/loda.sun.suryani/qijiatuku
PHP | 175 lines | 154 code | 11 blank | 10 comment | 24 complexity | f783fec4137eff368e2fd77613dca947 MD5 | raw file
  1. <?php
  2. class MoreCommentary extends CI_Model {
  3. function __construct() {
  4. // my_debug();
  5. parent::__construct ();
  6. $this->load->library ( 'CommonCache' , '' , 'cache' );
  7. }
  8. public function setting($ds_id = null) {
  9. // my_debug ( __FILE__ );
  10. if ($_POST and count ( $_POST )) {
  11. $this->process_form ();
  12. } else {
  13. $this->show_form ();
  14. }
  15. }
  16. public function get_data($ds_id) {
  17. $ret = array ();
  18. $ids = '';
  19. $this->page_cache_time = 30;
  20. if ($ds_id) {
  21. // 缓存处理
  22. $page_num = intval ( $this->input->get ( 'page' ) );
  23. if (! $page_num || $page_num < 1) {
  24. $page_num = 1;
  25. }
  26. $page_size = 20;
  27. $ds_record = $this->db->get_record_by_field ( 'cms_datasource' , 'ds_id' , $ds_id );
  28. $cid = $ds_record ['ds_api_config'];
  29. $pj_req = "{\"score\":3,\"category_id\":$cid,\"size\":$page_size,\"page\":$page_num}";
  30. $pj_json = $this->do_post_api ( "http://10.10.21.126:9105" , "/review/_getCategoryMoreRenewReview" , $pj_req );
  31. $pj_content = json_decode ( $pj_json , true );
  32. $pj_total = $pj_content ['content'] ['totalCount']; // 总评价数
  33. $pj_total_2 = $pj_content ['content'] ['scoreTotalCount']; // 符合要求的评价总数
  34. if ($page_num > ceil ( $pj_total_2 / $page_size )) {
  35. $page_num = ceil ( $pj_total_2 / $page_size );
  36. $pj_req = "{\"score\":4,\"category_id\":$cid,\"size\":$page_size,\"page\":$page_num}";
  37. $pj_json = $this->do_post_api ( "http://10.10.21.126:9105" , "/review/_getCategoryMoreRenewReview" , $pj_req );
  38. $pj_content = json_decode ( $pj_json , true );
  39. }
  40. $getpageinfo = toolkit_pages_c ( $page_num , $pj_total_2 , modify_build_url ( array ('page' => '' ) ) , $page_size , 8 , '' );
  41. $pagecode = $getpageinfo ['pagecode'];
  42. $pj_data = $pj_content ['content'] ['reviewList'];
  43. if ($pj_data) {
  44. foreach ( $pj_data as $k => $v ) {
  45. $user_req_url = "http://10.10.21.126:9005/user/getUserNameById?id=" . $v ['userID'];
  46. $user_json = $this->do_get_api ( $user_req_url );
  47. $user_data = json_decode ( $user_json , true );
  48. $user_name = $user_data ['result'];
  49. $ret [$k] ['key1'] = $v ['userID']; // 用户id
  50. $ret [$k] ['key2'] = $user_name; // 用户名
  51. $ret [$k] ['key3'] = $v ['addTime']; // 时间
  52. $ret [$k] ['key4'] = $v ['score']; // 最新打分
  53. // 2013.07.26
  54. if ($v ['content']) {
  55. $ret [$k] ['key5'] = $v ['content']; // 评价内容
  56. } else if ($v ['productReview']) {
  57. $ret [$k] ['key5'] = $v ['productReview']; // 评价内容
  58. } else if ($v ['serviceReview']) {
  59. $ret [$k] ['key5'] = $v ['serviceReview']; // 评价内容
  60. }
  61. // .end
  62. $ret [$k] ['key6'] = "http://mall.jia.com/item/" . $v ['itemID'] . "#evaluateDetail"; // 商品评价详情地址
  63. $ret [$k] ['key7'] = "http://imgmall.tg.com.cn/" . str_replace ( '.' , '_100x100.' , $v ['itemImage'] ); // 商品图片缩略图 100*100
  64. $ret [$k] ['key8'] = $v ['itemName']; // 商品标题
  65. if ($v ['explanation']) {
  66. $ret [$k] ['key9'] = $v ['explanation'] ['content']; // 评价解释
  67. $ret [$k] ['key10'] = $v ['explanation'] ['addTime']; // 解释时间
  68. } else {
  69. $ret [$k] ['key9'] = ''; // 评价解释
  70. $ret [$k] ['key10'] = ''; // 解释时间
  71. }
  72. $ret [$k] ['key11'] = $pj_total; // 评价总数
  73. $ret [$k] ['key12'] = $pagecode; // 分页代码
  74. }
  75. }
  76. // my_debug ( $ret );
  77. return $ret;
  78. }
  79. }
  80. private function show_form() {
  81. $ci = &get_instance ();
  82. // 显示配置表单
  83. echo form_open ( modify_build_url ( null ) , array ('name' => "theform","id" => "theform" ) );
  84. echo "请输入商品分类ID(例如:家居类商品,品类id为:15):";
  85. echo form_input ( "catgory_id" , $ci->input->post ( "catgory_id" ) , "id='course_ids' size='40' " );
  86. echo form_submit ( 'submitform' , '确定' , "id='submitform'" );
  87. echo form_close ();
  88. echo "<br>\n";
  89. }
  90. private function process_form() {
  91. // 接受表单提交,做相应处理
  92. $ci = &get_instance ();
  93. $cid = intval ( trim ( $ci->input->post ( 'catgory_id' ) ) );
  94. $success = 1;
  95. $tip = '';
  96. if ($cid) {
  97. $success = 1;
  98. } else {
  99. $success = '';
  100. }
  101. $pj_req = "{\"score\":4,\"category_id\":$cid,\"size\":20,\"page\":1}";
  102. $pj_json = $this->do_post_api ( "http://10.10.21.126:9105" , "/review/_getCategoryMoreRenewReview" , $pj_req );
  103. $pj_content = json_decode ( $pj_json , true );
  104. $pj_data = $pj_content ['content'];
  105. if ($pj_data) {
  106. $success = 1;
  107. } else {
  108. $success = '';
  109. $tip = $cid;
  110. }
  111. if ($success) {
  112. // =======================配置信息存入数据库=====
  113. $datasource_save = array ();
  114. $datasource_save ['ds_api_config'] = $cid;
  115. $datasource_save ['ds_type'] = 'api';
  116. $datasource_save ['ds_api_name'] = 'api/pingjia/morecommentary';
  117. $datasource_save ['create_time'] = time ();
  118. $datasource_save ['block_id'] = intval ( $ci->input->get ( 'block_id' ) );
  119. $ci->db->insert ( 'cms_datasource' , $datasource_save );
  120. echo "<script>if(parent.window.close_dialog){parent.window.close_dialog();}</script>";
  121. } else {
  122. $this->show_form ();
  123. if ($tip) {
  124. echo "类目<font color = red>{$tip}</font>尚无符合要求的评论,请检查!";
  125. } else {
  126. echo "请检查输入格式";
  127. }
  128. }
  129. }
  130. private function do_get_api($url) {
  131. $content = null;
  132. $ch = curl_init ();
  133. curl_setopt ( $ch , CURLOPT_URL , $url );
  134. curl_setopt ( $ch , CURLOPT_HEADER , 0 );
  135. curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , 1 );
  136. curl_setopt ( $ch , 156 , 500 ); // 156连接超时
  137. curl_setopt ( $ch , 155 , 3000 ); // 155总执行超时
  138. $content = curl_exec ( $ch );
  139. $curl_errno = curl_errno ( $ch );
  140. $curl_error = curl_error ( $ch );
  141. curl_close ( $ch );
  142. if ($curl_errno > 0) {
  143. // echo "ERROR,接口挂了:$url";
  144. return null;
  145. }
  146. return $content;
  147. }
  148. private function do_post_api($api_server, $api, $req_body, $port = 9091) {
  149. $ch = curl_init ();
  150. curl_setopt ( $ch , CURLOPT_URL , "$api_server$api" );
  151. curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , 1 );
  152. @curl_setopt ( $ch , CURLOPT_TIMEOUT_MS , 2000 );
  153. curl_setopt ( $ch , CURLOPT_POST , 1 );
  154. curl_setopt ( $ch , CURLOPT_POSTFIELDS , $req_body );
  155. $data = curl_exec ( $ch );
  156. $curl_errno = curl_errno ( $ch );
  157. $curl_error = curl_error ( $ch );
  158. curl_close ( $ch );
  159. if ($curl_errno > 0) {
  160. exit ( "function do_post_api error; <br>\ncURL Error ({$curl_errno}): {$curl_error}<br>\n{$api_server}{$api}<br>\n$req_body" );
  161. }
  162. return $data;
  163. }
  164. }