业务场景是:我们系统有一个抽奖环节,当用户抽奖抽到红包的时候,系统会自动发红包给用户,框架用的微擎,话不多说,直接上代码:
function getSetting() // 获取红包发送的基本设置,有商户id appid appsecret等等一系列的配置
{
global $_W,$_GPC;
load()->model('account');
load()->func('tpl');
$sql = "SELECT * FROM ".tablename('红包基本设置表')." WHERE uniacid = :uniacid";
$params = array(':uniacid'=>$_W['uniacid']);
$settings = pdo_get('sanshi_haibao_hb',array('uniacid'=>$_W['uniacid']));
return $settings;
}
function substr_cut($str_cut,$length)
{
if (strlen($str_cut) > $length)
{
for($i=0; $i < $length; $i++)
if (ord($str_cut[$i]) > 128) $i++;
$str_cut = substr($str_cut,0,$i)."..";
}
return $str_cut;
}
/*红包新商户订单号生成方式*/
function get_rand_number($pre = ''){
return $pre.date('ym').substr(time(),4).substr(microtime(),2,6).rand(18,99);
}
// 发红包
function sendhb($openid,$fee,$fansid){
global $_W;
if(empty($openid)){
message('用户openid为空');
}
if($fee<1 || $fee>200){
message('红包金额只能介于1-200元之间');
}
if(empty($fansid)){
message('用户fansid为空');
}
// 获取配置信息
$settings = getSetting();
if (empty($settings)) {
return error(-2, '商户信息没有填!');
}
if(empty($settings['sname'])){
$send_name = substr_cut($_W['account']['name'],30);
}else{
$send_name = $settings['sname'];
}
/*红包新商户订单号生成方式*/
$new_mch_billno = intval($fansid%100);
$mch_billno = get_rand_number($new_mch_billno);
$actname = empty($settings['actname']) ? '参与疯狂抢红包活动' : $settings['actname'];
if(empty($settings['wishing'])){
$wishing = '恭喜您,抽中了一个' . $fee . '元红包!';
}else{
$wishing = $settings['wishing'] . $fee . '元红包!';
}
$fee = floatval($fee)*100;//红包金额,单位为分;
$url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
$pars = array();
$pars['nonce_str'] = random(32);
$pars['mch_billno'] = $mch_billno; //红包新商户订单号生成方式
$pars['mch_id'] = $settings['mchid'];
$pars['wxappid'] = $settings['appid'];
$pars['nick_name'] = $_W['account']['name'];
$pars['send_name'] = $send_name;
$pars['re_openid'] = $openid;
$pars['total_amount'] = $fee;
$pars['min_value'] = $pars['total_amount'];
$pars['max_value'] = $pars['total_amount'];
$pars['total_num'] = 1;
$pars['wishing'] = $wishing;
$pars['client_ip'] = $settings['ip'];
$pars['act_name'] = $actname;
$pars['remark'] = '恭喜您的' . $fee . '元红包已经发放,请注意查收';
$pars['logo_imgurl'] = tomedia($settings['logo']);
ksort($pars, SORT_STRING);
// 签名算法
$string1 = '';
foreach ($pars as $k => $v) {
$string1 .= "{$k}={$v}&";
}
$string1 .= "key={$settings['password']}";
$pars['sign'] = strtoupper(md5($string1));
$xml = array2xml($pars);
$extras = array();
$extras['CURLOPT_CAINFO'] = '../addons/sanshi_poster/cert/apiclient_cert.p12.'.$_W['uniacid'];
$extras['CURLOPT_SSLCERT'] = '../addons/sanshi_poster/cert/apiclient_cert.pem.'.$_W['uniacid'];
$extras['CURLOPT_SSLKEY'] = '../addons/sanshi_poster/cert/apiclient_key.pem.'.$_W['uniacid'];
load()->func('communication');
$procResult = null;
$resp = ihttp_request($url, $xml, $extras);
// echo "<pre>";
// var_dump($extras);die;
if (is_error($resp)) {
$procResult = $resp;
} else {
$xml = '<?xml version="1.0" encoding="utf-8"?>' . $resp['content'];
$dom = new DOMDocument();
if ($dom->loadXML($xml)) {
$xpath = new DOMXPath($dom);
$code = $xpath->evaluate('string(//xml/return_code)');
$return_msg = $xpath->evaluate('string(//xml/return_msg)');
$ret = $xpath->evaluate('string(//xml/result_code)');
if (strtolower($code) == 'success' && strtolower($ret) == 'success') {
$procResult = true;
} else {
$error = $xpath->evaluate('string(//xml/err_code_des)');
$procResult = error(-2, $error);
}
} else {
$procResult = error(-1, 'error response');
}
}
$packpage['error_msg']=$return_msg;
$packpage['code']=$code;
// $packpage['error_msg']=$error;
if (is_error($procResult)) {
$packpage['isok']=false;
return $packpage;
} else {
$packpage['isok']=true;
return $packpage;
}
}
仅供参考,实习生小弟代码还很青涩,请赐教