Crystal.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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/payOrder
  22. * @method POST
  23. * @header name:Authorization require:1 desc:Token
  24. * @param name:o_id type:int require:1 default:-- desc:订单id
  25. * @return name:config.appId type:string default:-- desc:公众账号ID
  26. * @return name:config.nonceStr type:string default:-- desc:随机字符串
  27. * @return name:config.signType type:string default:-- desc:签名类型
  28. * @return name:config.paySign type:string default:-- desc:签名
  29. * @return name:config.timestamp type:string default:-- desc:时间戳
  30. */
  31. public function payOrder()
  32. {
  33. $o_id = input('post.o_id',0);
  34. $crystal_order =Db::table('crystal_order')->find($o_id);
  35. $user_info = Db::table('store_member')->where('id',$this->uid)->find();
  36. if(!$user_info['openid']) $this->error('请先完善用户信息');
  37. if(empty($crystal_order) || $crystal_order['pay_state'] !=0 ) $this->error('订单有误');
  38. Db::startTrans();
  39. $config = false;$msg= '';
  40. try{
  41. $notify_url = $this->request->root(true) . '/api/Pay/crystalRecharge';
  42. $pay_no = $crystal_order['pay_no'] ? $crystal_order['pay_no'] : get_order_sn();
  43. $config = Pay::wxPay('订单支付',$pay_no,$crystal_order['price_total'],$notify_url,'JSAPI',$user_info['openid']);
  44. if($config){
  45. Db::commit();
  46. }else{
  47. Db::rollback();
  48. }
  49. }catch (\Exception $e){
  50. Db::rollback();
  51. $msg = $e->getMessage();
  52. }
  53. if($config){
  54. $this->success('ok',['config'=>$config]);
  55. }else{
  56. $this->error($msg);
  57. }
  58. }
  59. /**
  60. * @title 水晶充值(立即充值)
  61. * @desc 水晶充值
  62. * @author qc
  63. * @url /api/Crystal/crystalRecharge
  64. * @method POST
  65. * @header name:Authorization require:1 desc:Token
  66. * @param name:crystal type:int require:1 default:-- desc:充值数量
  67. * @param name:price_total type:int require:1 default:-- desc:支付金额
  68. * @return name:config type:array default:0 desc:支付配置
  69. * @return name:config.appId type:string default:-- desc:公众账号ID
  70. * @return name:config.nonceStr type:string default:-- desc:随机字符串
  71. * @return name:config.signType type:string default:-- desc:签名类型
  72. * @return name:config.paySign type:string default:-- desc:签名
  73. * @return name:config.timestamp type:string default:-- desc:时间戳
  74. */
  75. public function crystalRecharge()
  76. {
  77. $crystal = input('post.crystal',0);
  78. $price_total = input('post.price_total',0);
  79. if($crystal <=0 || $price_total <=0 ) $this->error('充值数量有误!');
  80. $crystal_order = [
  81. 'uid' => $this->uid ,
  82. 'crystal' => $crystal ,
  83. 'price_total' => $price_total,
  84. 'order_no' =>get_order_sn(),
  85. 'pay_no' =>get_order_sn(),
  86. 'create_at' => date("Y-m-d H:i:s")
  87. ];
  88. $user_info = Db::table('store_member')->find($this->uid);
  89. Db::startTrans();
  90. $config = false;$msg= '';
  91. try{
  92. Db::table('crystal_order')->insert($crystal_order);
  93. $notify_url = $this->request->root(true) . '/api/Pay/crystalRecharge';
  94. $pay_no = $crystal_order['pay_no'] ? $crystal_order['pay_no'] : get_order_sn();
  95. $config = Pay::wxPay('订单支付',$pay_no,$crystal_order['price_total'],$notify_url,'JSAPI',$user_info['openid']);
  96. if($config){
  97. Db::commit();
  98. }else{
  99. Db::rollback();
  100. }
  101. }catch (\Exception $e){
  102. Db::rollback();
  103. $msg = $e->getMessage();
  104. }
  105. if($config){
  106. $this->success('ok',['config'=>$config]);
  107. }else{
  108. $this->error($msg);
  109. }
  110. }
  111. /**
  112. * @title 删除订单
  113. * @desc 删除订单
  114. * @author qc
  115. * @url /api/Crystal/delCrystalOrder
  116. * @method POST
  117. * @header name:Authorization require:1 desc:Token
  118. * @param name:order_id type:int require:1 default:-- desc:订单id
  119. */
  120. public function delCrystalOrder()
  121. {
  122. Db::table('crystal_order')->where(['uid'=>$this->uid,'id'=>input('post.order_id')])->delete();
  123. $this->success('ok');
  124. }
  125. /**
  126. * @title 获取水晶充值订单列表
  127. * @desc 获取水晶充值订单列表
  128. * @author qc
  129. * @url /api/Crystal/getCrystalOrderList
  130. * @method GET
  131. * @header name:Authorization require:1 desc:Token
  132. * @param name:page type:int : default:1 desc:页数
  133. * @param name:page_num type:int : default:20 desc:每页数
  134. * @param name:status type:int : default:-1 desc:订单状态(-1全部,0待支付,1已支付)
  135. * @return name:order_no type:string default:-- desc:订单号
  136. * @return name:price_total type:string default:-- desc:支付金额
  137. * @return name:crystal type:string default:-- desc:充值水晶数量
  138. * @return name:pay_state type:string default:-- desc:支付状态(0未支付,1已支付)
  139. * @return name:pay_at type:string default:-- desc:支付时间
  140. * @return name:pay_no type:string default:-- desc:支付单号
  141. */
  142. public function getCrystalOrderList()
  143. {
  144. $status = input('status',-1);
  145. $where=[];
  146. if($status > -1) $where['status'] = $status;
  147. $where['uid'] = $this->uid;
  148. $list = Db::table('crystal_order')
  149. ->where($where)
  150. ->order('id desc')
  151. ->limit($this->off_set,$this->page_num)
  152. ->select();
  153. $this->success('ok',['list'=>$list]);
  154. }
  155. /**
  156. * @title 获取水晶明细
  157. * @desc 获取水晶明细
  158. * @author qc
  159. * @url /api/Crystal/getCrystalLog
  160. * @method GET
  161. * @header name:Authorization require:1 desc:Token
  162. * @param name:page type:int : default:1 desc:页数
  163. * @param name:page_num type:int : default:20 desc:每页数
  164. * @return name:desc type:string default:-- desc:描述
  165. * @return name:type type:int default:-- desc:类型
  166. * @return name:crystal type:int default:-- desc:水晶变更数
  167. * @return name:create_at type:string default:-- desc:时间
  168. */
  169. public function getCrystalLog()
  170. {
  171. $list = Db::table('crystal_info')
  172. ->field('id,desc,crystal,type,create_at')
  173. ->where(['user_id'=>$this->uid])
  174. ->order('id desc')
  175. ->limit($this->page,$this->page_num)
  176. ->select();
  177. $this->success('ok',$list);
  178. }
  179. /**
  180. * @title 水晶提现申请
  181. * @desc 水晶提现申请
  182. * @author qc
  183. * @url /api/Crystal/crystalCash
  184. * @method POST
  185. * @header name:Authorization require:1 desc:Token
  186. * @param name:crystal type:float : default:1 desc:提现额度
  187. * @return name:id type:int default:-- desc:提现记录id
  188. */
  189. public function crystalCash()
  190. {
  191. $crystal = input('post.crystal',0);
  192. $user_info = Db::table('store_member')->where('id',$this->uid)->find();
  193. if($user_info['crystal_cash'] < $crystal) $this->error('可提现水晶不足!');
  194. $data = [
  195. 'uid' => $this->uid,
  196. 'order_no' => get_order_sn(),
  197. 'price_total' => $crystal,
  198. 'crystal' => $crystal,
  199. 'sh_status' => 0,
  200. 'status' => 0,
  201. 'create_at' => date('Y-m-d H:i:s'),
  202. ];
  203. Db::table('crystal_cash_order')->insert($data);
  204. $id = Db::getLastInsID();
  205. Db::table('store_member')->where('id',$user_info['id'])->update(['crystal_cash'=>bcsub($user_info['crystal_cash'],$crystal,2)]);
  206. crystal_log($this->uid,$crystal*-1,'水晶提现',4,$id);
  207. $this->success('申请成功,等待审核',['id'=>$id]);
  208. }
  209. /**
  210. * @title 取消水晶提现申请
  211. * @desc 取消水晶提现申请
  212. * @author qc
  213. * @url /api/Crystal/cancelCrystalCash
  214. * @method POST
  215. * @header name:Authorization require:1 desc:Token
  216. * @param name:id type:float : default:1 desc:提现记录id
  217. */
  218. public function cancelCrystalCash()
  219. {
  220. $id = input('post.id',0);
  221. $app_info = Db::table('crystal_cash_order')->where('cancel_status',0)->where('id',$id)->find();
  222. if(empty($app_info)) $this->error('提现记录不存在!');
  223. $user_info = Db::table('store_member')->field('crystal_cash')->where('id',$this->uid)->find();
  224. Db::table('store_member')->where('id',$this->uid)->update(['crystal_cash'=>bcadd($user_info['crystal_cash'],$app_info['crystal'],2)]);
  225. crystal_log($this->uid,$app_info['crystal'],'提现取消',7,$id);
  226. $this->success('取消成功',['id'=>$id]);
  227. }
  228. /**
  229. * @title 获取水晶提现申请记录
  230. * @desc 获取水晶提现申请记录
  231. * @author qc
  232. * @url /api/Crystal/getCrystalCashLog
  233. * @method GET
  234. * @header name:Authorization require:1 desc:Token
  235. * @param name:page type:int : default:1 desc:页数
  236. * @param name:page_num type:int : default:20 desc:每页数
  237. * @param name:sh_status type:int : default:-1 desc:审核状态
  238. * @return name:crystal type:int default:-- desc:提现额度
  239. * @return name:create_at type:string default:-- desc:时间
  240. * @return name:sh_status type:string default:-- desc:审核状态(0未审核1审核通过2审核不通过)
  241. * @return name:status type:string default:-- desc:处理状态0未完成1已完成
  242. */
  243. public function getCrystalCashLog()
  244. {
  245. $where = [];
  246. $where['user_id'] = $this->uid;
  247. $where['cancel_status'] = 0;
  248. $sh_status = input('get.sh_status',-1);
  249. if($sh_status >=0) $where['sh_status'] = $sh_status;
  250. $list = Db::table('crystal_cash_order')
  251. ->field('id,crystal,create_at,sh_status')
  252. ->where($where)
  253. ->order('id desc')
  254. ->limit($this->page,$this->page_num)
  255. ->select();
  256. $this->success('ok',['list'=>$list]);
  257. }
  258. }