User.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\model\Address;
  4. use app\api\model\Cashwechat;
  5. use app\api\model\Foot;
  6. use app\api\model\Realname;
  7. use app\api\model\Record;
  8. use app\api\model\UsersModel;
  9. use app\common\controller\Api;
  10. use app\common\library\Ems;
  11. use app\common\library\Sms;
  12. use fast\Random;
  13. use think\Cache;
  14. use think\Db;
  15. use think\Validate;
  16. /**
  17. * 会员接口
  18. */
  19. class User extends Api
  20. {
  21. protected $noNeedLogin = ['*'];
  22. protected $noNeedRight = '*';
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. }
  27. /**
  28. * 用户显示
  29. * @ApiMethod (POST)
  30. * @param string $user_id 用户id
  31. */
  32. public function userInfo()
  33. {
  34. $user_id = $this->request->post('user_id');
  35. if (!isset($user_id)) {
  36. return $this->result('网络错误', [], 100);
  37. }
  38. $data = UsersModel::where('user_id', $user_id)->find();
  39. $data['daifahuo'] = 1;
  40. $data['daishouhuo'] = 1;
  41. $data['tuikuan'] = 1;
  42. $data['daifukuan'] = 1;
  43. if ($data) {
  44. return $this->result('', $data, 200);
  45. } else {
  46. return $this->result('未获取用户信息', [], 100);
  47. }
  48. }
  49. /**
  50. * 修改昵称
  51. * @ApiMethod (POST)
  52. * @param string $user_id 用户id
  53. * @param string $user_nickname 用户昵称
  54. */
  55. public function updNickName()
  56. {
  57. $params = $this->request->post();
  58. if (!isset($params['user_id']) || !isset($params['user_nickname'])) {
  59. return $this->result('网络错误', [], 100);
  60. }
  61. $data['user_nickname'] = $params['user_nickname'];
  62. $data['user_id'] = $params['user_id'];
  63. $selNickName = UsersModel::where($data)->find();
  64. if ($selNickName) {
  65. return $this->result('请输入新昵称', [], 100);
  66. }
  67. $userModel = new UsersModel();
  68. $updNickName = $userModel->allowField(true)->save($data, ['user_id' => $params['user_id']]);
  69. if ($updNickName) {
  70. return $this->result('修改成功', [], 200);
  71. } else {
  72. return $this->result('修改失败', [], 100);
  73. }
  74. }
  75. /**
  76. * 修改密码
  77. * @ApiMethod (POST)
  78. * @param string $user_id 用户id
  79. * @param string $user_pwd 用户密码
  80. * @param string $user_qrpwd 重复 密码
  81. */
  82. public function updPwd()
  83. {
  84. $params = $this->request->post();
  85. $rules = [
  86. 'user_id' => 'require|number',
  87. 'user_pwd' => 'require|max:18|min:6',
  88. 'user_qrpwd' => 'require',
  89. ];
  90. $msg = [
  91. 'user_id.require' => '用户id不能为空',
  92. 'user_id.number' => '用户i为整形',
  93. 'user_pwd.require' => '密码不能为空',
  94. 'user_pwd.max' => '密码最大18位',
  95. 'user_pwd.min' => '密码最小6位',
  96. 'user_qrpwd.require' => '重复密码不能为空',
  97. ];
  98. $validata = $this->validate($params, $rules, $msg);
  99. if (is_string($validata)) {
  100. return $this->result($validata, [], 100);
  101. }
  102. if ($params['user_pwd'] != $params['user_qrpwd']) {
  103. return $this->result('两次密码输入不一致', [], 100);
  104. }
  105. $data = array(
  106. 'user_id' => $params['user_id'],
  107. 'user_pwd' => sha1(md5($params['user_pwd'])),
  108. );
  109. $model = new UsersModel();
  110. $updPwd = $model->allowField(true)->save($data, ['user_id' => $data['user_id']]);
  111. if ($updPwd) {
  112. return $this->result('修改成功', [], 200);
  113. } else {
  114. return $this->result('修改失败', [], 100);
  115. }
  116. }
  117. /**
  118. * 修改支付密码
  119. * @ApiMethod (POST)
  120. * @param string $user_id 用户id
  121. * @param string $user_tel 手机号
  122. * @param string $code 验证码
  123. * @param string $user_paypwd 支付密码
  124. * @param string $user_qrpaypwd 确认支付密码
  125. */
  126. public function updPayPwd()
  127. {
  128. $params = $this->request->post();
  129. $rules = [
  130. 'user_tel' => "require|number",
  131. 'user_id' => "require|number",
  132. 'code' => "require",
  133. 'user_paypwd' => "require|number",
  134. 'user_qrpaypwd' => "require",
  135. ];
  136. $msg = [
  137. 'user_tel.require' => '手机号不能为空',
  138. 'user_id.require' => '网络错误',
  139. 'user_code.require' => '验证码不能为空',
  140. 'user_paypwd.require' => '支付密码不能为空',
  141. 'user_qrpaypwd.require' => '支付密码不能为空',
  142. 'user_paypwd.number' => '支付密码只支持数字',
  143. 'user_code.number' => '验证码只支持数字',
  144. 'user_id.number' => '网络错误',
  145. 'user_tel.number' => '手机号不合法',
  146. ];
  147. $validata = $this->validate($params, $rules, $msg);
  148. if (is_string($validata)) {
  149. return $this->result($validata, [], 100);
  150. }
  151. if ($params['user_paypwd'] != $params['user_qrpaypwd']) {
  152. return $this->result('两次密码输入不一致', [], 100);
  153. }
  154. $code = Cache::get($params['code']);
  155. if (!$code) {
  156. return $this->result('验证码错误', [], 100); // 验证验证码
  157. }
  158. $data = array(
  159. 'user_id' => $params['user_id'],
  160. 'user_paypwd' => $params['user_paypwd'],
  161. );
  162. $user = new UsersModel();
  163. $validataTel = $user->where('user_id', $data['user_id'])->where('user_tel', $params['user_tel'])->find();
  164. if (!$validataTel) {
  165. return $this->result('请使用此账号手机号修改密码', [], 100);
  166. }
  167. $validataPayPwd = $user->where($data)->find();
  168. if ($validataPayPwd) {
  169. return $this->result('请输入新密码', [], 100);
  170. }
  171. $updPayPwd = $user->allowField(true)->save($data, ['user_id' => $data['user_id']]);
  172. if ($updPayPwd) {
  173. Cache::rm($params['code']); // 删除缓存验证码
  174. return $this->result('修改成功', [], 200);
  175. } else {
  176. return $this->result('修改失败', [], 100);
  177. }
  178. }
  179. /**
  180. * 实名认证
  181. *
  182. * @ApiMethod (POST)
  183. * @param string $user_id 用户id
  184. * @param string $user_nickname 姓名
  185. * @param string $idcard 身份证号
  186. */
  187. public function realName()
  188. {
  189. $params = $this->request->post();
  190. $rules = [
  191. 'user_id' => 'require|number',
  192. 'user_nickname' => 'require',
  193. 'idcard' => 'require'
  194. ];
  195. $msg = [
  196. 'user_id.require' => '网络错误',
  197. 'user_nickname.require' => '姓名不能为空',
  198. 'idcard.require' => '身份证号不能为空',
  199. 'user_id.number' => '网络错误',
  200. ];
  201. $validata = $this->validate($params, $rules, $msg);
  202. if (is_string($validata)) {
  203. return $this->result($validata, [], 100);
  204. }
  205. $is_name = UsersModel::where('user_id', $params['user_id'])->where('is_realname', 1)->find();
  206. if ($is_name) {
  207. return $this->result('您已经实名认证过了', [], 100);
  208. }
  209. $realName = new Realname();
  210. $res = $realName->realName($params);
  211. return $res;
  212. }
  213. /**
  214. * 常见问题
  215. * @ApiMethod (POST)
  216. */
  217. public function question()
  218. {
  219. $data = Db::name('question')->find();
  220. if ($data) {
  221. return $this->result('', $data, 200);
  222. } else {
  223. return $this->result('', $data, 100);
  224. }
  225. }
  226. /**
  227. * 我的团队
  228. * @ApiMethod (POST)
  229. * @param string $user_id 用户id
  230. */
  231. public function team()
  232. {
  233. $user_id = $this->request->post('user_id');
  234. if (!$user_id) {
  235. return $this->result('网络错误', [], 100);
  236. }
  237. $user = UsersModel::where('user_id', $user_id)->find();
  238. if (!$user) {
  239. return $this->result('网络错误', [], 100);
  240. }
  241. $data['syj'] = UsersModel::where('user_tel', $user['user_tjtel'])->find();
  242. if (!$data['syj']) {
  243. $data['syj'] = [];
  244. }
  245. $data['team'] = UsersModel::where('user_tjtel', $user['user_tel'])->select();
  246. if (!$data['team']) {
  247. $data['team'] = [];
  248. }
  249. return $this->result('', $data, 200);
  250. }
  251. /**
  252. * 我的地址
  253. * @ApiMethod (POST)
  254. * @param string $user_id 用户id
  255. */
  256. public function address()
  257. {
  258. $user_id = $this->request->post('user_id');
  259. if (!isset($user_id)) {
  260. return $this->result('网络错误', [], 100);
  261. }
  262. $data = Address::where('user_id', $user_id)->order('is_default desc')->select();
  263. if ($data) {
  264. return $this->result('', $data, 200);
  265. } else {
  266. return $this->result('暂无数据', [], 100);
  267. }
  268. }
  269. /**
  270. * 地址添加
  271. * @ApiMethod (POST)
  272. * @param string $user_id 用户id
  273. * @param string $a_name 姓名
  274. * @param string $a_tel 手机号
  275. * @param string $city 城市
  276. * @param string $area 详细地址
  277. * @param string $city_province 省的地标
  278. * @param string $city_city 市的地标
  279. * @param string $city_area 区的地标
  280. * @param string $is_default 1默认0不默认
  281. */
  282. public function addressAdd()
  283. {
  284. $params = $this->request->post();
  285. $rules = [
  286. 'user_id' => 'require|number',
  287. 'a_name' => 'require',
  288. 'city' => 'require',
  289. 'area' => 'require',
  290. 'is_default' => 'require|number|max:1',
  291. ];
  292. $msg = [
  293. 'user_id.require' => '网络错误',
  294. 'a_name.require' => '姓名不能为空',
  295. 'city.require' => '地区不能为空',
  296. 'area.require' => '详细地址不能为空',
  297. 'is_default.require' => '网络错误',
  298. 'is_default.number' => '网络错误',
  299. 'is_default.max' => '网络错误',
  300. 'user_id.number' => '网络错误',
  301. ];
  302. $validata = $this->validate($params, $rules, $msg);
  303. if (is_string($validata)) {
  304. return $this->result($validata, [], 100);
  305. }
  306. if (isset($params['a_tel'])) {
  307. $check = '/^(1(([35789][0-9])|(47)))\d{8}$/';
  308. if (!preg_match($check, $params['a_tel'])) {
  309. return $this->result('手机号不合法', [], 100);
  310. }
  311. } else {
  312. return $this->result('手机号不能为空', [], 100);
  313. }
  314. if ($params['is_default'] == 1) {
  315. // 如果添加一个默认地址德华,先把所有地址改为非默认
  316. $updallstate = Db::name('address')->where('user_id', $params['user_id'])->update(['is_default' => 0]);
  317. }
  318. $model = new Address();
  319. $addressAdd = $model->allowField(true)->save($params);
  320. if ($addressAdd) {
  321. return $this->result('添加成功', '', 200);
  322. } else {
  323. return $this->result('添加失败', '', 100);
  324. }
  325. }
  326. /**
  327. * 地址修改
  328. * @ApiMethod (POST)
  329. * @param string $a_id 地址id
  330. * @param string $user_id 用户id
  331. * @param string $a_name 姓名
  332. * @param string $a_tel 手机号
  333. * @param string $city 城市
  334. * @param string $city_province 省的地标
  335. * @param string $city_city 市的地标
  336. * @param string $city_area 区的地标
  337. * @param string $area 详细地址
  338. */
  339. public function addressUpd()
  340. {
  341. $params = $this->request->post();
  342. $count = count($params);
  343. if ($count < 2) {
  344. return $this->result('请至少修改一项', [], 100);
  345. }
  346. if (!isset($params['a_id'])) {
  347. return $this->result('网络错误', [], 100);
  348. }
  349. if (isset($params['is_default']) && $params['is_default'] == 1) {
  350. // 如果添加一个默认地址德华,先把所有地址改为非默认
  351. $updallstate = Db::name('address')->where('user_id', $params['user_id'])->update(['is_default' => 0]);
  352. }
  353. $model = new Address();
  354. $addressUpd = $model->allowField(true)->save($params, ['a_id' => $params['a_id']]);
  355. if ($addressUpd) {
  356. return $this->result('修改成功', '', 200);
  357. } else {
  358. return $this->result('修改失败', '', 100);
  359. }
  360. }
  361. /**
  362. * 地址改为默认
  363. * @ApiMethod (POST)
  364. * @param string $a_id 地址id
  365. * @param string $user_id 用户id
  366. * @param string $is_default 0否1是
  367. */
  368. public function is_default()
  369. {
  370. $params = $this->request->post();
  371. if (!isset($params['a_id']) || !isset($params['is_default'])) {
  372. return $this->result('网络错误', '', 100);
  373. }
  374. // 如果要改为默认,判断是否存在默认了
  375. if ($params['is_default'] == 1) {
  376. $where = array(
  377. 'user_id' => $params['user_id'],
  378. 'is_default' => 1
  379. );
  380. // 先把所有的都地址改为非默认
  381. $updallstate = Db::name('address')->where('user_id', $params['user_id'])->update(['is_default' => 0]);
  382. //在把选中的地址改为默认
  383. $addressUpd = Db::name('address')->where('a_id', $params['a_id'])->update(['is_default' => 1]);
  384. if ($addressUpd) {
  385. return $this->result('修改成功', '', 200);
  386. } else {
  387. return $this->result('修改失败', '', 100);
  388. }
  389. } //改为取消默认
  390. else {
  391. $addressUpd = Db::name('address')->where('a_id', $params['a_id'])->update(['is_default' => 0]);
  392. if ($addressUpd) {
  393. return $this->result('修改成功', '', 200);
  394. } else {
  395. return $this->result('修改失败', '', 100);
  396. }
  397. }
  398. }
  399. /**
  400. * 地址删除
  401. * @ApiMethod (POST)
  402. * @param string $a_id 地址id
  403. */
  404. public function addressDel()
  405. {
  406. $a_id = $this->request->post('a_id');
  407. if (!isset($a_id)) {
  408. return $this->result('网络错误', [], 100);
  409. }
  410. $model = new Address();
  411. $addressDel = $model->where('a_id', $a_id)->delete();
  412. if ($addressDel) {
  413. return $this->result('修改成功', '', 200);
  414. } else {
  415. return $this->result('修改失败', '', 100);
  416. }
  417. }
  418. /**
  419. * 我的客服
  420. * @ApiMethod (POST)
  421. */
  422. public function coursetomer()
  423. {
  424. $data = Db::name('coustomer')->select();
  425. if ($data) {
  426. return $this->result('', $data, 200);
  427. } else {
  428. return $this->result('暂无数据', [], 100);
  429. }
  430. }
  431. /**
  432. * 我的红豆
  433. * @ApiMethod (POST)
  434. * @param string $user_id 用户id
  435. */
  436. public function redbean()
  437. {
  438. $user_id = $this->request->post('user_id');
  439. if (!isset($user_id)) {
  440. return $this->result('网络错误', [], 100);
  441. }
  442. $redbean = UsersModel::where('user_id', $user_id)->field('user_id, user_redbean')->find();
  443. if ($redbean) {
  444. return $this->result('', $redbean, 200);
  445. } else {
  446. return $this->result('网络错误', [], 100);
  447. }
  448. }
  449. /**
  450. * 红豆兑换
  451. * @ApiMethod (POST)
  452. * @param string $user_id 用户id
  453. * @param string $user_redbean 兑换个数
  454. */
  455. public function useRedBean()
  456. {
  457. $params = $this->request->post();
  458. if (!isset($params['user_id'])) {
  459. return $this->result('网络错误', [], 100);
  460. }
  461. if (!isset($params['user_redbean'])) {
  462. return $this->result('网络错误', [], 100);
  463. }
  464. $redbean = UsersModel::where('user_id', $params['user_id'])->field('user_id, user_redbean')->find();
  465. if ($params['user_redbean'] > $redbean['user_redbean'] || $params['user_redbean'] < 1) {
  466. return $this->result('提现个数错误', [], 100);
  467. }
  468. Db::startTrans();
  469. try {
  470. $upd = UsersModel::where('user_id', $params['user_id'])->setDec('user_redbean', $params['user(redbean']);
  471. Db::commit();
  472. } catch (\Exception $e) {
  473. Db::rollback();
  474. $data = array(
  475. 'user_id' => $params['user_id'],
  476. 't_type' => 1,
  477. 'state' => 2,
  478. 'number' => '-' . $params['user_id'],
  479. 'create_time' => date("Y-m-d H:i:s", time()),
  480. );
  481. $record = new Record();
  482. $recordadd = $record->allowField(true)->save($data);
  483. if ($recordadd) {
  484. return $this->result('兑换失败', '', 100);
  485. } else {
  486. return $this->result('网络错误', '', 100);
  487. }
  488. }
  489. if ($upd) {
  490. $data = array(
  491. 'user_id' => $params['user_id'],
  492. 't_type' => 1,
  493. 'state' => 1,
  494. 'number' => '-' . $params['user_id'],
  495. 'create_time' => date("Y-m-d H:i:s", time()),
  496. );
  497. $record = new Record();
  498. $recordadd = $record->allowField(true)->save($data);
  499. if ($recordadd) {
  500. return $this->result('兑换成功', '', 200);
  501. } else {
  502. return $this->result('网络错误', '', 100);
  503. }
  504. } else {
  505. return $this->result('网络错误', '', 100);
  506. }
  507. }
  508. /**
  509. * 我的余额提现记录
  510. * @ApiMethod (POST)
  511. * @param string $user_id 用户id
  512. */
  513. public function moneyRecord()
  514. {
  515. $params = $this->request->post();
  516. if (!isset($params['user_id'])) {
  517. return $this->result('网络错误', [], 100);
  518. }
  519. $data =Db::name('cash_log')->where('user_id', $params['user_id'])->order('t_id desc')->select();
  520. if ($data) {
  521. return $this->result('', $data, 200);
  522. } else {
  523. return $this->result('暂无数据', [], 100);
  524. }
  525. }
  526. /**
  527. * 我的红豆兑换记录
  528. * @ApiMethod (POST)
  529. * @param string $user_id 用户id
  530. */
  531. public function redbeanRecord()
  532. {
  533. $params = $this->request->post();
  534. if (!isset($params['user_id'])) {
  535. return $this->result('网络错误', [], 100);
  536. }
  537. $data = Record::where('user_id', $params['user_id'])->where('t_type',1)->order('r_id desc')->select();
  538. if ($data) {
  539. return $this->result('', $data, 200);
  540. } else {
  541. return $this->result('暂无数据', [], 100);
  542. }
  543. }
  544. /**
  545. * 我的足迹显示
  546. * @ApiMethod (POST)
  547. * @param string $user_id 用户id
  548. */
  549. public function footIndex()
  550. {
  551. $params = $this->request->post();
  552. if (!isset($params['user_id'])) {
  553. return $this->result('网络错误', [], 100);
  554. }
  555. $data = Foot::with('commodity')
  556. ->where('user_id', $params['user_id'])
  557. ->select();
  558. if ($data) {
  559. return $this->result('', $data, 200);
  560. } else {
  561. return $this->result('暂无数据', '', 100);
  562. }
  563. }
  564. /**
  565. * 我的足迹删除
  566. * @ApiMethod (POST)
  567. * @param string $f_id 足迹id
  568. */
  569. public function footDel()
  570. {
  571. $params = $this->request->post();
  572. if (!isset($params['f_id'])) {
  573. return $this->result('网络错误', [], 100);
  574. }
  575. $footDel = Foot::where('f_id', $params['f_id'])->delete();
  576. if ($footDel) {
  577. return $this->result('删除成功', [], 200);
  578. } else {
  579. return $this->result('删除失败', [], 100);
  580. }
  581. }
  582. /**
  583. * 会员提现价格显示
  584. * @ApiMethod (POST)
  585. */
  586. public function cashMoney()
  587. {
  588. $data = Db::name('cash_money')->select();
  589. return $this->result('', $data, 200);
  590. }
  591. /**
  592. * 会员提现到第几次
  593. * @ApiMethod (POST)
  594. * @param string $user_id 用户id
  595. */
  596. public function cashIndex()
  597. {
  598. $params = $this->request->post();
  599. if (!isset($params['user_id'])) {
  600. return $this->result('网络错误', [], 100);
  601. }
  602. $data = UsersModel::Where('user_id', $params['user_id'])->field('user_id,cash_level')->find();
  603. if ($data) {
  604. return $this->result('', $data, 200);
  605. } else {
  606. return $this->result('网络错误', '', 100);
  607. }
  608. }
  609. /**
  610. * 会员提现
  611. * @ApiMethod (POST)
  612. * @param string $user_id 用户id
  613. * @param string $money 金额
  614. */
  615. public function cash()
  616. {
  617. $params = $this->request->post();
  618. $rules = [
  619. 'user_id' => 'require|number',
  620. 'money' => 'require|number',
  621. ];
  622. $msg = [
  623. 'user_id.require' => '网络错误',
  624. 'money.require' => '请选择提现金额',
  625. 'user_id.number' => '网络错误',
  626. 'money.number' => '网络错误',
  627. ];
  628. $validata = $this->validate($params, $rules, $msg);
  629. if (is_string($validata)) {
  630. return $this->result($validata, [], 100);
  631. }
  632. // 正则验证金额为正整数
  633. if (!preg_match("/^[1-9][0-9]*$/", $params['money'])) {
  634. return $this->result('请输入正整数', [], 100);
  635. }
  636. //检测提现金额表中是否有这个金额
  637. $is_money = Db::name('cash_money')->where('money', $params['money'])->find();
  638. // if (!$is_money) {
  639. // return $this->result('该金额暂不支持提现', [], 100);
  640. // }
  641. //判断是否该提现这个次数了
  642. $user_cash_level = UsersModel::where('user_id', $params['user_id'])->find();
  643. // if ($user_cash_level) {
  644. // if ($user_cash_level['cash_level'] + 1 != 9) { //最后一个不算
  645. // if ($is_money['c_id'] != $user_cash_level['cash_level'] + 1) {
  646. // return $this->result('请按照顺序提现', [], 100);
  647. // }
  648. // }
  649. // }
  650. // 判断余额
  651. if ($user_cash_level['user_money'] < $params['money']) {
  652. return $this->result('余额不足', [], 100);
  653. }
  654. // 判断是否微信网页受过权
  655. if ($user_cash_level['user_openid'] == '') {
  656. return $this->result('请先微信网页授权在提现', [], 10);
  657. }
  658. // 开启事务
  659. Db::startTrans();
  660. try {
  661. // 减少用户余额
  662. $updMoney = UsersModel::where('user_id', $params['user_id'])->setDec('user_money', $params['money']);
  663. // 关闭事务
  664. Db::commit();
  665. } catch (\Exception $e) {
  666. // 回滚
  667. Db::rollback();
  668. return $this->result('网络错误,提现失败', [], 100);
  669. }
  670. if (!$updMoney) {
  671. return $this->result('网络错误', [], 100);
  672. }
  673. // 生成订单号
  674. $number = rand(100, 999) . time();
  675. $data = [
  676. 'user_id' => $params['user_id'],
  677. 'money' => $params['money'],
  678. 'state' => 1,
  679. 'create_time' => date('Y-m-d H:i:s', time()),
  680. 'number' => $number,
  681. ];
  682. // 添加提现记录
  683. $add_log = Db::name('cash_log')->insert($data);
  684. if (!$add_log) {
  685. return $this->result('网络错误', [], 100);
  686. }
  687. // 实例化企业付款到零钱类
  688. $cashWeChat = new Cashwechat();
  689. // 调用企业付款接口接口
  690. $res = $cashWeChat->sendMoney($params['money'], $user_cash_level['user_openid'], '余额提现', $user_cash_level['user_nickname'], $number);
  691. return $res;
  692. }
  693. /**
  694. * 隐私协议
  695. * @ApiMethod (POST)
  696. */
  697. public function privacy()
  698. {
  699. $data = Db::name('privacy')->find();
  700. return json(['code' => 200, 'msg' => '', 'data' => $data]);
  701. }
  702. /**
  703. * 邀请二维码
  704. * @ApiMethod (POST)
  705. */
  706. public function weweima()
  707. {
  708. $url = "http://file02.16sucai.com/d/file/2015/0128/8b0f093a8edea9f7e7458406f19098af.jpg";
  709. return json(['code' => 200, 'msg' => '', 'data' => $url]);
  710. }
  711. }