Hexiao.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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.unishop_order_hexiaomoney_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 take_cash_config(){
  174. $plat_bilv = config('site.hexiao_takecash_plat_bili');
  175. $take_cash_days = config('site.hexiao_take_cash_days');
  176. $take_cash_days_str = implode(',',$take_cash_days);
  177. $data = [
  178. 'hexiaomoney' => model('wallet')->getwallet($this->auth->id,'hexiaomoney'),
  179. 'plat_bilv' => $plat_bilv,
  180. 'user_bank' => Db::name('user_bank')->where('user_id',$this->auth->id)->find(),
  181. 'take_cash_days' => $take_cash_days_str,
  182. 'remark' => '每月'.$take_cash_days_str.'日可以提现',
  183. 'take_cash_button' => in_array(date('d'),$take_cash_days) ? 1 : 0,
  184. ];
  185. $this->success('success',$data);
  186. }
  187. //提现before
  188. public function take_cash_before(){
  189. $freemoney = input('freemoney',0);
  190. if(!$freemoney){
  191. $this->error('请填写金额');
  192. }
  193. $money = floatval($freemoney);
  194. if($money<=0)
  195. {
  196. $this->error('金额必须大于0');
  197. }
  198. $min = config('site.hexiao_min_takecash_money');
  199. $max = config('site.hexiao_max_takecash_money');
  200. if($money < $min){
  201. $this->error('提现金额不能小于'.$min);
  202. }
  203. if($money > $max){
  204. $this->error('提现金额不能大于'.$max);
  205. }
  206. $user_money = model('wallet')->getwallet($this->auth->id,'hexiaomoney');
  207. if($money > $user_money){
  208. $this->error('提现金额不能大于可提现余额');
  209. }
  210. //平台手续费
  211. $plat_bilv = config('site.hexiao_takecash_plat_bili');
  212. $plat_money = bcdiv(bcmul($money,$plat_bilv,2),100,2);
  213. //减去手续费,得实得金额
  214. $get_money = bcsub($money,$plat_money,2);
  215. $data = [
  216. 'money' => $money,
  217. 'plat_bilv' => $plat_bilv,
  218. 'plat_money' => $plat_money,
  219. 'get_money' => $get_money,
  220. ];
  221. $this->success(1,$data);
  222. }
  223. //提现
  224. public function take_cash(){
  225. $freemoney = input('freemoney',0);
  226. // $type = input('type',1);
  227. $type = 2;
  228. if(!$freemoney){
  229. $this->error('请填写金额');
  230. }
  231. if (!in_array($type,[1,2,3])) {
  232. $this->error('未知的提现类型');
  233. }
  234. //提现日期限制
  235. $take_cash_days = config('site.hexiao_take_cash_days');
  236. if(!in_array(date('d'),$take_cash_days)){
  237. $take_cash_days_str = implode(',',$take_cash_days);
  238. $this->error('每月'.$take_cash_days_str.'日才可以提现');
  239. }
  240. //赋值money
  241. /*if($rc_id){
  242. $recharge_config = Db::name('take_cash_config')->where('id',$rc_id)->find();
  243. $money = $recharge_config['money'] ?: 0;
  244. }*/
  245. //自由输入覆盖
  246. if(!empty($freemoney)){
  247. $rc_id = 0;
  248. $money = floatval($freemoney);
  249. }
  250. //
  251. if($money<=0)
  252. {
  253. $this->error('金额必须大于0');
  254. }
  255. $min = config('site.hexiao_min_takecash_money');
  256. $max = config('site.hexiao_max_takecash_money');
  257. if($money < $min){
  258. $this->error('提现金额不能小于'.$min);
  259. }
  260. if($money > $max){
  261. $this->error('提现金额不能大于'.$max);
  262. }
  263. $check = Db::name('hexiao_user_take_cash')->where(['user_id'=>$this->auth->id,'status'=>0])->find();
  264. if($check){
  265. $this->error('您已经申请了提现,请等待审核');
  266. }
  267. $user_money = model('wallet')->getwallet($this->auth->id,'hexiaomoney');
  268. if($money > $user_money){
  269. $this->error('提现金额不能大于可提现余额');
  270. }
  271. if($type == 1){
  272. $table_name = 'user_alipay';
  273. $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
  274. if(empty($account_json)){
  275. $this->error('未绑定对应的提现账号');
  276. }
  277. }elseif($type == 2){
  278. $table_name = 'user_bank';
  279. $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
  280. if(empty($account_json)){
  281. $this->error('未绑定对应的提现账号');
  282. }
  283. }elseif($type == 3){
  284. //微信支付
  285. $table_name = 'user_wechat';
  286. $account_json = Db::name($table_name)->where('user_id',$this->auth->id)->find();
  287. if(empty($account_json)){
  288. $this->error('未绑定对应的提现账号');
  289. }
  290. }
  291. //平台手续费
  292. $plat_bilv = config('site.unishop_order_hexiaomoney_bili');
  293. $plat_money = bcdiv(bcmul($money,$plat_bilv,2),100,2);
  294. //减去手续费,得实得金额
  295. $get_money = bcsub($money,$plat_money,2);
  296. $data = [
  297. 'user_id' => $this->auth->id,
  298. 'money' => $money,
  299. 'plat_bilv' => $plat_bilv,
  300. 'plat_money' => $plat_money,
  301. 'get_money' => $get_money,
  302. 'type' => $type,
  303. 'acount_json' => json_encode($account_json),
  304. 'createtime' => time(),
  305. 'updatetime' => time(),
  306. 'status' => 0,
  307. ];
  308. Db::startTrans();
  309. $log_id = Db::name('hexiao_user_take_cash')->insertGetId($data);
  310. if(!$log_id){
  311. Db::rollback();
  312. $this->error('提现失败');
  313. }
  314. //扣除money
  315. $rs_wallet = model('Wallet')->lockChangeAccountRemain($this->auth->id,'hexiaomoney',-$money,221,'提现(审核中)','hexiao_user_take_cash',$log_id);
  316. if($rs_wallet['status']===false)
  317. {
  318. Db::rollback();
  319. $this->error($rs_wallet['msg']);
  320. }
  321. Db::commit();
  322. $this->success('申请成功请等待审核');
  323. }
  324. //提现记录
  325. public function take_cash_log(){
  326. $list = Db::name('hexiao_user_take_cash')->field('id,money,get_money,type,createtime,status')->where(['user_id'=>$this->auth->id])->autopage()->select();
  327. foreach($list as $key => &$val){
  328. $val['remark'] = '';
  329. if($val['type'] == 1){
  330. $val['remark'] = '支付宝提现';
  331. }elseif($val['type'] == 2){
  332. $val['remark'] = '银行卡提现';
  333. }else{
  334. $val['remark'] = '微信提现';
  335. }
  336. }
  337. $this->success('success',$list);
  338. }
  339. }