Userbank.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 绑定支付宝或银行
  7. */
  8. class Userbank extends Api
  9. {
  10. // 无需登录的接口,*表示全部
  11. protected $noNeedLogin = [''];
  12. // 无需鉴权的接口,*表示全部
  13. protected $noNeedRight = ['*'];
  14. //绑定支付宝
  15. public function addalipayaccount() {
  16. $id = input('id', 0, 'intval'); //账号id
  17. $type = input('type', 0, 'intval'); //账号类型:1=支付宝,2=银行卡
  18. // $type = 1;
  19. $realname = input('realname', '', 'trim'); //账户真实姓名
  20. $banknumber = input('banknumber', '', 'trim'); //卡号或账号
  21. $bankname = input('bankname', '', 'trim'); //银行名称
  22. // if ($id) {
  23. // $this->error('您已经拥有该类型账号,请联系后台解绑');
  24. // }
  25. if (!in_array($type, [1, 2])) {
  26. $this->error('参数错误');
  27. }
  28. /*if ($type == 1) {
  29. $this->error('暂不支持绑定支付宝');
  30. }*/
  31. if ($realname === '' || iconv_strlen($realname, 'utf-8') > 30) {
  32. $this->error('账号姓名1-30位');
  33. }
  34. if ($banknumber === '' || iconv_strlen($banknumber, 'utf-8') > 50) {
  35. $this->error('账号1-50位');
  36. }
  37. if ($type == 1) {
  38. if (iconv_strlen($bankname, 'utf-8') != 18) {
  39. $this->error('请输入正确身份证号');
  40. }
  41. }
  42. if ($type == 2) {
  43. if ($bankname === '' || iconv_strlen($bankname, 'utf-8') > 50) {
  44. $this->error('开户行名称1-50位');
  45. }
  46. }
  47. $user_bank = Db::name('user_bank');
  48. if ($id) {
  49. //编辑
  50. //查询账号是否存在
  51. $info = $user_bank->where(['id' => $id, 'user_id' => $this->auth->id])->find();
  52. if (!$info) {
  53. $this->error('账号不存在');
  54. }
  55. if ($info['type'] != $type) {
  56. $this->error('账号类型错误');
  57. }
  58. //查询是否未通过提现记录
  59. $count = Db::name('take_cash')->where(['user_id' => $this->auth->id, 'alipay_account' => $info['banknumber'], 'status' => 0])->count('id');
  60. if ($count) {
  61. $this->error('该账户有未审核的提现记录,暂不可修改');
  62. }
  63. $data['realname'] = $realname;
  64. $data['banknumber'] = $banknumber;
  65. $data['updatetime'] = time();
  66. // if ($type == 2) {
  67. $data['bankname'] = $bankname;
  68. // }
  69. $rs = $user_bank->where(['id' => $id, 'user_id' => $this->auth->id])->setField($data);
  70. if ($rs === false) {
  71. $this->error('修改失败');
  72. }
  73. $this->success('修改成功');
  74. } else {
  75. //添加
  76. //查询是否已经拥有该类型账号
  77. $count = $user_bank->where(['user_id' => $this->auth->id, 'type' => $type])->count('id');
  78. if ($count) {
  79. $this->error('您已经拥有该类型账号,请联系后台解绑');
  80. }
  81. $data['user_id'] = $this->auth->id;
  82. $data['type'] = $type;
  83. $data['realname'] = $realname;
  84. $data['banknumber'] = $banknumber;
  85. // if ($type == 2) {
  86. $data['bankname'] = $bankname;
  87. // }
  88. $data['createtime'] = time();
  89. $rs = $user_bank->insertGetId($data);
  90. if (!$rs) {
  91. $this->error('添加失败');
  92. }
  93. $this->success('添加成功');
  94. }
  95. }
  96. //查询支付宝/银行卡信息
  97. public function alipayinfo() {
  98. $type = input('type', 0, 'intval'); //账号类型:1=支付宝,2=银行卡
  99. $info = Db::name('user_bank')->field('id, realname, banknumber, bankname')->where(['user_id' => $this->auth->id, 'type' => $type])->find();
  100. if (!$info) {
  101. $info = (object)[];
  102. }
  103. $this->success('success', $info);
  104. }
  105. //绑定支付宝账号信息
  106. public function bindalipayaccount() {
  107. $mt_user_bank = Db::name('user_bank');
  108. $count = $mt_user_bank->where(['user_id' => $this->auth->id, 'type' => 1])->count();
  109. if ($count) {
  110. $this->error('您已绑定支付宝啦');
  111. }
  112. $auth_code = input('auth_code', '', 'trim');
  113. if (!$auth_code) {
  114. $this->error('授权码缺失');
  115. }
  116. //获取支付宝用户信息
  117. $rs = getAlipayInfo($auth_code);
  118. if ($rs['status'] == 0) {
  119. $this->error($rs['info']);
  120. }
  121. $rs = $rs['data'];
  122. /*
  123. * Array(
  124. [alipay_user_info_share_response] => Array
  125. (
  126. [code] => 10000
  127. [msg] => Success
  128. [avatar] => https://tfs.alipayobjects.com/images/partner/T1BQJsXhhbXXXXXXXX 头像
  129. [city] => 临沂市 市名称。
  130. [gender] => m 【注意】只有is_certified为T的时候才有意义,否则不保证准确性. 性别(F:女性;M:男性)。
  131. [is_certified] => T 是否通过实名认证。T是通过 F是没有实名认证。
  132. [is_student_certified] => F 是否是学生 T是 F否
  133. [nick_name] => 风的追求 用户昵称
  134. [province] => 山东省 省份名称
  135. [user_id] => 2088902918001020 支付宝用户的userId
  136. [user_status] => T 用户状态(Q/T/B/W)。 Q代表快速注册用户 T代表已认证用户 B代表被冻结账户 W代表已注册,未激活的账户
  137. [user_type] => 2 用户类型(1/2) 1代表公司账户2代表个人账户
  138. )
  139. )
  140. */
  141. $data['banknumber'] = $rs['user_id']; //支付宝用户id
  142. if (!$data['banknumber']) {
  143. $this->error('获取用户信息失败');
  144. }
  145. $bank_number_count = $mt_user_bank->where(['banknumber' => $data['banknumber'], 'type' => 1])->count();
  146. if ($bank_number_count) {
  147. $this->error('该支付宝账号已经绑定用户, 请先解除绑定');
  148. }
  149. if (isset($rs['nick_name'])) {
  150. $data['realname'] = $rs['nick_name']; //支付宝用户昵称
  151. }
  152. $data['user_id'] = $this->auth->id;
  153. $data['createtime'] = time();
  154. $data['type'] = 1;
  155. $rt = $mt_user_bank->insertGetId($data);
  156. if (!$rt) {
  157. $this->error('绑定失败');
  158. }
  159. $this->success('绑定成功');
  160. }
  161. }