Info.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace Account\Lib;
  3. use Dever;
  4. class Info
  5. {
  6. # 获取用户账户基本信息
  7. public function getUserInfo($uid, $config_id = false)
  8. {
  9. $info = $this->getInfo($uid, $config_id);
  10. return array('name' => $info['config']['name'], 'cash' => $info['cash'], 'info_id' => $info['id']);
  11. }
  12. # 获取账户信息
  13. public function getInfo($uid, $config_id = false, $check = 0)
  14. {
  15. $key = Dever::input('account_key');
  16. if ($key) {
  17. $config = Dever::db('account/config')->one(array('key' => $key));
  18. } else {
  19. if (!$config_id) {
  20. $config = Dever::db('account/config')->one(array('type' => 1));
  21. } else {
  22. $config = Dever::db('account/config')->one($config_id);
  23. }
  24. }
  25. if ($config) {
  26. # 检测是否可以充值
  27. if ($check == 1 && $config['is_pay'] == 2) {
  28. Dever::alert('当前账户不允许充值');
  29. }
  30. # 检测是否可以提现
  31. if ($check == 2 && $config['is_withdraw'] == 2) {
  32. Dever::alert('当前账户不允许提现');
  33. }
  34. $where = array();
  35. $where['uid'] = $uid;
  36. $where['config_id'] = $config['id'];
  37. $account = Dever::db('account/info')->one($where);
  38. if (!$account) {
  39. $id = Dever::db('account/info')->insert($where);
  40. $account = Dever::db('account/info')->one($id);
  41. }
  42. $account['config'] = $config;
  43. return $account;
  44. } else {
  45. Dever::alert('钱包配置不存在');
  46. }
  47. }
  48. # 入账
  49. public function up_commit($uid, $cash, $type, $config, $desc = '', $source = '', $source_id = '', $method = 1, $state = true, $result = false)
  50. {
  51. if ($cash == 0) {
  52. return $this->alert('金额不能为0', $state);
  53. }
  54. if (!is_array($config)) {
  55. $config = Dever::db('account/config')->find(array('key' => $config));
  56. }
  57. if (!$config) {
  58. return $this->alert('账户信息不存在', $state);
  59. }
  60. $project = Dever::db('account/config_project')->find($config['project_id']);
  61. $user = Dever::db($project['source'])->find($uid);
  62. if (!$user) {
  63. return $this->alert('用户信息不存在', $state);
  64. }
  65. if (!is_array($type)) {
  66. $type = Dever::db('account/config_type')->find(array('key' => $type));
  67. }
  68. if (!$type) {
  69. return $this->alert('交易类型不存在', $state);
  70. }
  71. if ($type['project_id'] != -1 && $type['project_id'] != $config['project_id']) {
  72. return $this->alert('交易类型不正确', $state);
  73. }
  74. $data['uid'] = $uid;
  75. $data['config_id'] = $config['id'];
  76. $data['project_id'] = $config['project_id'];
  77. $data['clear'] = true;
  78. $info = Dever::db('account/info')->find($data);
  79. if (!$info) {
  80. $info['id'] = Dever::db('account/info')->insert($data);
  81. if (!$info['id']) {
  82. return $this->alert('用户信息不存在', $state);
  83. }
  84. $info['cash'] = 0;
  85. }
  86. $data['status'] = 2;
  87. $scash = $cash;
  88. $func = 'inc';
  89. if ($type['type'] == 2) {
  90. if ($info['cash'] < $cash) {
  91. return $this->alert('账户余额不足', $state);
  92. }
  93. $func = 'dec';
  94. $scash = $scash*-1;
  95. }
  96. $data['info_id'] = $info['id'];
  97. $data['ycash'] = $cash;
  98. $data['cash'] = $cash;
  99. if ($type['key'] == 'tixian') {
  100. if ($config['is_withdraw'] == 2) {
  101. return $this->alert('当前账户不能提现', $state);
  102. }
  103. if ($config['withdraw_down'] > 0 && $cash < $config['withdraw_down']) {
  104. return $this->alert('提现金额不能少于' . $config['withdraw_down'], $state);
  105. }
  106. if ($config['withdraw_up'] > 0 && $cash > $config['withdraw_up']) {
  107. return $this->alert('提现金额不能大于' . $config['withdraw_up'], $state);
  108. }
  109. if ($cash > $info['cash']) {
  110. return $this->alert('提现金额不能大于账户余额', $state);
  111. }
  112. if ($config['withdraw_check']) {
  113. $msg = Dever::load($config['withdraw_check'], $uid, $cash);
  114. if ($msg != 'ok') {
  115. return $this->alert($msg, $state);
  116. }
  117. }
  118. if ($config['withdraw_audit'] == 1) {
  119. $data['status'] = 1;
  120. }
  121. if ($config['withdraw_fee']) {
  122. $fee = Dever::per($cash, $config['withdraw_fee']);
  123. $data['cash'] -= $fee;
  124. }
  125. }
  126. $data['type_id'] = $type['id'];
  127. if ($source) {
  128. $data['source'] = $source;
  129. }
  130. if ($source_id) {
  131. $data['source_id'] = $source_id;
  132. }
  133. if ($desc) {
  134. $data['desc'] = $desc;
  135. }
  136. if ($info) {
  137. if ($type['key'] == 'tixian') {
  138. $data['yue'] = $info['cash'] - $data['ycash'];
  139. } else {
  140. $data['yue'] = $info['cash'] + $scash;
  141. }
  142. if ($type['type'] == 2 && $config['balance_alert'] && $data['yue'] <= $config['balance_alert']) {
  143. return $this->alert('账户余额不足', $state);
  144. }
  145. } else {
  146. $data['yue'] = 0;
  147. }
  148. $yue = $data['yue'];
  149. $data['method'] = $method;
  150. $id = Dever::load('account/lib/log')->create($data);
  151. if ($id) {
  152. $update = array();
  153. $update['where_id'] = $info['id'];
  154. $update['set_cash'] = $scash;
  155. $update['set_col'] = $data['ycash'];
  156. $update['clear'] = true;
  157. Dever::db('account/info')->$func($update);
  158. }
  159. if ($result) {
  160. $data['id'] = $id;
  161. return $data;
  162. }
  163. return $yue;
  164. }
  165. private function alert($msg, $state = true)
  166. {
  167. if ($state) {
  168. return Dever::alert($msg);
  169. }
  170. return -1;
  171. }
  172. }