PayProc.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. <?php
  2. namespace loyalsoft;
  3. require_once __DIR__ . '/../service_call/pay/official/pay_op.php'; // 日志类型定义
  4. require_once __DIR__ . '/PayProc/MLogType.php'; // 日志类型定义
  5. if (PLAT == 'web') {
  6. include_once __DIR__ . '/PayProc/WebPayProc.php';
  7. }
  8. /**
  9. * 付费、游戏内消费相关处理
  10. * 全部动态交易数据需要保存到db作为[存根]
  11. * @version <br/>
  12. * 4.0.0 第四版: 言灵世界 商城模块
  13. * 2.2.0 第三版: 独立的游戏币<元宝>, 面向多渠道的充值模式, 拉起充值成功后, 给玩家发放元宝. 游戏内部消费元宝,钻石和金币.可以用元宝购买金币和钻石.这一版在上面的PayProc中 * 2015年9月23日 <br/>
  14. * 2.1.0 mn_pay mn_AddUserCash ... 以 mn_ 打头的函数是第二版, 修改为伪托管模式,除了充值的时候跟腾讯通讯,其余时间自己管理游戏币:钻石. * 2015年7月23日 <br/>
  15. * 2.0.1 m_pay m_AddUserCash ... 以 m_ 打头的函数是第一版, 完全按照应用宝官方要求托管游戏币的模式开发的. * 2015年4月23日 <br/>
  16. * 1.0.0 Created at 2016-4-20. by --gwang <br/>
  17. * @author gwang (mail@wanggangzero.cn)
  18. * @copyright © 2016-4-20, SJZ LoyalSoft Corporation & gwang. All rights reserved.
  19. */
  20. class PayProc {
  21. const orderTab = 'tpl_order_tab';
  22. /**
  23. * 逻辑分发
  24. * 所有的Proc中必须有这样一个方法
  25. */
  26. public static function procMain() {
  27. switch (req()->cmd) {
  28. // 支付相关的活动
  29. case CmdCode::cmd_pay_getfirstpaygift: # 8802 领取首充礼包
  30. return PayProc::m_GetFirstPayGift();
  31. // <editor-fold defaultstate="collapsed" desc=" 手机多渠道版 mpay ">
  32. case CmdCode::cmd_mpay_pay: # 8807 消费..
  33. return self::m_pay(); # ::==> 购买普通商城物品
  34. case CmdCode::cmd_mpay_get_balance: # 8808 刷新账号余额
  35. return self::m_refreshChargeOrders(); # ::==> 刷新订单-领取充值金额
  36. case CmdCode::cmd_mpay_buyDynamic: # 8809 购买动态商城物品
  37. return self::m_pay_buyDynamic();
  38. case CmdCode::cmd_mpay_getDynamic: # 8810 刷新神秘商城物品
  39. return self::m_pay_getDynamic();
  40. case CmdCode::cmd_mpay_selfCheckOrders: # 8811 自助检查异常订单
  41. return self::selfhelpCheckOrders();
  42. case CmdCode::cmd_mpay_getPayRecords: # 8812 查询订单记录
  43. return self::GetPayRecoreds();
  44. // </editor-fold>
  45. // <editor-fold defaultstate="collapsed" desc=" 支付新版 2021.11.16 ">
  46. case CmdCode::cmd_mpay_PayDeliverGoods: # 8813
  47. return self::PayDeliverGoods();
  48. case CmdCode::cmd_mpay_UserRechargeDrawPrize:
  49. return self::UserRechargeDrawPrize();
  50. case CmdCode::cmd_mpay_text:
  51. return self::text();
  52. // </editor-fold>
  53. default:
  54. return Resp::err(ErrCode::cmd_err);
  55. }
  56. }
  57. static function text() {
  58. return Resp::ok(array(
  59. "ret"=>null,
  60. ));
  61. }
  62. static $retArr;
  63. /**
  64. * 领取奖励
  65. * @return type
  66. */
  67. static function UserRechargeDrawPrize() {
  68. list($type,$typeId) = req()->paras;
  69. $user = req()->userInfo->game;
  70. self::$retArr = array();
  71. switch ($type) {
  72. case Enum_Recharge::FirstRecharge://首冲
  73. my_Assert($user->pay->firstRechage == 0, ErrCode::shop_firstRechargeNotRepeat);
  74. StoreProc::AddMultiItemInStore(GameConfig::globalsettings()->firstRechargePrizes);
  75. $user->pay->firstRechage = 1;
  76. self::$retArr[] = GameConfig::globalsettings()->firstRechargePrizes;
  77. break;
  78. case Enum_Recharge::DaliySpecialPackages://每日特惠
  79. my_Assert(!in_array($typeId, $user->shopdata->daliySpecialPackages), ErrCode::shop_daliySpecialRewardNotRepeat);
  80. $mo = GameConfig::shop_limit_getItem($typeId);
  81. my_Assert($mo != null, ErrCode::err_const_no);
  82. if($mo->price != 0){//购买60元7天特惠,则不是免费的礼包现在也可以领取。这里校验一下
  83. //my_Assert($user->shopdata->daliySpecial_ts!= 0, ErrCode::shop_daliySpecialRewardNotRepeat);
  84. $ts = $user->shopdata->daliySpecial_ts + 7*24*60*60;
  85. my_Assert(now() < $ts, ErrCode::shop_daliySpecialRewardNotRepeat);
  86. }
  87. $itemMO = GameConfig::item_package_getItem($mo->reward);
  88. my_Assert(null != $itemMO, ErrCode::err_const_no);
  89. StoreProc::AddMultiItemInStore($itemMO->contents);
  90. self::$retArr[] =$itemMO->contents;
  91. $shopdata = new Info_UserShop(req()->userInfo->game->shopdata);
  92. $shopdata->daliySpecialPackages[] = $typeId;
  93. req()->userInfo->game->shopdata = $shopdata;
  94. break;
  95. case Enum_Recharge::RechargeRebate://充值返利
  96. $mo = GameConfig::rechargeRebate_getItem($typeId);
  97. my_Assert($mo != null, ErrCode::err_const_no);
  98. my_Assert($user->baseInfo->charge_amt >= $mo->rmb, ErrCode::shop_RechargeRebateRmbNotEnough);
  99. my_Assert(!in_array($typeId, $user->shopdata->rechargeRebateRewardsList) , ErrCode::shop_RechargeRebateRewardNotRepeat);
  100. StoreProc::AddMultiItemInStore($mo->rewards);
  101. self::$retArr[] =$mo->rewards;
  102. break;
  103. case Enum_Recharge::LimitBuy://限购中免费礼包领取
  104. $packageCfg = GameConfig::shop_limit_getItem($typeId);
  105. my_Assert(null != $packageCfg, ErrCode::err_const_no);
  106. my_Assert($packageCfg->price == 0, ErrCode::shop_limit_NotFree);
  107. self::checklimitBuyRecharge($typeId);
  108. break;
  109. default:
  110. break;
  111. }
  112. req()->userInfo->game = $user;
  113. UserProc::updateUserInfo();
  114. return Resp::ok(array(
  115. "pay"=>$user->pay,
  116. "shopdata"=>req()->userInfo->game->shopdata,
  117. "base" => $user->baseInfo,
  118. "store" => $user->store,
  119. "ret"=>self::$retArr,
  120. ));
  121. }
  122. static function checklimitBuyRecharge($packageId) {
  123. //$packageId = req()->paras[0]; # 参数; 礼包id
  124. $shopdata = new Info_UserShop(req()->userInfo->game->shopdata);
  125. $packageCfg = GameConfig::shop_limit_getItem($packageId);
  126. my_Assert(null != $packageCfg, ErrCode::err_const_no);
  127. $arr = array();
  128. switch ((int) $packageCfg->subType) {
  129. case 1:
  130. $shopdata->purchasedDailyLimitPackages[] = $packageId;
  131. $arr = $shopdata->purchasedDailyLimitPackages;
  132. break;
  133. case 2:
  134. $shopdata->purchasedWeeklyLimitPackages[] = $packageId;
  135. $arr = $shopdata->purchasedWeeklyLimitPackages;
  136. break;
  137. case 3:
  138. $shopdata->purchasedMonthlyLimitPackages[] = $packageId;
  139. $arr = $shopdata->purchasedMonthlyLimitPackages;
  140. break;
  141. default:
  142. Err(ErrCode::err_innerfault);
  143. }
  144. $hasN = count(array_filter($arr, function ($val)use ($packageId) {
  145. return $val == $packageId;
  146. }));
  147. my_Assert($hasN <= $packageCfg->limit_num, ErrCode::shop_limit_max);
  148. $itemid = explode(',', $packageCfg->reward)[0]; # 奖励礼包Id
  149. $itemMO = GameConfig::item_package_getItem($itemid);
  150. my_Assert(null != $itemMO, ErrCode::err_const_no);
  151. $err = StoreProc::AddMultiItemInStore($itemMO->contents); # 发放奖励
  152. self::$retArr[] = $itemMO->contents;
  153. my_Assert(ErrCode::ok == $err, $err);
  154. req()->userInfo->game->shopdata = $shopdata; # 回写数据
  155. }
  156. /**
  157. * 充值发货
  158. */
  159. static function PayDeliverGoods() {
  160. list($cpOrderId) = req()->paras;
  161. $user = req()->userInfo->game;
  162. pay_op::CheckAndDrawOrder(req()->uid,$cpOrderId,array(new PayProc,'distributePayGoods'));
  163. req()->userInfo->game = $user;
  164. UserProc::updateUserInfo(); # 回写玩家数据
  165. return Resp::ok(array(
  166. "pay"=>$user->pay,
  167. "base" => $user->baseInfo,
  168. "shopdata"=>$user->shopdata,
  169. "store" => $user->store,
  170. "ret"=>self::$retArr,
  171. ));
  172. }
  173. //发货
  174. static function distributePayGoods($order = null) {
  175. my_Assert($order->product_id != null, ErrCode::pay_order_product_id);
  176. $user = req()->userInfo->game;
  177. self::$retArr = array();
  178. if ($order->product_id >= 6 && $order->product_id <= 1000) {//充值表
  179. $mo = GameConfig::recharge_getItem($order->product_id);
  180. my_Assert($mo != null, ErrCode::err_const_no);
  181. $cash = $mo->rmd *10;
  182. if($user->baseInfo->charge_amt <= 0){
  183. $cash = $mo->rmd *10* $mo->firstDouble;
  184. }
  185. $user->base()->Add_Cash($cash);
  186. self::$retArr[] = "2,".$cash;
  187. }
  188. elseif ($order->product_id >= 801001 && $order->product_id <= 801999) {//限购
  189. self::checklimitBuyRecharge($order->product_id);
  190. }
  191. elseif ($order->product_id >= 804001 && $order->product_id <= 804999) {//每日特惠
  192. $mo = GameConfig::shop_limit_getItem($order->product_id);
  193. if($mo == null){//校验看看是不是60元特惠礼包
  194. $list = GameConfig::shop_limit_type_getItemArray(Enum_Recharge::DaliySpecialPackages);
  195. $cur = 0;
  196. foreach ($list as $item) {
  197. $cur += $item->price;
  198. }
  199. //这临时先注释为了测试正式的应该解开注释
  200. //my_Assert($cur == $order->amount, ErrCode::err_const_no);
  201. $user->shopdata->daliySpecial_ts = now();
  202. } else {
  203. $itemMO = GameConfig::item_package_getItem($mo->reward);
  204. my_Assert(null != $itemMO, ErrCode::err_const_no);
  205. StoreProc::AddMultiItemInStore($itemMO->contents);
  206. $user->shopdata->daliySpecialPackages[] = $mo->typeId;
  207. self::$retArr[] = $itemMO->contents;
  208. }
  209. }
  210. elseif ($order->product_id >= 805001 && $order->product_id <= 805999) {//限时
  211. $mo = GameConfig::shop_limit_getItem($order->product_id);
  212. my_Assert($mo, ErrCode::err_const_no);
  213. my_Assert(!in_array($order->product_id, $user->shopdata->limitTsActiveRewardsList), ErrCode::err_const_no);
  214. $user->shopdata->limitTsActiveRewardsList[] = $mo->typeId;
  215. $itemMO = GameConfig::item_package_getItem($mo->reward);
  216. my_Assert(null != $itemMO, ErrCode::err_const_no);
  217. StoreProc::AddMultiItemInStore($itemMO->contents); //领取奖励
  218. //StoreProc::AddMultiItemInStore($mo->reward);
  219. self::$retArr[] = $itemMO->contents;
  220. }
  221. elseif ($order->product_id >= 901001 && $order->product_id <= 999999) {//月卡
  222. $shopdata = new Info_UserShop(req()->userInfo->game->shopdata);
  223. $packageCfg = GameConfig::shop_monthVIP_getItem($order->product_id);
  224. my_Assert(null != $packageCfg, ErrCode::err_const_no);
  225. if ($packageCfg->subType == 4) {
  226. my_Assert($shopdata->monthlyVIP1_ts + $packageCfg->ExpireTs * 3600 * 24 < now(), ErrCode::shop_monthlyvip_buyed);
  227. } elseif ($packageCfg->subType == 5) {
  228. my_Assert($shopdata->monthlyVIP2_ts + $packageCfg->ExpireTs * 3600 * 24 < now(), ErrCode::shop_monthlyvip_buyed);
  229. }
  230. $err = StoreProc::AddMultiItemInStore($packageCfg->pri_reward); # 发放一次性奖励
  231. self::$retArr[] = $packageCfg->pri_reward;
  232. my_Assert(ErrCode::ok == $err, $err);
  233. if ($packageCfg->subType == 4) {
  234. $shopdata->monthlyVIP1_ts = 3600 * 24 * (tsDay() + 1) - 1;
  235. } elseif ($packageCfg->subType == 5) {
  236. $shopdata->monthlyVIP2_ts = 3600 * 24 * (tsDay() + 1) - 1;
  237. }
  238. EmailProc::SendMonthlyVIPDailyReward(req()->zoneid, req()->uid, $packageCfg->name, $packageCfg->daily_reward);
  239. req()->userInfo->game->shopdata = $shopdata;
  240. }
  241. $user->baseInfo->charge_amt += $order->amount;
  242. req()->userInfo->game = $user;
  243. return true;
  244. //CLog::pay("支付奖励回存状态".$ret);
  245. //return $ret == ErrCode::ok? true:false;
  246. }
  247. /**
  248. * 8812 查询充值记录
  249. */
  250. static function GetPayRecoreds() {
  251. $uid = req()->uid;
  252. $table = "tpl_order_tab";
  253. $arr = daoInst()->select(" cpOrderId as '订单' ,product_name as '道具',amount as '金额' ,from_unixtime(order_ts) as '时间' ")->from($table)
  254. ->where("uid")->eq($uid)
  255. ->andWhere("status")->eq(1)
  256. ->fetchall();
  257. return Resp::ok($arr);
  258. }
  259. //
  260. // <editor-fold defaultstate="collapsed" desc=" 移动支付 - 刷新订单 ">
  261. /**
  262. * 刷新充值订单(领取充值结果)
  263. */
  264. public static function m_refreshChargeOrders() {
  265. # 客户端参数解析
  266. $uid = req()->uid; # userID
  267. $zoneid = req()->zoneid; # 分区Id
  268. if (count(req()->paras) <= 0) { # 参数为空, 执行自检逻辑
  269. return self::selfhelpCheckOrders();
  270. }
  271. $cpOrderId = req()->paras[0]; # 后台订单编号
  272. if (!$uid || ($zoneid < 1) || strlen($cpOrderId) != 16) { # 订单编号长16位
  273. return Resp::err(ErrCode::paras_err);
  274. }
  275. $order = daoInst()->select()->from(self::orderTab) # 取订单数据
  276. ->where('cpOrderId')->eq($cpOrderId)
  277. ->limit(1)->fetch();
  278. if (!$order) {
  279. // var_dump($order);
  280. CLog::pay(' 订单不存在! ' . $cpOrderId);
  281. return Resp::err(ErrCode::pay_order_no);
  282. }
  283. if (false === strpos($uid, $order->uid)) { # UID不符
  284. return Resp::err(ErrCode::pay_order_uid);
  285. }
  286. if ($order->status != 1) {
  287. CLog::pay(' 订单尚未完成支付!');
  288. return Resp::err(ErrCode::pay_order_paystatus);
  289. }
  290. if ($order->drawed_ts > 0) { # 定单已经发过货了
  291. CLog::pay(' 订单重复请求发货!' . $cpOrderId);
  292. return Resp::err(ErrCode::pay_order_drawed);
  293. }
  294. // 准备发货
  295. $product_id = $order->product_id;
  296. $product_count = $order->product_count;
  297. $typid = substr("$product_id", 0, 3); # 提取商品编号前缀
  298. switch ($typid) {
  299. case '802': # 神秘商城物品
  300. $item = GameConfig::secretshop_typeId_getItem($product_id);
  301. break;
  302. case '801': # 商城物品
  303. $item = GameConfig::shop_getItem($product_id);
  304. break;
  305. default :
  306. return Resp::err(ErrCode::pay_systembusy_err);
  307. }
  308. my_Assert(isset($item), ErrCode::pay_shopItem_cosnt_goods_err);
  309. if ($item->pricetype != 0) {
  310. CLog::pay(" 道具付费类型异常:" . $product_id);
  311. return Resp::err(ErrCode::pay_m_type_err);
  312. }
  313. for ($i = 0; $i < $product_count; $i++) { # 默认为1,还没有设计为n的情况
  314. StoreProc::AddMultiItemInStore($item->goods, 5); # 发货
  315. }
  316. $n = daoInst()->update(self::orderTab)
  317. ->data(array('drawed_ts' => now()))
  318. ->where('cpOrderId')->eq($cpOrderId)
  319. ->exec();
  320. my_Assert($n > 0, ErrCode::err_db); # 数据库操作失败
  321. req()->userInfo->game->baseInfo->charge_amt += $order->amount; # 历史充值记录(单位分)
  322. UserProc::updateUserInfo(); # 更新玩家数据信息
  323. Event::trigger('pay', array('amt' => $order->amount)); # 触发充值事件
  324. CLog::pay("订单发货成功: $cpOrderId");
  325. return Resp::ok(# 返回
  326. array('cash' => req()->userInfo->game->baseInfo->cash, # # 直接将游戏币的最新值返回给客户端
  327. 'charge_amt' => req()->userInfo->game->baseInfo->charge_amt, # 充值总记录
  328. 'store' => req()->userInfo->game->store, # # 背包
  329. 'goods' => $item->goods, # # 发货内容
  330. 'count' => $product_count, # # 发货数量
  331. )
  332. );
  333. }
  334. /**
  335. * 检查内侧订单
  336. */
  337. static function checkDeltest() {
  338. $uid = req()->uid;
  339. if (!isset(req()->userInfo->game->privateState->deltest)) {
  340. $orders = daoInst()->select()->from('tpl_order_tab_deltest')
  341. ->where('uid')->eq(substr($uid, strpos($uid, "-") + 1))
  342. ->andwhere('status')->eq(1)
  343. ->fetchall();
  344. if (count($orders) > 0) { # 有充值记录
  345. foreach ($orders as $order) {
  346. // 准备补发
  347. $product_id = $order->product_id;
  348. $product_count = $order->product_count;
  349. $typid = substr("$product_id", 0, 3); # 提取商品编号前缀
  350. switch ($typid) {
  351. case '802': # 神秘商城物品
  352. $item = GameConfig::secretshop_typeId_getItem($product_id);
  353. break;
  354. case '801': # 商城物品
  355. $item = GameConfig::shop_getItem($product_id);
  356. break;
  357. default :
  358. return Resp::err(ErrCode::pay_systembusy_err);
  359. }
  360. if (!$item) {
  361. return Resp::err(ErrCode::pay_shopItem_cosnt_goods_err);
  362. }
  363. if ($item->pricetype != 0) { # 并非人民币定价
  364. CLog::pay(" 道具付费类型异常:" . $product_id);
  365. return Resp::err(ErrCode::pay_m_type_err);
  366. }
  367. for ($i = 0; $i < $product_count; $i++) { # 默认为1,还没有设计为n的情况
  368. EmailProc::SendDelTestMail(req()->zoneid, $uid, $item->name, $item->goods);
  369. }
  370. CLog::pay(req()->uid . '发送删档内侧补偿邮件' . $order->cpOrderId);
  371. }
  372. }
  373. req()->userInfo->game->privateState->deltest = 0;
  374. }
  375. }
  376. /**
  377. * 检查异常订单
  378. */
  379. public static function selfhelpCheckOrders() {
  380. if ('ios' == PLAT) { # ios版的创建订单时带着前缀了。。。
  381. $uid = req()->uid;
  382. } else {
  383. $uid = substr(req()->uid, strpos(req()->uid, '-') + 1);
  384. }
  385. $orders = daoInst()->select()
  386. ->from('tpl_order_tab')
  387. ->where('uid')->eq($uid)
  388. ->andWhere('status')->eq(1)
  389. ->andWhere('drawed_ts')->le(0)
  390. ->fetchAll();
  391. if (count($orders) > 0) {
  392. foreach ($orders as $order) {
  393. // 准备补发
  394. $product_id = $order->product_id;
  395. $product_count = $order->product_count;
  396. $typid = substr("$product_id", 0, 3); # 提取商品编号前缀
  397. $goods = '';
  398. switch ($typid) {
  399. case '802': # 神秘商城物品
  400. $item = GameConfig::secretshop_typeId_getItem($product_id);
  401. break;
  402. case '801': # 商城物品
  403. $item = GameConfig::shop_getItem($product_id);
  404. break;
  405. default :
  406. return Resp::err(ErrCode::pay_systembusy_err);
  407. }
  408. if (!$item) {
  409. return Resp::err(ErrCode::pay_shopItem_cosnt_goods_err);
  410. }
  411. if ($item->pricetype != 0) { # 并非人民币定价
  412. CLog::pay(" 道具付费类型异常:" . $product_id);
  413. return Resp::err(ErrCode::pay_m_type_err);
  414. }
  415. for ($i = 0; $i < $product_count; $i++) { # 默认为1,还没有设计为n的情况
  416. StoreProc::AddMultiItemInStore($item->goods, 5); # 发货
  417. }
  418. $goods .= $item->goods;
  419. $n = daoInst()->update(self::orderTab) # 更新订单状态
  420. ->data(array('drawed_ts' => now()))
  421. ->where('cpOrderId')->eq($order->cpOrderId)
  422. ->exec();
  423. if (!$n) { # 影响条数应为1
  424. CLog::pay(req()->uid . ' 登录补发订单失败 ' . $order->cpOrderId);
  425. return Resp::err(ErrCode::err_db); # 数据库操作失败
  426. }
  427. req()->userInfo->game->baseInfo->charge_amt += $order->amount; # 历史充值记录(单位分)
  428. UserProc::updateUserInfo(); # 回写玩家数据
  429. CLog::pay(req()->uid . ' 登录补发订单 ' . $order->cpOrderId);
  430. }
  431. return Resp::ok(# 返回
  432. array('cash' => req()->userInfo->game->baseInfo->cash, # # 直接将游戏币的最新值返回给客户端
  433. 'store' => req()->userInfo->game->store, # # 背包
  434. 'goods' => $goods, # # 发货内容
  435. 'count' => count($orders), # # 发货数量
  436. 'msg' => "找到并处理" . count($orders) . "条未发货订单."
  437. ));
  438. }
  439. return Resp::ok('ok'); # everything is ok!
  440. }
  441. // </editor-fold>
  442. //
  443. // <editor-fold defaultstate="collapsed" desc=" 移动支付 - 商城购买 ">
  444. /**
  445. * 【移动支付】获取神秘商城物品
  446. * 刷新规则: 根据玩家拥有的英雄、装备、道具等数据进行刷新。(因英雄、道具、装备数量不足以支撑该刷新规则,目前先按照随机刷新做,几率平等)
  447. */
  448. public static function m_pay_getDynamic() {
  449. $user = req()->userInfo->game;
  450. $userSecretshop = new Info_UserSecretshop($user->userSecretshop);
  451. // 参数提取
  452. $refreshType = req()->paras[0]; # 刷新类型(参数)0,不刷,1,免费刷,2,钻石刷
  453. switch ($refreshType) {
  454. case 1: # 免费刷
  455. if (now() < $userSecretshop->lastRefreshTs + glc()->secretshop_refresh_interval) { // 检查是否达到免费刷新时间了, 执行自动更新
  456. return Resp::err(ErrCode::pay_secretshopt_freeRefresh_Time);
  457. }
  458. break;
  459. case 2: # 钻石刷
  460. if (glc()->secretshop_refresh_maxtimes <= $userSecretshop->refreshedTimes) { // 检查刷新次数, 已达上限, 返回错误信息
  461. return Resp::err(ErrCode::pay_refresh_times);
  462. } # 可以继续刷新,
  463. $cishu = $userSecretshop->refreshedTimes + 1; # 下次
  464. $amt = GameConfig::secretshop_refresh_getItem($cishu)->price;
  465. if (!$user->base()->Consume_Cash($amt)) { # 扣除本次所需费用, 余额不足, 返回错误信息
  466. return Resp::err(ErrCode::notenough_cash_msg);
  467. }
  468. $userSecretshop->refreshedTimes++; # 增加当天付费刷新计数
  469. break;
  470. case 0: # 不刷
  471. default : # 默认不刷
  472. // do nothing.
  473. break;
  474. }
  475. if ($refreshType != 0) { # 是否刷新
  476. $err = self::refreshDynamicShopItems($userSecretshop); # 更新物品表
  477. if ($err) {
  478. return Resp::err($err);
  479. }
  480. $user->userSecretshop = $userSecretshop;
  481. req()->userInfo->game = $user;
  482. UserProc::updateUserInfo();
  483. }
  484. // 返回最新物品表
  485. return Resp::ok(array(# # 成功后将最新的玩家数据返回给客户端
  486. 'gold' => $user->baseInfo->gold,
  487. 'tili' => $user->baseInfo->tili,
  488. 'cash' => $user->baseInfo->cash,
  489. 'uss' => $userSecretshop, # # 当前神秘商城数据
  490. ));
  491. }
  492. /**
  493. * 更新神秘商城物品
  494. * @param Info_UserSecretshop $userSecretshop Description
  495. */
  496. private static function refreshDynamicShopItems(&$userSecretshop) {
  497. $userSecretshop->lastRefreshTs = now();
  498. // todo: 这里补完更新物品的函数, // 第一版: 随机
  499. $userSecretshop->currentItems = ObjectInit();
  500. for ($i = 1; $i <= 3; $i++) { # 3种类型的商品
  501. $arr = GameConfig::secretshop_goodsType_getItem($i);
  502. if (count($arr) > 0) {
  503. $err = self::Dice(GameConfig::secretshop_goodsType_getItem($i), 1, $userSecretshop); # 一个种类一次1个物品
  504. if ($err) {
  505. return $err;
  506. }
  507. }
  508. }
  509. return ErrCode::ok;
  510. }
  511. /**
  512. * 投骰子
  513. * @param assoc_array $arr 奖池物品
  514. * @param int $number 提取数量
  515. * @return string itemid,num;itemid,num;...
  516. */
  517. static function Dice($arr, $number, &$userSecretshop) {
  518. $max = 0; # 计算物品权重总和
  519. array_walk($arr, function ($value) use (&$max) {
  520. $max += $value->probability;
  521. });
  522. if (!$max) { # 配置数据有问题
  523. return ErrCode::err_const_no;
  524. }
  525. for ($i = 0; $i < $number; $i++) {
  526. $rnd = CommUtil::random(1, $max); # 投骰子
  527. $start = 0;
  528. $rew = null;
  529. foreach ($arr as $item) { # 循环判断落入那个物品上
  530. if (CommUtil::isPropertyExists($item, 'probability') // #
  531. && $start < $rnd && $rnd <= $start + $item->probability) { # 落入区间
  532. $rew = $item; # 记录物品
  533. break;
  534. }
  535. $start += $item->probability; # 继续判断是否落入下一物品的区间
  536. } # foreach end
  537. if (!$rew) {
  538. return ErrCode::lottery_noselecteditem;
  539. }
  540. $id = $rew->typeId;
  541. $userSecretshop->currentItems->$id = 0;
  542. } # for end
  543. return ErrCode::ok;
  544. }
  545. /**
  546. * 清理每日各种上限
  547. */
  548. private static function clearDailyLimits() {
  549. $userSecretshop = new Info_UserSecretshop(req()->userInfo->game->userSecretshop);
  550. $userSecretshop->refreshedTimes = 0;
  551. req()->userInfo->game->userSecretshop = $userSecretshop;
  552. }
  553. /**
  554. * [8809]【移动支付】购买神秘商城物品
  555. * 规则: 商城物品会刷新
  556. */
  557. public static function m_pay_buyDynamic() {
  558. $user = new Data_UserGame(req()->userInfo->game);
  559. $userSecretshop = new Info_UserSecretshop($user->userSecretshop);
  560. $itemId = req()->paras[0]; # 商品id (点一次购买一个)
  561. $num = 1; # 暂时固定为一次购买一个
  562. #
  563. my_Assert(CommUtil::isPropertyExists($userSecretshop->currentItems, $itemId), ErrCode::pay_secretshop_noitem_err);
  564. $cishu = $userSecretshop->currentItems->$itemId; # 购买次数
  565. my_Assert($cishu < glc()->secretshop_itembuy_maxtimes, ErrCode::pay_secretshop_buytimes); # 判断购买次数
  566. $shopItem = GameConfig::secretshop_typeId_getItem($itemId); # 常量数据
  567. $amt = $shopItem->price * pow(2, $cishu); # 计算价格
  568. switch ($shopItem->pricetype) {
  569. case 1:
  570. my_Assert($user->base()->Consume_Cash($amt), ErrCode::notenough_cash_msg); # 扣除钻石
  571. break;
  572. case 2:
  573. my_Assert($user->base()->Consume_Gold($amt), ErrCode::notenough_gold_msg); # 扣除金币
  574. break;
  575. default :
  576. return Resp::err(ErrCode::pay_m_type_err);
  577. }
  578. // 道具解包/发货
  579. $err = self::expendSecretShopItem($shopItem, $num); # 发放商品
  580. if ($err != ErrCode::ok) { # 发货失败
  581. return Resp::err($err);
  582. }
  583. $userSecretshop->currentItems->$itemId += 1; # 购买次数+1
  584. $user->userSecretshop = $userSecretshop;
  585. req()->userInfo->game = $user;
  586. UserProc::updateUserInfo(); # 回写数据
  587. // StatProc::secretShopbuy($req->zoneid, $req->uid, $itemId, $num); # 统计/监控数据
  588. return Resp::ok(array(# # 成功后将最新的玩家数据返回给客户端
  589. 'gold' => $user->baseInfo->gold,
  590. 'tili' => $user->baseInfo->tili,
  591. 'cash' => $user->baseInfo->cash,
  592. 'store' => $user->baseInfo->store, # # 背包,(装备、碎片、。。)
  593. 'heros' => $user->baseInfo->heros # # 角色
  594. ));
  595. //
  596. }
  597. /**
  598. *
  599. * @param sm_secretshop $shopItem
  600. * @param type $num
  601. */
  602. private static function expendSecretShopItem($shopItem, $num) {
  603. for ($i = 0; $i < $num; $i++) {
  604. $err = StoreProc::AddMultiItemInStore($shopItem->goods, '神秘商城');
  605. if ($err) {
  606. return $err;
  607. }
  608. }
  609. return ErrCode::ok;
  610. }
  611. /**
  612. * [8807]【移动支付】 购买普通商城物品
  613. */
  614. public static function m_pay() {
  615. $user = req()->userInfo->game; # user引用
  616. my_Assert(count(req()->paras) >= 3, ErrCode::parasnotenough_msg); # 参数不足
  617. list($paytype, $itemId, $num) = req()->paras; # 付费类型, 1.钻石, 2.金币 ,道具ID ,数量, 默认为1
  618. my_Assert($num >= 1, ErrCode::paras_err); # 参数非法,
  619. $shopItem = GameConfig::shop_getItem($itemId); # 取商品的常量数据
  620. my_Assert(isset($shopItem), ErrCode::err_const_no); # 检测是否存在道具的常量数据
  621. my_Assert($shopItem->pricetype == $paytype, ErrCode::pay_price_err); # 商品定价类型检查
  622. $amt = $shopItem->price; # 道具价格(钻石)
  623. my_Assert($amt > 0, ErrCode::pay_price_err); # 商品定价数值检查
  624. $bDeal = false; # 成交标志位
  625. switch ($paytype) { # 1. 钻石, 2. 金币, other:非法
  626. case 1: # 钻石
  627. $bDeal = req()->userInfo->game->base()->Consume_Cash($amt); # 更新(扣除)玩家游戏币(钻石)余额
  628. my_Assert($bDeal, ErrCode::notenough_cash_msg); # 防范扣除失败
  629. break;
  630. case 2: # 金币
  631. $bDeal = $user->base()->Consume_Gold($amt * $num); # 更新玩家金币余额
  632. my_Assert($bDeal, ErrCode::notenough_gold_msg); # 防范扣除失败
  633. break;
  634. default : # 未知的支付类型
  635. return Resp::err(ErrCode::pay_m_type_err);
  636. }
  637. my_Assert($bDeal, ErrCode::err_innerfault); # 交易失败
  638. $err = self::expendShopItem($shopItem, $num); # 发放商品(普通商城只有金币和体力)
  639. my_Assert(ErrCode::ok == $err, $err); # 防范发货失败
  640. UserProc::updateUserInfo(); # 回写数据
  641. // StatProc::shopbuy($zoneid, $uid, $itemId, $num); # 统计/监控数据
  642. return Resp::ok(array(# # 成功后将最新的玩家数据返回给客户端
  643. 'gold' => $user->baseInfo->gold,
  644. 'tili' => $user->baseInfo->tili,
  645. 'cash' => $user->baseInfo->cash,
  646. 'store' => $user->store
  647. ));
  648. }
  649. /**
  650. * 解包商城物品
  651. * @param sm_Shop $shopItem
  652. * @param int $num 数量
  653. */
  654. static function expendShopItem($shopItem, $num) {
  655. for ($i = 0; $i < $num; $i++) {
  656. $err = StoreProc::AddMultiItemInStore($shopItem->goods, '商城');
  657. my_Assert(ErrCode::ok == $err, $err);
  658. }
  659. return ErrCode::ok;
  660. }
  661. // </editor-fold>
  662. //
  663. //
  664. // <editor-fold defaultstate="collapsed" desc=" 支付活动 - 首付礼包">
  665. /**
  666. * [8802] 领取首付礼包
  667. */
  668. public static function m_GetFirstPayGift() {
  669. $itemId = glc()->FirstPay_ItemId;
  670. $privateState = req()->userInfo->game->privateState;
  671. if (CommUtil::isPropertyExists($privateState, "firstPayGift") #
  672. && $privateState->firstPayGift) { # 如果已经领取首付礼包
  673. return Resp::err(ErrCode::pay_firstpaygetted);
  674. }
  675. my_Assert(req()->userInfo->game->baseInfo->charge_amt > 0, ErrCode::pay_firstpayno_err); # 如果尚未完成首付
  676. $itemModel = GameConfig::shop_getItem($itemId); # 取商品常量
  677. if ($itemModel == null) { # 检测首付礼包是否存在
  678. return Resp::err(ErrCode::err_const_no);
  679. }
  680. req()->userInfo->game->privateState->firstPayGift = true; # 设置首付标志
  681. StoreProc::AddMultiItemInStore($itemModel->goods); # 发放首付礼包到玩家仓库
  682. UserProc::updateUserInfo(); # 更新玩家数据
  683. return Resp::ok(array('itemid' => $itemId,
  684. 'rewardstr' => $itemModel->goods,
  685. 'store' => req()->userInfo->game->store)); # 回送成功信息
  686. }
  687. // </editor-fold>
  688. //
  689. //
  690. //
  691. }