Money.php 7.9 KB

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