public function extendFuncGroups($fgList, $durationInSecs)
 {
     Log::record('WebsiteUserFuncManager extend:' . print_r($fgList, true) . ' secs:' . $durationInSecs . ' uid:' . $this->websiteUserId);
     Log::save();
     $currTime = time();
     $tb_userFG = M('user_func_group');
     $existingRelations = $tb_userFG->where('user_id=' . $this->websiteUserId)->select();
     $existingGroupId2RealtionMap = array();
     foreach ($existingRelations as $relation) {
         $existingGroupId2RealtionMap[$relation['group_id']] = $relation;
     }
     foreach ($fgList as $codeFgId) {
         if (in_array($codeFgId, array_keys($existingGroupId2RealtionMap))) {
             // 该功能已经存在关系表里,只需要进行update
             $tmpUserFuncGroup = $existingGroupId2RealtionMap[$codeFgId];
             $tmp = array();
             if (!empty($tmpUserFuncGroup['expire_time']) && $tmpUserFuncGroup['expire_time'] > $currTime) {
                 // 该功能未过期
                 $tmp['start_time'] = $tmpUserFuncGroup['start_time'];
                 $tmp['expire_time'] = $tmpUserFuncGroup['expire_time'] + $durationInSecs;
                 $tmp['status'] = 1;
             } else {
                 $tmp['start_time'] = $currTime;
                 $tmp['expire_time'] = $currTime + $durationInSecs;
                 $tmp['status'] = 1;
             }
             $tb_userFG->where('id=' . $tmpUserFuncGroup['id'])->setField($tmp);
         } else {
             $tmp = array();
             $tmp['user_id'] = $this->websiteUserId;
             $tmp['group_id'] = $codeFgId;
             $tmp['start_time'] = $currTime;
             $tmp['expire_time'] = $currTime + $durationInSecs;
             $tmp['status'] = 1;
             $tb_userFG->add($tmp);
         }
     }
     // 检查该用户是否已经配置公共账号,如果配置了,自动开通功能组对应的功能项,并复制模版数据。
     $publicAccount = M('wxuser')->where(array('uid' => $this->websiteUserId, 'status' => 1))->field('token')->find();
     if ($publicAccount) {
         //$websiteUserId, $uname, $token
         $userInfo = M('users')->where(array('id' => $this->websiteUserId))->field('username')->find();
         $publicAccountFuncManager = new PublicAccountFuncManager($this->websiteUserId, $userInfo['username'], $publicAccount['token']);
         foreach ($fgList as $fgId) {
             $publicAccountFuncManager->openSingleFuncGroup($fgId);
         }
     }
 }
 public function addNewPublic()
 {
     $public_db = M('Wxuser');
     $uid = session('uid');
     $public = $public_db->where(array('uid' => $uid, 'status' => 1))->find();
     if ($public != false) {
         $this->error('您当前已经存在一个活跃的公众号,请先将其删除再尝试添加。', U(MODULE_NAME . '/index'));
     }
     $wx_name = $_POST['wxname'];
     $wx_id = $_POST['weixin'];
     $wx_ori = $_POST['wxid'];
     $wx_type = $_POST['type'];
     $wx_appid = $_POST['appid'];
     $wx_appsecret = $_POST['appsecret'];
     $wx_qrcode = $_POST['qrcode_pic'];
     //已经有token
     $now = time();
     $data['wxname'] = $wx_name;
     $data['wxid'] = $wx_ori;
     //目前token跟微信id是同一个
     $data['weixin'] = $wx_id;
     $data['type'] = $wx_type;
     $data['headerpic'] = C('site_url') . '/themes/a/images/logo.jpg';
     $data['qrcode_pic'] = $wx_qrcode;
     $data['createtime'] = $now;
     $data['updatetime'] = $now;
     $data['appid'] = $wx_appid;
     $data['appsecret'] = $wx_appsecret;
     $public = $public_db->where(array('uid' => $uid, 'status' => 0))->find();
     if ($public != false) {
         $data['status'] = 1;
         $ret = $public_db->where(array('id' => $public['id']))->save($data);
         if ($ret) {
             // 根据用户功能组权限,自动为公共账号开通功能项
             $publicAccountFuncManager = new PublicAccountFuncManager($uid, session('uname'), $public['token']);
             $publicAccountFuncManager->openFunctions();
             $this->success('操作成功', U(MODULE_NAME . '/index'));
         } else {
             $this->success('添加失败,请联系客服', U(MODULE_NAME . '/index'));
         }
     } else {
         $newToken = $this->generateToken();
         $code = $this->generateCode();
         $wx_user = $public_db->where("token = '{$newToken}' or code={$code}")->find();
         if ($wx_user) {
             // 理论上不会到该分支,因为token的生成算法已经保证了全局唯一
             $this->error('抱歉,添加公众号失败,请刷新重试。', U(MODULE_NAME . '/add'));
         } else {
             $data['uid'] = session('uid');
             //目前token跟微信id是同一个
             $data['weixin'] = $newToken;
             $data['token'] = $newToken;
             //str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT)
             $data['code'] = $code;
             $data['status'] = 1;
             //初始化空微信号
             $publicAccountId = $public_db->add($data);
             if ($publicAccountId) {
                 // 根据用户功能组权限,自动为公共账号开通功能项
                 $publicAccountFuncManager = new PublicAccountFuncManager($uid, session('uname'), $newToken);
                 $publicAccountFuncManager->openFunctions();
                 $this->success('操作成功', U(MODULE_NAME . '/index'));
             } else {
                 $this->success('添加失败,请联系客服', U(MODULE_NAME . '/index'));
             }
         }
     }
 }