Withdraw.php 16 KB

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