123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470 |
- <?php
- namespace app\api\controller;
- use app\admin\model\Company;
- use app\admin\model\WechatPlan;
- use app\admin\model\WechatPlanRecord;
- use app\common\controller\Api;
- use app\common\model\{Category, Lave as LaveModel, User};
- use app\common\model\LaveMonth as LaveMonthModel;
- use liuniu\repositories\LaveRepository;
- use liuniu\UtilService;
- use liuniu\WechatService;
- use think\Request;
- class Lave extends Api
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
- public function lst(Request $request)
- {
- $where = UtilService::getMore(
- [
- ['page', 1],
- ['limit', 10],
- ['cid', $this->cid],
- ['paid', 1],
- ['category_id', 0],
- ['order', ''],
- ['key', ''],
- ], $request
- );
- $this->success('获取成功', LaveModel::lst($where));
- }
- public function ify()
- {
- $this->success('获取成功', Category::getCategoryArray('lave', null, $this->cid));
- }
- public function create(Request $request)
- {
- $where = UtilService::postMore(
- [
- ['cid', $this->cid],
- ['user_id', $this->auth->getUserinfo()['id']],
- ['order_name', ''],
- ['category_id', 0],
- ['amount', 0],
- ['name', ''],
- ['contact', ''],
- ['tel', 0],
- ['address', ''],
- ['is_open', '0'],
- ['is_ticket', '0'],
- ['pay_type', '0'],
- ['type', '0'],
- ['help_id', 0],
- ['from', 'weixin'],
- ], $request
- );
- $where1 = $where;
- unset($where1['from']);
- $where1['order_id'] = LaveModel::getNewOrderId();
- if (cache('lave_' . $where['user_id'])) $this->error('正在处理中');
- @file_put_contents("lave.txt", json_encode($where1));
- $order = LaveModel::create($where1);
- cache('lave_' . $where['user_id'], '1', 10);
- if (!$order) $this->error(LaveModel::getErrorInfo());
- $orderId = $order['order_id'];
- $info = compact('orderId');
- if ($orderId) {
- $orderInfo = LaveModel::where('order_id', $orderId)->find();
- if (!$orderInfo || !isset($orderInfo['paid'])) $this->error('支付订单不存在!');
- if ($orderInfo['paid']) $this->error('支付已支付!');
- try {
- if ($where['from'] == 'routine') {
- $jsConfig = LaveRepository::jsPay($this->cid, $orderId);
- } else if ($where['from'] == 'weixinh5') {
- $jsConfig = LaveRepository::h5Pay($this->cid, $orderId);
- } else {
- $jsConfig = LaveRepository::wxPay($this->cid, $orderId);
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- $info['jsConfig'] = $jsConfig;
- return $this->success('订单创建成功', $info);
- } else $this->error(LaveModel::getErrorInfo());
- }
- public function mylst(Request $request)
- {
- $where = UtilService::getMore(
- [
- ['page', 1],
- ['limit', 10],
- ['cid', $this->cid],
- ['user_id', $this->auth->getUserinfo()['id']],
- ['paid', -1],
- ['category_id', 0],
- ], $request
- );
- $this->success('获取成功', LaveModel::lst($where));
- }
-
- public function pay(Request $request)
- {
- list($uni, $paytype, $from) = UtilService::postMore([
- ['uni', ''],
- ['paytype', '0'],
- ['from', 'weixin']
- ], $request, true);
- if (!$uni) $this->error('参数错误!');
- $order = LaveModel::where('cid', $this->cid)->where('order_id', $uni)->find();
- if (!$order)
- $this->error('订单不存在!');
- if ($order['paid'])
- $this->error('该订单已支付!');
- $order['pay_type'] = $paytype;
- switch ($order['pay_type']) {
- case '0':
- try {
- if ($from == 'routine') {
- $jsConfig = LaveRepository::jsPay($this->cid, $order);
- } else if ($from == 'weixinh5') {
- $jsConfig = LaveRepository::h5Pay($this->cid, $order);
- } else {
- $jsConfig = LaveRepository::wxPay($this->cid, $order);
- }
- } catch (\Exception $e) {
- $this->error($e->getMessage());
- }
- $this->success('获取成功', $jsConfig);
- break;
- }
- return $this->error('支付方式错误');
- }
- public function createSign(Request $request)
- {
- $where = UtilService::postMore(
- [
- ['cid', $this->cid],
- ['user_id', $this->auth->getUserinfo()['id']],
- ['order_name', ''],
- ['amount', 0],
- ['name', ''],
- ['contact', ''],
- ['tel', 0],
- ['address', ''],
- ['is_open', '0'],
- ['is_ticket', '0'],
- ['pay_type', '0'],
- ['type', '0'],
- ['plan_id', 0],
- ['from', 'weixin'],
- ], $request
- );
- $where1 = $where;
- unset($where1['from']);
- $where1['order_id'] = LaveMonthModel::getNewOrderId();
- if ($where['amount']>500){
- $this->error('捐款金额不能大于500');
- }
- $sign=WechatPlanRecord::where('uid', $where1['user_id'])->where('plan_id', $where1['plan_id'])->where('is_signing',0)->find();
- if (!empty($sign)){
- $this->error('您已签约,请勿重复签约!');
- }
- if (cache('lave_' . $where['user_id'])) $this->error('正在处理中');
- $plan_id=$where1['plan_id'];
- $where1['contract_code'] = WechatPlanRecord::getNewCode();
- $order = LaveMonthModel::create($where1);
- $where1['contract_display_account']=$where1['name'];
- cache('lave_' . $where['user_id'], '1', 10);
- if (!$order) $this->error(LaveMonthModel::getErrorInfo());
- $orderId = $order['order_id'];
- $info = compact('orderId');
- $order['plan_id'] = $plan_id;
- if ($orderId) {
- $orderInfo = LaveMonthModel::where('order_id', $orderId)->find();
- if (!$orderInfo || !isset($orderInfo['paid'])){
- $this->error('支付订单不存在!');
- }
- if ($orderInfo['paid']) $this->error('支付已支付!');
- $plan_record=[
- 'plan_id'=>$plan_id,
- 'cid'=>$where['cid'],
- 'uid'=>$where['user_id'],
- 'price'=>$where['amount'],
- 'is_signing'=>0,
- 'contract_code'=>$where1['contract_code'],
- 'contract_display_account'=>$where1['contract_display_account'],
- 'is_open'=>$where1['is_open'],
- 'tel'=>$where1['tel'],
- 'order_name'=>$where1['order_name'],
- ];
- $count=WechatPlanRecord::where('uid', $where1['user_id'])->where('plan_id',$plan_id)->count();
- $record=WechatPlanRecord::create($plan_record);
- @file_put_contents("quanju.txt", json_encode($record)."-签约模版记录\r\n", 8);
- LaveMonthModel::where('order_id', $orderId)->update(['record_id' => $record['id']]);
- $plan_record['spbill_create_ip']=User::where('id', $where1['user_id'])->value('loginip');
- $plan_record['contract_notify_url']=Request::instance()->domain() . "/api/wechat/notify/" . $where['cid'];
- try {
- if ($where['from'] == 'routine') {
- $jsConfig = LaveRepository::jsPaySign($this->cid, $orderId,$plan_record);
- } else if ($where['from'] == 'weixinh5') {
- $jsConfig = LaveRepository::h5PaySign($this->cid, $orderId,$plan_record);
- } else {
- $jsConfig = LaveRepository::wxPaySign($this->cid, $orderId,$plan_record);
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- $info['jsConfig'] = $jsConfig;
- if ($count==0){
- $num=WechatPlan::where('plan_id', $plan_id)->value('num');
- WechatPlan::where('plan_id', $plan_id)->update(['num' => $num+1]);
- }
- return $this->success('订单创建成功', $info);
- } else $this->error(LaveMonthModel::getErrorInfo());
- }
-
- public function payPap(Request $request)
- {
- $list=WechatPlanRecord::where('is_signing',0)->select();
- $cid=$this->cid;
- $arr=[];
- $arr['body']='月捐款';
- foreach ($list as $k => $v) {
- $day = intval(date('d', $v['createtime']));
- $date = date('Y-m', $v['createtime']);
- $last=date("t", time());
- $today = intval(date('d'));
- $now_date = date('Y-m', time());
- if ($date != $now_date) {
- if ($today == $day||$today==$last) {
- $cid=$v['cid'];
- $mch_id =Company::where('id',$cid)->value('pay_weixin_mchid');
- $where1['cid']=$cid;
- $where1['user_id']=$v['uid'];
- $where1['order_name']=$v['order_name'];
- $where1['order_id']=LaveMonthModel::getNewOrderId();;
- $where1['amount']=$v['price'];
- $where1['name']=$v['contract_display_account'];
- $where1['contact']='';
- $where1['tel']=$v['tel'];
- $where1['address']='用户未填写联系地址';
- $where1['is_open']=$v['is_open'];
- $where1['is_ticket']='0';
- $where1['type']='0';
- $where1['record_id']=$v['id'];
- $where1['contract_code']=$v['contract_code'];
- $where1['plan_id']=$v['plan_id'];
- var_dump($where1);die();
- $where1['nonce_str'] = uniqid();
- @file_put_contents("quanju.txt", json_encode($where1)."测试申请扣款-\r\n", 8);
- $order = LaveMonthModel::create($where1);
- var_dump(988745613);die();
- $rs = WechatService::papPayApply($mch_id,$where1['order_id'], $where1['amount'], "lave", '月捐款', 'PAP', [], $cid,$v['contract_id'],$where1['order_name'],$where1['nonce_str']);
- }
- }
- }
- }
- public function deleteSign(Request $request){
- $where = UtilService::postMore(
- [
- ['id',0],
- ['cid', $this->cid],
- ['user_id', $this->auth->getUserinfo()['id']],
- ], $request
- );
- if ($where['id']==0){
- $sign_info=WechatPlanRecord::where('cid', $where['cid'])->where('uid', $where['user_id'])->where('is_signing',0)->find();
- }else{
- $sign_info=WechatPlanRecord::where('id', $where['id'])->where('is_signing',0)->find();
- }
- if (!$sign_info){
- $this->error('未找到签约信息!');
- }
- $mch_id =Company::where('id', $where['cid'])->value('pay_weixin_mchid');
- $plan_id=$sign_info['plan_id'];
- $contract_code=$sign_info['contract_code'];
- $rs=WechatService::deleteSign($mch_id,$contract_code,$plan_id,'1.0',[],$where['cid']);
- @file_put_contents("quanju.txt", json_encode($rs)."-解除签约返回结果\r\n", 8);
- $this->success('解除签约成功!');
- }
- public function plan_lst(Request $request)
- {
- $where = UtilService::getMore(
- [
- ['page', 1],
- ['limit', 10],
- ['cid', $this->cid],
- ], $request
- );
- $this->success('获取成功', WechatPlan::lst($where));
- }
- public function plan_read(Request $request)
- {
- $where = UtilService::getMore(
- [
- ['id', 0],
- ['cid', $this->cid],
- ], $request
- );
- $this->success('获取成功', WechatPlan::read($where));
- }
- public function user_plan_lst(Request $request)
- {
- $where = UtilService::getMore(
- [
- ['cid', $this->cid],
- ['uid', $this->auth->getUserinfo()['id']],
- ], $request
- );
- if (!isset($where['uid'])) $this->error('用户不存在');
- if (!isset($where['cid'])) $this->error('请选择正确的城市的红十字会');
- $this->success('获取成功', WechatPlanRecord::record_lst($where));
- }
- public function user_record_info(Request $request)
- {
- $where = UtilService::getMore(
- [
- ['id', 0],
- ['cid', $this->cid],
- ['user_id', $this->auth->getUserinfo()['id']],
- ], $request
- );
- $this->success('获取成功', WechatPlanRecord::record_info($where));
- }
- }
- ?>
|