Box.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\library\AliPay;
  4. use EasyWeChat\Factory;
  5. use think\Db;
  6. /**
  7. * @title 盲盒
  8. * @controller Box
  9. * @package app\api\controller
  10. */
  11. class Box extends Base
  12. {
  13. public function initialize(){
  14. parent::check_login();
  15. }
  16. /**
  17. * @title 抽奖
  18. * @desc 抽奖
  19. * @author Gavin
  20. * @url /api/Box/lucky_draw
  21. * @method POST
  22. * @header name:Authorization require:1 desc:Token
  23. *
  24. */
  25. public function lucky_draw(){
  26. $user = getMemberInfoHash($this->uid); //获取用户信息
  27. //获取每日免费次数
  28. $free_number = getConfigValue('free_lucky_number');
  29. //获取当日免费抽奖记录
  30. $date = date('Y-m-d');
  31. $now_date_count = Db::name('store_blind_box_log')
  32. ->where('m_id',$this->uid)
  33. ->where('date',$date)
  34. ->where('is_free',0)
  35. ->count();
  36. $free = $free_number-$now_date_count; //剩余免费次数
  37. $number = $user['lottery_number'] + $free;
  38. if ($number<=0) $this->error('抽奖次数不足');
  39. checkBox();
  40. $prize_arr = Db::name('store_blind_box')
  41. ->where('status',1)
  42. ->where('is_del',1)
  43. ->field('id,rate')
  44. ->select();
  45. foreach ($prize_arr as $key => $val) {
  46. $arr[$val['id']] = $val['rate'];//概率数组
  47. }
  48. $rid = get_rand($arr); //根据概率获取奖项id
  49. $info = Db::name('store_blind_box')->where('id',$rid)->field('id,title,prize,is_prize')->find();
  50. $return = [
  51. 'is_prize'=>$info['is_prize']
  52. ];
  53. $coll_info = getCollectionInfoHash($info['prize']);
  54. if ($info['is_prize']==1){
  55. $json_info = json_encode($coll_info,true);
  56. $return['name'] = $info['title'];
  57. $return['cover'] = $coll_info['cover'];
  58. }else{
  59. $json_info = '';
  60. $return['name'] = '未中奖';
  61. $return['cover'] = '';
  62. }
  63. $com = true;
  64. Db::startTrans();
  65. try {
  66. $data = [
  67. 'm_id'=>$this->uid,
  68. 'b_id'=>$rid,
  69. 'is_prize'=>$info['is_prize'],
  70. 'name'=>$return['name'],
  71. 'cover'=>$return['cover'],
  72. 'info'=>$json_info,
  73. 'date'=>$date,
  74. 'is_free'=>$free>0 ? 0 : 1
  75. ];
  76. Db::name('store_blind_box_log')->insert($data);
  77. if (!$free){
  78. Db::name('store_member')->where('id',$this->uid)->setDec('lottery_number');
  79. }
  80. //减少数据库库存
  81. if ($info['is_prize']==1){
  82. Db::name('store_collection')->where('id',$info['prize'])->setDec('now_inventory');
  83. //获取排名
  84. $rank = getRanking($info['prize'])+1;
  85. $tag = getTag($info['prize'],$rank,$coll_info['inventory']);
  86. saveRanking($info['prize']);
  87. $company = '纪元部落有限公司';
  88. $hash = getCompanyHash($info['prize']);
  89. $company_hash = $hash['hash'];
  90. $ddcid = Db::name('hash')->where('hash',$hash['hash'])->value('ddcid');
  91. $company_hash_time = $hash['create_at'] ? $hash['create_at'] : date('Y-m-d H:i:s');
  92. Db::name('hash')->where('hash',$hash['hash'])->update(['status'=>1]);
  93. $collectors_hash = '';
  94. $date = [
  95. 'order_id'=>0,
  96. 'order_no'=>get_order_sn(),
  97. 'tag'=>$tag,
  98. 'mid'=>$this->uid,
  99. 'c_id'=>$info['prize'],
  100. 'pro_info'=>$json_info,
  101. 'company'=>$company,
  102. 'company_hash'=>$company_hash,
  103. 'company_hash_time'=>$company_hash_time,
  104. 'ddcid'=>$ddcid,
  105. 'collectors_hash'=>$collectors_hash,
  106. 'collectors_hash_time'=>''
  107. ];
  108. Db::name('store_order_info')->insert($date);
  109. }
  110. Db::commit();
  111. }catch (\Exception $e){
  112. $com=false;
  113. Db::rollback();
  114. }
  115. if ($com){
  116. setMemberInfoHash($this->uid);
  117. $this->success('成功',$return);
  118. }
  119. $this->error('系统错误,请稍后重试');
  120. }
  121. /**
  122. * @title 充值次数
  123. * @desc 充值次数
  124. * @author Gavin
  125. * @url /api/Box/recharge
  126. * @method POST
  127. * @header name:Authorization require:1 desc:Token
  128. * @param name:num type:int require:1 default:1 desc:充值次数
  129. * @param name:pay_type type:string require:1 default:wx desc:wx:微信zfb:支付宝
  130. * @param name:from type:string require:1 default:wx desc:wx:微信公众号h5:网页
  131. *
  132. * @return name:order_no type:int require:0 default:0 desc:订单号
  133. * @return name:pay type:string require:0 default:0 desc:支付信息
  134. */
  135. public function recharge(){
  136. $num = input('num',1);
  137. $pay_type = input('pay_type','wx');
  138. $from = input('from','wx');
  139. $user = getMemberInfoHash($this->uid); //获取用户信息
  140. if ($num<1) $this->error('数量错误');
  141. $com = true;
  142. Db::startTrans();
  143. try {
  144. $order_no = get_order_sn();
  145. //获取价格
  146. $price = getConfigValue('lucky_recharge_price');
  147. $total_fee = bcmul($price,$num,2);
  148. $data = [
  149. 'order_no'=>$order_no,
  150. 'm_id'=>$this->uid,
  151. 'num'=>$num,
  152. 'price'=>$price,
  153. 'pay_price'=>$total_fee,
  154. 'pay_type'=>$pay_type
  155. ];
  156. Db::name('store_blind_recharge')->insert($data);
  157. $body = '纪元部落充值盲盒次数';
  158. switch ($pay_type){
  159. case 'wx':
  160. $config = retrunWxConfig();
  161. $total_fee = $total_fee * 100;
  162. $config['notify_url'] = $this->request->root(true).'/api/Pay/BlindRechargeNotify';
  163. $app = Factory::payment($config);
  164. $post_data = [
  165. 'body' => $body,
  166. 'out_trade_no' => $order_no,
  167. 'total_fee' => $total_fee,
  168. 'attach'=>$this->uid, //自定义传值
  169. ];
  170. //trade_type SAPI--JSAPI支付(或小程序支付)、NATIVE--Native支付、APP--app支付,MWEB--H5支付
  171. if ($from=='wx'){
  172. $post_data['openid'] = $user['openid'];
  173. $post_data['trade_type'] = 'JSAPI';
  174. }elseif ($from=='h5'){
  175. $post_data['trade_type'] = 'MWEB';
  176. }
  177. $result = $app->order->unify([
  178. 'body' => $body,
  179. 'out_trade_no' => $order_no,
  180. 'total_fee' => $total_fee,
  181. ]);
  182. if ($result['return_msg']=='OK'){
  183. if ($result['result_code']=='FAIL'){
  184. $com = false;
  185. Db::rollback();
  186. }else{
  187. $order1 = $app->jssdk->bridgeConfig($result['prepay_id']);//执行二次签名返回参数
  188. $retrun_data['order_no'] = $order_no;
  189. $retrun_data['pay'] = json_decode($order1,true);
  190. Db::rollback();
  191. }
  192. }else{
  193. $com = false;
  194. Db::rollback();
  195. }
  196. break;
  197. case 'zfb':
  198. $zfb = new AliPay();
  199. $notify_url = $this->request->root(true).'/api/Pay/alipayBlindRechargeNotify';//回调地址
  200. $order = $zfb->aliPay($body, $total_fee, $order_no, $notify_url);//调用支付宝支付的方法
  201. $retrun_data['order_no'] = $order_no;
  202. $retrun_data['pay'] = $order;
  203. Db::commit();
  204. break;
  205. }
  206. }catch (\Exception $e){
  207. $com=false;
  208. Db::rollback();
  209. }
  210. if ($com){
  211. $this->success('成功',$retrun_data);
  212. }
  213. $this->error('失败,请稍后重试');
  214. }
  215. /**
  216. * @title 用户中奖纪录
  217. * @desc 用户中奖纪录
  218. * @author Gavin
  219. * @method POST
  220. * @tag 编辑信息
  221. * @url /api/Box/boxLog
  222. * @header name:Authorization require:1 desc:Token
  223. *
  224. * @return name:id type:int require:0 default:0 desc:ID
  225. * @return name:name type:string require:0 default:0 desc:奖品名称
  226. * @return name:cover type:string require:0 default:0 desc:藏品图片
  227. * @return name:member_name type:string require:0 default:0 desc:用户昵称
  228. * @return name:create_at type:string require:0 default:0 desc:时间
  229. */
  230. public function boxLog(){
  231. $list = Db::name('store_blind_box_log')
  232. ->whereIn('is_prize',1)
  233. ->field('id,m_id,name,cover,create_at')
  234. ->order('id desc')
  235. ->limit(20)
  236. ->select();
  237. foreach ($list as &$v){
  238. $v['member_name'] = Db::name('store_member')->where('id',$v['m_id'])->value('name');
  239. }
  240. $this->success('成功',$list);
  241. }
  242. }