Crystal.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace app\api\controller;
  3. use think\Controller;
  4. use think\Db;
  5. /**
  6. * @title 余额
  7. * Class Crystal
  8. * @controller Crystal
  9. * @group base
  10. */
  11. class Crystal extends Base
  12. {
  13. public function initialize()
  14. {
  15. parent::check_login();
  16. }
  17. /**
  18. * @title 余额充值(立即充值)
  19. * @desc 余额充值
  20. * @author qc
  21. * @url /api/Crystal/crystalRecharge
  22. * @method POST
  23. * @header name:Authorization require:1 desc:Token
  24. * @param name:crystal type:int require:1 default:-- desc:充值数量
  25. * @return name:config type:array default:0 desc:支付配置
  26. * @return name:config.appId type:string default:-- desc:公众账号ID
  27. * @return name:config.nonceStr type:string default:-- desc:随机字符串
  28. * @return name:config.signType type:string default:-- desc:签名类型
  29. * @return name:config.paySign type:string default:-- desc:签名
  30. * @return name:config.timestamp type:string default:-- desc:时间戳
  31. */
  32. public function crystalRecharge()
  33. {
  34. $crystal = input('post.crystal',0);
  35. $price_total = $crystal;
  36. if($price_total <=0 ) $this->error('充值数量有误!');
  37. $crystal_order = [
  38. 'uid' => $this->uid ,
  39. 'crystal' => $crystal ,
  40. 'price_total' => $price_total,
  41. 'order_no' =>get_order_sn(),
  42. 'pay_no' =>get_order_sn(),
  43. 'create_at' => date("Y-m-d H:i:s")
  44. ];
  45. Db::startTrans();
  46. $config = false;$msg= '';
  47. try{
  48. $user_info = Db::table('store_member')->find($this->uid);
  49. Db::table('crystal_order')->insert($crystal_order);
  50. $notify_url = $this->request->root(true) . '/api/Pay/crystalRecharge';
  51. $pay_no = $crystal_order['pay_no'] ? $crystal_order['pay_no'] : get_order_sn();
  52. $crystal_order['price_total'] = 0.01;
  53. $config = Pay::wxPay('订单支付',$pay_no,$crystal_order['price_total'],$notify_url,'JSAPI',$user_info['openid']);
  54. if($config){
  55. Db::commit();
  56. }else{
  57. Db::rollback();
  58. }
  59. }catch (\Exception $e){
  60. Db::rollback();
  61. $msg = $e->getMessage();
  62. }
  63. if($config){
  64. $this->success('ok',['config'=>$config]);
  65. }else{
  66. $this->error($msg);
  67. }
  68. }
  69. /**
  70. * @title 删除订单
  71. * @desc 删除订单
  72. * @author qc
  73. * @url /api/Crystal/delCrystalOrder
  74. * @method POST
  75. * @header name:Authorization require:1 desc:Token
  76. * @param name:order_id type:int require:1 default:-- desc:订单id
  77. */
  78. public function delCrystalOrder()
  79. {
  80. Db::table('crystal_order')->where(['uid'=>$this->uid,'id'=>input('post.order_id')])->delete();
  81. $this->success('ok');
  82. }
  83. /**
  84. * @title 获取余额充值订单列表
  85. * @desc 获取余额充值订单列表
  86. * @author qc
  87. * @url /api/Crystal/getCrystalOrderList
  88. * @method GET
  89. * @header name:Authorization require:1 desc:Token
  90. * @param name:page type:int : default:1 desc:页数
  91. * @param name:page_num type:int : default:20 desc:每页数
  92. * @param name:status type:int : default:-1 desc:订单状态(-1全部,0待支付,1已支付)
  93. * @return name:order_no type:string default:-- desc:订单号
  94. * @return name:price_total type:string default:-- desc:支付金额
  95. * @return name:crystal type:string default:-- desc:充值余额数量
  96. * @return name:pay_state type:string default:-- desc:支付状态(0未支付,1已支付)
  97. * @return name:pay_at type:string default:-- desc:支付时间
  98. * @return name:pay_no type:string default:-- desc:支付单号
  99. */
  100. public function getCrystalOrderList()
  101. {
  102. $status = input('status',-1);
  103. $where=[];
  104. if($status > -1) $where['status'] = $status;
  105. $where['uid'] = $this->uid;
  106. $list = Db::table('crystal_order')
  107. ->where($where)
  108. ->order('id desc')
  109. ->limit($this->off_set,$this->page_num)
  110. ->select();
  111. $this->success('ok',['list'=>$list]);
  112. }
  113. /**
  114. * @title 获取余额明细
  115. * @desc 获取余额明细
  116. * @author qc
  117. * @url /api/Crystal/getBalanceLog
  118. * @method GET
  119. * @header name:Authorization require:1 desc:Token
  120. * @param name:page type:int : default:1 desc:页数
  121. * @param name:page_num type:int : default:20 desc:每页数
  122. * @return name:desc type:string default:-- desc:描述
  123. * @return name:type type:int default:-- desc:类型
  124. * @return name:balance type:int default:-- desc:余额变更数
  125. * @return name:create_at type:string default:-- desc:时间
  126. */
  127. public function getBalanceLog()
  128. {
  129. $list = Db::table('balance_info')
  130. ->field('id,desc,balance,type,create_at')
  131. ->where(['user_id'=>$this->uid])
  132. ->order('id desc')
  133. ->limit($this->page,$this->page_num)
  134. ->select();
  135. $this->success('ok',$list);
  136. }
  137. /**
  138. * @title 获取明信片明细
  139. * @desc 获取明信片明细
  140. * @author qc
  141. * @url /api/Crystal/getCrystalLog
  142. * @method GET
  143. * @header name:Authorization require:1 desc:Token
  144. * @param name:page type:int : default:1 desc:页数
  145. * @param name:page_num type:int : default:20 desc:每页数
  146. * @return name:desc type:string default:-- desc:描述
  147. * @return name:type type:int default:-- desc:类型
  148. * @return name:crystal type:int default:-- desc:明信片变更数
  149. * @return name:create_at type:string default:-- desc:时间
  150. */
  151. public function getCrystalLog()
  152. {
  153. $list = Db::table('crystal_info')
  154. ->field('id,desc,crystal,type,create_at')
  155. ->where(['user_id'=>$this->uid])
  156. ->order('id desc')
  157. ->limit($this->page,$this->page_num)
  158. ->select();
  159. $this->success('ok',$list);
  160. }
  161. }