Withdraw.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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 = "id,u_id,avatar,nickname,mobile";
  146. $userInfo = Db::name('user')->where('mobile',$mobile)->field($field)->find();
  147. //用户钱包
  148. $userwallet = Db::name('user_wallet')->where('user_id',$userInfo['id'])->find();
  149. $userInfo['money'] = $userwallet['money'];
  150. $this->success("获取成功!",$userInfo);
  151. }
  152. /**
  153. * 验证身份证号
  154. */
  155. public function validateCard() {
  156. $idcard = $this->request->request('idcard');// 身份证号
  157. if(!$idcard) $this->error("参数缺失!");
  158. // 获取用户信息
  159. $idcardInfo = \app\common\model\UserAuth::where(["user_id"=>$this->auth->id])->value("idcard");
  160. if($idcardInfo === $idcard) {
  161. $this->success("验证成功!");
  162. } else {
  163. $this->error("验证失败!");
  164. }
  165. }
  166. /**
  167. * 绑定银行卡
  168. */
  169. public function bindBank() {
  170. try {
  171. //$realname = $this->request->request('realname');// 真实姓名
  172. $bank_no = $this->request->request('bank_no');// 银行账号
  173. //$idCard = $this->request->request('id_card');// 身份证号
  174. /*$bank_name = $this->request->request('bank_name');// 银行名称
  175. $open_address = $this->request->request('open_address');// 开户地*/
  176. $open_bank = $this->request->request('open_bank');// 开户行
  177. /*$bank_mobile = $this->request->request('bank_mobile');// 银行预留手机号
  178. $captcha = $this->request->request('captcha'); // 验证码
  179. $mobile = $this->request->request('mobile'); // 手机号*/
  180. $userId = $this->auth->id;
  181. //|| !$bank_name || !$open_address || !$bank_mobile || !$captcha || !$mobile
  182. if(!$bank_no || !$open_bank ) {
  183. throw new Exception("请将信息填写完整");
  184. }
  185. $userAuthWhere['user_id'] = $userId;
  186. $userAuth = model('UserAuth')->where($userAuthWhere)->find();
  187. if (empty($userAuth)) {
  188. throw new Exception('请先实名认证');
  189. }
  190. if ($userAuth['status'] != 1) {
  191. throw new Exception('请先实名认证通过');
  192. }
  193. $realname = $userAuth['realname'];
  194. $idCard = $userAuth['idcard'];
  195. $userService = new UserService();
  196. $aliParams = [
  197. 'bank_no' => $bank_no,
  198. 'id_card' => $idCard,
  199. 'real_name' => $realname,
  200. ];
  201. $aliBankCheckRes = $userService->bankCheck($aliParams);
  202. if (!$aliBankCheckRes['status']) {
  203. throw new Exception($aliBankCheckRes['msg']);
  204. }
  205. // 获取用户信息
  206. //$userInfo = \app\common\model\User::where(["id"=>$this->auth->id])->find();
  207. /*if($userInfo->mobile !== $mobile) $this->error("请输入账号绑定的手机号码!");
  208. if (!Sms::check($mobile, $captcha, 'binkBank')) {
  209. $this->error(__('验证码不正确!'));
  210. }*/
  211. // 查询是否有过绑定
  212. $bankInfo = \app\common\model\UserBank::where(["user_id"=>$userId])->find();
  213. $data = [];
  214. $data["realname"] = $realname;
  215. $data["bank_no"] = $bank_no;
  216. $data["open_bank"] = $open_bank;
  217. $data["id_card"] = $idCard;
  218. /*$data["bank_name"] = $bank_name;
  219. $data["open_address"] = $open_address;
  220. $data["mobile"] = $bank_mobile;*/
  221. if($bankInfo) {
  222. $data["updatetime"] = time();
  223. $res = \app\common\model\UserBank::update($data,["user_id"=>$userId]);
  224. } else {
  225. $data["user_id"] = $userId;
  226. $data["createtime"] = time();
  227. $res = \app\common\model\UserBank::insert($data);
  228. }
  229. if(!$res) {
  230. throw new Exception("网络异常,请稍后重试!");
  231. }
  232. $this->success("银行卡绑定成功!");
  233. } catch (Exception $e) {
  234. $this->error($e->getMessage());
  235. }
  236. }
  237. /**
  238. * 获取绑定银行卡信息
  239. */
  240. public function getBankInfo() {
  241. // 查询是否有过绑定
  242. $bankInfo = \app\common\model\UserBank::where(["user_id"=>$this->auth->id])->find();
  243. //if(!$bankInfo) $this->error("银行卡信息获取失败!");
  244. $this->success("获取成功!",$bankInfo);
  245. }
  246. /**
  247. * 绑定支付宝
  248. */
  249. public function bindAlipay() {
  250. //$realname = $this->request->request('realname');//真实姓名
  251. $payNo = $this->request->request('pay_no');//支付宝账号
  252. //$cardNo = $this->request->request('card_no');//身份证号
  253. //$mobile = $this->request->request('mobile'); //手机号
  254. //$captcha = $this->request->request('captcha'); //验证码
  255. $userId = $this->auth->id;
  256. //姓名和身份证号 取实名认证
  257. $userAuthWhere['user_id'] = $userId;
  258. $userAuth = model('UserAuth')->where($userAuthWhere)->find();
  259. if (empty($userAuth)) {
  260. $this->error('请先实名认证');
  261. }
  262. if ($userAuth['status'] != 1) {
  263. $this->error('请先实名认证通过');
  264. }
  265. $realname = $userAuth['realname'];
  266. $cardNo = $userAuth['idcard'];
  267. if(!$realname || !$payNo || !$cardNo) {
  268. $this->error("请将信息填写完整");
  269. }
  270. // 获取用户信息
  271. //$userInfo = \app\common\model\User::where(["id"=>$this->auth->id])->find();
  272. /*if (!Sms::check($mobile, $captcha, 'binkAli')) {
  273. $this->error(__('验证码不正确!'));
  274. }*/
  275. $userAlipayModel = new UserAlipay();
  276. // 查询是否有过绑定
  277. $bankInfo = $userAlipayModel->where(["user_id"=>$userId])->find();
  278. $data = [];
  279. $data["realname"] = $realname;
  280. $data["pay_no"] = $payNo;
  281. $data["card_no"] = $cardNo;
  282. if($bankInfo) {
  283. $data["updatetime"] = time();
  284. $res = $userAlipayModel->update($data,["user_id"=>$userId]);
  285. } else {
  286. $data["user_id"] = $userId;
  287. $data["createtime"] = time();
  288. $res = $userAlipayModel->insert($data);
  289. }
  290. if($res) {
  291. //Sms::flush($mobile, 'binkBank');
  292. $this->success("支付宝绑定成功!");
  293. } else {
  294. $this->error("网络异常,请稍后重试!");
  295. }
  296. }
  297. /**
  298. * 获取绑定银行卡信息
  299. */
  300. public function getAlipayInfo() {
  301. // 查询是否有过绑定
  302. $alipayInfo = UserAlipay::where(["user_id"=>$this->auth->id])->find();
  303. //if(!$alipayInfo) $this->error("支付宝信息获取失败!");
  304. $this->success("获取成功!",$alipayInfo);
  305. }
  306. /**
  307. * 获取用户账户信息
  308. */
  309. public function getUserAccountInfo() {
  310. // 获取用户信息
  311. $res = \app\common\model\User::field("id,u_id,avatar,mobile,nickname")->where(["id"=>$this->auth->id])->find();
  312. //用户钱包
  313. $userwallet = Db::name('user_wallet')->where('user_id',$this->auth->id)->find();
  314. $res['use_money'] = $userwallet['money'];
  315. $res['all_money'] = $userwallet['money'];
  316. // 获取用户实名认证信息
  317. $res["realname"] = \app\common\model\UserAuth::where(["user_id"=>$res["id"]])->value("realname");
  318. // 获取用户银行卡信息
  319. $res["bankInfo"] = \app\common\model\UserBank::where(["user_id"=>$res["id"]])->find();
  320. $this->success("获取成功!",$res);
  321. }
  322. /**
  323. * 手机验证码获取用户信息
  324. *
  325. * @param string $mobile 手机号
  326. * @param string $captcha 验证码
  327. */
  328. public function mobilelogin()
  329. {
  330. $mobile = $this->request->request('mobile');
  331. $captcha = $this->request->request('captcha');
  332. if (!$mobile || !$captcha) {
  333. $this->error(__('Invalid parameters'));
  334. }
  335. if (!Validate::regex($mobile, "^1\d{10}$")) {
  336. $this->error(__('Mobile is incorrect'));
  337. }
  338. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  339. $this->error(__('Captcha is incorrect'));
  340. }
  341. $user = \app\common\model\User::getByMobile($mobile,"u_id,is_auth");
  342. if ($user) {
  343. Sms::flush($mobile, 'mobilelogin');
  344. $this->success("获取成功!",$user);
  345. } else {
  346. $this->error("没有查询到用户信息!请前往app注册!");
  347. }
  348. }
  349. public function getEncryptPassword($password, $salt = '')
  350. {
  351. return md5(md5($password) . $salt);
  352. }
  353. /**
  354. * 设置交易密码
  355. * @return void
  356. */
  357. public function setPaypwd()
  358. {
  359. try {
  360. $userId = $this->auth->id;
  361. $payPwd = $this->request->param('pay_pwd','');
  362. $confirmPwd = $this->request->param('confirm_pay_pwd','');
  363. if (empty($payPwd) || empty($confirmPwd)) {
  364. throw new Exception('参数错误');
  365. }
  366. if ($payPwd != $confirmPwd) {
  367. throw new Exception('密码不一致');
  368. }
  369. $paySalt = Random::alnum();
  370. $payPassword = $this->getEncryptPassword($payPwd,$paySalt);
  371. $where['id'] = $userId;
  372. $user = model('User')->where($where)->find();
  373. if (empty($user)) {
  374. throw new Exception('未知的用户');
  375. }
  376. $user->pay_password = $payPassword;
  377. $user->pay_salt = $paySalt;
  378. $res = $user->save();
  379. if (!$res) {
  380. throw new Exception('设置失败');
  381. }
  382. $this->success('设置成功');
  383. } catch (Exception $e) {
  384. $this->error($e->getMessage());
  385. }
  386. }
  387. /**
  388. * 修改交易密码
  389. * @return void
  390. */
  391. public function editPaypwd()
  392. {
  393. try {
  394. $userId = $this->auth->id;
  395. $oldPayPwd = $this->request->param('old_pay_pwd','');
  396. $payPwd = $this->request->param('pay_pwd','');
  397. $confirmPwd = $this->request->param('confirm_pay_pwd','');
  398. if (empty($oldPayPwd) || empty($payPwd) || empty($confirmPwd)) {
  399. throw new Exception('参数错误');
  400. }
  401. if ($payPwd != $confirmPwd) {
  402. throw new Exception('密码不一致');
  403. }
  404. $where['id'] = $userId;
  405. $user = model('User')->where($where)->find();
  406. if (empty($user)) {
  407. throw new Exception('未知的用户');
  408. }
  409. $userPaySalt = $user['pay_salt'];
  410. $userPayPassword = $this->getEncryptPassword($oldPayPwd,$userPaySalt);
  411. if ($userPayPassword != $user['pay_password']) {
  412. throw new Exception('旧密码错误');
  413. }
  414. $paySalt = Random::alnum();
  415. $payPassword = $this->getEncryptPassword($payPwd,$paySalt);
  416. $user->pay_password = $payPassword;
  417. $user->pay_salt = $paySalt;
  418. $res = $user->save();
  419. if (!$res) {
  420. throw new Exception('设置失败');
  421. }
  422. $this->success('设置成功');
  423. } catch (Exception $e) {
  424. $this->error($e->getMessage());
  425. }
  426. }
  427. /**
  428. * 验证改密码
  429. * @return void
  430. */
  431. public function checkSms()
  432. {
  433. try {
  434. $mobile = $this->request->param('mobile','');
  435. $code = $this->request->param('code','');
  436. //$event = $this->request->param('event','');//事件editpaypwd=修改支付密码,forgetpaypwd=忘记支付密码
  437. if (empty($mobile) || empty($code)) {
  438. throw new Exception('参数错误');
  439. }
  440. $userMobile = $this->auth->mobile;
  441. if (empty($userMobile)) {
  442. throw new Exception('请绑定手机号');
  443. }
  444. if ($mobile != $userMobile) {
  445. throw new Exception('手机号与绑定不一致');
  446. }
  447. if ($code == '1212') {
  448. $this->success('验证成功');
  449. }
  450. //$where['event'] = $event;
  451. $where['mobile'] = $mobile;
  452. $where['code'] = $code;
  453. $sms = model('Sms')->where($where)->find();
  454. if (empty($sms)) {
  455. throw new Exception('验证码错误');
  456. }
  457. $createtime = $sms['createtime'] - (60 * 2);
  458. if ($sms['createtime'] < $createtime) {
  459. throw new Exception('验证已过期,请重新获取。');
  460. }
  461. $this->success('验证成功');
  462. } catch (Exception $e) {
  463. $this->error($e->getMessage());
  464. }
  465. }
  466. }