Api.php 8.9 KB

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