Withdraw.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\library\Sms;
  4. use app\api\controller\Common;
  5. use app\common\service\UserService;
  6. use fast\Random;
  7. use think\Db;
  8. use think\Exception;
  9. use think\Validate;
  10. use app\common\model\UserAlipay;
  11. /**
  12. *
  13. */
  14. class Withdraw extends Common
  15. {
  16. protected $layout = 'default';
  17. protected $noNeedLogin = [];
  18. protected $noNeedRight = ['*'];
  19. /**
  20. * 绑定银行卡
  21. */
  22. public function bindBank() {
  23. try {
  24. //$realname = $this->request->request('realname');// 真实姓名
  25. $bank_no = $this->request->request('bank_no');// 银行账号
  26. //$idCard = $this->request->request('id_card');// 身份证号
  27. /*$bank_name = $this->request->request('bank_name');// 银行名称
  28. $open_address = $this->request->request('open_address');// 开户地*/
  29. $open_bank = $this->request->request('open_bank');// 开户行
  30. /*$bank_mobile = $this->request->request('bank_mobile');// 银行预留手机号
  31. $captcha = $this->request->request('captcha'); // 验证码
  32. $mobile = $this->request->request('mobile'); // 手机号*/
  33. $userId = $this->auth->id;
  34. //|| !$bank_name || !$open_address || !$bank_mobile || !$captcha || !$mobile
  35. if(!$bank_no || !$open_bank ) {
  36. throw new Exception("请将信息填写完整");
  37. }
  38. $userAuthWhere['user_id'] = $userId;
  39. $userAuth = model('UserAuth')->where($userAuthWhere)->find();
  40. if (empty($userAuth)) {
  41. throw new Exception('请先实名认证');
  42. }
  43. if ($userAuth['status'] != 1) {
  44. throw new Exception('请先实名认证通过');
  45. }
  46. $realname = $userAuth['realname'];
  47. $idCard = $userAuth['idcard'];
  48. $userService = new UserService();
  49. $aliParams = [
  50. 'bank_no' => $bank_no,
  51. 'id_card' => $idCard,
  52. 'real_name' => $realname,
  53. ];
  54. $aliBankCheckRes = $userService->bankCheck($aliParams);
  55. if (!$aliBankCheckRes['status']) {
  56. throw new Exception($aliBankCheckRes['msg']);
  57. }
  58. // 获取用户信息
  59. //$userInfo = \app\common\model\User::where(["id"=>$this->auth->id])->find();
  60. /*if($userInfo->mobile !== $mobile) $this->error("请输入账号绑定的手机号码!");
  61. if (!Sms::check($mobile, $captcha, 'binkBank')) {
  62. $this->error(__('验证码不正确!'));
  63. }*/
  64. // 查询是否有过绑定
  65. $bankInfo = \app\common\model\UserBank::where(["user_id"=>$userId])->find();
  66. $data = [];
  67. $data["realname"] = $realname;
  68. $data["bank_no"] = $bank_no;
  69. $data["open_bank"] = $open_bank;
  70. $data["id_card"] = $idCard;
  71. /*$data["bank_name"] = $bank_name;
  72. $data["open_address"] = $open_address;
  73. $data["mobile"] = $bank_mobile;*/
  74. if($bankInfo) {
  75. $data["updatetime"] = time();
  76. $res = \app\common\model\UserBank::update($data,["user_id"=>$userId]);
  77. } else {
  78. $data["user_id"] = $userId;
  79. $data["createtime"] = time();
  80. $res = \app\common\model\UserBank::insert($data);
  81. }
  82. if(!$res) {
  83. throw new Exception("网络异常,请稍后重试!");
  84. }
  85. $this->success("银行卡绑定成功!");
  86. } catch (Exception $e) {
  87. $this->error($e->getMessage());
  88. }
  89. }
  90. /**
  91. * 获取绑定银行卡信息
  92. */
  93. public function getBankInfo() {
  94. // 查询是否有过绑定
  95. $bankInfo = \app\common\model\UserBank::where(["user_id"=>$this->auth->id])->find();
  96. //if(!$bankInfo) $this->error("银行卡信息获取失败!");
  97. $this->success("获取成功!",$bankInfo);
  98. }
  99. /**
  100. * 绑定支付宝
  101. */
  102. public function bindAlipay() {
  103. //$realname = $this->request->request('realname');//真实姓名
  104. $payNo = $this->request->request('pay_no');//支付宝账号
  105. //$cardNo = $this->request->request('card_no');//身份证号
  106. //$mobile = $this->request->request('mobile'); //手机号
  107. //$captcha = $this->request->request('captcha'); //验证码
  108. $userId = $this->auth->id;
  109. //姓名和身份证号 取实名认证
  110. $userAuthWhere['user_id'] = $userId;
  111. $userAuth = model('UserAuth')->where($userAuthWhere)->find();
  112. if (empty($userAuth)) {
  113. $this->error('请先实名认证');
  114. }
  115. if ($userAuth['status'] != 1) {
  116. $this->error('请先实名认证通过');
  117. }
  118. $realname = $userAuth['realname'];
  119. $cardNo = $userAuth['idcard'];
  120. if(!$realname || !$payNo || !$cardNo) {
  121. $this->error("请将信息填写完整");
  122. }
  123. // 获取用户信息
  124. //$userInfo = \app\common\model\User::where(["id"=>$this->auth->id])->find();
  125. /*if (!Sms::check($mobile, $captcha, 'binkAli')) {
  126. $this->error(__('验证码不正确!'));
  127. }*/
  128. $userAlipayModel = new UserAlipay();
  129. // 查询是否有过绑定
  130. $bankInfo = $userAlipayModel->where(["user_id"=>$userId])->find();
  131. $data = [];
  132. $data["realname"] = $realname;
  133. $data["pay_no"] = $payNo;
  134. $data["card_no"] = $cardNo;
  135. if($bankInfo) {
  136. $data["updatetime"] = time();
  137. $res = $userAlipayModel->update($data,["user_id"=>$userId]);
  138. } else {
  139. $data["user_id"] = $userId;
  140. $data["createtime"] = time();
  141. $res = $userAlipayModel->insert($data);
  142. }
  143. if($res) {
  144. //Sms::flush($mobile, 'binkBank');
  145. $this->success("支付宝绑定成功!");
  146. } else {
  147. $this->error("网络异常,请稍后重试!");
  148. }
  149. }
  150. /**
  151. * 获取绑定银行卡信息
  152. */
  153. public function getAlipayInfo() {
  154. // 查询是否有过绑定
  155. $alipayInfo = UserAlipay::where(["user_id"=>$this->auth->id])->find();
  156. //if(!$alipayInfo) $this->error("支付宝信息获取失败!");
  157. $this->success("获取成功!",$alipayInfo);
  158. }
  159. /**
  160. * 获取用户账户信息
  161. */
  162. public function getUserAccountInfo() {
  163. // 获取用户信息
  164. $res = \app\common\model\User::field("id,u_id,avatar,mobile,nickname")->where(["id"=>$this->auth->id])->find();
  165. //用户钱包
  166. $userwallet = Db::name('user_wallet')->where('user_id',$this->auth->id)->find();
  167. $res['use_money'] = $userwallet['money'];
  168. $res['all_money'] = $userwallet['money'];
  169. // 获取用户实名认证信息
  170. $res["realname"] = \app\common\model\UserAuth::where(["user_id"=>$res["id"]])->value("realname");
  171. // 获取用户银行卡信息
  172. $res["bankInfo"] = \app\common\model\UserBank::where(["user_id"=>$res["id"]])->find();
  173. $this->success("获取成功!",$res);
  174. }
  175. public function getEncryptPassword($password, $salt = '')
  176. {
  177. return md5(md5($password) . $salt);
  178. }
  179. /**
  180. * 设置交易密码
  181. * @return void
  182. */
  183. public function setPaypwd()
  184. {
  185. try {
  186. $userId = $this->auth->id;
  187. $payPwd = $this->request->param('pay_pwd','');
  188. $confirmPwd = $this->request->param('confirm_pay_pwd','');
  189. if (empty($payPwd) || empty($confirmPwd)) {
  190. throw new Exception('参数错误');
  191. }
  192. if ($payPwd != $confirmPwd) {
  193. throw new Exception('密码不一致');
  194. }
  195. $paySalt = Random::alnum();
  196. $payPassword = $this->getEncryptPassword($payPwd,$paySalt);
  197. $where['id'] = $userId;
  198. $user = model('User')->where($where)->find();
  199. if (empty($user)) {
  200. throw new Exception('未知的用户');
  201. }
  202. $user->pay_password = $payPassword;
  203. $user->pay_salt = $paySalt;
  204. $res = $user->save();
  205. if (!$res) {
  206. throw new Exception('设置失败');
  207. }
  208. $this->success('设置成功');
  209. } catch (Exception $e) {
  210. $this->error($e->getMessage());
  211. }
  212. }
  213. /**
  214. * 修改交易密码
  215. * @return void
  216. */
  217. public function editPaypwd()
  218. {
  219. try {
  220. $userId = $this->auth->id;
  221. $oldPayPwd = $this->request->param('old_pay_pwd','');
  222. $payPwd = $this->request->param('pay_pwd','');
  223. $confirmPwd = $this->request->param('confirm_pay_pwd','');
  224. if (empty($oldPayPwd) || empty($payPwd) || empty($confirmPwd)) {
  225. throw new Exception('参数错误');
  226. }
  227. if ($payPwd != $confirmPwd) {
  228. throw new Exception('密码不一致');
  229. }
  230. $where['id'] = $userId;
  231. $user = model('User')->where($where)->find();
  232. if (empty($user)) {
  233. throw new Exception('未知的用户');
  234. }
  235. $userPaySalt = $user['pay_salt'];
  236. $userPayPassword = $this->getEncryptPassword($oldPayPwd,$userPaySalt);
  237. if ($userPayPassword != $user['pay_password']) {
  238. throw new Exception('旧密码错误');
  239. }
  240. $paySalt = Random::alnum();
  241. $payPassword = $this->getEncryptPassword($payPwd,$paySalt);
  242. $user->pay_password = $payPassword;
  243. $user->pay_salt = $paySalt;
  244. $res = $user->save();
  245. if (!$res) {
  246. throw new Exception('设置失败');
  247. }
  248. $this->success('设置成功');
  249. } catch (Exception $e) {
  250. $this->error($e->getMessage());
  251. }
  252. }
  253. /**
  254. * 验证改密码
  255. * @return void
  256. */
  257. public function checkSms()
  258. {
  259. try {
  260. $mobile = $this->request->param('mobile','');
  261. $code = $this->request->param('code','');
  262. //$event = $this->request->param('event','');//事件editpaypwd=修改支付密码,forgetpaypwd=忘记支付密码
  263. if (empty($mobile) || empty($code)) {
  264. throw new Exception('参数错误');
  265. }
  266. $userMobile = $this->auth->mobile;
  267. if (empty($userMobile)) {
  268. throw new Exception('请绑定手机号');
  269. }
  270. if ($mobile != $userMobile) {
  271. throw new Exception('手机号与绑定不一致');
  272. }
  273. if ($code == '1212') {
  274. $this->success('验证成功');
  275. }
  276. //$where['event'] = $event;
  277. $where['mobile'] = $mobile;
  278. $where['code'] = $code;
  279. $sms = model('Sms')->where($where)->find();
  280. if (empty($sms)) {
  281. throw new Exception('验证码错误');
  282. }
  283. $createtime = $sms['createtime'] - (60 * 2);
  284. if ($sms['createtime'] < $createtime) {
  285. throw new Exception('验证已过期,请重新获取。');
  286. }
  287. $this->success('验证成功');
  288. } catch (Exception $e) {
  289. $this->error($e->getMessage());
  290. }
  291. }
  292. }