'wxdf21b47ec5632158', 'appsecret' => 'b319fbe508acdbc4c8d9ddaa5bd23444' ); public function __construct($options = array()) { $this->we_appid = isset($options['appid'])?$options['appid']:''; $this->we_appsecret = isset($options['appsecret'])?$options['appsecret']:''; $this->ci = & get_instance(); } /** * 初始化 * @param int $appid (默认家加贷) */ public function init($appid = 1){ switch($appid){ case 2://小红榜 break; case 3://座上宾 break; default://家加贷 $this->app = 'jjd'; } $config = $this->config; $this->we_appid = $config['appid']; $this->we_appsecret = $config['appsecret']; } /** * 发送小程序消息 * @param $key(0放款, 1消费, 2扣款, 3照片审核成功, 4照片审核失败) * @param $openid * @param $params * @param string $page * @return bool */ public function send_app_msg($key, $openid, $params, $page = ''){ $template_id = self::$tmpId_arr[$key]; $status = true; $msg = ''; //access_token $access_token = $this->get_access_token(); if(!$access_token){ $status = false; $msg = 'not access_token'; goto end; } //form_id $form_id = $this->pop_form_id($openid); if(!$form_id){ $status = false; $msg = 'not form_id'; goto end; } $sdata = array( 'touser' => $openid, 'template_id' => $template_id, 'form_id' => $form_id, 'page' => $page, ); $sdata['data'] = $this->get_msg_data($key, $params); $result = $this->http_post(self::API_URL_PREFIX.self::APP_MSG_URL.'?access_token='. $access_token, json_encode($sdata)); if ($result) { $json = json_decode($result,true); if ($json['errcode']) { $status = false; $msg = $result; } } end: if(!$status) {debug_log($msg, 'wechat_msg.log');} return $status; } /** * 获取要提交的消息内容 * @param $key * @param $params * @return mixed */ public function get_msg_data($key, $params){ switch($key){ case 0://放款成功 $arr = array( $params['time'],//日期 $params['title'],//产品 $params['user'],//客户 $params['money'],//金额 $params['descrip'],//流程阶段 ); break; case 1://消费成功 $arr = array( $params['money'],//消费金额 $params['balance'],//账户余额 $params['id'],//订单号 $params['time'],//消费时间 $params['descrip'],//消费详情 $params['addr'],//消费门店 $params['shop'],//商户名称 $params['type'],//支付方式 $params['note'],//温馨提示 ); break; case 2://扣款成功 $arr = array( $params['money'],//扣款金额 $params['descrip'],//扣款原因 $params['remark'],//备注 ); break; case 3://照片审核通过 $arr = array( $params['title'],//审核项目 $params['time'],//上传时间 $params['descrip'],//审核结果 $params['remark'],//备注信息 ); break; case 4://审核不通过 $arr = array( $params['title'],//审核项目 $params['time'],//上传时间 $params['descrip'],//审核结果 $params['note'],//拒绝理由 $params['remark'],//备注信息 ); break; default: $arr = array(); } $data = array(); foreach($arr as $k => $v){ $kd = $k+1; $data["keyword{$kd}"] = array( 'value' => $v, ); } return $data; } /** * 收集新的form_id * @param $openid * @param $form_id * @param $expire (到期时间戳) * @return bool */ public function push_form_id($openid, $form_id, $expire = 0){ //从redis获取 $r = &load_cache('redis'); $redis = $r->redis(); $key = "wechat_formid_{$this->we_appid}_{$openid}"; $long = 7 * 24 * 3600; if($this->app){ !$expire && $expire = time() + $long; $form = array('id' => $form_id, 'expire' => $expire); $ret = $redis->lPush($key, json_encode($form)); $len = $redis->lLen($key); //限制redis列表长度,去掉旧的 $ret && $len > 100 && $redis->rPop($key); //更新redis有效期 $redis->expire($key, $long); } return false; } /** * form_id数量 * @param $openid * @return mixed */ public function count_form_id($openid){ $r = &load_cache('redis'); $redis = $r->redis(); $key = "wechat_formid_{$this->we_appid}_{$openid}"; return $redis->lLen($key); } /** * 清除缓存 * @param string $key * @return bool */ public function clear($key = ''){ if(!$key){return false;} $r = &load_cache('redis'); $redis = $r->redis(); return $redis->del($key); } /** * 获取access_token * @return string */ private function get_access_token(){ if($this->we_access_token){ return $this->we_access_token;} if(!$this->we_appid){ return '';} //从redis获取 $r = &load_cache('redis'); $redis = $r->redis(); $key = "wechat_accesstoken_{$this->we_appid}"; if($this->app){ $access_token = $this->we_access_token = $redis->get($key); if($access_token){return $access_token;} } $url = self::API_URL_PREFIX . self::ACCESS_TOKEN_URL. "?grant_type=client_credential&appid={$this->we_appid}&secret={$this->we_appsecret}"; $res = json_decode(file_get_contents($url), true); $access_token = $res['access_token']; //存入redis if($access_token && $this->app){ $redis->set($key, $access_token); $redis->expire($key, 7000); } return $access_token; } /** * @param $openid * @return int */ private function pop_form_id($openid){ //从redis获取 $r = &load_cache('redis'); $redis = $r->redis(); $key = "wechat_formid_{$this->we_appid}_{$openid}"; if($this->app){ $now = time(); while(1) {//循环获取fromid $form = $redis->rPop($key); $form && $form = json_decode($form, true); if(!$form){break;} if($form['expire'] >= $now){ return $form['id']; } } } return ''; } /** * POST 请求 * @param string $url * @param array|string $param * @param boolean $post_file 是否文件上传 * @return string content */ private function http_post($url, $param, $post_file = false) { $oCurl = curl_init(); if(stripos($url,"https://")!==FALSE){ curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1 } if (is_string($param) || $post_file) { $strPOST = $param; } else { $aPOST = array(); foreach($param as $key=>$val){ $aPOST[] = $key."=".urlencode($val); } $strPOST = join("&", $aPOST); } if ($post_file){ curl_setopt ($oCurl, CURLOPT_SAFE_UPLOAD, false); } curl_setopt($oCurl, CURLOPT_URL, $url); curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt($oCurl, CURLOPT_POST,true); curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST); $sContent = curl_exec($oCurl); $aStatus = curl_getinfo($oCurl); curl_close($oCurl); if(intval($aStatus["http_code"])==200){ return $sContent; }else{ return false; } } }