Withdraw.php 17 KB

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