Exemple #1
0
 /**
  * 从缓存获取联系人列表或联系人详情
  * @param int $user_id  用户ID
  * @param int $id       联系人ID
  * @param string $callback id为NULL时,允许设置回调方法
  * @param int $info     无实际用途,仅兼容原方法
  * @return array|Contact|bool
  */
 public function get($user_id, $id = NULL, $callback = '', $info = 1)
 {
     $callback = $callback == '' ? 'get_list' : $callback;
     if ($id !== NULL) {
         $result = $this->cache->get($this->cache_pre . 'find_by_id_' . $user_id . '_' . $id);
     } else {
         $result = $this->cache->get($this->cache_pre . $callback . '_' . $user_id);
     }
     //重新生成缓存
     /*
     if ($id === NULL AND ! empty($result) AND ! isset($result[key($result)]['tels']))
     {
     	$result = NULL;
     }
     */
     if ($id === NULL and $result) {
         foreach ($result as $value) {
             if (!isset($value['id'])) {
                 $result = NULL;
                 break;
             }
         }
     }
     if (empty($result)) {
         if ($id !== NULL) {
             $result = call_user_func(array($this, 'find_by_id'), $user_id, $id);
             $this->cache->set($this->cache_pre . 'find_by_id_' . $user_id . '_' . $id, $result);
         } else {
             $result = call_user_func(array($this, $callback), $user_id);
             if ($callback == 'get_list') {
                 //获取电话信息
                 $tels = $this->contact_mapper->get_info_list($user_id, NULL, 'tels', FALSE, FALSE, array_merge(array('cid'), Contact::$allow_cols['tels']));
                 //获取邮箱信息
                 $emails = $this->contact_mapper->get_info_list($user_id, NULL, 'emails', FALSE, FALSE, array_merge(array('cid'), Contact::$allow_cols['emails']));
                 foreach ($tels as $value) {
                     $cid = $value['cid'];
                     unset($value['cid']);
                     if (in_array($cid, array_keys($result))) {
                         $result[$cid]['tels'][] = $value;
                     }
                 }
                 foreach ($emails as $value) {
                     $cid = $value['cid'];
                     unset($value['cid']);
                     if (in_array($cid, array_keys($result))) {
                         $result[$cid]['emails'][] = $value;
                     }
                 }
             }
             $this->cache->set($this->cache_pre . $callback . '_' . $user_id, $result);
         }
     }
     return $result;
 }