123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 |
- <?php
- namespace Common\Controller;
- use Think\Controller;
- class ApiController extends Controller {
- public $userId = 0;
- public function __construct(){
- parent::__construct();
- $token = I('token', '');
- if(ACTION_NAME=='push'){
- return;
- }
- if (I('debug')==1){
- $this->userId = I('user_id','1000001');
- }elseif (!empty($token)){
- $user = S('user_'.$token);
- if (!empty($user)){
- $user = json_decode($user,true);
- }else{
- $data = array('code'=>2,'msg'=>'Token失效, 请重新登录!');
- $this->ajaxReturn($data);
- }
- $this->userId = $user['user_id'];
- }else{
- $this->userId = session('user_id');
- }
- if ($this->userId<0){
- $data = array('code'=>2,'msg'=>'Token失效, 请重新登录!');
- $this->ajaxReturn($data);
- }
- $info = M('User')->where(array('user_id'=>$this->userId))->find();
- $imei = I('user_imei');
- if (empty($imei)){
- $imei = $_SERVER['HTTP_IMEI'];
- }
- if (!empty($imei) && !empty($info['imei']) && $imei!=$info['imei']){
- $title = '已在另一地点登录,如非本人操作,请及时修改密码!';
- $data = array('code'=>2,'msg'=>$title);
- $this->returnData($data);
- }
- if ($info==NULL){
- $data = array('code'=>2,'msg'=>'用户id错误');
- $this->ajaxReturn($data);
- }
- //S('userInfo'.$this->userId,$info);
- }
-
- //获取单词释义字符串
- public function getWordsText($row){
- $str = '';
- $wordsKeys = array('words_n','words_pron','words_adj','words_num','words_v','words_adv','words_art','words_prep','words_conj','words_int','words_vt','words_vi','words_other',);
- foreach ($wordsKeys as $key){
- if (!empty($row[$key])){
- $arr = explode('_', $key);
- $str .= $arr[1].'.'.$row[$key];
- }
- }
- return $str;
- }
-
- //获取单词释义字符串
- public function getWordsText1($row,$type){
- $str = array();
- $wordsKeys = array('words_n','words_pron','words_adj','words_num','words_v','words_adv','words_art','words_prep','words_conj','words_int','words_vt','words_vi','words_other',);
- foreach ($wordsKeys as $key){
- if (!empty($row[$key])){
- $arr = explode('_', $key);
- if ($type==2){
- $str[] = $row[$key];
- }else{
- $str[] = $arr[1].'. '.$row[$key];
- }
- }
- }
- if ($type==4){
- return mb_substr(implode($str, ' '), 0, 60,'UTF-8');
- }else{
- return $str;
- }
- }
-
- /**
- * 发送短信注册码
- * @param unknown $mobile
- * @param unknown $code
- */
- public function sendPhone($mobile, $code){
- Vendor('Sms.SmsSingleSender');
- $smsConf = C('SMS_CONFIG');
- $sender=new \SmsSingleSender($smsConf['appId'], $smsConf['appKey']);
- $result = $sender->send(0, "86", $mobile,
- "您在碰词儿的验证码是".$code.",请于5分钟内填写。如非本人操作,请忽略本短信。", "", "");
- $rsp = json_decode($result,true);
- if(isset($rsp['result']) && $rsp['result']==0){
- return TRUE;
- }else{
- return FALSE;
- }
- }
-
- //获取课程名称相同时,新的课程名称
- public function getCourseName($courseName){
- $course = M('Course')->where(array('course_name'=>$courseName,'is_delete'=>1))->field('course_id')->find();
- $res = M('TitleNum')->where(array('title_name'=>$courseName))->field('title_id,title_num')->find();
- if ($res!=NULL){
- $courseName = $courseName.'('.($res['title_num']+1).')';
- M('TitleNum')->where(array('title_id'=>$res['title_id']))->setInc('title_num');
- }else if($course!=NULL){
- $courseName = $courseName.'('.($res['title_num']+1).')';
- M('TitleNum')->add(array('title_name'=>$courseName,'operate_dt'=>time()));
- //$courseName = $courseName;
- }
- return $courseName;
- }
-
- //复制一份课程下面的单词
- public function copyWords($oldCourseId,$newCourseId,$wordsId=''){
- $wordsSortInfo = M('WordsSort')->where(array('course_id'=>$oldCourseId,'user_id'=>$this->userId))->field('sort_id')->find();
- if ($wordsSortInfo != NULL){
- M('WordsSort')->where(array('sort_id'=>$wordsSortInfo['sort_id']))->save(array('course_id'=>$newCourseId));
- }
- if (empty($wordsId)){
- $wordsList = M('Words')->where(array('course_id'=>$oldCourseId,'is_delete'=>1))->order('words_id asc')->select();
- }else{
- $wordsList = M('Words')->where(array('course_id'=>$oldCourseId,'is_delete'=>1,'words_id'=>array('NOT IN',$wordsId),))->order('words_id asc')->select();
- }
- foreach ($wordsList as $words){
- if (empty($words['words_name'])){
- continue;
- }
- $words['s_words_id']=$words['words_id'];
- unset($words['words_id']);
- $words['course_id'] = $newCourseId;
- $words['words_source'] = 2;
- $words['operate_dt'] = time();
- M('Words')->add($words);
- }
- }
-
- //推送消息
- public function pushMsg($content, $userId=''){
- $httpCurl = new \Org\Net\HttpCurl();
- $pushApiUrl = 'http://127.0.0.1:2121/';
- if (is_array($userId)){
- foreach ($userId as $uid){
- $data = array(
- 'type' => 'publish',
- 'content' => $content,
- //'content' => '{"code": 0,"msg": "已应战,请准备开始对战!","data": {"record_id": "11","user_id": "1000005","ref_user_id": "1000010"}}',
- 'to' => $uid,
- );
- $res = $httpCurl->post($pushApiUrl, $data, '');
- if ($res=='offline'){
- //return $uid;
- return $this->retryPushMsg($content, $uid);
- }
- }
- return FALSE;
- }else if(!empty($userId)){
- $data = array(
- 'type' => 'publish',
- 'content' => $content,
- 'to' => $userId,
- );
- $res = $httpCurl->post($pushApiUrl, $data, '');
- if ($res=='offline'){
- //return $userId;
- return $this->retryPushMsg($content, $userId);
- }
- return FALSE;
- }else{
- $data = array(
- 'type' => 'publish',
- 'content' => $content,
- 'to' => '',
- );
- $res = $httpCurl->post($pushApiUrl, $data, '');
- if ($res=='offline'){
- //return $uid;
- return $this->retryPushMsg($content, $userId);
- }
- return FALSE;
- }
- }
-
- //重试socket推送消息
- private function retryPushMsg($content, $to_user, $retry=2){
- sleep(2);
- $pushApiUrl = 'http://127.0.0.1:2121/';
- $httpCurl = new \Org\Net\HttpCurl();
- $data = array(
- 'type' => 'publish',
- 'content' => $content,
- 'to' => $to_user,
- );
- $res = $httpCurl->post($pushApiUrl, $data, '');
- if ($res=='offline' && $retry==2){
- $this->retryPushMsg($content, $to_user, 3);
- }
- if ($res=='offline'){
- return $to_user;
- }
- return FALSE;
- }
-
- //获取在线用户
- public function online(){
- $pushApiUrl = 'http://127.0.0.1:2121/';
- $httpCurl = new \Org\Net\HttpCurl();
- $data = array('type' => 'online',);
- $res = $httpCurl->post($pushApiUrl, $data, '');
- return json_decode($res,TRUE);
- }
-
- /**
- * 发送友盟推送消息
- * @param integer $uid 用户id
- * @param string $title 推送的标题
- * @param integer $type 1:官方小秘书 2:我的评论
- * @return boolear 是否成功
- */
- public function umengPush($uid,$title){
- // 获取token
- $userInfo = D('User')->where(array('user_id'=>$uid))->field('access_token')->find();
- // 如果没有token说明移动端没有登录;则不发送通知
- if ($userInfo==NULL || empty($userInfo['access_token'])) {
- return false;
- }
- $device_tokens = $userInfo['access_token'];
- // 导入友盟
- Vendor('Umeng.Umeng');
- // 自定义字段 根据实际环境分配;如果不用可以忽略
- $status=1;
- // 消息未读总数统计 根据实际环境获取未读的消息总数 此数量会显示在app图标右上角
- $count_number=1;
- $data=array(
- 'key'=>'status',
- 'value'=>"$status",
- 'count_number'=>$count_number
- );
- // 判断device_token 64位表示为苹果 否则为安卓
- if(strlen($device_tokens)==64){
- $key=C('UMENG_IOS_APP_KEY');
- $timestamp=C('UMENG_IOS_SECRET');
- $umeng=new \Umeng($key, $timestamp);
- $umeng->sendIOSUnicast($data,$title,$device_tokens);
- }else{
- $key=C('UMENG_ANDROID_APP_KEY');
- $timestamp=C('UMENG_ANDROID_SECRET');
- $umeng=new \Umeng($key, $timestamp);
- $umeng->sendAndroidUnicast($data,$title,$device_tokens);
- }
- return true;
- }
-
- //返回数据
- public function returnData($data){
- if (!is_array($data['data'])){
- $data['data'] = new \stdClass();
- }
- $this->ajaxReturn($data);
- }
-
- //获取好友昵称
- public function getUserNickname(){
- $userNameArr = S($this->userId.'_user_nickname');
- if (!$userNameArr && !is_array($userNameArr)){
- $condition['user_id|ref_user_id'] = $this->userId;
- $condition['friend_status'] = 2;
- $res = M('Friend')->where($condition)->field('user_id,ref_user_id,user_nickname,ref_user_nickname')->order('friend_id desc')->select();
- if ($res==NULL){
- $userNameArr = array();
- }
- $userNameArr = array();
- foreach ($res as $r){
- if ($r['user_id']==$this->userId){
- $userNameArr[$r['ref_user_id']] = $r['ref_user_nickname'];
- }else{
- $userNameArr[$r['user_id']] = $r['user_nickname'];
- }
- }
- S($this->userId.'_user_nickname',$userNameArr);
- }
- return $userNameArr;
- }
-
- /**
- * 添加队列(lpush)
- * @param string $value
- * @return int 队列长度
- */
- public function pushQueue($key, $value){
- //Vendor('Sms.SmsSingleSender');
- //$sender=new \SmsSingleSender($smsConf['appId'], $smsConf['appKey']);
- $queue = new \Com\Queue();
- //$queueName = 'pk_words_'.$recordId;
- $queue->setQueueName($key);
- $queueVal = serialize($value);
- return $queue->push($queueVal);
- }
-
- /**
- * 读取队列(brpop)
- * @return string|nil
- */
- public function popQueue($key){
- $queue = new \Com\Queue();
- //$queueName = 'pk_words_'.$recordId;
- $queue->setQueueName($key);
- $queueVal = $queue->pop();
- if (empty($queueVal)){
- return FALSE;
- }else{
- return unserialize($queueVal);
- }
- }
-
- public function flushQueue($key){
- $queue = new \Com\Queue();
- $queue->setQueueName($key);
- $queue->flushQueue();
- }
-
- //添加循环队列
- public function rpushQueue($key,$value){
- $queue = new \Com\RingQueue();
- //$queueName = 'pk_words_'.$recordId;
- $queue->setQueueName($key);
- $queueVal = serialize($value);
- return $queue->push($queueVal);
- }
-
- //读取循环队列
- public function rpopQueue($key){
- $queue = new \Com\RingQueue();
- //$queueName = 'pk_words_'.$recordId;
- $queue->setQueueName($key);
- $queueVal = $queue->pop();
- if (empty($queueVal)){
- return FALSE;
- }else{
- return unserialize($queueVal);
- }
- }
-
- /**
- * 连接redis
- * @return \Redis
- */
- public function getRedis(){
-
- if ( !extension_loaded('redis') ) {
- E(L('_NOT_SUPPORT_').':redis');
- }
- $options = array(
- 'host' => C('REDIS_HOST') ? : '127.0.0.1',
- 'port' => C('REDIS_PORT') ? : 6379,
- 'timeout' => C('DATA_CACHE_TIMEOUT') ? : false,
- 'expire'=>3600,
- 'persistent' => false,
- );
-
- $this->options = $options;
- $this->options['expire'] = isset($options['expire'])? $options['expire'] : C('DATA_CACHE_TIME');
- $this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX');
- $this->options['length'] = isset($options['length'])? $options['length'] : 0;
- $func = $options['persistent'] ? 'pconnect' : 'connect';
- $this->redis = new \Redis;
- $options['timeout'] === false ?
- $this->redis->$func($options['host'], $options['port']) :
- $this->redis->$func($options['host'], $options['port'], $options['timeout']);
-
- //添加redis 密码
- if(C('REDIS_AUTH')){
- $this->redis->auth(C('REDIS_AUTH'));
- }
- return $this->redis;
- }
-
- }
|