Box.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. $company_hash_time = $hash['create_at'];
  91. Db::name('hash')->where('hash',$hash['hash'])->update(['status'=>1]);
  92. $collectors_hash = '';
  93. $date = [
  94. 'order_id'=>0,
  95. 'order_no'=>get_order_sn(),
  96. 'tag'=>$tag,
  97. 'mid'=>$this->uid,
  98. 'c_id'=>$info['prize'],
  99. 'name'=>$coll_info['name'],
  100. 'cover'=>$coll_info['cover'],
  101. 'pro_info'=>$json_info,
  102. 'company'=>$company,
  103. 'company_hash'=>$company_hash,
  104. 'company_hash_time'=>$company_hash_time,
  105. 'collectors_hash'=>$collectors_hash,
  106. 'collectors_hash_time'=>'',
  107. 'status'=>4
  108. ];
  109. Db::name('store_order_info')->insert($date);
  110. }
  111. Db::commit();
  112. }catch (\Exception $e){
  113. $com=false;
  114. Db::rollback();
  115. }
  116. if ($com){
  117. setMemberInfoHash($this->uid);
  118. if ($info['is_prize']==1){
  119. //减掉库存
  120. loseCollectionInventory($info['prize'],1);
  121. }
  122. $this->success('成功',$return);
  123. }
  124. $this->error('系统错误,请稍后重试');
  125. }
  126. /**
  127. * @title 充值次数
  128. * @desc 充值次数
  129. * @author Gavin
  130. * @url /api/Box/recharge
  131. * @method POST
  132. * @header name:Authorization require:1 desc:Token
  133. * @param name:num type:int require:1 default:1 desc:充值次数
  134. * @param name:pay_type type:string require:1 default:wx desc:wx:微信zfb:支付宝
  135. * @param name:from type:string require:1 default:wx desc:wx:微信公众号h5:网页
  136. *
  137. * @return name:order_no type:int require:0 default:0 desc:订单号
  138. * @return name:pay type:string require:0 default:0 desc:支付信息
  139. */
  140. public function recharge(){
  141. $num = input('num',1);
  142. $pay_type = input('pay_type','wx');
  143. $from = input('from','wx');
  144. $user = getMemberInfoHash($this->uid); //获取用户信息
  145. if ($num<1) $this->error('数量错误');
  146. $com = true;
  147. Db::startTrans();
  148. try {
  149. $order_no = get_order_sn();
  150. //获取价格
  151. $price = getConfigValue('lucky_recharge_price');
  152. $total_fee = bcmul($price,$num,2);
  153. $data = [
  154. 'order_no'=>$order_no,
  155. 'm_id'=>$this->uid,
  156. 'num'=>$num,
  157. 'price'=>$price,
  158. 'pay_price'=>$total_fee,
  159. 'pay_type'=>$pay_type
  160. ];
  161. Db::name('store_blind_recharge')->insert($data);
  162. $body = '梵易数藏充值盲盒次数';
  163. switch ($pay_type){
  164. case 'wx':
  165. $config = retrunWxConfig();
  166. $total_fee = $total_fee * 100;
  167. $config['notify_url'] = 'https://'.$_SERVER['SERVER_NAME'].'/api/Pay/BlindRechargeNotify';
  168. $app = Factory::payment($config);
  169. $post_data = [
  170. 'body' => $body,
  171. 'out_trade_no' => $order_no,
  172. 'total_fee' => $total_fee,
  173. 'attach'=>$this->uid, //自定义传值
  174. ];
  175. //trade_type SAPI--JSAPI支付(或小程序支付)、NATIVE--Native支付、APP--app支付,MWEB--H5支付
  176. if ($from=='wx'){
  177. $post_data['openid'] = $user['openid'];
  178. $post_data['trade_type'] = 'JSAPI';
  179. }elseif ($from=='h5'){
  180. $post_data['trade_type'] = 'MWEB';
  181. }
  182. $result = $app->order->unify($post_data);
  183. if ($result['return_msg']=='OK'){
  184. if ($result['result_code']=='FAIL'){
  185. $com = false;
  186. Db::rollback();
  187. }else{
  188. $order1 = $app->jssdk->bridgeConfig($result['prepay_id']);//执行二次签名返回参数
  189. $retrun_data['order_no'] = $order_no;
  190. $retrun_data['pay'] = json_decode($order1,true);
  191. Db::commit();
  192. }
  193. }else{
  194. $com = false;
  195. Db::rollback();
  196. }
  197. break;
  198. case 'zfb':
  199. $zfb = new AliPay();
  200. $notify_url = 'https://'.$_SERVER['SERVER_NAME'].'/api/Pay/alipayBlindRechargeNotify';//回调地址
  201. $order = $zfb->ali_pay_pc($body, $total_fee, $order_no, $notify_url,'https://'.$_SERVER['SERVER_NAME'].'/web/h5/pages/manghe/manghe');//调用支付宝支付的方法
  202. $retrun_data['order_no'] = $order_no;
  203. $retrun_data['pay'] = $order;
  204. Db::commit();
  205. break;
  206. }
  207. }catch (\Exception $e){
  208. $com=false;
  209. Db::rollback();
  210. }
  211. if ($com){
  212. $this->success('成功',$retrun_data);
  213. }
  214. $this->error('失败,请稍后重试');
  215. }
  216. /**
  217. * @title 用户中奖纪录
  218. * @desc 用户中奖纪录
  219. * @author Gavin
  220. * @method POST
  221. * @tag 编辑信息
  222. * @url /api/Box/boxLog
  223. * @header name:Authorization require:1 desc:Token
  224. *
  225. * @return name:id type:int require:0 default:0 desc:ID
  226. * @return name:name type:string require:0 default:0 desc:奖品名称
  227. * @return name:cover type:string require:0 default:0 desc:藏品图片
  228. * @return name:member_name type:string require:0 default:0 desc:用户昵称
  229. * @return name:create_at type:string require:0 default:0 desc:时间
  230. */
  231. public function boxLog(){
  232. $list = Db::name('store_blind_box_log')
  233. ->whereIn('is_prize',1)
  234. ->field('id,m_id,name,cover,create_at')
  235. ->order('id desc')
  236. ->limit(10)
  237. ->select();
  238. foreach ($list as &$v){
  239. $v['member_name'] = Db::name('store_member')->where('id',$v['m_id'])->value('name');
  240. }
  241. $this->success('成功',$list);
  242. }
  243. }