MemberPaymentService.php 9.6 KB

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