Pay.php 25 KB

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