Lave.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. <?php
  2. namespace app\api\controller;
  3. use app\admin\model\Company;
  4. use app\admin\model\WechatPlan;
  5. use app\admin\model\WechatPlanRecord;
  6. use app\common\controller\Api;
  7. use app\common\model\{Category, Lave as LaveModel, User};
  8. use app\common\model\LaveMonth as LaveMonthModel;
  9. use liuniu\repositories\LaveRepository;
  10. use liuniu\UtilService;
  11. use liuniu\WechatService;
  12. use think\Request;
  13. class Lave extends Api
  14. {
  15. protected $noNeedLogin = ['*'];
  16. protected $noNeedRight = ['*'];
  17. public function lst(Request $request)
  18. {
  19. $where = UtilService::getMore(
  20. [
  21. ['page', 1],
  22. ['limit', 10],
  23. ['cid', $this->cid],
  24. ['paid', 1],
  25. ['category_id', 0],
  26. ['order', ''],
  27. ['key', ''],
  28. ], $request
  29. );
  30. $this->success('获取成功', LaveModel::lst($where));
  31. }
  32. public function ify()
  33. {
  34. $this->success('获取成功', Category::getCategoryArray('lave', null, $this->cid));
  35. }
  36. public function create(Request $request)
  37. {
  38. $where = UtilService::postMore(
  39. [
  40. ['cid', $this->cid],
  41. ['user_id', $this->auth->getUserinfo()['id']],
  42. ['order_name', ''],
  43. ['category_id', 0],
  44. ['amount', 0],
  45. ['name', ''],
  46. ['contact', ''],
  47. ['tel', 0],
  48. ['address', ''],
  49. ['is_open', '0'],
  50. ['is_ticket', '0'],
  51. ['pay_type', '0'],
  52. ['type', '0'],
  53. ['help_id', 0],
  54. ['from', 'weixin'],
  55. ], $request
  56. );
  57. $where1 = $where;
  58. unset($where1['from']);
  59. $where1['order_id'] = LaveModel::getNewOrderId();
  60. if (cache('lave_' . $where['user_id'])) $this->error('正在处理中');
  61. @file_put_contents("lave.txt", json_encode($where1));
  62. $order = LaveModel::create($where1);
  63. cache('lave_' . $where['user_id'], '1', 10);
  64. if (!$order) $this->error(LaveModel::getErrorInfo());
  65. $orderId = $order['order_id'];
  66. $info = compact('orderId');
  67. if ($orderId) {
  68. $orderInfo = LaveModel::where('order_id', $orderId)->find();
  69. if (!$orderInfo || !isset($orderInfo['paid'])) $this->error('支付订单不存在!');
  70. if ($orderInfo['paid']) $this->error('支付已支付!');
  71. try {
  72. if ($where['from'] == 'routine') {
  73. $jsConfig = LaveRepository::jsPay($this->cid, $orderId); //创建订单jspay
  74. } else if ($where['from'] == 'weixinh5') {
  75. $jsConfig = LaveRepository::h5Pay($this->cid, $orderId);
  76. } else {
  77. $jsConfig = LaveRepository::wxPay($this->cid, $orderId);
  78. }
  79. } catch (\Exception $e) {
  80. return $this->error($e->getMessage());
  81. }
  82. $info['jsConfig'] = $jsConfig;
  83. return $this->success('订单创建成功', $info);
  84. } else $this->error(LaveModel::getErrorInfo());
  85. }
  86. public function mylst(Request $request)
  87. {
  88. $where = UtilService::getMore(
  89. [
  90. ['page', 1],
  91. ['limit', 10],
  92. ['cid', $this->cid],
  93. ['user_id', $this->auth->getUserinfo()['id']],
  94. ['paid', -1],
  95. ['category_id', 0],
  96. ], $request
  97. );
  98. $this->success('获取成功', LaveModel::lst($where));
  99. }
  100. /**
  101. * 订单支付
  102. * @param Request $request
  103. * @return mixed
  104. */
  105. public function pay(Request $request)
  106. {
  107. list($uni, $paytype, $from) = UtilService::postMore([
  108. ['uni', ''],
  109. ['paytype', '0'],
  110. ['from', 'weixin']
  111. ], $request, true);
  112. if (!$uni) $this->error('参数错误!');
  113. $order = LaveModel::where('cid', $this->cid)->where('order_id', $uni)->find();
  114. if (!$order)
  115. $this->error('订单不存在!');
  116. if ($order['paid'])
  117. $this->error('该订单已支付!');
  118. $order['pay_type'] = $paytype; //重新支付选择支付方式
  119. switch ($order['pay_type']) {
  120. case '0':
  121. try {
  122. if ($from == 'routine') {
  123. $jsConfig = LaveRepository::jsPay($this->cid, $order); //订单列表发起支付
  124. } else if ($from == 'weixinh5') {
  125. $jsConfig = LaveRepository::h5Pay($this->cid, $order);
  126. } else {
  127. $jsConfig = LaveRepository::wxPay($this->cid, $order);
  128. }
  129. } catch (\Exception $e) {
  130. $this->error($e->getMessage());
  131. }
  132. $this->success('获取成功', $jsConfig);
  133. break;
  134. }
  135. return $this->error('支付方式错误');
  136. }
  137. // 支付中签约
  138. public function createSign(Request $request)
  139. {
  140. // var_dump(123);die();
  141. $where = UtilService::postMore(
  142. [
  143. ['cid', $this->cid],
  144. ['user_id', $this->auth->getUserinfo()['id']],
  145. // ['user_id', 0],
  146. ['order_name', ''],
  147. // ['category_id', 0],
  148. ['amount', 0],
  149. ['name', ''],
  150. ['contact', ''],
  151. ['tel', 0],
  152. ['address', ''],
  153. ['is_open', '0'],
  154. ['is_ticket', '0'],
  155. ['pay_type', '0'],
  156. ['type', '0'],
  157. // ['help_id', 0],
  158. ['plan_id', 0],
  159. ['from', 'weixin'],
  160. ], $request
  161. );
  162. $where1 = $where;
  163. unset($where1['from']);
  164. $where1['order_id'] = LaveMonthModel::getNewOrderId();
  165. // $contract_display_account = User::where('id', $where1['user_id'])->value('nickname');
  166. // if (empty($contract_display_account)){
  167. // $this->error('用户不存在!');
  168. // }
  169. if ($where['amount']>500){
  170. $this->error('捐款金额不能大于500');
  171. }
  172. $sign=WechatPlanRecord::where('uid', $where1['user_id'])->where('plan_id', $where1['plan_id'])->where('is_signing',0)->find();
  173. if (!empty($sign)){
  174. $this->error('您已签约,请勿重复签约!');
  175. }
  176. if (cache('lave_' . $where['user_id'])) $this->error('正在处理中');
  177. $plan_id=$where1['plan_id'];
  178. // unset($where1['plan_id']);
  179. $where1['contract_code'] = WechatPlanRecord::getNewCode();
  180. $order = LaveMonthModel::create($where1);
  181. $where1['contract_display_account']=$where1['name'];
  182. cache('lave_' . $where['user_id'], '1', 10);
  183. if (!$order) $this->error(LaveMonthModel::getErrorInfo());
  184. $orderId = $order['order_id'];
  185. $info = compact('orderId');
  186. $order['plan_id'] = $plan_id;
  187. if ($orderId) {
  188. $orderInfo = LaveMonthModel::where('order_id', $orderId)->find();
  189. if (!$orderInfo || !isset($orderInfo['paid'])){
  190. $this->error('支付订单不存在!');
  191. }
  192. if ($orderInfo['paid']) $this->error('支付已支付!');
  193. // 创建签约
  194. $plan_record=[
  195. 'plan_id'=>$plan_id,
  196. 'cid'=>$where['cid'],
  197. 'uid'=>$where['user_id'],
  198. 'price'=>$where['amount'],
  199. 'is_signing'=>0,
  200. 'contract_code'=>$where1['contract_code'],
  201. 'contract_display_account'=>$where1['contract_display_account'],
  202. 'is_open'=>$where1['is_open'],
  203. 'tel'=>$where1['tel'],
  204. // 'category_id'=>$where1['category_id'],
  205. 'order_name'=>$where1['order_name'],
  206. ];
  207. $count=WechatPlanRecord::where('uid', $where1['user_id'])->where('plan_id',$plan_id)->count();
  208. $record=WechatPlanRecord::create($plan_record);
  209. @file_put_contents("quanju.txt", json_encode($record)."-签约模版记录\r\n", 8);
  210. LaveMonthModel::where('order_id', $orderId)->update(['record_id' => $record['id']]);
  211. $plan_record['spbill_create_ip']=User::where('id', $where1['user_id'])->value('loginip');
  212. $plan_record['contract_notify_url']=Request::instance()->domain() . "/api/wechat/notify/" . $where['cid'];
  213. try {
  214. if ($where['from'] == 'routine') {
  215. $jsConfig = LaveRepository::jsPaySign($this->cid, $orderId,$plan_record); //创建订单jspay
  216. } else if ($where['from'] == 'weixinh5') {
  217. $jsConfig = LaveRepository::h5PaySign($this->cid, $orderId,$plan_record);
  218. } else {
  219. $jsConfig = LaveRepository::wxPaySign($this->cid, $orderId,$plan_record);
  220. }
  221. } catch (\Exception $e) {
  222. return $this->error($e->getMessage());
  223. }
  224. $info['jsConfig'] = $jsConfig;
  225. // 增加参与人数
  226. // @file_put_contents("quanju.txt", $count."-参与次数\r\n", 8);
  227. if ($count==0){
  228. $num=WechatPlan::where('plan_id', $plan_id)->value('num');
  229. // @file_put_contents("quanju.txt", $num."-参与人数\r\n", 8);
  230. WechatPlan::where('plan_id', $plan_id)->update(['num' => $num+1]);
  231. }
  232. return $this->success('订单创建成功', $info);
  233. } else $this->error(LaveMonthModel::getErrorInfo());
  234. }
  235. // /**
  236. // * 签约订单支付
  237. // * @param Request $request
  238. // * @return mixed
  239. // */
  240. // public function paySign(Request $request)
  241. // {
  242. //// var_dump(456);die();
  243. // list($uni, $paytype, $from) = UtilService::postMore([
  244. // ['uni', ''],
  245. // ['paytype', '0'],
  246. // ['from', 'weixin']
  247. // ], $request, true);
  248. // if (!$uni) $this->error('参数错误!');
  249. // $order = LaveMonthModel::where('cid', $this->cid)->where('order_id', $uni)->find();
  250. // if (!$order)
  251. // $this->error('订单不存在!');
  252. // if ($order['paid'])
  253. // $this->error('该订单已支付!');
  254. // $order['pay_type'] = $paytype; //重新支付选择支付方式
  255. // switch ($order['pay_type']) {
  256. // case '0':
  257. // try {
  258. // if ($from == 'routine') {
  259. // $jsConfig = LaveRepository::jsPaySign($this->cid, $order); //订单列表发起支付
  260. // } else if ($from == 'weixinh5') {
  261. // $jsConfig = LaveRepository::h5PaySign($this->cid, $order);
  262. // } else {
  263. // $jsConfig = LaveRepository::wxPaySign($this->cid, $order);
  264. // }
  265. // } catch (\Exception $e) {
  266. // $this->error($e->getMessage());
  267. // }
  268. // $this->success('获取成功', $jsConfig);
  269. // break;
  270. // }
  271. // return $this->error('支付方式错误');
  272. // }
  273. // 申请扣款
  274. public function payPap(Request $request)
  275. {
  276. // @file_put_contents("quanju.txt", "测试申请扣款-\r\n", 8);
  277. // die();
  278. $list=WechatPlanRecord::where('is_signing',0)->select();
  279. $cid=$this->cid;
  280. $arr=[];
  281. $arr['body']='月捐款';
  282. foreach ($list as $k => $v) {
  283. $day = intval(date('d', $v['createtime']));
  284. $date = date('Y-m', $v['createtime']);
  285. $last=date("t", time()); //当月最后一天
  286. // @file_put_contents("quanju.txt", $day."-日期\r\n", 8);
  287. $today = intval(date('d'));
  288. $now_date = date('Y-m', time());
  289. // var_dump($day);
  290. // var_dump($today);
  291. // var_dump($last);
  292. // var_dump($date);
  293. // var_dump($now_date);
  294. if ($date != $now_date) { //先确定月份不同
  295. if ($today == $day||$today==$last) { //再确定是不是签约那天或者是当月的最后一天
  296. // var_dump(123);
  297. $cid=$v['cid'];
  298. // if ($this->querySign($cid,$v['uid'],$v['plan_id'],$v['contract_code'])){
  299. // $arr['mch_id'] =Company::where('id', $cid)->value('pay_weixin_mchid');
  300. // $arr['total_fee'] = $v['price'];
  301. // $arr['trade_type'] = 'PAP';
  302. // $arr['contract_id'] = Company::where('id', $cid)->value('contract_id');
  303. // $rs = WechatService::paymentOrder($openid, $orderInfo['order_id'], $orderInfo['amount'], "lave", $orderInfo['order_name'], '', 'JSAPI', [], $cid);
  304. $mch_id =Company::where('id',$cid)->value('pay_weixin_mchid');
  305. $where1['cid']=$cid;
  306. $where1['user_id']=$v['uid'];
  307. $where1['order_name']=$v['order_name'];
  308. $where1['order_id']=LaveMonthModel::getNewOrderId();;
  309. // $where1['category_id']=$v['category_id'];
  310. $where1['amount']=$v['price'];
  311. $where1['name']=$v['contract_display_account'];
  312. $where1['contact']='';
  313. $where1['tel']=$v['tel'];
  314. $where1['address']='用户未填写联系地址';
  315. $where1['is_open']=$v['is_open'];
  316. $where1['is_ticket']='0';
  317. $where1['type']='0';
  318. $where1['record_id']=$v['id'];
  319. $where1['contract_code']=$v['contract_code'];
  320. $where1['plan_id']=$v['plan_id'];
  321. // $where1['help_id']='0';
  322. $order = LaveMonthModel::create($where1);
  323. // }
  324. $rs = WechatService::papPayApply($mch_id,$where1['order_id'], $where1['amount'], "lave", '月捐款', 'PAP', [], $cid,$v['contract_id'],$where1['order_name']);
  325. }
  326. }
  327. }
  328. // die();
  329. // }
  330. }
  331. // 解除签约
  332. public function deleteSign(Request $request){
  333. $where = UtilService::postMore(
  334. [
  335. ['id',0],
  336. ['cid', $this->cid],
  337. ['user_id', $this->auth->getUserinfo()['id']],
  338. ], $request
  339. );
  340. if ($where['id']==0){
  341. $sign_info=WechatPlanRecord::where('cid', $where['cid'])->where('uid', $where['user_id'])->where('is_signing',0)->find();
  342. }else{
  343. $sign_info=WechatPlanRecord::where('id', $where['id'])->where('is_signing',0)->find();
  344. }
  345. if (!$sign_info){
  346. $this->error('未找到签约信息!');
  347. }
  348. $mch_id =Company::where('id', $where['cid'])->value('pay_weixin_mchid');
  349. $plan_id=$sign_info['plan_id'];
  350. $contract_code=$sign_info['contract_code'];
  351. // $mch_id,$contract_code,$pan_id,$version='1.0',$options=[],$cid=0)
  352. $rs=WechatService::deleteSign($mch_id,$contract_code,$plan_id,'1.0',[],$where['cid']);
  353. @file_put_contents("quanju.txt", json_encode($rs)."-解除签约返回结果\r\n", 8);
  354. $this->success('解除签约成功!');
  355. // if ($rs){
  356. // WechatPlanRecord::where('cid', $where['cid'])->where('uid', $where['user_id'])->where('is_signing',0)->update(['is_signing'=>1]);
  357. // $this->success('解除签约成功!');
  358. // }else{
  359. // $this->error('解除签约失败!');
  360. // }
  361. }
  362. // 查询签约状态
  363. // public function querySign(int $cid,int $uid,int $plan_id,string $contract_code)
  364. // {
  365. //// $where = UtilService::postMore(
  366. //// [
  367. //// ['cid', $this->cid],
  368. //// ['user_id', $this->auth->getUserinfo()['id']],
  369. //// ], $request
  370. //// );
  371. // $sign_info = WechatPlanRecord::where('cid', $cid)->where('uid', $uid)->where('is_signing', 1)->find();
  372. // if (!$sign_info) {
  373. // $this->error('未找到签约信息!');
  374. // }
  375. // $mch_id = Company::where('id', $cid)->value('pay_weixin_mchid');
  376. //// $plan_id = $sign_info['plan_id'];
  377. //// $contract_code = $sign_info['contract_code'];
  378. // $rs = WechatService::querySign($mch_id, $contract_code, $plan_id);
  379. // @file_put_contents("quanju.txt", json_encode($rs) . "-查询签约状态返回结果\r\n", 8);
  380. // if ($rs['return_code'] == 'SUCCESS') {
  381. // if ($rs['contract_state'] == 1) {
  382. // WechatPlanRecord::where('cid',$cid)->where('uid', $uid)->where('is_signing', 1)->update(['is_signing' => 0]);
  383. // return false;
  384. // }else{
  385. // WechatPlanRecord::where('cid',$cid)->where('uid', $uid)->where('is_signing', 1)->update(['contract_id' => $rs['contract_id']]);
  386. // return true;
  387. // }
  388. // } else {
  389. // $this->error('查询签约状态失败!');
  390. // }
  391. // }
  392. public function plan_lst(Request $request)
  393. {
  394. $where = UtilService::getMore(
  395. [
  396. ['page', 1],
  397. ['limit', 10],
  398. ['cid', $this->cid],
  399. // ['paid', 1],
  400. // ['category_id', 0],
  401. // ['order', ''],
  402. // ['key', ''],
  403. ], $request
  404. );
  405. $this->success('获取成功', WechatPlan::lst($where));
  406. }
  407. public function plan_read(Request $request)
  408. {
  409. $where = UtilService::getMore(
  410. [
  411. ['id', 0],
  412. ['cid', $this->cid],
  413. ], $request
  414. );
  415. $this->success('获取成功', WechatPlan::read($where));
  416. }
  417. // 签约列表
  418. public function user_plan_lst(Request $request)
  419. {
  420. $where = UtilService::getMore(
  421. [
  422. // ['cid', 0],
  423. ['cid', $this->cid],
  424. ['uid', $this->auth->getUserinfo()['id']],
  425. // ['uid', 0],
  426. ], $request
  427. );
  428. if (!isset($where['uid'])) $this->error('用户不存在');
  429. if (!isset($where['cid'])) $this->error('请选择正确的城市的红十字会');
  430. $this->success('获取成功', WechatPlanRecord::record_lst($where));
  431. }
  432. // 签约记录详情
  433. public function user_record_info(Request $request)
  434. {
  435. $where = UtilService::getMore(
  436. [
  437. ['id', 0],
  438. // ['cid', 0],
  439. ['cid', $this->cid],
  440. ['user_id', $this->auth->getUserinfo()['id']],
  441. // ['user_id', 0],
  442. ], $request
  443. );
  444. $this->success('获取成功', WechatPlanRecord::record_info($where));
  445. }
  446. }
  447. ?>