MemberPaymentService.php 8.3 KB

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