Withdraw.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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 = ['getUserInfoByMobile','bindBank','mobilelogin'];
  18. protected $noNeedRight = ['*'];
  19. // /**
  20. // * 余额提现
  21. // * @return string
  22. // */
  23. // public function withdraw()
  24. // {
  25. // $config = get_addon_config('withdraw');
  26. // $this->view->assign('addonConfig', $config);
  27. // $this->view->assign('title', __('Withdraw'));
  28. // return $this->view->fetch();
  29. // }
  30. //
  31. // /**
  32. // * 余额日志
  33. // * @return string
  34. // */
  35. // public function withdrawlog()
  36. // {
  37. // $withdrawloglist = \addons\withdraw\model\Withdraw::where(['user_id' => $this->auth->id])
  38. // ->order('id desc')
  39. // ->paginate(10);
  40. //
  41. // $this->view->assign('title', __('Withdraw log'));
  42. // $this->view->assign('withdrawloglist', $withdrawloglist);
  43. // return $this->view->fetch();
  44. // }
  45. // /**
  46. // * 创建订单并发起支付请求
  47. // * @throws \think\exception\DbException
  48. // */
  49. // public function submit()
  50. // {
  51. // $money = $this->request->request('money');
  52. // $account = $this->request->request('account');
  53. // $name = $this->request->request('name');
  54. // $type = $this->request->request('type','alipay');
  55. //// $type = 'alipay';
  56. //
  57. //
  58. // if ($money <= 0) {
  59. // $this->error('提现金额不正确');
  60. // }
  61. //
  62. // if (!$account) {
  63. // $this->error("提现账户不能为空");
  64. // }
  65. // if (!$name) {
  66. // $this->error("真实姓名不能为空");
  67. // }
  68. // if (!Validate::is($account, "email") && !Validate::is($account, "/^1\d{10}$/")) {
  69. // $this->error("提现账户只能是手机号或Email");
  70. // }
  71. //
  72. // $config = get_addon_config('withdraw');
  73. // if (isset($config['minmoney']) && $money < $config['minmoney']) {
  74. // $this->error('提现金额不能低于' . $config['minmoney'] . '元');
  75. // }
  76. // if ($config['monthlimit']) {
  77. // $count = \addons\withdraw\model\Withdraw::where('user_id', $this->auth->id)->whereTime('createtime', 'month')->count();
  78. // if ($count >= $config['monthlimit']) {
  79. // $this->error("已达到本月最大可提现次数");
  80. // }
  81. // }
  82. // Db::startTrans();
  83. // try {
  84. // $data = [
  85. // 'orderid' => date("YmdHis") . sprintf("%08d", $this->auth->id) . mt_rand(1000, 9999),
  86. // 'user_id' => $this->auth->id,
  87. // 'money' => $money,
  88. // 'type' => $type,
  89. // 'account' => $account,
  90. // 'name' => $name,
  91. // ];
  92. // \addons\withdraw\model\Withdraw::create($data);
  93. // \app\common\model\User::money(-$money, $this->auth->id, "提现");
  94. // Db::commit();
  95. // } catch (Exception $e) {
  96. // Db::rollback();
  97. // $this->error($e->getMessage());
  98. // }
  99. // $this->success("提现申请成功!请等待后台审核!", url("withdraw/withdrawlog"));
  100. // return;
  101. // }
  102. //
  103. // /**
  104. // * 企业支付通知和回调
  105. // * @throws \think\exception\DbException
  106. // */
  107. // public function epay()
  108. // {
  109. // $type = $this->request->param('type');
  110. // $paytype = $this->request->param('paytype');
  111. // if ($type == 'notify') {
  112. // $pay = \addons\epay\library\Service::checkNotify($paytype);
  113. // if (!$pay) {
  114. // echo '签名错误';
  115. // return;
  116. // }
  117. // $data = $pay->verify();
  118. // try {
  119. // $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
  120. // \addons\recharge\model\Order::settle($data['out_trade_no'], $payamount);
  121. // } catch (Exception $e) {
  122. // }
  123. // echo $pay->success();
  124. // } else {
  125. // $pay = \addons\epay\library\Service::checkReturn($paytype);
  126. // if (!$pay) {
  127. // $this->error('签名错误');
  128. // }
  129. // //微信支付没有返回链接
  130. // if ($pay === true) {
  131. // $this->success("请返回网站查看支付状态!", "");
  132. // }
  133. //
  134. // //你可以在这里定义你的提示信息,但切记不可在此编写逻辑
  135. // $this->success("恭喜你!充值成功!", url("user/index"));
  136. // }
  137. // return;
  138. // }
  139. /**
  140. * 根据手机号获取用户信息
  141. */
  142. public function getUserInfoByMobile() {
  143. $mobile = $this->request->request('mobile');// 手机号
  144. if(!$mobile) $this->error("请输入手机号!");
  145. $field = "u_id,avatar,money,nickname,mobile";
  146. $userInfo = \app\common\model\User::getByMobile($mobile,$field);
  147. $this->success("获取成功!",$userInfo);
  148. }
  149. /**
  150. * 验证身份证号
  151. */
  152. public function validateCard() {
  153. $idcard = $this->request->request('idcard');// 身份证号
  154. if(!$idcard) $this->error("参数缺失!");
  155. // 获取用户信息
  156. $idcardInfo = \app\common\model\UserAuth::where(["user_id"=>$this->auth->id])->value("idcard");
  157. if($idcardInfo === $idcard) {
  158. $this->success("验证成功!");
  159. } else {
  160. $this->error("验证失败!");
  161. }
  162. }
  163. /**
  164. * 绑定银行卡
  165. */
  166. public function bindBank() {
  167. try {
  168. //$realname = $this->request->request('realname');// 真实姓名
  169. $bank_no = $this->request->request('bank_no');// 银行账号
  170. //$idCard = $this->request->request('id_card');// 身份证号
  171. /*$bank_name = $this->request->request('bank_name');// 银行名称
  172. $open_address = $this->request->request('open_address');// 开户地*/
  173. $open_bank = $this->request->request('open_bank');// 开户行
  174. /*$bank_mobile = $this->request->request('bank_mobile');// 银行预留手机号
  175. $captcha = $this->request->request('captcha'); // 验证码
  176. $mobile = $this->request->request('mobile'); // 手机号*/
  177. $userId = $this->auth->id;
  178. //|| !$bank_name || !$open_address || !$bank_mobile || !$captcha || !$mobile
  179. if(!$bank_no || !$open_bank ) {
  180. throw new Exception("请将信息填写完整");
  181. }
  182. $userAuthWhere['user_id'] = $userId;
  183. $userAuth = model('UserAuth')->where($userAuthWhere)->find();
  184. if (empty($userAuth)) {
  185. throw new Exception('请先实名认证');
  186. }
  187. if ($userAuth['status'] != 1) {
  188. throw new Exception('请先实名认证通过');
  189. }
  190. $realname = $userAuth['realname'];
  191. $idCard = $userAuth['idcard'];
  192. $userService = new UserService();
  193. $aliParams = [
  194. 'bank_no' => $bank_no,
  195. 'id_card' => $idCard,
  196. 'real_name' => $realname,
  197. ];
  198. $aliBankCheckRes = $userService->bankCheck($aliParams);
  199. if (!$aliBankCheckRes['status']) {
  200. throw new Exception($aliBankCheckRes['msg']);
  201. }
  202. // 获取用户信息
  203. //$userInfo = \app\common\model\User::where(["id"=>$this->auth->id])->find();
  204. /*if($userInfo->mobile !== $mobile) $this->error("请输入账号绑定的手机号码!");
  205. if (!Sms::check($mobile, $captcha, 'binkBank')) {
  206. $this->error(__('验证码不正确!'));
  207. }*/
  208. // 查询是否有过绑定
  209. $bankInfo = \app\common\model\UserBank::where(["user_id"=>$userId])->find();
  210. $data = [];
  211. $data["realname"] = $realname;
  212. $data["bank_no"] = $bank_no;
  213. $data["open_bank"] = $open_bank;
  214. $data["id_card"] = $idCard;
  215. /*$data["bank_name"] = $bank_name;
  216. $data["open_address"] = $open_address;
  217. $data["mobile"] = $bank_mobile;*/
  218. if($bankInfo) {
  219. $data["updatetime"] = time();
  220. $res = \app\common\model\UserBank::update($data,["user_id"=>$userId]);
  221. } else {
  222. $data["user_id"] = $userId;
  223. $data["createtime"] = time();
  224. $res = \app\common\model\UserBank::insert($data);
  225. }
  226. if(!$res) {
  227. throw new Exception("网络异常,请稍后重试!");
  228. }
  229. $this->success("银行卡绑定成功!");
  230. } catch (Exception $e) {
  231. $this->error($e->getMessage());
  232. }
  233. }
  234. /**
  235. * 获取绑定银行卡信息
  236. */
  237. public function getBankInfo() {
  238. // 查询是否有过绑定
  239. $bankInfo = \app\common\model\UserBank::where(["user_id"=>$this->auth->id])->find();
  240. //if(!$bankInfo) $this->error("银行卡信息获取失败!");
  241. $this->success("获取成功!",$bankInfo);
  242. }
  243. /**
  244. * 绑定支付宝
  245. */
  246. public function bindAlipay() {
  247. //$realname = $this->request->request('realname');//真实姓名
  248. $payNo = $this->request->request('pay_no');//支付宝账号
  249. //$cardNo = $this->request->request('card_no');//身份证号
  250. //$mobile = $this->request->request('mobile'); //手机号
  251. //$captcha = $this->request->request('captcha'); //验证码
  252. $userId = $this->auth->id;
  253. //姓名和身份证号 取实名认证
  254. $userAuthWhere['user_id'] = $userId;
  255. $userAuth = model('UserAuth')->where($userAuthWhere)->find();
  256. if (empty($userAuth)) {
  257. $this->error('请先实名认证');
  258. }
  259. if ($userAuth['status'] != 1) {
  260. $this->error('请先实名认证通过');
  261. }
  262. $realname = $userAuth['realname'];
  263. $cardNo = $userAuth['idcard'];
  264. if(!$realname || !$payNo || !$cardNo) {
  265. $this->error("请将信息填写完整");
  266. }
  267. // 获取用户信息
  268. //$userInfo = \app\common\model\User::where(["id"=>$this->auth->id])->find();
  269. /*if (!Sms::check($mobile, $captcha, 'binkAli')) {
  270. $this->error(__('验证码不正确!'));
  271. }*/
  272. $userAlipayModel = new UserAlipay();
  273. // 查询是否有过绑定
  274. $bankInfo = $userAlipayModel->where(["user_id"=>$userId])->find();
  275. $data = [];
  276. $data["realname"] = $realname;
  277. $data["pay_no"] = $payNo;
  278. $data["card_no"] = $cardNo;
  279. if($bankInfo) {
  280. $data["updatetime"] = time();
  281. $res = $userAlipayModel->update($data,["user_id"=>$userId]);
  282. } else {
  283. $data["user_id"] = $userId;
  284. $data["createtime"] = time();
  285. $res = $userAlipayModel->insert($data);
  286. }
  287. if($res) {
  288. //Sms::flush($mobile, 'binkBank');
  289. $this->success("支付宝绑定成功!");
  290. } else {
  291. $this->error("网络异常,请稍后重试!");
  292. }
  293. }
  294. /**
  295. * 获取绑定银行卡信息
  296. */
  297. public function getAlipayInfo() {
  298. // 查询是否有过绑定
  299. $alipayInfo = UserAlipay::where(["user_id"=>$this->auth->id])->find();
  300. //if(!$alipayInfo) $this->error("支付宝信息获取失败!");
  301. $this->success("获取成功!",$alipayInfo);
  302. }
  303. /**
  304. * 获取用户账户信息
  305. */
  306. public function getUserAccountInfo() {
  307. // 获取用户信息
  308. $res = \app\common\model\User::field("id,u_id,avatar,mobile,nickname,money as use_money,frozen")->where(["id"=>$this->auth->id])->find();
  309. $res["all_money"] = bcadd($res["use_money"],$res["frozen"],2);
  310. unset($res["frozen"]);
  311. // 获取用户实名认证信息
  312. $res["realname"] = \app\common\model\UserAuth::where(["user_id"=>$res["id"]])->value("realname");
  313. // 获取用户银行卡信息
  314. $res["bankInfo"] = \app\common\model\UserBank::where(["user_id"=>$res["id"]])->find();
  315. $this->success("获取成功!",$res);
  316. }
  317. /**
  318. * 手机验证码获取用户信息
  319. *
  320. * @param string $mobile 手机号
  321. * @param string $captcha 验证码
  322. */
  323. public function mobilelogin()
  324. {
  325. $mobile = $this->request->request('mobile');
  326. $captcha = $this->request->request('captcha');
  327. if (!$mobile || !$captcha) {
  328. $this->error(__('Invalid parameters'));
  329. }
  330. if (!Validate::regex($mobile, "^1\d{10}$")) {
  331. $this->error(__('Mobile is incorrect'));
  332. }
  333. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  334. $this->error(__('Captcha is incorrect'));
  335. }
  336. $user = \app\common\model\User::getByMobile($mobile,"u_id,is_auth");
  337. if ($user) {
  338. Sms::flush($mobile, 'mobilelogin');
  339. $this->success("获取成功!",$user);
  340. } else {
  341. $this->error("没有查询到用户信息!请前往app注册!");
  342. }
  343. }
  344. public function getEncryptPassword($password, $salt = '')
  345. {
  346. return md5(md5($password) . $salt);
  347. }
  348. /**
  349. * 设置交易密码
  350. * @return void
  351. */
  352. public function setPaypwd()
  353. {
  354. try {
  355. $userId = $this->auth->id;
  356. $payPwd = $this->request->param('pay_pwd','');
  357. $confirmPwd = $this->request->param('confirm_pay_pwd','');
  358. if (empty($payPwd) || empty($confirmPwd)) {
  359. throw new Exception('参数错误');
  360. }
  361. if ($payPwd != $confirmPwd) {
  362. throw new Exception('密码不一致');
  363. }
  364. $paySalt = Random::alnum();
  365. $payPassword = $this->getEncryptPassword($payPwd,$paySalt);
  366. $where['id'] = $userId;
  367. $user = model('User')->where($where)->find();
  368. if (empty($user)) {
  369. throw new Exception('未知的用户');
  370. }
  371. $user->pay_password = $payPassword;
  372. $user->pay_salt = $paySalt;
  373. $res = $user->save();
  374. if (!$res) {
  375. throw new Exception('设置失败');
  376. }
  377. $this->success('设置成功');
  378. } catch (Exception $e) {
  379. $this->error($e->getMessage());
  380. }
  381. }
  382. /**
  383. * 修改交易密码
  384. * @return void
  385. */
  386. public function editPaypwd()
  387. {
  388. try {
  389. $userId = $this->auth->id;
  390. $oldPayPwd = $this->request->param('old_pay_pwd','');
  391. $payPwd = $this->request->param('pay_pwd','');
  392. $confirmPwd = $this->request->param('confirm_pay_pwd','');
  393. if (empty($oldPayPwd) || empty($payPwd) || empty($confirmPwd)) {
  394. throw new Exception('参数错误');
  395. }
  396. if ($payPwd != $confirmPwd) {
  397. throw new Exception('密码不一致');
  398. }
  399. $where['id'] = $userId;
  400. $user = model('User')->where($where)->find();
  401. if (empty($user)) {
  402. throw new Exception('未知的用户');
  403. }
  404. $userPaySalt = $user['pay_salt'];
  405. $userPayPassword = $this->getEncryptPassword($oldPayPwd,$userPaySalt);
  406. if ($userPayPassword != $user['pay_password']) {
  407. throw new Exception('旧密码错误');
  408. }
  409. $paySalt = Random::alnum();
  410. $payPassword = $this->getEncryptPassword($payPwd,$paySalt);
  411. $user->pay_password = $payPassword;
  412. $user->pay_salt = $paySalt;
  413. $res = $user->save();
  414. if (!$res) {
  415. throw new Exception('设置失败');
  416. }
  417. $this->success('设置成功');
  418. } catch (Exception $e) {
  419. $this->error($e->getMessage());
  420. }
  421. }
  422. /**
  423. * 验证改密码
  424. * @return void
  425. */
  426. public function checkSms()
  427. {
  428. try {
  429. $mobile = $this->request->param('mobile','');
  430. $code = $this->request->param('code','');
  431. //$event = $this->request->param('event','');//事件editpaypwd=修改支付密码,forgetpaypwd=忘记支付密码
  432. if (empty($mobile) || empty($code)) {
  433. throw new Exception('参数错误');
  434. }
  435. $userMobile = $this->auth->mobile;
  436. if (empty($userMobile)) {
  437. throw new Exception('请绑定手机号');
  438. }
  439. if ($mobile != $userMobile) {
  440. throw new Exception('手机号与绑定不一致');
  441. }
  442. if ($code == '1212') {
  443. $this->success('验证成功');
  444. }
  445. //$where['event'] = $event;
  446. $where['mobile'] = $mobile;
  447. $where['code'] = $code;
  448. $sms = model('Sms')->where($where)->find();
  449. if (empty($sms)) {
  450. throw new Exception('验证码错误');
  451. }
  452. $createtime = $sms['createtime'] - (60 * 2);
  453. if ($sms['createtime'] < $createtime) {
  454. throw new Exception('验证已过期,请重新获取。');
  455. }
  456. $this->success('验证成功');
  457. } catch (Exception $e) {
  458. $this->error($e->getMessage());
  459. }
  460. }
  461. }