public function Model($tab = '', $dbname = null) { if ($tab != '') { $this->tab = $tab; } $this->db = Factory::getDb($dbname); }
public function __construct(Factory $factory) { $this->factory = $factory; $this->db = $factory->getDb(); $this->logger = $factory->getLogger(); $this->mainTable = $this->getMainTable(); $this->relationsTable = $this->getRelationsTable(); $this->createTables(); }
/** * 通过活动关键字获取活动信息 */ public function getInfoByActionKey($actionKey) { try{ $strsql = "SELECT * FROM `".DBConfig::$table['config']."` WHERE `key` = '{$actionKey}' LIMIT 1"; $res = Factory::getDb("weixin_active","MySql")->getRow($strsql); return (is_array($res) && $res) ? $res : array(); } catch (Exception $e) { Logger::error("通过关键字获取活动信息失败".$e->getmessage()); return false; } }
/** * 获取用户绑定信息 * @param string $uid 用户id * @param int $aid 活动id * @return int state 0-没有参与绑定 1-绑定用户 2-解绑用户 */ public function getBindingInfo($uid,$aid) { try{ $strsql = "SELECT * FROM ".DBConfig::$table['user']." WHERE `uid` = '{$uid}' AND `aid` = {$aid} LIMIT 1"; $res = Factory::getDb("weixin_active")->getRow($strsql); return ($res['state']) ? $res['state'] : 0; } catch (Exception $e) { Logger::error("获取用户绑定信息失败".$e->getMessage()); return false; } }
/** * 插入一条来源记录 * @param int $aid 活动id * @param int $scontent 来源值 * @param int $memo 备注信息(默认空字符) * @return int $sid 插入的id即(来源id) */ public function insertSource($aid,$scontent,$memo=""){ try { $scontent = mysql_real_escape_string($scontent); if($memo) $memo = mysql_real_escape_string($memo); $sid = Factory::getDb("weixin_active")->insert(DBConfig::$table['source'],array("aid"=>$aid,"svalue"=>$scontent,"memo"=>$memo)); return $sid; } catch (Exception $e) { Logger::error("插入一条来源记录失败".$e->getmessage()); return false; } }
/** * 插入业务数据或者更新之 */ public function updateData($uid,$aid,$wid,$key,$data) { try { $curTime = time(); $data = mysql_real_escape_string($data); $strsql = "INSERT INTO `".DBConfig::$table['data']."`(`uid`,`aid`,`wid`,`key`,`data`,`time`) VALUES ('{$uid}',{$aid},{$wid},'{$key}','{$data}',{$curTime}) ON DUPLICATE KEY UPDATE `filed`=`data`,`data`='{$data}',`time`={$curTime}"; Factory::getDb("weixin_active")->query($strsql); return Factory::getDb("weixin_active")->affectedRows(); } catch (Exception $e) { Logger::error("插入业务数据或者更新之失败".$e->getmessage()); return false; } }
/** * 插入记录 */ public function insertInfo($data) { if(!$uid = $data['uid']) return false; if(!$aid = $data['aid']) return false; if(!$wid = $data['wid']) return false; $name = $data['name']; $cardID = $data['cardID']; if(!$address = $data['address']) return false; $code = $data['code']; if(!$tel = $data['tel']) return false; if(!$rid = $data['rid']) return false; Factory::getDb("weixin_active")->insert(DBConfig::$table['mail'],array('uid'=>$uid,'aid'=>$aid,'wid'=>$wid,'name'=>$name,'cardID'=>$cardID,'address'=>$address,'code'=>$code,'tel'=>$tel,'rid'=>$rid,'time'=>time())); return Factory::getDb("weixin_active")->insertId(); }
/** * 插入一条操作记录或更新一条操作记录 */ public function updateRecord($uid,$aid,$key,$filed = "") { try { $curTime = time(); if($filed) { $strsql = "INSERT INTO `".DBConfig::$table['oper']."`(`uid`,`aid`,`operNum`,`operKey`,`operValue`,`time`) VALUES ('{$uid}',{$aid},1,'{$key}','{$filed}',{$curTime}) ON DUPLICATE KEY UPDATE `operNum`=`operNum`+1,`operValue`=CONCAT(`operValue`,'{$filed}'),`time`={$curTime}"; } else { $strsql = "INSERT INTO `".DBConfig::$table['oper']."`(`uid`,`aid`,`operNum`,`operKey`,`operValue`,`time`) VALUES ('{$uid}',{$aid},1,'{$key}','{$filed}',{$curTime}) ON DUPLICATE KEY UPDATE `operNum`=`operNum`+1,`time`={$curTime}"; } Factory::getDb("weixin_active")->query($strsql); return Factory::getDb("weixin_active")->affectedRows(); } catch (Exception $e) { Logger::error("插入业务数据或者更新之失败".$e->getmessage()); return false; } }
/** * 列表 */ public function create() { $tab_name = I('tab_name', ''); if (!empty($tab_name)) { $this->table = $tab_name; $db = Factory::getDb('DEFAULT_DB'); //得到具体的业务逻辑行数,进行tpl处理 $sql = "show full columns from {$tab_name}"; $list = $db->query($sql); if (empty($list)) { exit('数据表无法读取'); } $row_id = array_slice($list, 0, 1); $row_date = array_slice($list, -2, 2); $this->columns = array_slice($list, 1, -3); foreach ($this->columns as $v) { $this->field[] = $v['Field']; } //得到语句 $sql = "SHOW CREATE TABLE {$tab_name}"; $row = $db->query($sql); $tab = $row[0]['Create Table']; //生成add,tpl,edit tpl,list tpl $action_tpl = $add_tpl = $edit_tpl = $list_tpl = ''; $add_tpl = $this->getTpl('add'); $edit_tpl = $this->getTpl('edit'); $list_tpl = $this->getTpl('list'); $action_tpl = $this->getTpl('act'); $this->assign('table_ddl', $tab); $this->assign('tab_name', $tab_name); $this->assign("action_tpl", $action_tpl); $this->assign("add_tpl", $add_tpl); $this->assign("edit_tpl", $edit_tpl); $this->assign("list_tpl", $list_tpl); $this->assign("showType", 'tpl'); } $this->display(); }
/** * 获取db实例 * @param string $dbname 配置中数据库名称 * @param string $dbClassName db */ public function getDb ($dbname = null, $dbClassName = 'MySql') { return Factory::getDb($dbname, $dbClassName); }
/** * 处理关注事件 * * @param array $params [description] * * @return void */ public function subscrib(array $params) { if (!isset($params['openid']) || !isset($params['event_key'])) { return false; } // 只记录新关注的用户 // {"a":"Invitation","apiKey":"9d351e5444ca8328721988dc84234e3d","timestamp":"1446186825","sig":"72b2481e8696a09119406955daa489cd","openid":"onwtvsw1aSkZDwyeQtac7DElG9O8","event_key":"456"} // event_key 参数的数值带 “qrscene_xxx” 标识表示成功关注了的,只是数字的话表示 已关注过 重新扫描进来的。 $event_key = explode('_', $params['event_key']); // 入库并发放奖励 if ($event_key[0] == 'qrscene' && !empty($event_key[1])) { // 根据 qimg_id 获得 scene_id $ywcDb = Factory::getDb('YWC_DB'); $codeSceneParams = array( 'where' => array( 'scene_id' => $event_key[1], ), ); $sceneInfo = $ywcDb->get('wx_qr_code_scene', $codeSceneParams, true); if (empty($sceneInfo)) { // 获取用户信息失败 Logger::error('合伙人信息不存在! OpenId' . $params['openid']); return false; } // 根据 qimg_id 获得合伙人的 openId $invitationQrcParams = array( 'qimg_id' => intval($sceneInfo['qimg_id']), ); $qrInfo = loadModel('Index.InvitationQrc')->getDetail($invitationQrcParams); if (!empty($qrInfo)) { //关注后发送卡券领取消息 $source_openid = $qrInfo['open_id'] ? $qrInfo['open_id'] : ''; $url = 'http://sh.app.socialjia.com/5100App/www/partners.php?a=Draw&m=index&openid=' . $params['openid'] . '&source_openid=' . $source_openid . '&wechat_card_js=1'; $msg_text = "5100Skincare,<a href=\"{$url}\">领取优惠券</a>"; //关注后被动回复发送卡券领取消息 //echo (loadModel('Common')->msgText($params['openid'], '领取优惠券')); //发送消息 Logger::debug('关注后发送领取卡券信息', $params); loadModel('Common')->insertOneMessage($params['openid'], $msg_text); // 获得关注用户的基础信息 $userInfo = loadModel('Common')->getUserToApi($params['openid']); $userInfo = json_decode($userInfo, true); if ($userInfo['error'] != 0) { // 获取用户信息失败 Logger::error('获取用户信息失败 OpenId' . $params['openid']); } // 检查用户是否重复关注 $invitationParams = array( 'open_id' => $params['openid'], ); $userWxInfo = $this->getDetail($invitationParams); if (!empty($userWxInfo)) { // 用户重复关注 Logger::error('用户重复关注 OpenId' . $params['openid']); return true; } // 获得发放奖励的参数 $awardParams = array( 'where' => array( 'id' => 1, ), ); $award = $this->_db->get('wx_partner_award', $awardParams, true); if (empty($award)) { // 没有配置奖励参数 Logger::error('没有配置奖励参数 OpenId' . $params['openid']); } $score = intval($award['score']); $money = floatval($award['money']); $limitMoney = 500; // 每月上限金额 // 检查当月奖励金额是否到达每月上限 $sql = "SELECT SUM(money) money FROM wx_partner_invitation WHERE `invitation_open_id`='{$qrInfo['open_id']}' AND DATE_FORMAT(create_time, '%Y-%m') = '" . date('Y-m') . "' "; $totalMoney = $this->_db->getOne($sql); $totalMoney = floatval($totalMoney); if (($totalMoney + $money) > $limitMoney) { if ($totalMoney < $limitMoney) { $money = number_format($limitMoney - $totalMoney, 2); } else { $money = 0; } } $insert = array( 'open_id' => $params['openid'], 'wx_name' => $userInfo['data']['nickname'], 'wx_img' => $userInfo['data']['headimgurl'], 'score' => $score, 'money' => $money, 'create_time' => date('Y-m-d H:i:s'), 'invitation_open_id' => $qrInfo['open_id'], ); if ($this->_db->insert($this->_table, $insert)) { // 更新用户的可用金额及积分 $sql = "UPDATE `wx_partner_info` SET `integral`=integral+{$insert['score']} WHERE `openid`='{$insert['invitation_open_id']}'"; $this->_db->query($sql); // 更新用户的可提现返利 $sql = "UPDATE `wx_partner_statistics` SET `notaccount_money`=notaccount_money+{$insert['money']},`summon_money`=summon_money+{$insert['money']} WHERE `openid`='{$insert['invitation_open_id']}'"; $this->_db->query($sql); } } } return true; }
public function __construct() { parent::__construct(); $this->_db = Factory::getDb(); }
public function __construct () { $this->_db = Factory::getDb(); }
/** * 更新用户业务数据状态 */ public function updateState($wid,$data) { try { $data['time'] = time(); $where = "`wid` = '{$wid}'"; Factory::getDb("weixin_active")->update(DBConfig::$table['record'],$where,$data); return Factory::getDb("weixin_active")->affectedRows(); } catch (Exception $e) { Logger::error("更新用户业务数据状态失败".$e->getMessage()); return false; } }
/** * 更新用户最近的获奖记录状态 * @param int $id 记录id * @param int $state 状态 */ public function updateLastRewardInfo($id,$state) { try{ $strsql = "UPDATE ".DBConfig::$table['ureward']." SET `state` = {$state} WHERE `id` = '{$id}'"; Factory::getDb("weixin_active")->query($strsql); return Factory::getDb("weixin_active")->affectedRows(); }catch (Exception $e) { Logger::error("更新用户最近的获奖记录状态".$e->getmessage()); return false; } }