Pay.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use addons\epay\library\Service;
  6. use app\common\model\Wallet;
  7. use think\Exception;
  8. use app\utils\CurlUtil;
  9. /**
  10. * 充值配置与充值订单
  11. */
  12. class Pay extends Api
  13. {
  14. protected $noNeedLogin = ['testpay'];
  15. protected $noNeedRight = ['*'];
  16. //vip用的
  17. public function vip_config(){
  18. $list = Db::name('payvip_config')->where('is_show',1)->order('weight asc,id asc')->select();
  19. foreach ($list as &$v) {
  20. $v['diff_price'] = $v['info'] - $v['money'];
  21. }
  22. $data['vipconfig'] = $list;
  23. $data['vip_endtime'] = model('wallet')->getWallet($this->auth->id,'vip_endtime');
  24. $this->success('success',$data);
  25. }
  26. //vip用的,创建订单
  27. /*public function vip_recharge(){
  28. $rc_id = input('rc_id',0);
  29. $pay_type = input('pay_type','wechat');
  30. $uid = $this->auth->id;
  31. if(!$rc_id){
  32. $this->error('请选择会员套餐');
  33. }
  34. //赋值money
  35. $recharge_config = Db::name('payvip_config')->where('id',$rc_id)->find();
  36. $money = $recharge_config['money'];
  37. if($money<=0)
  38. {
  39. $this->error('支付金额必须大于0');
  40. }
  41. if($money > 10000){
  42. $this->error('支付金额太大');
  43. }
  44. //会员等级冲突
  45. //当前是会员,但是却要向下级续费,直接提示报错
  46. //修改等级,向上立刻改,向下不允许
  47. $wallet_info = model('wallet')->getWallet($this->auth->id);
  48. if($wallet_info['vip_endtime'] > time() && $recharge_config['vip_level'] < $wallet_info['vip_level']){
  49. $this->error('当前会员没有过期,不能续费');
  50. }
  51. //创建订单
  52. $data = [];
  53. $data['status'] = 0;
  54. $pay_no = createUniqueNo('V',$uid);
  55. $data['pay_no'] = $pay_no;
  56. $data['money'] = $money;
  57. $data['payment_class'] = $pay_type;
  58. $data['user_id'] = $uid;
  59. $data['ext_info'] = json_encode(['subject'=>'充值vip支付']);
  60. $data['memo'] = '充值会员支付';
  61. $data['createtime'] = time();
  62. //$data['payment'] = 'miniapp';
  63. $data['payment'] = 'app';
  64. $orderid = Db::name('pay_order')->insertGetId($data);
  65. //创建回调
  66. $even_data = [];
  67. $even_data['event'] = 'success';
  68. $even_data['class'] = 'app\common\model\Recharge';
  69. $even_data['method'] = 'vippaysucc';
  70. $even_data['args'] = json_encode(['user_id'=>$uid,'days'=>$recharge_config['days'],'vip_level'=>$recharge_config['vip_level'],'gold_num'=>$recharge_config['gold_num'],'money'=>$money]);
  71. $even_data['pay_no'] = $pay_no;
  72. Db::name('pay_event')->insertGetId($even_data);
  73. //下单
  74. $params = [
  75. 'type' => $pay_type,
  76. 'orderid' => $pay_no,
  77. 'title' => $data['memo'],
  78. 'amount' => $data['money'],
  79. //'amount' => 0.01,
  80. //'method' => 'miniapp',
  81. 'method' => 'app',
  82. //'openid' => $openid,
  83. 'notifyurl' => $this->request->root(true) . '/notify.php/paytype/'.$pay_type,
  84. 'returnurl' => '',
  85. ];
  86. $res = Service::submitOrder($params);
  87. if($pay_type == 'wechat'){
  88. $this->success('success',json_decode($res,true));
  89. }else{
  90. $this->success('success',$res);
  91. }
  92. }*/
  93. public function vip_recharge()
  94. {
  95. Db::startTrans();
  96. try {
  97. $rc_id = input('rc_id',0);
  98. $pay_type = input('pay_type','wechat');
  99. $uid = $this->auth->id;
  100. if(!$rc_id){
  101. throw new Exception('请选择会员套餐');
  102. }
  103. //赋值money
  104. $recharge_config = Db::name('payvip_config')->where('id',$rc_id)->find();
  105. $money = $recharge_config['money'];
  106. if($money <= 0) {
  107. throw new Exception('支付金额必须大于0');
  108. }
  109. if($money > 10000){
  110. throw new Exception('支付金额太大');
  111. }
  112. //会员等级冲突
  113. //当前是会员,但是却要向下级续费,直接提示报错
  114. //修改等级,向上立刻改,向下不允许
  115. $wallet_info = model('wallet')->getWallet($this->auth->id);
  116. if($wallet_info['vip_endtime'] > time() && $recharge_config['vip_level'] < $wallet_info['vip_level']){
  117. throw new Exception('当前会员没有过期,不能续费');
  118. }
  119. //创建订单
  120. $data = [];
  121. $data['status'] = 0;
  122. $pay_no = createUniqueNo('V',$uid);
  123. $data['pay_no'] = $pay_no;
  124. $data['money'] = $money;
  125. $data['payment_class'] = $pay_type;
  126. $data['user_id'] = $uid;
  127. $data['ext_info'] = json_encode(['subject'=>'充值vip支付']);
  128. $data['memo'] = '充值会员支付';
  129. $data['createtime'] = time();
  130. //$data['payment'] = 'miniapp';
  131. $data['payment'] = 'app';
  132. $orderid = Db::name('pay_order')->insertGetId($data);
  133. //创建回调
  134. $even_data = [];
  135. $even_data['event'] = 'success';
  136. $even_data['class'] = 'app\common\model\Recharge';
  137. $even_data['method'] = 'vippaysucc';
  138. $even_data['args'] = json_encode(['user_id'=>$uid,'days'=>$recharge_config['days'],'vip_level'=>$recharge_config['vip_level'],'gold_num'=>$recharge_config['gold_num'],'money'=>$money]);
  139. $even_data['pay_no'] = $pay_no;
  140. Db::name('pay_event')->insertGetId($even_data);
  141. if ($pay_type == 'wechat') {
  142. //$money = 0.01;
  143. $sandpay = new \app\common\library\Sandpay();
  144. $sandpayParams = [
  145. 'order_no' => $pay_no,
  146. 'goods_name' => '充值vip支付',
  147. 'money' => $money,
  148. 'type' => 'vip',
  149. ];
  150. $sandRes = $sandpay->wechath5($sandpayParams);
  151. if ($sandRes['status'] != 1) {
  152. throw new Exception('失败');
  153. }
  154. $res = $sandRes['data'];
  155. } else {
  156. //下单
  157. $params = [
  158. 'type' => $pay_type,
  159. 'orderid' => $pay_no,
  160. 'title' => $data['memo'],
  161. 'amount' => $data['money'],
  162. //'amount' => 0.01,
  163. 'method' => 'app',
  164. 'notifyurl' => CurlUtil::getHttp('/notify.php/paytype/'.$pay_type),
  165. 'returnurl' => '',
  166. ];
  167. $res = Service::submitOrder($params);
  168. }
  169. Db::commit();
  170. $this->success('success',$res);
  171. } catch (Exception $e) {
  172. Db::rollback();
  173. $this->error($e->getMessage());
  174. }
  175. }
  176. //金币充值
  177. public function gold_config(){
  178. $list = Db::name('paygold_config')->where('is_show',1)->order('weight asc,id asc')->select();
  179. $data['goldconfig'] = $list;
  180. $data['gold'] = model('wallet')->getWallet($this->auth->id,'gold');
  181. $data['money_to_gold'] = config('site.money_to_gold');
  182. $data['is_first'] = Db::name('user_paygold_log')->where(['uid' => $this->auth->id])->count('id');
  183. if ($list) {
  184. $arr = [
  185. 'id' => -1,
  186. 'money' => 0,
  187. 'gold' => 0,
  188. 'title' => '自定义',
  189. 'is_show' => 1,
  190. 'weight' => 1,
  191. 'first_gold' => 0,
  192. 'first_vipdays' => 0
  193. ];
  194. array_push($data['goldconfig'], $arr);
  195. foreach ($data['goldconfig'] as &$v) {
  196. $v['is_first'] = $data['is_first'];
  197. }
  198. }
  199. $this->success('success',$data);
  200. }
  201. //充值金币 创建订单
  202. /*public function gold_recharge(){
  203. $rc_id = input_post('rc_id',0);
  204. $pay_type = input_post('pay_type','wechat');
  205. $freemoney = input_post('freemoney', 0, 'intval');
  206. $uid = $this->auth->id;
  207. if(!$rc_id && !$freemoney){
  208. $this->error('请选择或填写充值金额');
  209. }
  210. //赋值money
  211. if($rc_id){
  212. $recharge_config = Db::name('paygold_config')->where('id',$rc_id)->find();
  213. $money = $recharge_config ? $recharge_config['money']: 0;
  214. $gold = $recharge_config ? $recharge_config['gold'] : 0;
  215. $first_gold = $recharge_config ? $recharge_config['first_gold'] : 0;
  216. $first_vipdays = $recharge_config ? $recharge_config['first_vipdays'] : 0;
  217. $vip_gold = $recharge_config ? $recharge_config['vip_gold'] : 0;
  218. }
  219. //自由输入覆盖
  220. if(!empty($freemoney)){
  221. $rc_id = 0;
  222. $money = floatval($freemoney);
  223. $bili = config('site.money_to_gold') ?: 10;
  224. $gold = bcmul($money,$bili,0);
  225. $first_gold = 0;
  226. $first_vipdays = 0;
  227. $vip_gold = 0;
  228. }
  229. //
  230. if($money<=0)
  231. {
  232. $this->error('支付金额必须大于0');
  233. }
  234. if($money > 10000){
  235. $this->error('支付金额太大');
  236. }
  237. //查询是不是会员,若不是则不赠送金币
  238. $vip_endtime = Db::name('user_wallet')->where('user_id',$this->auth->id)->value('vip_endtime');
  239. if ($vip_endtime < time()) {
  240. $vip_gold = 0;
  241. }
  242. //创建订单
  243. $data = [];
  244. $data['status'] = 0;
  245. $pay_no = createUniqueNo('P',$uid);
  246. $data['pay_no'] = $pay_no;
  247. $data['money'] = $money;
  248. $data['payment_class'] = $pay_type;
  249. $data['user_id'] = $uid;
  250. $data['ext_info'] = json_encode(['subject'=>'充值金币支付']);
  251. $data['memo'] = '充值金币支付';
  252. $data['createtime'] = time();
  253. $data['payment'] = 'app';
  254. $orderid = Db::name('pay_order')->insertGetId($data);
  255. //创建回调
  256. $even_data = [];
  257. $even_data['event'] = 'success';
  258. $even_data['class'] = 'app\common\model\Recharge';
  259. $even_data['method'] = 'goldpaysucc';
  260. $even_data['args'] = json_encode(['user_id'=>$uid,'gold'=>$gold,'money'=>$money,'pg_id'=>$rc_id,'first_gold'=>$first_gold,'first_vipdays'=>$first_vipdays, 'intro_uid' => $this->auth->intro_uid, 'vip_gold' => $vip_gold]);
  261. $even_data['pay_no'] = $pay_no;
  262. Db::name('pay_event')->insertGetId($even_data);
  263. //下单
  264. $params = [
  265. 'type' => $pay_type,
  266. 'orderid' => $pay_no,
  267. 'title' => $data['memo'],
  268. 'amount' => $data['money'],
  269. // 'amount' => 0.01,
  270. 'method' => 'app',
  271. 'notifyurl' => $this->request->root(true) . '/notify.php/paytype/'.$pay_type,
  272. 'returnurl' => '',
  273. ];
  274. $res = Service::submitOrder($params);
  275. if($pay_type == 'wechat'){
  276. $this->success('success',json_decode($res,true));
  277. }else{
  278. $this->success('success',$res);
  279. }
  280. }*/
  281. //充值金币 创建订单
  282. public function gold_recharge()
  283. {
  284. Db::startTrans();
  285. try {
  286. $rc_id = input_post('rc_id',0);
  287. $pay_type = input_post('pay_type','wechat');
  288. $freemoney = input_post('freemoney', 0, 'intval');
  289. $uid = $this->auth->id;
  290. if(!$rc_id && !$freemoney){
  291. throw new Exception('请选择或填写充值金额');
  292. }
  293. if (!in_array($pay_type,['wechat','alipay'])) {
  294. throw new Exception('错误的支付类型');
  295. }
  296. //赋值money
  297. if($rc_id){
  298. $recharge_config = Db::name('paygold_config')->where('id',$rc_id)->find();
  299. $money = $recharge_config ? $recharge_config['money']: 0;
  300. $gold = $recharge_config ? $recharge_config['gold'] : 0;
  301. $first_gold = $recharge_config ? $recharge_config['first_gold'] : 0;
  302. $first_vipdays = $recharge_config ? $recharge_config['first_vipdays'] : 0;
  303. $vip_gold = $recharge_config ? $recharge_config['vip_gold'] : 0;
  304. }
  305. //自由输入覆盖
  306. if(!empty($freemoney)){
  307. $rc_id = 0;
  308. $money = floatval($freemoney);
  309. $bili = config('site.money_to_gold') ?: 10;
  310. $gold = bcmul($money,$bili,0);
  311. $first_gold = 0;
  312. $first_vipdays = 0;
  313. $vip_gold = 0;
  314. }
  315. if($money <= 0) {
  316. throw new Exception('支付金额必须大于0');
  317. }
  318. if($money > 10000){
  319. throw new Exception('支付金额太大');
  320. }
  321. //查询是不是会员,若不是则不赠送金币
  322. $vip_endtime = Db::name('user_wallet')->where('user_id',$this->auth->id)->value('vip_endtime');
  323. if ($vip_endtime < time()) {
  324. $vip_gold = 0;
  325. }
  326. //创建订单
  327. $data = [];
  328. $data['status'] = 0;
  329. $pay_no = createUniqueNo('P',$uid);
  330. $data['pay_no'] = $pay_no;
  331. $data['money'] = $money;
  332. $data['payment_class'] = $pay_type;
  333. $data['user_id'] = $uid;
  334. $data['ext_info'] = json_encode(['subject'=>'充值金币支付']);
  335. $data['memo'] = '充值金币支付';
  336. $data['createtime'] = time();
  337. $data['payment'] = 'app';
  338. $orderid = Db::name('pay_order')->insertGetId($data);
  339. //创建回调
  340. $even_data = [];
  341. $even_data['event'] = 'success';
  342. $even_data['class'] = 'app\common\model\Recharge';
  343. $even_data['method'] = 'goldpaysucc';
  344. $even_data['args'] = json_encode(['user_id'=>$uid,'gold'=>$gold,'money'=>$money,'pg_id'=>$rc_id,'first_gold'=>$first_gold,'first_vipdays'=>$first_vipdays, 'intro_uid' => $this->auth->intro_uid, 'vip_gold' => $vip_gold]);
  345. $even_data['pay_no'] = $pay_no;
  346. Db::name('pay_event')->insertGetId($even_data);
  347. if ($pay_type == 'wechat') {
  348. //$money = 0.01;
  349. $sandpay = new \app\common\library\Sandpay();
  350. $sandpayParams = [
  351. 'order_no' => $pay_no,
  352. 'goods_name' => '充值金币支付',
  353. 'money' => $money,
  354. 'type' => 'gold',
  355. ];
  356. $sandRes = $sandpay->wechath5($sandpayParams);
  357. if ($sandRes['status'] != 1) {
  358. throw new Exception('失败');
  359. }
  360. $res = $sandRes['data'];
  361. } else {
  362. //下单
  363. $params = [
  364. 'type' => $pay_type,
  365. 'orderid' => $pay_no,
  366. 'title' => $data['memo'],
  367. 'amount' => $data['money'],
  368. //'amount' => 0.01,
  369. 'method' => 'app',
  370. 'notifyurl' => CurlUtil::getHttp('/notify.php/paytype/'.$pay_type),
  371. 'returnurl' => '',
  372. ];
  373. $res = Service::submitOrder($params);
  374. }
  375. Db::commit();
  376. $this->success('成功', $res);
  377. } catch (Exception $e) {
  378. Db::rollback();
  379. $this->error($e->getMessage());
  380. }
  381. }
  382. public function testpay(){
  383. $sandpay = new \app\common\library\Sandpay();
  384. $sandpayParams = [];
  385. $res = $sandpay->wechath5($sandpayParams);
  386. if ($res['status'] == 1) {
  387. $this->success('成功', $res['data']);
  388. }else {
  389. $this->error('失败', $res['data']);
  390. }
  391. }
  392. public function testsuningpay(){
  393. $suningpay = new \app\common\library\SuningPay();
  394. $suningpayParams = [];
  395. $res = $suningpay->wechat($suningpayParams);
  396. if ($res['status'] == 1) {
  397. $this->success('成功', $res['data']);
  398. }else {
  399. $this->error('失败', $res['data']);
  400. }
  401. }
  402. //vip用的,创建订单废弃
  403. public function sand_vip_recharge(){
  404. $rc_id = input('rc_id',0);
  405. $pay_type = input('pay_type','wechat');
  406. $uid = $this->auth->id;
  407. if(!$rc_id){
  408. $this->error('请选择会员套餐');
  409. }
  410. //赋值money
  411. $recharge_config = Db::name('payvip_config')->where('id',$rc_id)->find();
  412. $money = $recharge_config['money'];
  413. if($money<=0)
  414. {
  415. $this->error('支付金额必须大于0');
  416. }
  417. if($money > 10000){
  418. $this->error('支付金额太大');
  419. }
  420. //会员等级冲突
  421. //当前是会员,但是却要向下级续费,直接提示报错
  422. //修改等级,向上立刻改,向下不允许
  423. $wallet_info = model('wallet')->getWallet($this->auth->id);
  424. if($wallet_info['vip_endtime'] > time() && $recharge_config['vip_level'] < $wallet_info['vip_level']){
  425. $this->error('当前会员没有过期,不能续费');
  426. }
  427. //创建订单
  428. $data = [];
  429. $data['status'] = 0;
  430. $pay_no = createUniqueNo('V',$uid);
  431. $data['pay_no'] = $pay_no;
  432. $data['money'] = $money;
  433. $data['payment_class'] = $pay_type;
  434. $data['user_id'] = $uid;
  435. $data['ext_info'] = json_encode(['subject'=>'充值vip支付']);
  436. $data['memo'] = '充值会员支付';
  437. $data['createtime'] = time();
  438. //$data['payment'] = 'miniapp';
  439. $data['payment'] = 'app';
  440. $orderid = Db::name('pay_order')->insertGetId($data);
  441. //创建回调
  442. $even_data = [];
  443. $even_data['event'] = 'success';
  444. $even_data['class'] = 'app\common\model\Recharge';
  445. $even_data['method'] = 'sandvippaysucc';
  446. $even_data['args'] = json_encode(['user_id'=>$uid,'days'=>$recharge_config['days'],'vip_level'=>$recharge_config['vip_level'],'gold_num'=>$recharge_config['gold_num'],'money'=>$money]);
  447. $even_data['pay_no'] = $pay_no;
  448. Db::name('pay_event')->insertGetId($even_data);
  449. //下单
  450. $sandpay = new \app\common\library\Sandpay();
  451. $sandpayParams = [
  452. 'order_no' => $pay_no,//订单号
  453. 'goods_name' => '充值会员',//商品名称
  454. 'money' => $money,//金额
  455. 'type' => 'vip',//会员
  456. ];
  457. $res = $sandpay->wechat($sandpayParams);
  458. if ($res['status'] == 1) {
  459. $this->success('成功', $res['data']);
  460. }else {
  461. $this->error('失败', $res['data']);
  462. }
  463. }
  464. //充值金币 创建订单废弃
  465. public function sand_gold_recharge(){
  466. $rc_id = input_post('rc_id',0);
  467. $pay_type = input_post('pay_type','wechat');
  468. $freemoney = input_post('freemoney', 0, 'intval');
  469. $uid = $this->auth->id;
  470. if(!$rc_id && !$freemoney){
  471. $this->error('请选择或填写充值金额');
  472. }
  473. // if(!$rc_id){
  474. // $this->error('请选择充值金额');
  475. // }
  476. //赋值money
  477. if($rc_id){
  478. $recharge_config = Db::name('paygold_config')->where('id',$rc_id)->find();
  479. $money = $recharge_config ? $recharge_config['money']: 0;
  480. $gold = $recharge_config ? $recharge_config['gold'] : 0;
  481. $first_gold = $recharge_config ? $recharge_config['first_gold'] : 0;
  482. $first_vipdays = $recharge_config ? $recharge_config['first_vipdays'] : 0;
  483. $vip_gold = $recharge_config ? $recharge_config['vip_gold'] : 0;
  484. }
  485. //自由输入覆盖
  486. if(!empty($freemoney)){
  487. $rc_id = 0;
  488. $money = floatval($freemoney);
  489. $bili = config('site.money_to_gold') ?: 10;
  490. $gold = bcmul($money,$bili,0);
  491. $first_gold = 0;
  492. $first_vipdays = 0;
  493. $vip_gold = 0;
  494. }
  495. //
  496. if($money<=0)
  497. {
  498. $this->error('支付金额必须大于0');
  499. }
  500. if($money > 10000){
  501. $this->error('支付金额太大');
  502. }
  503. //查询是不是会员,若不是则不赠送金币
  504. $vip_endtime = Db::name('user_wallet')->where('user_id',$this->auth->id)->value('vip_endtime');
  505. if ($vip_endtime < time()) {
  506. $vip_gold = 0;
  507. }
  508. //创建订单
  509. $data = [];
  510. $data['status'] = 0;
  511. $pay_no = createUniqueNo('P',$uid);
  512. $data['pay_no'] = $pay_no;
  513. $data['money'] = $money;
  514. $data['payment_class'] = $pay_type;
  515. $data['user_id'] = $uid;
  516. $data['ext_info'] = json_encode(['subject'=>'充值金币支付']);
  517. $data['memo'] = '充值金币支付';
  518. $data['createtime'] = time();
  519. $data['payment'] = 'app';
  520. $orderid = Db::name('pay_order')->insertGetId($data);
  521. //创建回调
  522. $even_data = [];
  523. $even_data['event'] = 'success';
  524. $even_data['class'] = 'app\common\model\Recharge';
  525. $even_data['method'] = 'sandgoldpaysucc';
  526. $even_data['args'] = json_encode(['user_id'=>$uid,'gold'=>$gold,'money'=>$money,'pg_id'=>$rc_id,'first_gold'=>$first_gold,'first_vipdays'=>$first_vipdays, 'intro_uid' => $this->auth->intro_uid, 'vip_gold' => $vip_gold]);
  527. $even_data['pay_no'] = $pay_no;
  528. Db::name('pay_event')->insertGetId($even_data);
  529. //下单
  530. $sandpay = new \app\common\library\Sandpay();
  531. $sandpayParams = [
  532. 'order_no' => $pay_no,//订单号
  533. 'goods_name' => '充值金币',//商品名称
  534. 'money' => $money,//金额
  535. 'type' => 'gold',//充值
  536. ];
  537. $res = $sandpay->wechat($sandpayParams);
  538. if ($res['status'] == 1) {
  539. $this->success('成功', $res['data']);
  540. }else {
  541. $this->error('失败', $res['data']);
  542. }
  543. }
  544. //开通会员(废弃)
  545. public function addvip() {
  546. $rc_id = input('id',0);
  547. $uid = $this->auth->id;
  548. if(!$rc_id){
  549. $this->error('请选择会员套餐');
  550. }
  551. //赋值money
  552. $recharge_config = Db::name('payvip_config')->where('id',$rc_id)->find();
  553. $money = $recharge_config['money'];
  554. if($money<=0) {
  555. $this->error('支付金额必须大于0');
  556. }
  557. if($money > 10000){
  558. $this->error('支付金额太大');
  559. }
  560. //验证余额
  561. $user_info = model('wallet')->getWallet($this->auth->id);
  562. if ($user_info['money'] < $money) {
  563. $this->success('您的余额不足', ['code' => 2]);
  564. }
  565. if($user_info['vip_endtime'] > time() && $recharge_config['vip_level'] < $user_info['vip_level']){
  566. $this->error('当前会员没有过期,不能续费');
  567. }
  568. if($user_info['vip_endtime'] < time()){
  569. //过期了
  570. $vip_endtime = time() + (intval($recharge_config['days']) * 86400);
  571. }else{
  572. //追加vip
  573. $vip_endtime = $user_info['vip_endtime'] + (intval($recharge_config['days']) * 86400);
  574. }
  575. //开启事务
  576. Db::startTrans();
  577. //修改会员时间
  578. $update_data = [
  579. 'vip_endtime'=>$vip_endtime,
  580. 'vip_level' =>$recharge_config['vip_level'], //修改等级,同级不影响,向上立刻改,向下不允许
  581. ];
  582. $result = Db::name('user_wallet')->where('user_id',$uid)->update($update_data);
  583. if($result === false) {
  584. Db::rollback();
  585. $this->error('您的网络开小差啦~');
  586. }
  587. //扣费
  588. $wallet_rs = model('wallet')->lockChangeAccountRemain($uid,0,'money',-$money,16,'开通会员:'.$recharge_config["title"]);
  589. if($wallet_rs['status'] === false){
  590. Db::rollback();
  591. $this->error($wallet_rs['msg']);
  592. }
  593. //赠送金币
  594. $wallet_rs = model('wallet')->lockChangeAccountRemain($uid,0,'gold',$recharge_config['gold_num'],17,'开通会员赠送金币');
  595. if($wallet_rs['status'] === false){
  596. Db::rollback();
  597. $this->error($wallet_rs['msg']);
  598. }
  599. //tag任务赠送金币
  600. //开通VIP 50金币
  601. // $task_rs = \app\common\model\TaskLog::tofinish($uid,9);
  602. // if($task_rs === false){
  603. // Db::rollback();
  604. // $this->error('完成任务赠送奖励失败');
  605. // }
  606. Db::commit();
  607. $this->success('开通成功');
  608. }
  609. }