Appauth.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3. /**
  4. * Class 接入授权管理类
  5. */
  6. class Appauth extends MY_Controller
  7. {
  8. function __construct()
  9. {
  10. parent::__construct();
  11. $this->load->model("appauth_model");
  12. $this->load->library('MY_pagination');
  13. $this->load->helper(array('url', 'form', 'date'));
  14. $this->assign("appauth_status", $this->appauth_status);
  15. }
  16. /**
  17. * 接入授权列表页
  18. */
  19. public function index()
  20. {
  21. $url = site_url("appauth/index?");
  22. $wheres = array();
  23. $order_info = array();
  24. $keyword = $this->input->get("keyword", TRUE);
  25. $status = $this->input->get("status", TRUE);
  26. $order = $this->input->get("order", TRUE);
  27. $page_num = $this->input->get("per_page", TRUE);
  28. $page_size = $this->input->get("page_size", TRUE);
  29. if ($keyword) {
  30. $url .= "&keyword=" . $keyword;
  31. }
  32. if ($status) {
  33. $wheres['status'] = $status;
  34. $url .= "&status=" . $status;
  35. }
  36. if ($order) {
  37. $orders = explode(" ", $order);
  38. if (count($orders) == 2) {
  39. $order_info[$orders[0]] = $orders[1];
  40. $url .= "&order=" . $order;
  41. }
  42. }
  43. if ($page_size) {
  44. $this->page_size = $page_size;
  45. $url .= "&page_size=" . $page_size;
  46. }
  47. $count = $this->appauth_model->count_appauth($keyword, $wheres);
  48. $this->assign("count", $count);
  49. $config = $this->page_config($count, $this->page_size, $url);
  50. $this->my_pagination->initialize($config);
  51. if ($page_num && $page_num > 1) {
  52. $offset = (intval($page_num) - 1) * $this->page_size;
  53. } else {
  54. $offset = 0;
  55. }
  56. $appauth_list = $this->appauth_model->list_appauth($this->page_size, $offset, $keyword, $wheres, $order_info);
  57. $this->assign("keyword", $keyword);
  58. $this->assign("status", $status);
  59. $this->assign("order", $order);
  60. $this->assign("page_size", $this->page_size);
  61. $this->assign("page", $this->my_pagination->create_pages());
  62. $this->assign("appauth_list", $appauth_list);
  63. $this->display("appauth/index.html");
  64. }
  65. /**
  66. * 查看接入授权信息
  67. * @param $app_id APPID
  68. */
  69. public function view($app_id)
  70. {
  71. $message = "";
  72. $appauth = $this->appauth_model->get_appauth_with_id($app_id);
  73. if($appauth){
  74. $this->assign("appauth",$appauth);
  75. }else{
  76. $this->assign("appauth",$this->appauth_model->get_model());
  77. $message = "接入授权信息不存在或者已经被删除";
  78. }
  79. $this->assign("message",$message);
  80. $this->display("appauth/view.html");
  81. }
  82. /**
  83. * 增加接入授权
  84. */
  85. public function add()
  86. {
  87. $app_id = md5(uniqid(md5(microtime(true)), true));
  88. $secret = md5(uniqid(md5(microtime(true)), true));
  89. $this->assign("app_id", $app_id);
  90. $this->assign("secret", $secret);
  91. $this->display("appauth/add.html");
  92. }
  93. /**
  94. * 保存接入授权信息
  95. */
  96. public function save()
  97. {
  98. $msg = array();
  99. $data = array();
  100. $msg['code'] = 1;
  101. $msg['icon'] = 2;
  102. $data['app_id'] = $app_id = $this->input->post("app_id", true);
  103. $data['app_name'] = $app_name = $this->input->post("app_name", true);
  104. $data['secret'] = $secret = $this->input->post("secret", true);
  105. if (!$app_name) {
  106. $msg['code'] = 0;
  107. $msg['msg'] = "应用名称不能为空!";
  108. } elseif ($this->appauth_model->is_exists("app_name", $app_name) && $msg['code']) {
  109. $msg['code'] = 0;
  110. $msg['msg'] = "应用名称已存在!";
  111. }
  112. if (!$app_id && $msg['code']) {
  113. $msg['code'] = 0;
  114. $msg['msg'] = "APPID不能为空!";
  115. } elseif ($this->appauth_model->is_exists("app_id", $app_id) && $msg['code']) {
  116. $msg['code'] = 0;
  117. $msg['msg'] = "APPID已存在!请刷新后重试!";
  118. }
  119. if (!$secret && $msg['code']) {
  120. $msg['code'] = 0;
  121. $msg['msg'] = "SECRET不能为空!";
  122. }
  123. if ($msg['code']) {
  124. $data['create_time'] = new MongoDB\BSON\UTCDateTime(time()*1000);
  125. $data['status'] = "10";
  126. $this->appauth_model->insert_appauth($data);
  127. $msg['icon'] = 1;
  128. $msg['msg'] = "接入授权信息保存成功!";
  129. }
  130. $this->response($msg);
  131. }
  132. /**
  133. * 更新接入授权信息
  134. */
  135. public function update()
  136. {
  137. $msg = array();
  138. $data = array();
  139. $msg['code'] = 1;
  140. $msg['icon'] = 2;
  141. $msg = array();
  142. $data = array();
  143. $msg['code'] = 1;
  144. $msg['icon'] = 2;
  145. $data['app_id']= $app_id = $this->input->post("app_id", true);
  146. $old_appauth = $this->appauth_model->get_appauth_with_id($app_id);
  147. $data['app_name'] = $app_name = $this->input->post("app_name", true);
  148. $data['secret'] = $secret = $this->input->post("secret", true);
  149. if (!$app_name) {
  150. $msg['code'] = 0;
  151. $msg['msg'] = "应用名称不能为空!";
  152. } elseif ($this->appauth_model->is_exists("app_name", $app_name,$old_appauth['app_name']) && $msg['code']) {
  153. $msg['code'] = 0;
  154. $msg['msg'] = "应用名称已存在!";
  155. }
  156. if (!$secret && $msg['code']) {
  157. $msg['code'] = 0;
  158. $msg['msg'] = "SECRET不能为空!";
  159. }
  160. if ($msg['code']) {
  161. $this->appauth_model->update_appauth($data);
  162. $msg['icon'] = 1;
  163. $msg['msg'] = "接入授权信息保存成功!";
  164. }
  165. $this->response($msg);
  166. }
  167. /**
  168. * 删除接入授权
  169. * @param $app_id
  170. */
  171. public function delete($app_id)
  172. {
  173. $data['icon'] = 1;
  174. if ($app_id) {
  175. $appauth = $this->appauth_model->get_appauth_with_id($app_id);
  176. if ($appauth) {
  177. $this->appauth_model->delete_appauth($app_id);
  178. $data['msg'] = "接入授权信息删除成功!";
  179. } else {
  180. $data['icon'] = 2;
  181. $data['msg'] = "接入授权信息不存在或者已经被删除!";
  182. }
  183. } else {
  184. $data['icon'] = 2;
  185. $data['msg'] = "参数错误请刷新后重试!";
  186. }
  187. $this->response($data);
  188. }
  189. /**
  190. * 禁用接入授权
  191. * @param $app_id
  192. */
  193. public function stopuse($app_id)
  194. {
  195. $data['icon'] = 1;
  196. if ($app_id) {
  197. $appauth = $this->appauth_model->get_appauth_with_id($app_id);
  198. if ($appauth) {
  199. $appauth['status'] = "40";
  200. $this->appauth_model->update_appauth($appauth);
  201. $data['msg'] = "接入授权信息停用成功!";
  202. } else {
  203. $data['icon'] = 2;
  204. $data['msg'] = "接入授权信息不存在或者已经被删除!";
  205. }
  206. } else {
  207. $data['icon'] = 2;
  208. $data['msg'] = "参数错误请刷新后重试!";
  209. }
  210. $this->response($data);
  211. }
  212. /**
  213. * 启用接入授权
  214. * @param $app_id
  215. */
  216. public function restore($app_id)
  217. {
  218. $data['icon'] = 1;
  219. if ($app_id) {
  220. $appauth = $this->appauth_model->get_appauth_with_id($app_id);
  221. if ($appauth) {
  222. $appauth['status'] = "10";
  223. $this->appauth_model->update_appauth($appauth);
  224. $data['msg'] = "接入授权恢复成功!";
  225. } else {
  226. $data['icon'] = 2;
  227. $data['msg'] = "接入授权信息不存在或者已经被删除!";
  228. }
  229. } else {
  230. $data['icon'] = 2;
  231. $data['msg'] = "参数错误请刷新后重试!";
  232. }
  233. $this->response($data);
  234. }
  235. /**
  236. * 批量禁用接入授权
  237. */
  238. public function disable_all(){
  239. $ids = rtrim($this->input->post("ids",true),",");
  240. $ids = explode(",",$ids);
  241. if(count($ids)<1){
  242. $data['icon'] = 2;
  243. $data['msg'] = "参数错误请刷新后重试!";
  244. }else{
  245. $wheres = array('app_id'=>array('$in'=>$ids));
  246. $this->appauth_model->set_val("status",$wheres,"40");
  247. $data['icon'] = 1;
  248. $data['msg'] = "批量禁用接入授权成功!";
  249. }
  250. $this->response($data);
  251. }
  252. /**
  253. * 批量启用接入授权
  254. */
  255. public function enable_all(){
  256. $ids = rtrim($this->input->post("ids",true),",");
  257. $ids = explode(",",$ids);
  258. if(count($ids)<1){
  259. $data['icon'] = 2;
  260. $data['msg'] = "参数错误请刷新后重试!";
  261. }else{
  262. $wheres = array('app_id'=>array('$in'=>$ids));
  263. $this->appauth_model->set_val("status",$wheres,"10");
  264. $data['icon'] = 1;
  265. $data['msg'] = "批量启用接入授权成功!";
  266. }
  267. $this->response($data);
  268. }
  269. }