Wallet.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace app\common\model;
  3. use app\common\service\UserService;
  4. use think\Model;
  5. use think\Db;
  6. /**
  7. * 货币模型
  8. */
  9. class Wallet extends Model
  10. {
  11. /**
  12. * 获取交易类型配置
  13. * @return mixed
  14. */
  15. public function getlogtype($type = '')
  16. {
  17. $conf = config('wallet.logtype');
  18. if($type){
  19. return $conf[$type] ?: $type;
  20. }
  21. return $conf;
  22. }
  23. //获取钱包名称
  24. public function getwalletname($name = ''){
  25. $conf = config('wallet.moneyname');
  26. if($name){
  27. return $conf[$name] ?: $name;
  28. }
  29. return $conf;
  30. }
  31. /**
  32. * 获取钱包余额
  33. * @param string $username 用户编号
  34. * @param string $wallet_name 指定钱包名
  35. * @return array|float
  36. */
  37. public function getWallet($user_id = '', $wallet_name = '')
  38. {
  39. //所有钱包余额
  40. $wallet = Db::name('user_wallet')->lock(true)->where(['user_id' => $user_id])->find();
  41. if(!$wallet) {
  42. abort(500,'钱包余额获取失败');
  43. }
  44. if($wallet_name) { //返回指定钱包
  45. return isset($wallet[$wallet_name]) ? $wallet[$wallet_name] : 0;
  46. } else { //返回所有钱包
  47. return $wallet;
  48. }
  49. }
  50. /**
  51. *
  52. * @param floatval $number 金额(正数进账,负数出账)
  53. * @param $accountType 货币类型,money,score
  54. * @param $logtype 日志的类型
  55. * @param $remark 备注
  56. * @param $user_id 用户id
  57. * @param $table 来源表
  58. * @param $data_id 表id
  59. * @param $isAdmin 是否是管理员处理
  60. * @return array
  61. * @return array[status]
  62. * @return array[msg]
  63. * @return array[log_table]
  64. * @return array[log_id]
  65. */
  66. public function lockChangeAccountRemain($user_id,$money,$mode,$before = 0,$remark='',$logtype,$accountType='money',$extend = [])
  67. {
  68. //初始化
  69. $result = array(
  70. 'status'=>false,
  71. 'code' => 0, //报错时候采用
  72. 'msg'=>'',
  73. 'log_table' => '',
  74. 'log_id' => '',
  75. );
  76. //限制权限
  77. /*$power_rs = $this->power_limit_logtype($user_id,$logtype);
  78. if($power_rs !== true){
  79. $result['msg'] = $power_rs;
  80. return $result;
  81. }*/
  82. //获取小数点
  83. $point = $accountType == 'money' ? 2 : 0;
  84. bcscale($point);
  85. //钱包名称
  86. $wallet_name = $this->getwalletname($accountType);
  87. //数字纠正
  88. $money = abs($money);
  89. if($mode == '-'){
  90. $number = -$money;
  91. }else{
  92. $number = $money;
  93. }
  94. //检测
  95. $number = floatval( $number );
  96. if( $number == 0 )
  97. {
  98. $result['msg'] = '交易金额:0';
  99. return $result;
  100. }
  101. if(0 === bccomp($number, 0)){
  102. $result['msg'] = '交易金额:0';
  103. return $result;
  104. }
  105. //检测
  106. $wallet = Db::name('user_wallet')->lock(true)->where(['user_id'=>$user_id])->find();
  107. if(!$wallet)
  108. {
  109. $result['msg'] = '不存在的用户';
  110. return $result;
  111. }
  112. if(bccomp(bcadd($wallet[$accountType], $number), 0) === -1)
  113. {
  114. $result['code'] = 10;
  115. $result['msg'] = $wallet_name.'余额不足!';
  116. return $result;
  117. }
  118. if(0 !== bccomp($number, 0))
  119. {
  120. //钱币记录
  121. $data = array();
  122. $data['user_id'] = $user_id;
  123. $data['value'] = $money;
  124. $data['mode'] = $mode;
  125. $data['before'] = $wallet[$accountType];
  126. $data['balance'] = bcadd($wallet[$accountType], $number);
  127. $data['detail'] = $remark;
  128. $data['createtime'] = time();
  129. $data['type'] = $logtype;
  130. if ($accountType == 'jewel') {
  131. $data['cityname'] = isset($extend['cityname']) ? $extend['cityname'] : '';
  132. }
  133. //新的方式
  134. $rs1 = Db::name('user_wallet')->where(['user_id'=>$user_id])->update([$accountType => $data['balance']]);
  135. /////////////
  136. $log_table = 'user_'.$accountType.'_log';
  137. $rs2_id = Db::name($log_table)->insertGetId($data);
  138. if($rs1 === false || $rs2_id === false){
  139. $result['msg'] = '更新财务记录失败!';
  140. return $result;
  141. }
  142. //用不到了
  143. /*if($accountType == 'jewel'){
  144. //$userService = new UserService();
  145. //$userService->updateGuildMember(['user_id'=>$user_id]);
  146. //更新放到计划任务里,这里做标记
  147. $rs3 = Db::name('user_power')->where(['user_id'=>$user_id])->update(['guild_status' => 0]);
  148. }*/
  149. if( $rs1 !== false && $rs2_id !== false )
  150. {
  151. $result['status'] = true;
  152. $result['msg'] = '账户余额已更新!';
  153. $result['log_table'] = $accountType.'_log';
  154. $result['log_id'] = $rs2_id;
  155. return $result;
  156. }
  157. else
  158. {
  159. $result['msg'] = '更新财务记录失败!';
  160. return $result;
  161. }
  162. } else {
  163. $result['msg'] = '金额不足0.01';
  164. return $result;
  165. }
  166. }
  167. //user_power代表用户权限
  168. //多个字段限制了用户的消费,当某个字段为1代表为禁止,结合logtype,报错到前台
  169. /*public function power_limit_logtype($user_id,$logtype){
  170. $enum = [
  171. 0 => 'payorder',
  172. 1 => 'recharge',
  173. // 3 => 'give_gift',//接口处直接判断
  174. 5 => 'noble',
  175. 6 => 'attire',
  176. 11 => 'transfer',
  177. 13 => 'raffle',
  178. 104 => 'withdraw',
  179. ];
  180. //不受限制
  181. if(!isset($enum[$logtype])){
  182. return true;
  183. }
  184. $power = Db::name('user_power')->where('user_id',$user_id)->find();
  185. //没有权限表,直接报错
  186. if(empty($power)){
  187. return '不存在的用户';
  188. }
  189. $field = $enum[$logtype];
  190. //受限制
  191. if($power[$field] == 1){
  192. $logtype_name = $this->getlogtype($logtype);
  193. return '您已被限制:'.$logtype_name.',请联系管理员';
  194. }
  195. return true;
  196. }*/
  197. }