MemberPaymentService.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\MemberModel;
  13. use App\Models\MemberPaymentModel;
  14. use App\Models\User;
  15. use App\Models\UserModel;
  16. use App\Services\BaseService;
  17. use App\Services\ConfigService;
  18. use Earnp\GoogleAuthenticator\GoogleAuthenticator;
  19. /**
  20. * 交易员收款方式-服务类
  21. * Class MemberPaymentService
  22. * @package App\Services\Common
  23. */
  24. class MemberPaymentService extends BaseService
  25. {
  26. // 静态对象
  27. protected static $instance = null;
  28. /**
  29. * 构造函数
  30. * @since 2020/11/10
  31. */
  32. public function __construct()
  33. {
  34. $this->model = new MemberPaymentModel();
  35. $this->memberModel = new MemberModel();
  36. $this->userModel = new UserModel();
  37. }
  38. /**
  39. * 静态入口
  40. * @return static|null
  41. */
  42. public static function make()
  43. {
  44. if (!self::$instance) {
  45. self::$instance = (new static());
  46. }
  47. return self::$instance;
  48. }
  49. /**
  50. * 获取选项列表
  51. * @param $userId
  52. * @return mixed
  53. */
  54. public function getOptionList($userId, $type = 1, $pageSize = 9999)
  55. {
  56. $list = $this->model->where(function ($query) use ($userId, $type) {
  57. $query->where(['user_id' => $userId,'type'=> $type,'mark'=>1])->where('status','>',0);
  58. })
  59. ->selectRaw('id,type,real_name,logo,bank_name,branch_name,account,qrcode,is_default,bank_card,trade_num,used_num,trade_quota,used_quota,(trade_quota - used_quota) as credit,status')
  60. ->orderBy('is_default','asc')
  61. ->orderBy('status','asc')
  62. ->orderBy('id','desc')
  63. ->paginate($pageSize>0? $pageSize : 9999999);
  64. $list = $list? $list->toArray() :[];
  65. if($list){
  66. foreach($list['data'] as &$item){
  67. $item['logo'] = $item['logo'] ? get_image_url($item['logo']) : '';
  68. $item['qrcode'] = $item['qrcode'] ? get_image_url($item['qrcode']) : '';
  69. $item['realname_text'] = $item['real_name'] ? format_realname($item['real_name']) : '';
  70. $item['account_text'] = $item['account'] ? format_account($item['account']) : '';
  71. $item['bank_card_text'] = $item['bank_card'] ? format_bank_card($item['bank_card']) : '';
  72. if ($item['type'] == 1) {
  73. $item['show_text'] = $item['bank_name'] . ' ' . $item['realname_text'] . ' ' . $item['bank_card_text'];
  74. } else {
  75. $item['show_text'] = $item['realname_text'] . ' ' . $item['account_text'];
  76. }
  77. }
  78. }
  79. return [
  80. 'pageSize'=> $pageSize,
  81. 'total'=>isset($list['total'])? $list['total'] : 0,
  82. 'list'=> isset($list['data'])? $list['data'] : []
  83. ];
  84. }
  85. /**
  86. * 是否已经设置了收款方式
  87. * @param $userId
  88. * @return mixed
  89. */
  90. public function checkHasByUser($userId)
  91. {
  92. return $this->model->where(['user_id' => $userId, 'status' => 1,'mark'=>1])->count('id');
  93. }
  94. /**
  95. * 更新状态
  96. * @param $id
  97. * @param $status
  98. * @param $userId
  99. * @return mixed
  100. */
  101. public function setPayment($id, $status, $userId){
  102. $status = $status? 1 : 2;
  103. return $this->model->where(['user_id' => $userId, 'id' => $id,'mark'=>1])->update(['status'=> $status]);
  104. }
  105. /**
  106. * 删除
  107. * @param $id
  108. * @param $userId
  109. * @return mixed
  110. */
  111. public function delPayment($id, $userId){
  112. // 永久删除
  113. $this->model->where(['user_id' => $userId, 'id' => $id,'status'=> 0,'mark'=>1])
  114. ->where('update_time','<', time() - 2 * 86400)
  115. ->delete();
  116. return $this->model->where(['user_id' => $userId, 'id' => $id])->update(['status'=> 0,'mark'=>1]);
  117. }
  118. /**
  119. * 详情
  120. * @param $id
  121. * @return mixed
  122. */
  123. public function getInfo($id)
  124. {
  125. $info = $this->model->where(['id' => $id,'mark'=>1])->where('status','>', 0)->first();
  126. if($info){
  127. $info['logo'] = $info['logo']? get_image_url($info['logo']) : '';
  128. $info['status'] = $info['status']==1? true : false;
  129. }
  130. return $info;
  131. }
  132. /**
  133. * 获取默认的收款方式,若没有则返回其他一个
  134. * @param $userId
  135. * @return array
  136. */
  137. public function getPayment($userId)
  138. {
  139. $info = $this->model->where(['user_id' => $userId,'mark'=>1,'status'=> 1])
  140. ->whereRaw('(trade_num<=0 or trade_quota <= 0 or (trade_num-used_num>0 and trade_quota-used_quota>0))')
  141. ->select(['id','type','logo','real_name','bank_name','branch_name','bank_card','qrcode','account'])
  142. ->orderBy('is_default','asc')
  143. ->orderBy('id','desc')
  144. ->first();
  145. $info = $info? $info->toArray() : [];
  146. if($info){
  147. $info['logo'] = $info['logo']? get_image_url($info['logo']) : '';
  148. }
  149. return $info;
  150. }
  151. /**
  152. * 后台添加/编辑
  153. * @return array
  154. */
  155. public function saveData($userId){
  156. $data = request()->all();
  157. $id = isset($data['id'])? intval($data['id']) : 0;
  158. $googleCode = isset($data['google_code'])? $data['google_code'] : '';
  159. $type = isset($data['type'])? $data['type'] : 1;
  160. $bankCard = isset($data['bank_card'])? $data['bank_card'] : '';
  161. $isDefault = isset($data['is_default'])? intval($data['is_default']) : 2;
  162. if($type ==1){
  163. if(empty($bankCard) || !preg_match('/^[1-9][0-9]{12,19}$/', $bankCard)){
  164. return returnJson('银行卡号格式不正确,请输入13-19位银行卡号', false);
  165. }
  166. if($this->model->where(['bank_card'=> $bankCard])->whereNotIn('id', [$id])->value('id')){
  167. return returnJson('您添加的银行卡号已存在,请核对正确或联系客服处理', false);
  168. }
  169. }
  170. if(empty($googleCode)){
  171. return returnJson('请输入您的谷歌验证码,或先绑定再来操作', false);
  172. }
  173. $userInfo = $this->userModel->where(['user_id'=> $userId,'mark'=>1])
  174. ->select(['id','user_id','google_secret','status'])
  175. ->first();
  176. if(empty($userInfo)){
  177. return returnJson('您的账号异常无法操作,请联系客服处理', false);
  178. }
  179. $googleSecret = isset($userInfo['google_secret'])? $userInfo['google_secret'] : '';
  180. if(empty($googleSecret)){
  181. return returnJson(2017, false);
  182. }
  183. if (!GoogleAuthenticator::CheckCode($googleSecret, $googleCode)) {
  184. return returnJson(2018, false);
  185. }
  186. $limitCount = ConfigService::make()->getConfigByCode('payment_limit_num');
  187. $limitCount = $limitCount>0? $limitCount : 30;
  188. if($this->model->where(['user_id'=> $userId, 'mark'=> 1])->count('id') >= $limitCount){
  189. return returnJson('您当前的收款账号数量已经超出限制,请删除无用账号或联系客服处理', false);
  190. }
  191. // LOGO
  192. if(isset($data['logo'])){
  193. $logo = trim($data['logo']);
  194. if (strpos($logo, "temp")) {
  195. $data['logo'] = save_image($logo, 'business');
  196. } else {
  197. $data['logo'] = str_replace(IMG_URL, "", $data['logo']);
  198. }
  199. }
  200. // QRCODE
  201. if(isset($data['qrcode'])){
  202. $qrcode = trim($data['qrcode']);
  203. if (strpos($qrcode, "temp")) {
  204. $data['qrcode'] = save_image($qrcode, 'business');
  205. } else {
  206. $data['qrcode'] = str_replace(IMG_URL, "", $data['qrcode']);
  207. }
  208. }
  209. if($isDefault == 1){
  210. $this->model->where(['user_id'=> $userId])->update(['is_default'=> 2]);
  211. }
  212. $data['user_id'] = $userId;
  213. if(isset($data['used_at'])){
  214. $data['used_at'] = $data['used_at']? $data['used_at'] : time();
  215. }
  216. return parent::edit($data);
  217. }
  218. }