Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

294 řádky
9.6 KiB

  1. <?php
  2. /**
  3. * 红包功能处理层
  4. * ============================================================================
  5. * * 版权所有 蜘蛛出行 * *
  6. * 网站地址: http://www.zhizhuchuxing.com
  7. * ----------------------------------------------------------------------------
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
  9. * 使用;不允许对程序代码以任何形式任何目的的再发布。
  10. * ============================================================================
  11. * Author By: 倪宗锋
  12. * PhpStorm RedPackService.php
  13. * Create By 2016/12/5 11:56 $
  14. */
  15. namespace Activity\Service;
  16. use Activity\Model\ActivityRedPack;
  17. use Base\Tool\WxPayService;
  18. use Model\Model\WechatUser;
  19. use Util\Util\CurlInterface;
  20. use Util\Util\Util;
  21. class RedPackService
  22. {
  23. public function __construct($activityId = 1)
  24. {
  25. $this->config = Util::getWxRedPackConfig($activityId);
  26. }
  27. /**
  28. * Function Description:发送红包参数校验
  29. * Function Name: checkParams
  30. * @param $params
  31. *
  32. * @return array
  33. *
  34. * @author 倪宗锋
  35. */
  36. public function checkParams($params)
  37. {
  38. if (!isset($params['sign'])) {
  39. return Util::returnArrEr('缺少签名参数!');
  40. }
  41. $getSign = WxPayService::getSign($params);
  42. if ($getSign != $params['sign']) {
  43. return Util::returnArrEr('无效的签名!');
  44. }
  45. if (empty($params['openid'])) {
  46. return Util::returnArrEr('参数错误!');
  47. }
  48. return Util::returnArrSu();
  49. }
  50. /**
  51. * Function Description:发送红包
  52. * Function Name: sendRedPack
  53. * @param $openid
  54. *
  55. * @return array
  56. *
  57. * @author 倪宗锋
  58. */
  59. public function sendRedPack($openid)
  60. {
  61. //校验活动时间
  62. $isTime = $this->isTime();
  63. if($isTime['flag'] == false) {
  64. return $isTime;
  65. }
  66. //校验用户是否关注公众号
  67. $isFocus = $this->isFocus($openid);
  68. if ($isFocus['flag'] == false) {
  69. return $isFocus;
  70. }
  71. //检查红包是否超过总数
  72. $isTotalCount = $this->isTotalCount();
  73. if ($isTotalCount['flag'] == false) {
  74. return $isTotalCount;
  75. }
  76. //检查个人领取红包是否超过次数
  77. $isPersonNum = $this->isPersonNum($openid);
  78. if ($isPersonNum['flag'] == false) {
  79. return $isPersonNum;
  80. }
  81. //添加红包数据到数据库
  82. $addActivityRedPack = $this->addActivityRedPack($openid);
  83. if ($addActivityRedPack['flag'] == false) {
  84. return $addActivityRedPack;
  85. }
  86. $orderId = $addActivityRedPack['data']['oderId'];//订单号
  87. $redPackId = $addActivityRedPack['data']['redPackId'];//添加的记录的ID
  88. $price = $addActivityRedPack['data']['price'];//发送金额
  89. //调用微信发送红包接口
  90. $sendWxRedPack = $this->sendWxRedPack($openid, $orderId, $price);
  91. if ($sendWxRedPack['flag'] == true) {
  92. return Util::returnArrSu();//发送成功
  93. }
  94. //发送失败则删除红包
  95. $this->delActivityRedPack($redPackId);
  96. return $sendWxRedPack;
  97. }
  98. /**
  99. * Function Description:校验活动时间
  100. * Function Name: isTime
  101. *
  102. * @return array
  103. *
  104. * @author 倪宗锋
  105. */
  106. public function isTime()
  107. {
  108. $now = time();
  109. if ($this->config['start_time'] != false) {//存在开始时间则校验
  110. $start = strtotime($this->config['start_time']);
  111. if ($now < $start) {
  112. return Util::returnArrEr('活动未开始!');
  113. }
  114. }
  115. if ($this->config['end_time'] != false) {//存在结束时间则校验
  116. $end = strtotime($this->config['end_time']);
  117. if ($now > $end) {
  118. return Util::returnArrEr('活动已结束!');
  119. }
  120. }
  121. return Util::returnArrSu();
  122. }
  123. /**
  124. * Function Description:检查是否关注
  125. * Function Name: is_focus
  126. * @param $openid
  127. *
  128. * @return array
  129. *
  130. * @author 倪宗锋
  131. */
  132. public function isFocus($openid)
  133. {
  134. $userTab = new WechatUser();
  135. //获取用户信息
  136. if ($this->config['is_focus']) {//是否需要在关注后才可以领取
  137. $userInfo = $userTab->getUserInfoByOpenId($openid);
  138. if (empty($userInfo['ID']) == false) {
  139. return Util::returnArrEr('该微信号账号未关注本公众号!');
  140. }
  141. }
  142. return Util::returnArrSu();
  143. }
  144. /**
  145. * Function Description:是否超过总红包个数
  146. * Function Name: isTotalCount
  147. *
  148. * @return array
  149. *
  150. * @author 倪宗锋
  151. */
  152. public function isTotalCount()
  153. {
  154. $redPackTab = new ActivityRedPack();
  155. if ($this->config['num_total']) {
  156. $getTotal = $redPackTab->getTotalNum($this->config['act_id']);
  157. if ($getTotal >= $this->config['num_total']) {
  158. return Util::returnArrEr('红包存量不足!红包已经发完!');
  159. }
  160. }
  161. return Util::returnArrSu();
  162. }
  163. /**
  164. * Function Description:校验每人领取个数
  165. * Function Name: isPersonNum
  166. * @param $openid
  167. *
  168. * @return array
  169. *
  170. * @author 倪宗锋
  171. */
  172. public function isPersonNum($openid)
  173. {
  174. $redPackTab = new ActivityRedPack();
  175. if ($this->config['num_one_person'] == false) {//不限制每人领取红包个数时
  176. return Util::returnArrSu();
  177. }
  178. if ($this->config['unm_one_person_type'] == 1) {
  179. //用户总的订单数
  180. $getPersonCount = $redPackTab->getCountByOpenId($openid, $this->config['act_id']);
  181. $msg = '该活动每人只能参加' . $this->config['num_one_person'] . '次!';
  182. } else {
  183. //用的当天的订单数
  184. $getPersonCount = $redPackTab->getCountByOpenidForToday($openid, $this->config['act_id']);
  185. $msg = '该活动每人每天只能参加' . $this->config['num_one_person'] . '次!';
  186. }
  187. if ($getPersonCount >= $this->config['num_one_person']) {//超过个人领取个数时报错
  188. return Util::returnArrEr($msg);
  189. }
  190. return Util::returnArrSu();
  191. }
  192. /**
  193. * Function Description:添加红包记录
  194. * Function Name: addActivityRedPack
  195. * @param $openid
  196. *
  197. * @return array
  198. *
  199. * @author 倪宗锋
  200. */
  201. public function addActivityRedPack($openid)
  202. {
  203. $wxConfig = Util::getWxPayConfig();
  204. $redPackTab = new ActivityRedPack();
  205. $oderId = $wxConfig['mch_id'] . time().rand(1000,9999);//订单号 组成:mch_id+yyyymmdd+10位一天内不能重复的数字。
  206. $data = array(
  207. 'openid' => $openid,
  208. 'orderid' => $oderId,
  209. 'activity_id' => 1,
  210. 'price' => rand($this->config['price_min'], $this->config['price_max'])
  211. );
  212. $redPackId = $redPackTab->insert($data, 1);//添加记录并获取记录ID
  213. if ($redPackId == false) {
  214. return Util::returnArrEr('添加红包记录失败!');
  215. }
  216. $return = array('oderId' => $oderId, 'redPackId' => $redPackId, 'price' => $data['price']);
  217. return Util::returnArrSu('', $return);
  218. }
  219. /**
  220. * Function Description:修改
  221. * Function Name: delActivityRedPack
  222. * @param $redPackId
  223. *
  224. * @return bool
  225. *
  226. * @author 倪宗锋
  227. */
  228. public function delActivityRedPack($redPackId)
  229. {
  230. $redPackTab = new ActivityRedPack();
  231. $where = ' id='.$redPackId;
  232. if($redPackTab->delete($where)){
  233. return Util::returnArrEr('删除失败!');
  234. }
  235. return Util::returnArrSu('删除成功!');
  236. }
  237. /**
  238. * Function Description:发送红包到微信
  239. * Function Name: sendWxRedPack
  240. * @param $openid
  241. * @param $orderId
  242. * @param $price
  243. * @return array
  244. *
  245. * @author 倪宗锋
  246. */
  247. public function sendWxRedPack($openid, $orderId, $price)
  248. {
  249. //参数准备
  250. $wxConfig = Util::getWxPayConfig();
  251. $wxParams = array(
  252. 'nonce_str' => WxPayService::getNonceStr(),//随机字符串
  253. 'mch_billno' => $orderId,//商户订单号
  254. 'mch_id' => $wxConfig['mch_id'],//商户号
  255. 'wxappid' => $wxConfig['appid'],//公众账号appid
  256. 'send_name' => $wxConfig['appName'],//商户名称
  257. 're_openid' => $openid,//用户openid
  258. 'total_amount' => $price,//付款金额
  259. 'total_num' => 1,//红包发放总人数
  260. 'wishing' => $this->config['wishing'],//红包祝福语
  261. 'client_ip' => Util::getRealIp(),//客户端IP
  262. 'act_name' => $this->config['act_name'],//活动名称
  263. 'remark' => $this->config['remark'],//备注
  264. );
  265. $wxParams['sign'] = WxPayService::getSign($wxParams);
  266. //调用接口
  267. $curlInterface = new CurlInterface($wxParams, 2);
  268. $curlInterface->setBaseUrl('https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack');
  269. $curlInterface->setCert($wxConfig);
  270. $result = $curlInterface->execute('', 'POST');
  271. //返回值处理 两个返回码都是成功 才是真的发送成功
  272. if (empty($result['return_code']) == false && $result['return_code'] == 'SUCCESS') {
  273. if(empty($result['result_code']) == false && $result['result_code'] == 'SUCCESS') {
  274. return Util::returnArrSu();
  275. }
  276. }
  277. return Util::returnArrEr('发送红包到微信失败!' . $result['return_code']);
  278. }
  279. }