chengkun
2025-05-26 4462855c0033970c39ac8d0da704b7dc41eabbfe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
 
class wxredpack_model extends model{
    /**
     * 添加红包发送记录
     * @param array $data
     */
    public function addInfo($data = array()){
    
        return $this -> insert_into('wxredpack', $data);
    }
    /**
     * 修改红包发送记录
     * @param array $whereData
     * @param array $upData
     */
    public function upInfo($whereData,$upData){
        
        return $this -> update_once('wxredpack', $upData, $whereData);
    }
    
    //新查询所有微信红包记录
    public function getWxRedPackList($whereData,$data=array()){
        $ListNew    =    array();
        
        $List        =    $this -> select_all('wxredpack',$whereData);
        
        if(!empty( $List )){
            
            $ListNew['list']    =    $List;
        }
        
        return $ListNew;
    }
    //微信红包总函数 data type:类型 1:关注 2:绑定帐号 3:创建第一份简历 4:完善企业资料;  openid:用户wx身份id;  uid:用户ID (与openid 不会同时存在)
    public function sendRedPack($data)
    {
        if(!empty($data['type']) && $this->config['sy_wxredpack_isopen'.$data['type']]=='1'){
            
            $type  =  intval($data['type']);
            
            if($data['openid']){
                
                $openid  =  $data['openid'];
                
            }else{
                if(!empty($data['uid'])){//判断是否传入UID 根据UID 获取用户绑定的openid
                    
                    $uid     =  intval($data['uid']);
                    
                    $userWx  =  $this -> select_once('member', array('uid'=>$uid), 'wxid,username,usertype');
                    
                    if($userWx['wxid']){
                        
                        $openid  =  $userWx['wxid'];
                        
                    }
                    
                }else{
                    
                    return array('msg'=>'请选择要发红包的用户');
                }
            }
                //红包需要微信支付 则该公众号必定是已经认证 拥有获取用户资料接口权限
                if(!empty($openid)){
                    //获取用户真实资料
                    require_once 'weixin.model.php';
                    
                    $wxM         =  new weixin_model($this->db, $this->def);
                    
                    $wxUserInfo  =  $wxM -> getWxUser($openid);
                    
                    //根据传入类型判断该用户是否处于第一次获取红包状态
                    $wxRedPack   = $this -> select_once('wxredpack', array('re_openid'=>$openid,"type"=>$type), '`id`,`status`');
                    
                    //如status 不为1 则该用户未接受过该类型红包或红包发放失败
                    if($wxRedPack['status'] != '1'){
                        //获取红包对应的参数 金额 发放人
                        $wxRedPackArr['openid']        =  $openid;//微信用户身份ID
                        $wxRedPackArr['send_name']     =  $this->config['sy_wxredpack_nick'];//红包发送者名称
                        $wxRedPackArr['total_amount']  =  intval($this->config['sy_wxredpack_money'.$type]*100);//发放金额
                        $wxRedPackArr['mch_billno']    =  time().rand(1000,9999);//当前红包订单ID 自定义生成
                        $wxRedPackArr['client_ip']     =  $this->config['sy_wxredpack_ip'];//服务器IP
                        $wxRedPackArr['wishing']       =  $this->config['sy_wxredpack_wishing'.$type];//服务器IP
                        $wxRedPackArr['act_name']      =  $this->config['sy_wxredpack_actname'.$type];//活动名称
                        $wxRedPackArr['remark']        =  $this->config['sy_wxredpack_remark'];//红包备注
                        $wxRedPackArr['scene_id']      =  intval($this->config['sy_wxredpack_pro'.$type]);//发放场景
                        //调用红包接口类
                        include(LIB_PATH."ApiWxHb.class.php");
                        
                        $wxHb     =  new ApiWxHb();
                        
                        $wxHbMsg  =  $wxHb -> sendHb($wxRedPackArr);
                        
                        //红包记录存入数据库
                        if($wxHbMsg['result_code'] == 'SUCCESS'){
                            
                            $wxRedInfo['status'] = '1';
                            
                        }else{
                            
                            $wxRedInfo['status'] = '0';
                        }
                        $wxRedInfo['msg']           =  $wxHbMsg['return_msg'];
                        $wxRedInfo['orderid']       =  $wxRedPackArr['mch_billno'];//订单号
                        $wxRedInfo['re_openid']     =  $openid;//用户身份标识
                        $wxRedInfo['re_nick']       =  $wxUserInfo['nickname'];//用户昵称
                        $wxRedInfo['total_amount']  =  $wxRedPackArr['total_amount']/100;//发放金额
                        $wxRedInfo['wishing']       =  $wxRedPackArr['wishing'];//红包祝福语
                        $wxRedInfo['type']          =  $type;//发放类型
                        $wxRedInfo['time']          =  time();//红包祝福语
                        $wxRedInfo['uid']           =  $uid;
                        $wxRedInfo['username']      =  $userWx['username'];
                        $wxRedInfo['usertype']      =  $userWx['usertype'];
                        
                        if(empty($wxRedPack)){
                            
                            $this -> addInfo($wxRedInfo);
                        }else{
                            
                            $this -> upInfo(array('re_openid'=>$openid), array('status'=>$wxRedInfo['status'],'msg'=>$wxHbMsg['return_msg']));//更新原有纪录
                        }
                    }
                }
        }
    }
}
?>