Token.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /*
  3. * Token
  4. */
  5. class Api_Token extends PhalApi_Api {
  6. public function getRules() {
  7. return array(
  8. 'getToken'=>array(
  9. 'uid' => array('name' => 'uid', 'type' => 'int', 'min' => 1, 'require' => true, 'desc' => '用户ID'),
  10. 'token' => array('name' => 'token', 'type' => 'string', 'require' => true, 'desc' => '用户token'),
  11. ),
  12. 'setToken'=>array(
  13. 'param' => array('name' => 'param', 'type' => 'string', 'require' => true, 'desc' => '参数'),
  14. ),
  15. );
  16. }
  17. /**
  18. * 获取Token
  19. * @desc 用于获取Token
  20. * @return int code 操作码,0表示成功
  21. * @return array info
  22. * @return string msg 提示信息
  23. */
  24. public function getToken() {
  25. $rs = array('code' => 0, 'msg' => '', 'info' => array());
  26. $uid=checkNull($this->uid);
  27. $token=checkNull($this->token);
  28. $isBlackUser=isBlackUser($uid);
  29. if($isBlackUser=='0'){
  30. $rs['code'] = 700;
  31. $rs['msg'] = '该账号已被禁用';
  32. return $rs;
  33. }
  34. $checkToken=checkToken($uid,$token);
  35. if($checkToken==700){
  36. $rs['code'] = $checkToken;
  37. $rs['msg'] = '您的登陆状态失效,请重新登陆!';
  38. return $rs;
  39. }else if($checkToken==10020){
  40. $rs['code'] = 700;
  41. $rs['msg'] = '该账号已被禁用';
  42. return $rs;
  43. }
  44. $domain = new Domain_Auth();
  45. $info = $domain->getAuth($uid);
  46. $rs['info'][0]=$info;
  47. return $rs;
  48. }
  49. /**
  50. * @param int $type 1生成昵称,2生成姓名
  51. * //汉语 - 给用户自动生成昵称
  52. */
  53. function nickname(): string
  54. {
  55. // 定义一个随机名字数组
  56. $firstNames = array('赵','钱','孙','李','周','吴','郑','王','冯','陈','褚','卫','蒋','沈','韩','杨',
  57. '朱','秦','尤','许','何','吕','施','张','孔','曹','严','华','金','魏','陶','姜','戚','谢','邹','喻',
  58. '柏','水','窦','章','云','苏','潘','葛','奚','范','彭','郎','鲁','韦','昌','马','苗','凤','花','方',
  59. '任','袁','柳','鲍','史','唐','费','薛','雷','贺','倪','汤','滕','殷','罗','毕','郝','安','常','傅',
  60. '卞','齐','元','顾','孟','平','黄','穆','萧','尹','姚','邵','湛','汪','祁','毛','狄','米','伏','成',
  61. '戴','谈','宋','茅','庞','熊','纪','舒','屈','项','祝','董','梁','杜','阮','蓝','闵','季','贾','路',
  62. '娄','江','童','颜','郭','梅','盛','林','钟','徐','邱','骆','高','夏','蔡','田','樊','胡','凌','霍',
  63. '虞','万','支','柯','管','卢','莫','柯','房','裘','缪','解','应','宗','丁','宣','邓','单','杭','洪',
  64. '包','诸','左','石','崔','吉','龚','程','嵇','邢','裴','陆','荣','翁','荀','于','惠','甄','曲','封',
  65. '储','仲','伊','宁','仇','甘','武','符','刘','景','詹','龙','叶','幸','司','黎','溥','印','怀','蒲',
  66. '邰','从','索','赖','卓','屠','池','乔','胥','闻','莘','党','翟','谭','贡','劳','逄','姬','申','扶',
  67. '堵','冉','宰','雍','桑','寿','通','燕','浦','尚','农','温','别','庄','晏','柴','瞿','阎','连','习',
  68. '容','向','古','易','廖','庾','终','步','都','耿','满','弘','匡','国','文','寇','广','禄','阙','东',
  69. '欧','利','师','巩','聂','关','荆','司马','上官','欧阳','夏侯','诸葛','闻人','东方','赫连','皇甫',
  70. '尉迟','公羊','澹台','公冶','宗政','濮阳','淳于','单于','太叔','申屠','公孙','仲孙','轩辕','令狐',
  71. '徐离','宇文','长孙','慕容','司徒','司空');
  72. $lastNames = array('伟','刚','勇','毅','俊','峰','强','军','平','保','东','文','辉','力','明','永',
  73. '健','世','广','志','义','兴','良','海','山','仁','波','宁','贵','福','生','龙','元','全','国','胜',
  74. '学','祥','才','发','武','新','利','清','飞','彬','富','顺','信','子','杰','涛','昌','成','康','星',
  75. '光','天','达','安','岩','中','茂','进','林','有','坚','和','彪','博','诚','先','敬','震','振','壮',
  76. '会','思','群','豪','心','邦','承','乐','绍','功','松','善','厚','庆','磊','民','友','裕','河','哲',
  77. '江','超','浩','亮','政','谦','亨','奇','固','之','轮','翰','朗','伯','宏','言','若','鸣','朋','斌',
  78. '梁','栋','维','启','克','伦','翔','旭','鹏','泽','晨','辰','士','以','建','家','致','树','炎','德',
  79. '行','时','泰','盛','雄','琛','钧','冠','策','腾','楠','榕','风','航','弘','秀','娟','英','华','慧',
  80. '巧','美','娜','静','淑','惠','珠','翠','雅','芝','玉','萍','红','娥','玲','芬','芳','燕','彩','春',
  81. '菊','兰','凤','洁','梅','琳','素','云','莲','真','环','雪','荣','爱','妹','霞','香','月','莺','媛',
  82. '艳','瑞','凡','佳','嘉','琼','勤','珍','贞','莉','桂','娣','叶','璧','璐','娅','琦','晶','妍','茜',
  83. '秋','珊','莎','锦','黛','青','倩','婷','姣','婉','娴','瑾','颖','露','瑶','怡','婵','雁','蓓','纨',
  84. '仪','荷','丹','蓉','眉','君','琴','蕊','薇','菁','梦','岚','苑','婕','馨','瑗','琰','韵','融','园',
  85. '艺','咏','卿','聪','澜','纯','毓','悦','昭','冰','爽','琬','茗','羽','希','欣','飘','育','滢','馥',
  86. '筠','柔','竹','霭','凝','晓','欢','霄','枫','芸','菲','寒','伊','亚','宜','可','姬','舒','影','荔',
  87. '枝','丽','阳','妮','宝','贝','初','程','梵','罡','恒','鸿','桦','骅','剑','娇','纪','宽','苛','灵',
  88. '玛','媚','琪','晴','容','睿','烁','堂','唯','威','韦','雯','苇','萱','阅','彦','宇','雨','洋','忠',
  89. '宗','曼','紫','逸','贤','蝶','菡','绿','蓝','儿','翠','烟');
  90. // 随机选择姓和名
  91. $firstName = $firstNames[array_rand($firstNames)];
  92. $lastName = $lastNames[array_rand($lastNames)];
  93. // 生成随机的3位数字
  94. $randomNumber = mt_rand(100, 999);
  95. // 拼接姓名和随机数字
  96. return $firstName . $lastName . $randomNumber;
  97. }
  98. /**
  99. * 设置Token
  100. * @desc 用于生成用户Token
  101. * @return int code 操作码,0表示成功
  102. * @return array info
  103. * @return string msg 提示信息
  104. */
  105. public function setToken() {
  106. $rs = array('code' => 0, 'msg' => '已生成token', 'info' => array());
  107. // 前端传过来的唯一值
  108. $param=checkNull($this->param);
  109. $num = DI()->redis->exists('token_'.$param);
  110. if ($num == 0) {
  111. // $token=md5(md5($param.time()));
  112. $nowtime = time();
  113. $expiretime = $nowtime + 60 * 60 * 24 * 300;
  114. $rs['info'] = [
  115. 'uid' => $param,
  116. 'token' => $param,
  117. 'expire_time' => $expiretime,
  118. "id" => $param,
  119. "user_nicename" => $this->nickname(),
  120. "avatar" => "/default.jpg",
  121. "avatar_thumb" => "/default_thumb.jpg",
  122. "sex" => "1",
  123. "signature" => "这家伙很懒,什么都没留下",
  124. "consumption" => "1204",
  125. "votestotal" => "311",
  126. "province" => "",
  127. "city" => "",
  128. "birthday" => "0",
  129. "user_status" => "1",
  130. "location" => ""
  131. ];
  132. setcaches("token_" . $param, $rs['info'], $expiretime);
  133. return $rs;
  134. }
  135. else{
  136. $json = DI()->redis->get('token_'.$param);
  137. $rs['info'] = json_decode($json, JSON_UNESCAPED_UNICODE);
  138. return $rs;
  139. }
  140. }
  141. }