Money.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. namespace Biz\Money;
  3. /**
  4. * 资金操作相关类
  5. */
  6. class Money {
  7. /**
  8. * 资金明细表
  9. * @var string
  10. */
  11. private $money_table = 'fund_detailed';
  12. /**
  13. * 用户详情表
  14. * @var string
  15. */
  16. private $account_detailed = 'account_detailed';
  17. /**
  18. * 用户银行绑定记录表
  19. * @var string
  20. */
  21. private $bank_table = 'account_bank';
  22. /**
  23. * 用户表
  24. * @var string
  25. */
  26. private $account_table = 'account';
  27. /**
  28. * 交易密码表
  29. * @var string
  30. */
  31. private $pay_password = 'pay_password';
  32. /**
  33. * 资金明细表
  34. * @var string
  35. */
  36. private $money_details = 'money_details';
  37. /**
  38. * 资金记录修改
  39. * @param
  40. * string $account_identity 用户id
  41. * @param string $money 金额
  42. * @param 订单号 $order_id 订单号
  43. * @param int $type 交易类型
  44. * @return [type] [description]
  45. */
  46. public function insertMoney($account_identity, $money, $order_id, $type, $game_no = '',$UUID,$account=array())
  47. {
  48. //_beginTransaction();
  49. //获取用户信息
  50. if(empty($account)){
  51. return '-4020';
  52. }
  53. //查询余额
  54. $getAccount = $this->getAccount($account_identity);
  55. //验证流水金额与余额是否正常
  56. $checkWater = $this->checkWater($account_identity,$getAccount['available_cash']);
  57. if($checkWater != 1){
  58. return $checkWater;
  59. }
  60. //获取money_type
  61. $m_type = $this->getMtype($type);
  62. //判断加减金额
  63. if ($m_type == 1) {
  64. $nowMoney = $getAccount['available_cash'] + $money;
  65. $nowCash = $getAccount['cash'] + $money;
  66. } else {
  67. $nowMoney = $getAccount['available_cash'] - $money;
  68. $nowCash = $getAccount['cash'] - $money;
  69. }
  70. //更新用户余额
  71. $updateAccountMoney = $this->updateAccountMoney($account_identity,$nowMoney, $nowCash);
  72. var_dump($updateAccountMoney);
  73. exit;
  74. if($updateAccountMoney != 1){
  75. return $updateAccountMoney;
  76. }else {
  77. //插入资金详情
  78. $insertDetail = $this->insertDetail($account_identity, $order_id, $money, $nowMoney, $type,$UUID,$account);
  79. if($insertDetail !=1){
  80. return $insertDetail;
  81. }
  82. }
  83. return '1';
  84. }
  85. /**
  86. * 更新用户金额
  87. * @param [type] $account_identity 用户唯一ID
  88. * @param [type] $nowMoney 操作后可提现金额
  89. * @param [type] $nowCash 操作后总金额
  90. */
  91. public function updateAccountMoney($account_identity, $nowMoney,$nowCash)
  92. {
  93. echo '</br>';
  94. var_dump($account_identity);
  95. echo '</br>';
  96. var_dump($nowMoney);
  97. echo '</br>';
  98. var_dump($nowCash);
  99. if(empty($account_identity)||empty($nowMoney)||empty($nowCash)){
  100. return '-2255';
  101. }
  102. //更新用户余额
  103. $res = lm($this->account_detailed, 'Commons')
  104. ->where('account_identity',$account_identity)
  105. ->update([
  106. 'cash'=> $nowCash,
  107. 'available_cash' => $nowMoney
  108. ]);
  109. if(!$res){
  110. return '-7024';
  111. }
  112. return '1';
  113. }
  114. /**
  115. * 插入资金详情
  116. * @param [type] $account_identity 用户唯一id
  117. * @param [type] $order_id 交易id
  118. * @param [type] $money 交易金额
  119. * @param [type] $nowMoney 剩余金额
  120. * @param [type] $type 交易类型的数字表示
  121. * @return [type] [description]
  122. */
  123. public function insertDetail($account_identity, $order_id, $money, $nowMoney, $type,$UUID,$account)
  124. {
  125. //获取用户信息
  126. if(empty($account)){
  127. return '-4020';
  128. }
  129. //获取money_type
  130. $m_type = $this->getMtype($type);
  131. $trade_desc = $this->getDesc($account['account'], $money, $order_id, $type);
  132. $data2 = array(
  133. 'info_identity' => $UUID,
  134. 'trade_id' => $order_id,
  135. 'account_name' => $account['account'],
  136. 'account_identity' => $account_identity,
  137. 'money' => $money,
  138. 'money_time' => date('Y-m-d H:i:s', time()),
  139. 'money_type' => $m_type,
  140. 'money_cash' => $nowMoney,
  141. //'status' => $status,
  142. 'trade_type' => $type,
  143. 'trade_desc' => $trade_desc,
  144. );
  145. $res3 = lm($this->money_details,'commons')->insert($data2);
  146. if ($res3) {
  147. return '1';
  148. }
  149. return '-7025';
  150. }
  151. /**
  152. * 获取用户余额信息
  153. * @param [type] $account_identity 用户唯一id
  154. * @return [type] 用户资金相关数组
  155. */
  156. public function getAccount($account_identity) {
  157. $res = lm($this->account_detailed, 'Commons')
  158. ->select('available_cash', 'cash', 'frozen_cash', 'account_identity')
  159. ->where('account_identity', $account_identity)
  160. ->first();
  161. if(!$res){
  162. Render('', '2115',lang('Errors','Api')->get('error-2115'));
  163. }
  164. $res->toarray();
  165. return $res;
  166. }
  167. /**
  168. * 验证账户金额是否足够
  169. * @param [type] $money 操作所需金额
  170. * @param [type] $available_cash 账户可用金额
  171. * @return [type] [description]
  172. */
  173. public function verifyMoney($money, $available_cash) {
  174. if ($available_cash > $money) {
  175. return true;
  176. } else {
  177. return false;
  178. }
  179. }
  180. /**
  181. * 检测是否设置交易密码
  182. * @param [type] $account_identity 用户id
  183. */
  184. public function checkPayPwd($account_identity='')
  185. {
  186. //$account_identity = isset($_SESSION['uinfo']['account_identity']) ? $_SESSION['uinfo']['account_identity'] : '';
  187. if (empty($account_identity)) {
  188. Render('', '-2001', lang()->get('user not login'));
  189. }
  190. //检查用户是否已经设置密码
  191. $judge = lm($this->pay_password, 'commons')->where(['account_identity' => $account_identity])->first();
  192. if ($judge) {
  193. return '1';
  194. } else {
  195. return '-1024';
  196. }
  197. }
  198. /**
  199. * 检测资金流水情况
  200. * @param $account_identity
  201. * @param $cash
  202. */
  203. public function checkWater($account_identity, $NowCash)
  204. {
  205. //查询最后一条资金余额
  206. $last_data = lm($this->money_details, 'commons')->where('account_identity', $account_identity)->orderBy('id', 'desc')->first();
  207. if (empty($last_data)) {
  208. return -500147;
  209. }
  210. $last_data = $last_data->toArray();
  211. if ($last_data['money_cash'] != $NowCash) {
  212. return -2255;
  213. }
  214. return 1;
  215. }
  216. /**
  217. * 验证交易密码
  218. * @param [type] $account_identity 用户唯一id
  219. * @param [type] $pwd 密码
  220. * @return [type] true or false
  221. */
  222. public function verifyPayPwd($account_identity,$pwd) {
  223. $enc = lm($this->pay_password,'commons')->select('encryption')->where('account_identity', $account_identity)->first()->toArray();
  224. $VerPwd = md5(md5($enc["encryption"] . $pwd));
  225. $res = lm($this->pay_password,'commons')->where(['account_identity' => $account_identity, 'pay_password' => $VerPwd])->first();
  226. if ($res) {
  227. return true;
  228. } else {
  229. return false;
  230. }
  231. }
  232. /**
  233. * 反水
  234. * @param string $account_identity 用户唯一ID
  235. * @param string $account 用户名
  236. * @param string $bet_money 投注总金额
  237. */
  238. public function fanshui($account_identity,$account,$bet_money,$order_id){
  239. //查询是否可以反水
  240. $account_info = lm('Account','Api')->where([['identity',$account_identity],['account',$account]])->first();
  241. if(!$account_info){
  242. Render('', '51030',lang('Errors','Api')->get('error-51030'));
  243. }
  244. //查询反水比例
  245. $Setinfo = lm('SetInfo','Api')->where([['infoname','fanshui'],['status',1]])->first();
  246. if(empty($Setinfo)){
  247. Render('', '51034',lang('Errors','Api')->get('error-51034'));
  248. }
  249. $account_info->toArray();
  250. $Setinfo->toArray();
  251. if($account_info['fanshui'] == 1){
  252. //执行插入金额详情与用户余额加反水
  253. // $order_id = OrderID(); //订单ID
  254. $UUID = UUID(); //信息ID
  255. $type = 7; //反水
  256. $bet_money = $bet_money * $Setinfo['infocontent']; //计算反水额
  257. $res = $this->insertMoney($account_identity, $bet_money, $order_id, $type, $game_no = '',$UUID,$account_info);
  258. if($res != 1){
  259. Render('', $res,lang('Errors','Api')->get('error'.$res));
  260. }
  261. }
  262. }
  263. /**
  264. * 获取描述信息
  265. * @param string $account_name 用户名
  266. * @param string $money 金额
  267. * @param string $order_id 订单号
  268. * @param int $type 操作类型
  269. * @return [type] [description]
  270. */
  271. public function getDesc($account_name, $money, $order_id, $type)
  272. {
  273. switch ($type) {
  274. case 1:
  275. return $account_name . '投注' . $money . '元。' . '订单号' . $order_id;
  276. break;
  277. case 2:
  278. return $account_name . '追号' . $money . '元。' . '订单号' . $order_id;
  279. break;
  280. case 3:
  281. return $account_name . '撤单' . '订单号' . $order_id;
  282. break;
  283. case 4:
  284. return $account_name . '中奖' . $money . '元。' . '订单号' . $order_id;
  285. break;
  286. case 5:
  287. return $account_name . '申请提现' . $money . '元。' . '订单号' . $order_id;
  288. break;
  289. case 6:
  290. return $account_name . '充值' . $money . '元。' . '订单号' . $order_id;
  291. break;
  292. case 7:
  293. return $account_name . '反水' . $money . '元。' . '订单号' . $order_id;
  294. break;
  295. case 8:
  296. return $account_name . '回水' . $money . '元。' . '订单号' . $order_id;
  297. break;
  298. case 9:
  299. return $account_name . '管理员扣款' . $money . '元。' . '订单号' . $order_id;
  300. break;
  301. case 10:
  302. return $account_name . '佣金提成' . $money . '元。' . '订单号' . $order_id;
  303. break;
  304. default:
  305. return '';
  306. break;
  307. }
  308. }
  309. /**
  310. * 返回money_type
  311. * @param [type] $type 类型
  312. * @return [type] [description]
  313. */
  314. public function getMtype($type)
  315. {
  316. $arr = array(
  317. '1' => 2,
  318. '2' => 2,
  319. '3' => 1,
  320. '4' => 1,
  321. '5' => 2,
  322. '6' => 1,
  323. '7' => 1,
  324. '8' => 1,
  325. '9' => 2,
  326. '10' => 1,
  327. '11' => 2,
  328. '15' => 2,
  329. );
  330. return $arr[$type];
  331. }
  332. }
  333. ?>