|
- <?php
- /**
- * 红包功能处理层
- * ============================================================================
- * * 版权所有 蜘蛛出行 * *
- * 网站地址: http://www.zhizhuchuxing.com
- * ----------------------------------------------------------------------------
- * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
- * 使用;不允许对程序代码以任何形式任何目的的再发布。
- * ============================================================================
- * Author By: 倪宗锋
- * PhpStorm RedPackService.php
- * Create By 2016/12/5 11:56 $
- */
-
-
- namespace Activity\Service;
-
-
- use Activity\Model\ActivityRedPack;
- use Base\Tool\WxPayService;
- use Model\Model\WechatUser;
- use Util\Util\CurlInterface;
- use Util\Util\Util;
-
- class RedPackService
- {
-
- public function __construct($activityId = 1)
- {
- $this->config = Util::getWxRedPackConfig($activityId);
- }
-
- /**
- * Function Description:发送红包参数校验
- * Function Name: checkParams
- * @param $params
- *
- * @return array
- *
- * @author 倪宗锋
- */
- public function checkParams($params)
- {
- if (!isset($params['sign'])) {
- return Util::returnArrEr('缺少签名参数!');
- }
- $getSign = WxPayService::getSign($params);
- if ($getSign != $params['sign']) {
- return Util::returnArrEr('无效的签名!');
- }
- if (empty($params['openid'])) {
- return Util::returnArrEr('参数错误!');
- }
- return Util::returnArrSu();
- }
-
- /**
- * Function Description:发送红包
- * Function Name: sendRedPack
- * @param $openid
- *
- * @return array
- *
- * @author 倪宗锋
- */
- public function sendRedPack($openid)
- {
- //校验活动时间
- $isTime = $this->isTime();
- if($isTime['flag'] == false) {
- return $isTime;
- }
- //校验用户是否关注公众号
- $isFocus = $this->isFocus($openid);
- if ($isFocus['flag'] == false) {
- return $isFocus;
- }
- //检查红包是否超过总数
- $isTotalCount = $this->isTotalCount();
- if ($isTotalCount['flag'] == false) {
- return $isTotalCount;
- }
- //检查个人领取红包是否超过次数
- $isPersonNum = $this->isPersonNum($openid);
- if ($isPersonNum['flag'] == false) {
- return $isPersonNum;
- }
- //添加红包数据到数据库
- $addActivityRedPack = $this->addActivityRedPack($openid);
- if ($addActivityRedPack['flag'] == false) {
- return $addActivityRedPack;
- }
- $orderId = $addActivityRedPack['data']['oderId'];//订单号
- $redPackId = $addActivityRedPack['data']['redPackId'];//添加的记录的ID
- $price = $addActivityRedPack['data']['price'];//发送金额
- //调用微信发送红包接口
- $sendWxRedPack = $this->sendWxRedPack($openid, $orderId, $price);
- if ($sendWxRedPack['flag'] == true) {
- return Util::returnArrSu();//发送成功
- }
- //发送失败则删除红包
- $this->delActivityRedPack($redPackId);
- return $sendWxRedPack;
- }
-
- /**
- * Function Description:校验活动时间
- * Function Name: isTime
- *
- * @return array
- *
- * @author 倪宗锋
- */
- public function isTime()
- {
- $now = time();
- if ($this->config['start_time'] != false) {//存在开始时间则校验
- $start = strtotime($this->config['start_time']);
- if ($now < $start) {
- return Util::returnArrEr('活动未开始!');
- }
- }
- if ($this->config['end_time'] != false) {//存在结束时间则校验
- $end = strtotime($this->config['end_time']);
- if ($now > $end) {
- return Util::returnArrEr('活动已结束!');
- }
- }
- return Util::returnArrSu();
- }
-
- /**
- * Function Description:检查是否关注
- * Function Name: is_focus
- * @param $openid
- *
- * @return array
- *
- * @author 倪宗锋
- */
- public function isFocus($openid)
- {
- $userTab = new WechatUser();
- //获取用户信息
- if ($this->config['is_focus']) {//是否需要在关注后才可以领取
- $userInfo = $userTab->getUserInfoByOpenId($openid);
- if (empty($userInfo['ID']) == false) {
- return Util::returnArrEr('该微信号账号未关注本公众号!');
- }
- }
- return Util::returnArrSu();
- }
-
- /**
- * Function Description:是否超过总红包个数
- * Function Name: isTotalCount
- *
- * @return array
- *
- * @author 倪宗锋
- */
- public function isTotalCount()
- {
- $redPackTab = new ActivityRedPack();
- if ($this->config['num_total']) {
- $getTotal = $redPackTab->getTotalNum($this->config['act_id']);
- if ($getTotal >= $this->config['num_total']) {
- return Util::returnArrEr('红包存量不足!红包已经发完!');
- }
- }
- return Util::returnArrSu();
- }
-
- /**
- * Function Description:校验每人领取个数
- * Function Name: isPersonNum
- * @param $openid
- *
- * @return array
- *
- * @author 倪宗锋
- */
- public function isPersonNum($openid)
- {
- $redPackTab = new ActivityRedPack();
- if ($this->config['num_one_person'] == false) {//不限制每人领取红包个数时
- return Util::returnArrSu();
- }
- if ($this->config['unm_one_person_type'] == 1) {
- //用户总的订单数
- $getPersonCount = $redPackTab->getCountByOpenId($openid, $this->config['act_id']);
- $msg = '该活动每人只能参加' . $this->config['num_one_person'] . '次!';
- } else {
- //用的当天的订单数
- $getPersonCount = $redPackTab->getCountByOpenidForToday($openid, $this->config['act_id']);
- $msg = '该活动每人每天只能参加' . $this->config['num_one_person'] . '次!';
- }
- if ($getPersonCount >= $this->config['num_one_person']) {//超过个人领取个数时报错
- return Util::returnArrEr($msg);
- }
- return Util::returnArrSu();
- }
-
- /**
- * Function Description:添加红包记录
- * Function Name: addActivityRedPack
- * @param $openid
- *
- * @return array
- *
- * @author 倪宗锋
- */
- public function addActivityRedPack($openid)
- {
- $wxConfig = Util::getWxPayConfig();
- $redPackTab = new ActivityRedPack();
- $oderId = $wxConfig['mch_id'] . time().rand(1000,9999);//订单号 组成:mch_id+yyyymmdd+10位一天内不能重复的数字。
- $data = array(
- 'openid' => $openid,
- 'orderid' => $oderId,
- 'activity_id' => 1,
- 'price' => rand($this->config['price_min'], $this->config['price_max'])
- );
- $redPackId = $redPackTab->insert($data, 1);//添加记录并获取记录ID
- if ($redPackId == false) {
- return Util::returnArrEr('添加红包记录失败!');
- }
- $return = array('oderId' => $oderId, 'redPackId' => $redPackId, 'price' => $data['price']);
- return Util::returnArrSu('', $return);
- }
-
- /**
- * Function Description:修改
- * Function Name: delActivityRedPack
- * @param $redPackId
- *
- * @return bool
- *
- * @author 倪宗锋
- */
- public function delActivityRedPack($redPackId)
- {
- $redPackTab = new ActivityRedPack();
- $where = ' id='.$redPackId;
- if($redPackTab->delete($where)){
- return Util::returnArrEr('删除失败!');
- }
- return Util::returnArrSu('删除成功!');
- }
-
- /**
- * Function Description:发送红包到微信
- * Function Name: sendWxRedPack
- * @param $openid
- * @param $orderId
- * @param $price
- * @return array
- *
- * @author 倪宗锋
- */
- public function sendWxRedPack($openid, $orderId, $price)
- {
- //参数准备
- $wxConfig = Util::getWxPayConfig();
- $wxParams = array(
- 'nonce_str' => WxPayService::getNonceStr(),//随机字符串
- 'mch_billno' => $orderId,//商户订单号
- 'mch_id' => $wxConfig['mch_id'],//商户号
- 'wxappid' => $wxConfig['appid'],//公众账号appid
- 'send_name' => $wxConfig['appName'],//商户名称
- 're_openid' => $openid,//用户openid
- 'total_amount' => $price,//付款金额
- 'total_num' => 1,//红包发放总人数
- 'wishing' => $this->config['wishing'],//红包祝福语
- 'client_ip' => Util::getRealIp(),//客户端IP
- 'act_name' => $this->config['act_name'],//活动名称
- 'remark' => $this->config['remark'],//备注
- );
- $wxParams['sign'] = WxPayService::getSign($wxParams);
- //调用接口
- $curlInterface = new CurlInterface($wxParams, 2);
- $curlInterface->setBaseUrl('https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack');
- $curlInterface->setCert($wxConfig);
- $result = $curlInterface->execute('', 'POST');
- //返回值处理 两个返回码都是成功 才是真的发送成功
- if (empty($result['return_code']) == false && $result['return_code'] == 'SUCCESS') {
- if(empty($result['result_code']) == false && $result['result_code'] == 'SUCCESS') {
- return Util::returnArrSu();
- }
- }
- return Util::returnArrEr('发送红包到微信失败!' . $result['return_code']);
- }
- }
|