Bank.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 用户银行表 Table<ai_allwin_bank>
  7. */
  8. namespace app\allwin\model;
  9. use think\Model;
  10. use think\facade\Validate;
  11. use app\allwin\model\AllwinStore;
  12. use app\allwin\model\BankBill;
  13. use app\common\facade\Inform;
  14. use app\common\facade\WechatPay;
  15. class Bank extends Model{
  16. protected $pk = 'id';
  17. protected $table = 'ai_allwin_bank';
  18. protected $autoWriteTimestamp = true;
  19. protected $createTime = false;
  20. //用户
  21. public function user(){
  22. return $this->hasOne('app\common\model\SystemUser','id','user_id');
  23. }
  24. /**
  25. * 后台在使用
  26. * 帐号余额
  27. */
  28. public static function bank($miniapp_id,$keyword = ''){
  29. $condition[] = ['allwin_bank.member_miniapp_id','=',$miniapp_id];
  30. if(!empty($keyword)){
  31. if(Validate::isMobile($keyword)){
  32. $condition[] = ['system_user.phone_uid','=',$keyword];
  33. }else{
  34. $condition[] = ['system_user.nickname','like','%'.$keyword.'%'];
  35. }
  36. }
  37. return self::view('allwin_bank','*')->view('system_user','nickname,phone_uid,face','allwin_bank.user_id = system_user.id')->where($condition);
  38. }
  39. /**
  40. * 根据是否提交成功结算资金
  41. * @param integer $uid
  42. * @param float $money(元)
  43. * @param boolean $isSuccess
  44. * @return boolean
  45. */
  46. protected static function isPassCash(int $uid,float $money,$is_success = true){
  47. $info = self::where(['user_id' => $uid])->find();
  48. $money = $money*100;
  49. if ($info->lack_money < $money) {
  50. return;
  51. }
  52. $info->lack_money = ['dec',$money];
  53. if($is_success == false){
  54. $info->due_money = ['inc',$money];
  55. $info->money = ['inc',$money];
  56. }
  57. $info->update_time = time();
  58. return $info->save();
  59. }
  60. /**
  61. * 提现申请审核操作
  62. * @param array $param
  63. * @return boolean
  64. */
  65. public static function isPass(array $param,$member_miniapp_id){
  66. $id = intval($param['id']);
  67. $ispass = intval($param['ispass']);
  68. $miniapp_id = intval($param['miniapp_id']);
  69. $cash = model('BankCash')->where(['member_miniapp_id' =>$miniapp_id,'id'=> $id,'state' => 0])->find();
  70. if($cash){
  71. $setting = model('AllwinConfig')->getConfig($miniapp_id);
  72. /**
  73. * 判断是否好店老板
  74. * 是老板->不扣除提现手续费
  75. * 非老板->扣除提现手续费
  76. */
  77. $is_manage = AllwinStore::manageStore($cash->user_id);
  78. if(empty($is_manage)){
  79. $realmoney = money($cash->money - $cash->money*($setting->cash_charges/1000));
  80. }else{
  81. $realmoney = $cash->money;
  82. }
  83. //判断是否成功
  84. $is_success = false;
  85. $is_diy = false;
  86. $trade_no = $cash->user->invite_code.order_no().'WE';
  87. if($ispass){
  88. if($setting->is_wechat_touser && (!empty($cash->user->official_uid) || $realmoney < 5000)){
  89. $trade_no .= 'WE';
  90. $app = WechatPay::doPay($miniapp_id,true);
  91. $rel = $app->transfer->toBalance(['partner_trade_no' => $trade_no,'openid' => $cash->user->official_uid,'check_name' => 'NO_CHECK','amount' => $realmoney*100,'desc' => '酬劳与收入']);
  92. if ($rel['return_code'] === 'SUCCESS') {
  93. if ($rel['result_code'] === 'SUCCESS') {
  94. $state = 1;
  95. $is_success = true;
  96. $is_diy = true;
  97. $message = "[通过]申请转出已结算到微信钱包";
  98. }else{
  99. $message = $rel['err_code_des'];
  100. }
  101. }else{
  102. $message = $rel['return_msg'];
  103. }
  104. }else{
  105. $trade_no .= 'BANK';
  106. $is_diy = true;
  107. $message = "[通过]申请转出已结算到您填写的账户";
  108. }
  109. if($is_diy){
  110. $bank = self::isPassCash($cash->user_id,$cash->money);
  111. if($bank){
  112. $state = 1;
  113. $is_success = true;
  114. }
  115. }
  116. }else{
  117. $trade_no .= 'BANK';
  118. $bank = self::isPassCash($cash->user_id,$cash->money,false);
  119. if($bank){
  120. $state = -1;
  121. $is_success = true;
  122. $message = "[失败]申请转出,已退回账户";
  123. }
  124. }
  125. if($is_success){
  126. BankBill::add(['store_id' => 0,'pay_uid' => 0,'miniapp_id'=>$miniapp_id,'uid'=>$cash->user_id,'money'=>money($realmoney)],$message);
  127. $data['state'] = $state;
  128. $data['realmoney'] = $realmoney;
  129. $data['audit_time'] = time();
  130. $data['trade_no'] = $trade_no;
  131. $data['msg'] = $message;
  132. model('BankCash')->where(['member_miniapp_id' =>$miniapp_id,'id'=> $id])->update($data);
  133. //通知申请者到微信
  134. if($state){
  135. Inform::sms($cash->user_id,$member_miniapp_id,['title' =>'您的账户进行了一笔转账交易','type' => '个人转出','content' =>$cash->money.'元','state' => '成功']);
  136. }else{
  137. Inform::sms($cash->user_id,$member_miniapp_id,['title' =>'您的账户进行了一笔转账交易','type' => '个人转出','content' =>$cash->money.'元','state' => '失败']);
  138. }
  139. return ['code'=>200,'msg' => $message,'url' => url('allwin/bank/cashpass',['id' => $id])];
  140. }else{
  141. return ['code'=>0,'msg'=>$message,'url' => url('allwin/bank/cashpass',['id' => $id])];
  142. }
  143. }else{
  144. return ['code'=>0,'msg'=>'操作失败,为找到申请提现记录.'];
  145. }
  146. }
  147. /**
  148. * 应付款增加
  149. * @param integer $miniapp_id
  150. * @param integer $uid
  151. * @param integer $due_money(元)
  152. * @return void
  153. */
  154. public static function dueMoney(int $miniapp_id,int $uid,float $money){
  155. $info = self::where(['user_id' => $uid])->find();
  156. $money = $money*100;
  157. if(empty($info)){
  158. $data['member_miniapp_id'] = $miniapp_id;
  159. $data['user_id'] = $uid;
  160. $data['income_money'] = $money;
  161. $data['money'] = $money;
  162. $data['due_money'] = $money;
  163. $data['update_time'] = time();
  164. return self::insert($data);
  165. }else{
  166. $info->income_money = ['inc',$money];
  167. $info->money = ['inc',$money];
  168. $info->due_money = ['inc',$money];
  169. $info->update_time = time();
  170. return $info->save();
  171. }
  172. }
  173. /**
  174. * 帐号充值
  175. * @param integer $miniapp_id
  176. * @param integer $uid
  177. * @param integer $money(元)
  178. * @return void
  179. */
  180. public static function recharge(int $miniapp_id,int $uid,float $money){
  181. $info = self::where(['member_miniapp_id'=>$miniapp_id,'user_id' => $uid])->find();
  182. $money = $money * 100;
  183. if(empty($info)){
  184. $data['member_miniapp_id'] = $miniapp_id;
  185. $data['user_id'] = $uid;
  186. $data['due_money'] = $money;
  187. $data['money'] = $money;
  188. $data['update_time'] = time();
  189. return self::insert($data);
  190. }else{
  191. $info->due_money = ['inc',$money];
  192. $info->money = ['inc',$money];
  193. $info->update_time = time();
  194. return $info->save();
  195. }
  196. }
  197. /**
  198. * 积分增加
  199. * @param integer $miniapp_id
  200. * @param integer $uid
  201. * @param integer $money(元)
  202. * @return void
  203. */
  204. public static function rePoints(int $miniapp_id,int $uid,int $points){
  205. $info = self::where(['member_miniapp_id' => $miniapp_id,'user_id' => $uid])->find();
  206. if(empty($info)){
  207. $data['member_miniapp_id'] = $miniapp_id;
  208. $data['user_id'] = $uid;
  209. $data['shop_money'] = $points;
  210. $data['update_time'] = time();
  211. return self::insert($data);
  212. }else{
  213. $info->shop_money = ['inc',$points];
  214. $info->update_time = time();
  215. return $info->save();
  216. }
  217. }
  218. /**
  219. * 提现申请(小程序API)
  220. * @param integer $miniapp_id
  221. * @param integer $uid
  222. * @param integer $money(元)
  223. * @return void
  224. */
  225. public static function cash(int $miniapp_id,int $uid,float $money){
  226. $info = self::where(['member_miniapp_id' => $miniapp_id,'user_id' => $uid])->find();
  227. if(empty($info)){
  228. return;
  229. }
  230. $money = $money * 100;
  231. if($info->due_money < $money || $info->money < $money ){
  232. return;
  233. }
  234. $info->due_money = ['dec',$money];
  235. $info->money = ['dec',$money];
  236. $info->lack_money = ['inc',$money];
  237. $info->update_time = time();
  238. return $info->save();
  239. }
  240. }