/** * * uid新增downloadedcoupon * 即使uid和couponId是错误的也会新建,所以这里需要对uid和couponid做一个validate,是否是有效的 * */ public function myDownloadedCoupon_post() { $uid = $this->post('uid'); $couponId = $this->post('couponId'); $sessionToken = $this->post('sessionToken'); $this->load->model('user2_m', 'user'); $this->load->model('coupon2_m', 'coupon'); if (empty($uid) || empty($couponId) || empty($sessionToken)) { return $this->output_error(ErrorEmptyParameter); } if (!($user = $this->user->isSessionValid($uid, $sessionToken))) { return $this->output_error(ErrorInvalidSession); } $data['uid'] = $uid; $data['couponId'] = $couponId; /// 数据库判断user是否能下载coupon $code = $this->user->can_user_dcoupon($uid, $couponId); if ($code !== true) { return $this->output_error($code); } // --- End of 判断用户能否下载 /** * 如果unionUid不存在,只要存在本地数据库就行 * 如果unionUid存在,那么必须要先存在银联数据库,再存在本地。 */ $unionUid = $user['unionId']; $transSeq = "C{$uid}" . "D{$couponId}" . "T" . now(); //C+uid+ unionCouponId + datetime // 如果用户绑定银联钱包,先从银联下载优惠券 if (!empty($unionUid)) { $coupon = $this->coupon->get($couponId); $unionCouponId = $coupon['unionCouponId']; if (empty($unionCouponId)) { return $this->output_error(ErrorEmptyUnionCouponId); } // 从银联下载优惠券 $result = $this->kqlibrary->download_union_coupon($uid, $user['username'], $unionUid, $unionCouponId, $transSeq); if ($result == ErrorUnionInvalidCoupon || $result == ErrorUnionInvalidParameter || $result == ErrorUnionNoCardBunden) { //处理无效的uionUid和unionCouponId, 用户没有绑卡错误 return $this->output_error($result); } else { if (!is_array($result)) { // 其他union下载的错误 return $this->output_error($result); } } // 成功从银联下载了优惠券 } // 服务器下载快券 $couponId = $this->kqlibrary->download_coupon($uid, $couponId, $transSeq); // 是否应该在这里做download的increment工作? if ($couponId === false) { return $this->output_error(ErrorDBInsert); } else { //如果没有绑定银联用户,发送站内信 if (empty($unionUid)) { // 发送站内信 unset($data); $data['uid'] = $uid; $data['title'] = '下载快券'; // 要获得优惠券的完整title $completeTitle = $this->coupon->get_complete_title($couponId); $data['text'] = "您已成功下载" . $completeTitle . "快券,添加任意一张银联卡就可以开始享受快券的优惠咯!"; $this->load->model('news2_m', 'news'); $newsId = $this->news->insert($data); if (empty($newsId)) { // 如果没有insert成功 log_message('error', 'download coupon insert news error, uid #' . $uid); } /// --- Endof发送站内信 } return $this->output_success(); } }