Hexiao.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 核销
  7. */
  8. class Hexiao extends Api
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = ['*'];
  12. public function __construct(){
  13. parent::__construct();
  14. if($this->auth->is_hexiao != 1){$this->error('没有核销权限');}
  15. }
  16. //首页
  17. public function index(){
  18. $wallet = Db::name('user_wallet')->where(['user_id' => $this->auth->id])->find();
  19. $rs = [
  20. 'hexiaomoney' => $wallet['hexiaomoney'],
  21. 'takecash' => $wallet['hexiaomoney'],
  22. 'wait' => '0.00',
  23. ];
  24. $this->success(1,$rs);
  25. }
  26. //扫核销码获取订单详情
  27. //code:order_hexiao_no|2024090466d7c9d5abb1c1
  28. //order_no:2024090466d7c9d5abb1c1
  29. public function order_info()
  30. {
  31. $code = input('code','');
  32. if(strpos($code,'order_hexiao_no|') !== 0){
  33. $this->error('识别不到的订单');
  34. }
  35. $order_no = substr($code,16);
  36. $order_info = Db::name('unishop_order')->where('out_trade_no',$order_no)->find();
  37. if(empty($order_info)){
  38. $this->error('不存在的订单');
  39. }
  40. if($order_info['status'] != 1){
  41. $this->error('非正常的订单');
  42. }
  43. if($order_info['have_paid'] == 0){
  44. $this->error('未支付的订单');
  45. }
  46. if($order_info['have_received'] != 0){
  47. $this->error('该订单已核销');
  48. }
  49. //下面是从unishop/order.php detail方法里拿过来的
  50. $order_id = $order_info['id'];
  51. $orderModel = new \addons\unishop\model\Order();
  52. $order = $orderModel
  53. ->with([
  54. 'products' => function ($query) {
  55. $query->field('id,order_id,image,number,price,spec,title,product_id');
  56. },
  57. ])
  58. ->where(['id' => $order_id])->find();
  59. if ($order) {
  60. $order = $order->append(['state', 'paidtime'])->toArray();
  61. foreach ($order['products'] as &$product) {
  62. $product['image'] = cdnurl($product['image']);
  63. }
  64. unset($order['pay_out_trade_no']);
  65. }
  66. $this->success(1,$order);
  67. }
  68. //完成核销动作
  69. public function hexiao(){
  70. $code = input('code','');
  71. if(strpos($code,'order_hexiao_no|') !== 0){
  72. $this->error('识别不到的订单');
  73. }
  74. $out_trade_no = substr($code,16);
  75. Db::startTrans();
  76. $order = Db::name('unishop_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
  77. if(empty($order)){
  78. Db::rollback();
  79. $this->error('不存在的订单');
  80. }
  81. if($order['status'] != 1){
  82. Db::rollback();
  83. $this->error('非正常的订单');
  84. }
  85. if($order['have_paid'] == 0){
  86. Db::rollback();
  87. $this->error('未支付的订单');
  88. }
  89. if($order['have_received'] != 0){
  90. Db::rollback();
  91. $this->error('该订单已核销');
  92. }
  93. $update = [
  94. 'have_received' => time(),
  95. 'hexiao_uid' => $this->auth->id,
  96. ];
  97. $order_rs = Db::name('unishop_order')->where('id',$order['id'])->update($update);
  98. if($order_rs === false){
  99. Db::rollback();
  100. $this->error('核销失败');
  101. }
  102. //给核销人结算
  103. $bili = config('site.hexiao_order_money_bili');
  104. $money = bcdiv(bcmul($order['total_price'],$bili,2),100,2);
  105. if($money > 0){
  106. $rs_wallet = model('Wallet')->lockChangeAccountRemain($this->auth->id,'hexiaomoney',$money,201,'核销订单:'.$out_trade_no,'unishop_order',$order['id']);
  107. if($rs_wallet['status']===false)
  108. {
  109. Db::rollback();
  110. $this->error($rs_wallet['msg']);
  111. }
  112. }
  113. Db::commit();
  114. $this->success('核销成功');
  115. }
  116. //核销统计
  117. public function order_sum(){
  118. $startdate = input('startdate',date('Y-m-d'));
  119. $enddate = input('enddate' ,date('Y-m-d'));
  120. $starttime = strtotime($startdate);
  121. $endtime = strtotime($enddate) + 86399;
  122. $condition = [
  123. 'hexiao_uid' => $this->auth->id,
  124. 'status' => 1,
  125. 'have_paid' => ['gt',0],
  126. 'have_delivered' => ['gt',0],
  127. 'have_received' => ['gt',0],
  128. ];
  129. $count = Db::name('unishop_order')->where($condition)->where('have_received','BETWEEN',[$starttime,$endtime])->count();
  130. $total_price = Db::name('unishop_order')->where($condition)->where('have_received','BETWEEN',[$starttime,$endtime])->sum('total_price');
  131. $rs = [
  132. 'count' => $count,
  133. 'total_price' => $total_price,
  134. ];
  135. $this->success(1,$rs);
  136. }
  137. //核销记录
  138. public function order_list(){
  139. $startdate = input('startdate',date('Y-m-d'));
  140. $enddate = input('enddate' ,date('Y-m-d'));
  141. $starttime = strtotime($startdate);
  142. $endtime = strtotime($enddate) + 86399;
  143. $orderModel = new \addons\unishop\model\Order();
  144. $condition = [
  145. 'hexiao_uid' => $this->auth->id,
  146. 'status' => 1,
  147. 'have_paid' => ['gt',0],
  148. 'have_delivered' => ['gt',0],
  149. 'have_received' => ['gt',0],
  150. ];
  151. $result = $orderModel
  152. ->with([
  153. 'products' => function($query) {
  154. $query->field('id,title,image,number,price,spec,order_id,product_id');
  155. },
  156. ])
  157. ->where($condition)
  158. ->where('have_received','BETWEEN',[$starttime,$endtime])
  159. ->order(['have_received' => 'desc'])
  160. ->autopage()
  161. ->select();
  162. foreach ($result as &$item) {
  163. $item->append(['order_id','state']);
  164. $item = $item->toArray();
  165. unset($item['pay_out_trade_no']);
  166. foreach ($item['products'] as &$product) {
  167. $product['image'] = cdnurl($product['image']);
  168. }
  169. }
  170. $this->success(1,$result);
  171. }
  172. //我的核销余额日志
  173. public function my_intromoney_log(){
  174. $type = input('type',0);
  175. $map = [
  176. 'user_id' => $this->auth->id,
  177. ];
  178. if($type == 1){
  179. $map['change_value'] = ['gt',0];
  180. }
  181. if($type == 2){
  182. $map['change_value'] = ['lt',0];
  183. }
  184. $list = Db::name('user_intromoney_log')
  185. ->field('id,log_type,before,change_value,remain,remark,createtime')
  186. ->where($map)->order('id desc')->autopage()->select();
  187. // $list = $this->list_appen_logtext($list);
  188. $this->success('success',$list);
  189. }
  190. //提现配置
  191. public function take_cash_config(){
  192. $plat_bilv = config('site.hexiao_takecash_plat_bili');
  193. $take_cash_days = config('site.hexiao_takecash_days');
  194. $take_cash_days_str = implode(',',$take_cash_days);
  195. $data = [
  196. 'hexiaomoney' => model('wallet')->getwallet($this->auth->id,'hexiaomoney'),
  197. 'plat_bilv' => $plat_bilv,
  198. 'user_bank' => Db::name('user_bank')->where('user_id',$this->auth->id)->find(),
  199. 'take_cash_days' => $take_cash_days_str,
  200. 'remark' => '每月'.$take_cash_days_str.'日可以提现',
  201. 'take_cash_button' => in_array(date('d'),$take_cash_days) ? 1 : 0,
  202. ];
  203. $this->success('success',$data);
  204. }
  205. //提现before
  206. public function take_cash_before(){
  207. $freemoney = input('freemoney',0);
  208. if(!$freemoney){
  209. $this->error('请填写金额');
  210. }
  211. $money = floatval($freemoney);
  212. if($money<=0)
  213. {
  214. $this->error('金额必须大于0');
  215. }
  216. $min = config('site.hexiao_takecash_min_money');
  217. $max = config('site.hexiao_takecash_max_money');
  218. if($money < $min){
  219. $this->error('提现金额不能小于'.$min);
  220. }
  221. if($money > $max){
  222. $this->error('提现金额不能大于'.$max);
  223. }
  224. $user_money = model('wallet')->getwallet($this->auth->id,'hexiaomoney');
  225. if($money > $user_money){
  226. $this->error('提现金额不能大于可提现余额');
  227. }
  228. //平台手续费
  229. $plat_bilv = config('site.hexiao_takecash_plat_bili');
  230. $plat_money = bcdiv(bcmul($money,$plat_bilv,2),100,2);
  231. //减去手续费,得实得金额
  232. $get_money = bcsub($money,$plat_money,2);
  233. $data = [
  234. 'money' => $money,
  235. 'plat_bilv' => $plat_bilv,
  236. 'plat_money' => $plat_money,
  237. 'get_money' => $get_money,
  238. ];
  239. $this->success(1,$data);
  240. }
  241. //提现
  242. public function take_cash(){
  243. $freemoney = input('freemoney',0);
  244. // $type = input('type',1);
  245. $type = 2;
  246. if(!$freemoney){
  247. $this->error('请填写金额');
  248. }
  249. if (!in_array($type,[1,2,3])) {
  250. $this->error('未知的提现类型');
  251. }
  252. //提现日期限制
  253. $take_cash_days = config('site.hexiao_takecash_days');
  254. if(!in_array(date('d'),$take_cash_days)){
  255. $take_cash_days_str = implode(',',$take_cash_days);
  256. $this->error('每月'.$take_cash_days_str.'日才可以提现');
  257. }
  258. //赋值money
  259. /*if($rc_id){
  260. $recharge_config = Db::name('take_cash_config')->where('id',$rc_id)->find();
  261. $money = $recharge_config['money'] ?: 0;
  262. }*/
  263. //自由输入覆盖
  264. if(!empty($freemoney)){
  265. $rc_id = 0;
  266. $money = floatval($freemoney);
  267. }
  268. //
  269. if($money<=0)
  270. {
  271. $this->error('金额必须大于0');
  272. }
  273. $min = config('site.hexiao_takecash_min_money');
  274. $max = config('site.hexiao_takecash_max_money');
  275. if($money < $min){
  276. $this->error('提现金额不能小于'.$min);
  277. }
  278. if($money > $max){
  279. $this->error('提现金额不能大于'.$max);
  280. }
  281. $check = Db::name('hexiao_user_take_cash')->where(['user_id'=>$this->auth->id,'status'=>0])->find();
  282. if($check){
  283. $this->error('您已经申请了提现,请等待审核');
  284. }
  285. $user_money = model('wallet')->getwallet($this->auth->id,'hexiaomoney');
  286. if($money > $user_money){
  287. $this->error('提现金额不能大于可提现余额');
  288. }
  289. if($type == 1){
  290. $table_name = 'user_alipay';
  291. $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
  292. if(empty($account_json)){
  293. $this->error('未绑定对应的提现账号');
  294. }
  295. }elseif($type == 2){
  296. $table_name = 'user_bank';
  297. $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
  298. if(empty($account_json)){
  299. $this->error('未绑定对应的提现账号');
  300. }
  301. }elseif($type == 3){
  302. //微信支付
  303. $table_name = 'user_wechat';
  304. $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
  305. if(empty($account_json)){
  306. $this->error('未绑定对应的提现账号');
  307. }
  308. }
  309. //平台手续费
  310. $plat_bilv = config('site.hexiao_takecash_plat_bili');
  311. $plat_money = bcdiv(bcmul($money,$plat_bilv,2),100,2);
  312. //减去手续费,得实得金额
  313. $get_money = bcsub($money,$plat_money,2);
  314. $data = [
  315. 'user_id' => $this->auth->id,
  316. 'money' => $money,
  317. 'plat_bilv' => $plat_bilv,
  318. 'plat_money' => $plat_money,
  319. 'get_money' => $get_money,
  320. 'type' => $type,
  321. 'acount_json' => json_encode($account_json),
  322. 'createtime' => time(),
  323. 'updatetime' => time(),
  324. 'status' => 0,
  325. ];
  326. Db::startTrans();
  327. $log_id = Db::name('hexiao_user_take_cash')->insertGetId($data);
  328. if(!$log_id){
  329. Db::rollback();
  330. $this->error('提现失败');
  331. }
  332. //扣除money
  333. $rs_wallet = model('Wallet')->lockChangeAccountRemain($this->auth->id,'hexiaomoney',-$money,221,'提现(审核中)','hexiao_user_take_cash',$log_id);
  334. if($rs_wallet['status']===false)
  335. {
  336. Db::rollback();
  337. $this->error($rs_wallet['msg']);
  338. }
  339. Db::commit();
  340. $this->success('申请成功请等待审核');
  341. }
  342. //提现记录
  343. public function take_cash_log(){
  344. $list = Db::name('hexiao_user_take_cash')->field('id,money,get_money,type,createtime,status')->where(['user_id'=>$this->auth->id])->autopage()->select();
  345. foreach($list as $key => &$val){
  346. $val['remark'] = '';
  347. if($val['type'] == 1){
  348. $val['remark'] = '支付宝提现';
  349. }elseif($val['type'] == 2){
  350. $val['remark'] = '银行卡提现';
  351. }else{
  352. $val['remark'] = '微信提现';
  353. }
  354. }
  355. $this->success('success',$list);
  356. }
  357. }