Api.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3. require_once APPPATH.'libraries/HtmlClean/HTMLPurifier.auto.php';
  4. class Api extends Public_Controller
  5. {
  6. function __construct()
  7. {
  8. parent::__construct();
  9. $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
  10. $this->load->model("user_model");
  11. $this->load->model("token_model");
  12. $this->load->model("branch_model");
  13. $this->load->model("workorder_model");
  14. $this->load->model("warning_model");
  15. $this->load->model("appauth_model");
  16. $this->load->library('MY_pagination');
  17. $this->load->helper(array('url', 'date', 'sms'));
  18. }
  19. public function get_token()
  20. {
  21. $msg = array();
  22. $appid = $this->input->get("appid", true);
  23. $secret = $this->input->get("secret", true);
  24. $appauth = $this->appauth_model->get_appauth_with_id($appid);
  25. if ($appid && $secret && $appauth && $secret == $appauth['secret'] && $appauth['status']=="10") {
  26. $data['token'] = $token = $this->create_token();
  27. $expires = new DateTime();
  28. $expires->modify("+2 hours");
  29. $data['expires_time'] = new MongoDB\BSON\UTCDateTime($expires->getTimestamp()*1000);
  30. if ($this->token_model->save_token($data)) {
  31. $msg['errcode'] = 0;
  32. $msg['errmsg'] = "ok";
  33. $msg['access_token'] = $token;
  34. $msg['expires_in'] = 7200;
  35. } else {
  36. $msg['errcode'] = 40013;
  37. $msg['errmsg'] = "invalid appid";
  38. }
  39. } else {
  40. $msg['errcode'] = 40013;
  41. $msg['errmsg'] = "invalid appid";
  42. }
  43. $this->response($msg);
  44. }
  45. public function upload_data()
  46. {
  47. $access_token = $this->input->get("access_token", true);
  48. $msg = array();
  49. if ($access_token && $this->token_exists($access_token)) {
  50. $json = $this->get_json();
  51. $data = json_decode($json, true);
  52. if(count($data)>=1){
  53. $upload_info = $data['upload_info'];
  54. $device = $data['device'];
  55. if($upload_info['type'] == "20"){
  56. $branch_list = $data['data'];
  57. if (count($branch_list) > 0) {
  58. foreach ($branch_list as $key => $val) {
  59. $val['device'] = $device;
  60. $this->branch_model->save_branch($val);
  61. $this->update_branch_cache();
  62. }
  63. }
  64. }elseif($upload_info['type'] == "10") {
  65. $warning_list = $data['data'];
  66. if (count($warning_list) > 0) {
  67. $config = HTMLPurifier_Config::createDefault();
  68. $purifier = new HTMLPurifier($config);
  69. foreach ($warning_list as $key => $val) {
  70. if(array_key_exists("warning_name",$val)){
  71. $val['warning_name'] = $purifier->purify($val['warning_name']);
  72. }
  73. $val['status'] = "10";
  74. $val['create_time'] = new MongoDB\BSON\UTCDateTime(time()*1000);
  75. $val['device'] = $device;
  76. $this->warning_model->insert_warning($val);
  77. }
  78. }
  79. }
  80. }
  81. $msg['errcode'] = 0;
  82. $msg['errmsg'] = "OK";
  83. } else {
  84. $msg['errcode'] = 40012;
  85. $msg['errmsg'] = "invalid access token";
  86. }
  87. $this->response($msg);
  88. }
  89. public function update_branch_cache(){
  90. $branch_list = $this->branch_model->select_branch();
  91. if($branch_list){
  92. $branch = array();
  93. foreach($branch_list as $key=>$val){
  94. $branch[$val['branch_id']] = $val;
  95. }
  96. $this->cache->save('branchs', $branch, 30000);
  97. }else{
  98. $this->cache->save('branchs', array(), 30000);
  99. }
  100. }
  101. public function get_status()
  102. {
  103. $access_token = $this->input->get("access_token", true);
  104. $msg = array();
  105. if ($access_token && $this->token_exists($access_token)) {
  106. $json = $this->get_json();
  107. $data = json_decode($json, true);
  108. if(array_key_exists("id",$data)){
  109. $ids = $data['id'];
  110. for($i = 0;$i<count($ids);$i++){
  111. $waning = $this->warning_model->select_warning(array("status"),$ids[$i]);
  112. if($waning){
  113. if($waning['status'] == "40"){
  114. $msg[$ids[$i]] = true;
  115. }else {
  116. $msg[$ids[$i]] = false;
  117. }
  118. }else{
  119. $msg[$ids[$i]] = false;
  120. }
  121. }
  122. }
  123. $msg['errcode'] = 0;
  124. $msg['errmsg'] = "OK";
  125. } else {
  126. $msg['errcode'] = 40012;
  127. $msg['errmsg'] = "invalid access token";
  128. }
  129. $this->response($msg);
  130. }
  131. public function get_login_url()
  132. {
  133. $access_token = $this->input->get("access_token", true);
  134. $msg = array();
  135. if ($access_token && $this->token_exists($access_token)) {
  136. $json = $this->get_json();
  137. $data = json_decode($json, true);
  138. $user_model = $this->user_model;
  139. //$user_model->set_collection_name("sso_users");
  140. if(array_key_exists('type',$data)) {
  141. if($data['type'] == 1 || $data['type'] == '1') {
  142. if ($user_model->is_exists("username", $data['username'])) {
  143. $info['last_login_time'] = new MongoDB\BSON\UTCDateTime(time() * 1000);
  144. $info['code'] = $this->create_token();
  145. $info['from'] = $data['from'];
  146. $user_model->set_val($info, array("username" => $data['username']));
  147. $msg["login_url"] = site_url("v1/api/sso_login?code=" . $info['code']);
  148. } else {
  149. $data['user_id'] = $this->create_id();
  150. $data['status'] = "10";
  151. $data['create_time'] = new MongoDB\BSON\UTCDateTime(time() * 1000);
  152. $data['last_login_time'] = new MongoDB\BSON\UTCDateTime(time() * 1000);
  153. $data['code'] = $this->create_token();
  154. $user_model->insert_user($data);
  155. $msg["login_url"] = site_url("v1/api/sso_login?code=" . $data['code']);
  156. }
  157. $msg['errcode'] = 0;
  158. $msg['errmsg'] = "OK";
  159. }else{
  160. $msg['errcode'] = 40013;
  161. $msg['errmsg'] = "该类型的用户允许免登录!";
  162. }
  163. }else{
  164. $msg['errcode'] = 40014;
  165. $msg['errmsg'] = "参数错误,未知的用户类型!";
  166. }
  167. } else {
  168. $msg['errcode'] = 40012;
  169. $msg['errmsg'] = "invalid access token";
  170. }
  171. $this->response($msg);
  172. }
  173. public function sso_login(){
  174. $code = $this->input->get("code", true);
  175. $msg = array();
  176. $user_model = $this->user_model;
  177. //$user_model->set_collection_name("sso_users");
  178. $sso_user = $user_model->get_user_with_code($code);
  179. if($code && $sso_user){
  180. $sso_user['code'] = "";
  181. $rand = substr(md5(microtime()), rand(0, 26), 5);
  182. $session_data = array("user_id" => (string)$sso_user["user_id"], "current_key" => $rand, "username" => $sso_user['username'], "user_type" => $sso_user['user_type'], "from" => $sso_user['from']);
  183. $sso_user['last_login_time'] = new MongoDB\BSON\UTCDateTime(time()*1000);
  184. $user_model->update_user($sso_user);
  185. $this->session->set_userdata($session_data);
  186. redirect("main/index");
  187. }else{
  188. show_error("invalid code!");
  189. }
  190. }
  191. public function send_sms(){
  192. $access_token = $this->input->get("access_token", true);
  193. $msg = array();
  194. if ($access_token && $this->token_exists($access_token)) {
  195. $json = $this->get_json();
  196. $data = json_decode($json, true);
  197. if(count($data)==2 && array_key_exists("mobile",$data) && array_key_exists("content",$data)){
  198. for($i = 0; $i< count($data['mobile']);$i++){
  199. $number = $data['mobile'][$i];
  200. if(send_by_modem($this->cache->get('setting'),$number,$data['content'])){
  201. $msg[$number]=true;
  202. }else{
  203. $msg[$number]=false;
  204. }
  205. }
  206. }
  207. $msg['errcode'] = 0;
  208. $msg['errmsg'] = "OK";
  209. }else {
  210. $msg['errcode'] = 40012;
  211. $msg['errmsg'] = "invalid access token";
  212. }
  213. $this->response($msg);
  214. }
  215. private function create_token()
  216. {
  217. do {
  218. $salt = base_convert(bin2hex($this->security->get_random_bytes(64)), 11, 36);
  219. if ($salt === FALSE) {
  220. $salt = hash('sha256', time() . mt_rand());
  221. }
  222. $token = substr($salt, 0, config_item('key_length'));
  223. } while ($this->token_exists($token));
  224. return $token;
  225. }
  226. private function token_exists($token)
  227. {
  228. if ($token) {
  229. if ($this->token_model->find_token($token, new MongoDB\BSON\UTCDateTime(time()*1000))) {
  230. return true;
  231. } else {
  232. return false;
  233. }
  234. } else {
  235. return false;
  236. }
  237. }
  238. private function get_json(){
  239. $postjson = file_get_contents('php://input');
  240. return $postjson;
  241. }
  242. }