User.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. <?php
  2. namespace app\api\controller\v1;
  3. use app\api\controller\ApiController;
  4. use app\api\service\SmsCode;
  5. use app\common\model\SystemConfig;
  6. use app\common\model\Users;
  7. use app\common\model\UsersBalanceRecord;
  8. use app\http\IResponse;
  9. use EasyWeChat\Factory;
  10. use Lettered\Support\Upload;
  11. use Lettered\Support\Exceptions\TokenException;
  12. use think\Db;
  13. use think\Exception;
  14. class User extends ApiController
  15. {
  16. /**
  17. * 获取个人中心数据
  18. * 1. 用户昵称、手机号、头像、余额、提现
  19. *
  20. * @author 许祖兴 < zuxing.xu@lettered.cn>
  21. * @date 2020/6/30 15:46
  22. *
  23. * @return \think\response\Json
  24. * @throws \Lettered\Support\Exceptions\FailedException
  25. */
  26. public function getUserCenter()
  27. {
  28. $user = $this->auth->user();
  29. // 用户判断是不是被删除了,及时退出
  30. if($user){
  31. if ($user['status'] == 0) {
  32. throw new TokenException([
  33. 'errmsg' => 'Unauthorized: 用户冻结!'
  34. ]);
  35. }
  36. unset($user['paycode']);
  37. // 2. 加载已提现金额
  38. $user['withdraw'] = model('common/UsersWithdraw')
  39. ->where([
  40. 'user_id' => $this->auth->user()['id'],
  41. 'status' => 2
  42. ])->sum('amount');
  43. return $this->ApiJson(0,'加载用户数据成功', $user);
  44. }
  45. throw new TokenException([
  46. 'errmsg' => 'Unauthorized:Request token denied!'
  47. ]);
  48. }
  49. /**
  50. * 更新用户信息
  51. *
  52. * @author 许祖兴 < zuxing.xu@lettered.cn>
  53. * @date 2020/7/6 15:33
  54. *
  55. * @return \think\response\Json
  56. * @throws \Lettered\Support\Exceptions\FailedException
  57. */
  58. public function updateUserInfo()
  59. {
  60. // 接收参数
  61. $params = $this->request->param();
  62. // 参数校验
  63. $valid = $this->validate($params, [
  64. 'field|更新字段' => 'require',
  65. 'value|更新内容' => 'require'
  66. ]);
  67. // 错误返回
  68. if (true !== $valid) {
  69. return $this->ApiJson(-1, $valid);
  70. }
  71. // 交易密码要验证手机码
  72. // if ($params['field'] == 'paycode'){
  73. // 验证
  74. $sms = new SmsCode();
  75. // if (!$sms->verify(input('mobile'),input('vercode'))) {
  76. // return $this->ApiJson(-1, "验证码错误!");
  77. // }
  78. // }
  79. // 改
  80. model('common/Users')
  81. ->updateBy($this->auth->user()['id'], [
  82. $params['field'] => $params['value']
  83. ]);
  84. return $this->ApiJson(0, '更新信息成功');
  85. }
  86. /**
  87. * 金额提现
  88. *
  89. * @author 许祖兴 < zuxing.xu@lettered.cn>
  90. * @date 2020/7/8 18:30
  91. *
  92. * @return \think\response\Json
  93. * @throws \Lettered\Support\Exceptions\FailedException
  94. */
  95. public function userWithdraw()
  96. {
  97. // 接收参数
  98. $params = $this->request->param();
  99. // 参数校验
  100. $valid = $this->validate($params, [
  101. 'realname|真实姓名' => 'require',
  102. 'type|收款方式' => 'require',
  103. // 'bank|开户行' => 'requireIf:type,1',
  104. 'account|收款账号' => 'require',
  105. 'amount|提现金额' => 'require',
  106. 'paycode|交易密码' => 'require'
  107. ]);
  108. // 错误返回
  109. if(true !== $valid){
  110. return $this->ApiJson(-1, $valid);
  111. }
  112. // 密码验证
  113. $user = $this->auth->user();
  114. if ($user['paycode'] !== $params['paycode']){
  115. return $this->ApiJson(-1, "交易密码有误,请检查!");
  116. }
  117. // 资金是不是提现之后再扣减还是直接扣减,驳回的时候再返回
  118. // 用户余额限制
  119. if ($user['balance'] <= sys_config('user_withdraw_limit','user')){
  120. return $this->ApiJson(-1, "当前可提现金额不满足!余额:【". $user['balance'] . "】");
  121. }
  122. // 最低限制
  123. if ($params['amount'] <= ($limit = sys_config('user_withdraw_limit','user'))){
  124. // return $this->ApiJson(-1, "申请提现金额不满足最低提现!最低:【" . $limit . "】");
  125. }
  126. // 最高限制
  127. if ($user['balance'] <= $params['amount']){
  128. return $this->ApiJson(-1, "当前可提现金额不满足!余额:【". $user['balance'] . "】");
  129. }
  130. // 写入用户ID
  131. $params['user_id'] = $this->auth->user()['id'];
  132. // 交易单号
  133. $params['draw_no'] = get_order_no();
  134. // p($params, 1);
  135. // 写入数据
  136. $result = false;
  137. Db::startTrans();
  138. try {
  139. $params['status'] = 2;
  140. $ret = model('common/UsersWithdraw')::create($params,true);
  141. // 加载配置
  142. $wechat = sys_config('', 'wechat');
  143. $config = [
  144. // 前面的appid什么的也得保留哦
  145. 'app_id' => $wechat['mini_appid'],
  146. 'mch_id' => $wechat['pay_mch_id'],
  147. 'key' => $wechat['pay_secret_key'],
  148. // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
  149. 'cert_path' => $wechat['cert_path'], // XXX: 绝对路径!!!!
  150. 'key_path' => $wechat['key_path'], // XXX: 绝对路径!!!!
  151. // 'notify_url' => 'https://api.gxrrj.cn/api/v1/wechat/notify',
  152. // 'notify_url' => 'http://rrj.gxnwsoft.com/api/v1/wechat/refundNotify',
  153. // 'sandbox' => true
  154. ];
  155. // 创建应用实例
  156. $app = Factory::payment($config);
  157. $result = $app->transfer->toBalance([
  158. 'partner_trade_no' => $ret['draw_no'], // 商户订单号,需保持唯一性(只能是字母或者数字,不能包含有符号)
  159. 'openid' => $user['open_id'],
  160. 'check_name' => 'FORCE_CHECK', // NO_CHECK:不校验真实姓名, FORCE_CHECK:强校验真实姓名
  161. 're_user_name' => $ret['realname'], // 如果 check_name 设置为FORCE_CHECK,则必填用户真实姓名
  162. 'amount' => $ret['amount'] * 100, // 企业付款金额,单位为分
  163. 'desc' => '用户提现', // 企业付款操作说明信息。必填
  164. ]);
  165. $result = $result['return_code'] == 'SUCCESS' && $result['result_code'] != 'FAIL';
  166. $Users = new Users();
  167. $Users->changeBalance($user['id'], $ret['amount'], '余额提现');
  168. Db::commit();
  169. }
  170. catch(Exception $e) {
  171. Db::rollback();
  172. }
  173. if (!$result){
  174. return $this->ApiJson(-1, '数据异常,请稍后重试');
  175. }
  176. return $this->ApiJson(0, '更新信息成功');
  177. }
  178. /**
  179. *
  180. * @author 许祖兴 < zuxing.xu@lettered.cn>
  181. * @date 2020/7/2 10:50
  182. *
  183. * @return \think\response\Json
  184. * @throws \Lettered\Support\Exceptions\FailedException
  185. */
  186. public function userVerify()
  187. {
  188. // 获取信息
  189. $verify = model('common/UsersVerify')->getBy(['user_id' => $this->auth->user()['id']]);
  190. // 这简单做,get 查状态 post 修改数据
  191. if ($this->request->isPost()){
  192. // 接收参数
  193. $params = $this->request->param();
  194. // 参数校验
  195. $valid = $this->validate($params, [
  196. 'name|真实姓名' => 'require',
  197. 'id_card|身份证号' => 'require',
  198. 'id_card_img|身份证信息' => 'require'
  199. ]);
  200. // 错误返回
  201. if(true !== $valid){
  202. return $this->ApiJson(-1, $valid);
  203. }
  204. // 取得当前用户
  205. $user = $this->auth->user();
  206. if (!$verify) {
  207. // 写入用户ID
  208. $params['user_id'] = $user['id'];
  209. // 写入数据
  210. $ret = model('common/UsersVerify')::create($params,true);
  211. }else {
  212. // 更新数据
  213. $ret = $verify->updateBy($verify['id'], $params);
  214. }
  215. // 提交或者创建,用户这都是待审核状态 1
  216. model('common/Users')->updateBy($user['id'], [
  217. 'is_verify' => 1
  218. ]);
  219. // 消息
  220. if ($ret) {
  221. return $this->ApiJson(0,'身份信息提交成功,请等待审核');
  222. }
  223. return $this->ApiJson(-1,'数据异常,请稍后再试');
  224. }
  225. return $this->ApiJson(0,'获取信息成功', $verify);
  226. }
  227. /**
  228. * 获取用户资金记录
  229. * 资金记录表和资产记录表应该可以合并为一张表,然后字段区分
  230. *
  231. * @author 许祖兴 < zuxing.xu@lettered.cn>
  232. * @date 2020/7/9 11:39
  233. *
  234. * @return \think\response\Json
  235. * @throws \Lettered\Support\Exceptions\FailedException
  236. * @throws \think\db\exception\DataNotFoundException
  237. * @throws \think\db\exception\ModelNotFoundException
  238. * @throws \think\exception\DbException
  239. */
  240. public function getUserBalance()
  241. {
  242. $param = $this->request->param();
  243. $limit = 10;
  244. // 数据校验
  245. $valid = $this->validate($param, [
  246. 'page' => 'require',
  247. ]);
  248. // 错误
  249. if (true !== $valid) {
  250. return $this->ApiJson(-1, $valid);
  251. }
  252. $withdraw = [];
  253. switch ($param['type']){
  254. case "balance":
  255. $model = "UsersBalanceRecord";
  256. break;
  257. case "property":
  258. $model = "UsersPropertyRecord";
  259. break;
  260. default :
  261. $model = "UsersWithdraw";
  262. }
  263. // 按月份
  264. for($month = 1; $month <= date('m'); $month ++ ){
  265. $data = model('common/' . $model)
  266. ->where(['user_id' => $this->auth->user()['id']])
  267. ->field("*,FROM_UNIXTIME(created_at, '%Y-%m-%d %H:%i:%s') as created_at")
  268. ->whereTime('created_at', 'between', $this->getMonthTime($month))
  269. // ->limit((($param['page'] - 1) * $limit) . "," . $limit)
  270. ->order(['id' => 'desc', 'updated_at' => 'desc'])
  271. ->select();
  272. if (count($data) != 0)
  273. $withdraw[date('Y') . $month] = $data;
  274. }
  275. return $this->ApiJson(0, '获取信息成功', $withdraw);
  276. }
  277. private function getMonthTime($month)
  278. {
  279. //
  280. $firstday = date('Y-m-01', strtotime(date('Y') . '-' . $month . '-1'));
  281. $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
  282. return array($firstday,$lastday);
  283. }
  284. /**
  285. * 获取用户分享二维码
  286. * 1.自己的码还是小程序码?
  287. *
  288. * @author 许祖兴 < zuxing.xu@lettered.cn>
  289. * @date 2020/6/30 15:59
  290. *
  291. * @return \think\response\Json
  292. */
  293. public function getUserSpread()
  294. {
  295. // 生成小程序码
  296. // 1. 是否生成
  297. // 2. 为生成创建返回/直接返回
  298. //$Qr = (new Qrcode())->createServer("/pages/index/index?spd=RYVBJKC");
  299. // return $this->ApiJson(0,'',$Qr);
  300. }
  301. /**
  302. *
  303. * @author 许祖兴 < zuxing.xu@lettered.cn>
  304. * @date 2020/7/2 16:16
  305. *
  306. * @return \think\response\Json
  307. * @throws \Lettered\Support\Exceptions\FailedException
  308. */
  309. public function joinInStore()
  310. {
  311. // 获取信息
  312. $store = model('common/Seller')->getBy(['user_id' => $this->auth->user()['id']]);
  313. // 这简单做,get 查状态 post 修改数据
  314. if ($this->request->isPost()){
  315. // 接收参数
  316. $params = $this->request->param();
  317. // 参数校验
  318. $valid = $this->validate($params, [
  319. 'area_id|区域信息' => 'require',
  320. 'lng|经度位置' => 'require',
  321. 'lat|维度位置' => 'require',
  322. 'contact|联系人' => 'require',
  323. 'seller_name|店铺信息' => 'require',
  324. 'products|主营产品' => 'require',
  325. 'mobile|手机号' => 'require',
  326. 'province|详细地址' => 'require',
  327. 'city|详细地址' => 'require',
  328. 'country|详细地址' => 'require',
  329. 'address|详细地址' => 'require',
  330. 'fd_img|门店照片' => 'require',
  331. 'bi_license|营业执照' => 'require',
  332. ]);
  333. // 错误返回
  334. if(true !== $valid){
  335. return $this->ApiJson(-1, $valid);
  336. }
  337. // 验证码
  338. // $sms = new SmsCode();
  339. // if (!$sms->verify(input('mobile'),input('vercode'))) {
  340. // return $this->ApiJson(-1, "验证码错误!");
  341. // }
  342. // 再次验证身份信息
  343. $verify = model("common/UsersVerify")
  344. ->getBy(['user_id' => $this->auth->user()['id']]);
  345. if($verify['status'] != 2){
  346. return $this->ApiJson(-1, "身份验证尚未完成,请完善后再试!");
  347. }
  348. // 用户身份信息复用
  349. $params['id_card'] = $verify['id_card'];
  350. $params['id_card_img'] = $verify['id_card_img'];
  351. if (!$store) {
  352. // 写入用户信息 身份证信息
  353. $params['user_id'] = $this->auth->user()['id'];
  354. // 写入数据
  355. $ret = model('common/Seller')::create($params,true);
  356. }else {
  357. // 更新数据
  358. $ret = $store->updateBy($store['id'], $params);
  359. }
  360. // 消息
  361. if ($ret) {
  362. return $this->ApiJson(0,'入驻信息提交成功,请等待审核');
  363. }
  364. return $this->ApiJson(-1,'数据异常,请稍后再试');
  365. }
  366. return $this->ApiJson(0,'获取信息成功', $store);
  367. }
  368. /**
  369. * 代理统计
  370. *
  371. * @author 许祖兴 < zuxing.xu@lettered.cn>
  372. * @date 2020/7/10 16:43
  373. *
  374. * @return \think\response\Json
  375. * @throws \Lettered\Support\Exceptions\FailedException
  376. * @throws \think\db\exception\DataNotFoundException
  377. * @throws \think\db\exception\ModelNotFoundException
  378. * @throws \think\exception\DbException
  379. */
  380. public function userAgent()
  381. {
  382. // 接收参数
  383. $params = $this->request->param();
  384. // 参数校验
  385. $valid = $this->validate($params, [
  386. 'area_id|地区数据' => 'require'
  387. ]);
  388. // 错误返回
  389. if(true !== $valid){
  390. return $this->ApiJson(-1, $valid);
  391. }
  392. // 1. 查我的代理信息
  393. $agent = model('common/UsersAgent')->getBy(['user_id' => $this->auth->user()['id']]);
  394. // 我是代理
  395. if ($agent && $agent['status'] == 2){
  396. // 1. 查我代理区域的订单信息和营业额信息
  397. $where = ['area_id' => $agent['area_id']];
  398. // 商品订单统计
  399. // 摩的订单统计
  400. // 配送订单统计
  401. // 技能订单统计
  402. $data = [
  403. 'goods' => db('goods_order')->where($where)->field("count(id) as total_order,sum(pay_price) as total_price")->find(),
  404. 'motor' => db('taxi_order')->where($where)->field("count(id) as total_order,sum(price) as total_price")->find(),
  405. 'skill' => db('skill_order')->where($where)->field("count(id) as total_order,sum(price) as total_price")->find(),
  406. 'mission' => db('mission_order')->where($where)->field("count(id) as total_order,sum(price) as total_price")->find(),
  407. ];
  408. $total_order = 0;
  409. $total_fee = 0;
  410. foreach ($data as $item){
  411. $total_order += $item['total_order'];
  412. $total_fee += $item['total_price'];
  413. }
  414. // 2. 我的区域信息
  415. $info['area'] = db('china')->where(['id' => $agent['area_id']])->value('name');
  416. // 总营业额
  417. $info['total_fee'] = sprintf("%.2f",$total_fee);
  418. // 总订单
  419. $info['total_order'] = $total_order;
  420. // 商户统计
  421. $info['total_store'] = db('seller')->where(['area_id' => $agent['area_id']])->count();
  422. return $this->ApiJson(0,'获取信息成功',[
  423. 'info' => $info,
  424. 'data' => $data,
  425. 'agent' => $agent
  426. ]);
  427. }
  428. // 获取当前地区已经申请通过的
  429. $pass = model('common/UsersAgent')->where('area_id','like', substr($params['area_id'], 0, strlen($params['area_id']) - 2) . '%' )
  430. ->where('status','=','2')->select();
  431. $area = model('common/China')->findBy($params['area_id']);
  432. $model = model('common/China');
  433. $where = [];
  434. foreach ($pass as $p){
  435. $where[] = ['id','<>', $p['area_id']];
  436. }
  437. $areas = $model->where($where)->where(['parent_id' => $area['parent_id']])->select();
  438. // ->where('id', ['>', 0], ['<>', 10], 'and')
  439. return $this->ApiJson(0,'获取信息成功', ['agent' => $agent,'areas' => $areas]);
  440. }
  441. /**
  442. * 月订单统计和列表
  443. *
  444. * @author 许祖兴 < zuxing.xu@lettered.cn>
  445. * @date 2020/7/13 10:11
  446. *
  447. * @return \think\response\Json
  448. * @throws \think\db\exception\DataNotFoundException
  449. * @throws \think\db\exception\ModelNotFoundException
  450. * @throws \think\exception\DbException
  451. */
  452. public function userAgentMonth()
  453. {
  454. // 接收参数
  455. $params = $this->request->param();
  456. $limit = 10;
  457. // 参数校验
  458. $valid = $this->validate($params, [
  459. 'area_id|地区数据' => 'require',
  460. 'otype|订单类型' => 'require',
  461. 'month|记录月份' => 'require',
  462. 'page' => 'require',
  463. ]);
  464. // 错误返回
  465. if(true !== $valid){
  466. return $this->ApiJson(-1, $valid);
  467. }
  468. switch ($params['otype']){
  469. case "goods":
  470. $model = "GoodsOrder";
  471. break;
  472. case "skill":
  473. $model = "SkillOrder";
  474. break;
  475. case "motor":
  476. $model = "TaxiOrder";
  477. break;
  478. case "mission":
  479. $model = "MissionOrder";
  480. break;
  481. default :
  482. $model = "GoodsOrder";
  483. }
  484. // 查询字段
  485. $price_field = ($params['otype'] == 'goods') ? 'pay_price' : 'price';
  486. // 月份处理
  487. list($year, $month) = str2arr($params['month'],'-');
  488. // 数据以及订单
  489. $data = model('common/' . $model)
  490. ->where(['area_id' => $params['area_id']])
  491. ->field("FROM_UNIXTIME(created_at,'%Y-%m') as month,count(id) as total_order,sum($price_field) total_price")
  492. ->whereTime('created_at', 'between', [$params['month'] . '-1', $year . '-' . ($month + 1) . '-1'])
  493. ->group('month')
  494. ->find();
  495. $data['order'] = model('common/' . $model)
  496. ->where(['area_id' => $params['area_id']])
  497. ->whereTime('created_at', 'between', [$params['month'] . '-1', $year . '-' . ($month + 1) . '-1'])
  498. ->limit((($params['page'] - 1) * $limit) . "," . $limit)
  499. ->select();
  500. return $this->ApiJson(0,'获取信息成功', $data);
  501. }
  502. /**
  503. * 提交申请
  504. *
  505. * @author 许祖兴 < zuxing.xu@lettered.cn>
  506. * @date 2020/8/28 9:08
  507. *
  508. * @return \think\response\Json
  509. * @throws \Lettered\Support\Exceptions\FailedException
  510. * @throws \think\db\exception\DataNotFoundException
  511. * @throws \think\db\exception\ModelNotFoundException
  512. * @throws \think\exception\DbException
  513. */
  514. public function userAgentApply()
  515. {
  516. // 接收参数
  517. $params = $this->request->param();
  518. // 参数校验
  519. $valid = $this->validate($params, [
  520. 'area_id|地区数据' => 'require'
  521. ]);
  522. // 错误返回
  523. if(true !== $valid){
  524. return $this->ApiJson(-1, $valid);
  525. }
  526. // 先看是否已经有这个区域代理了
  527. if (model('common/UsersAgent')->getBy(['area_id' => $params['area_id'],'status' => 2])){
  528. // 查找其他区域
  529. $area = model('common/China')->findBy($params['area_id']);
  530. $parent = model('common/China')->where('id','<>',$params['area_id'])
  531. ->where(['parent_id' => $area['parent_id']])->select();
  532. return $this->ApiJson(-1, "抱歉,当前区域已经存在代理,请选择其他区域申请!");
  533. }
  534. // 查找
  535. $agent = model('common/UsersAgent')
  536. ->getBy(['user_id' => $this->auth->user()['id'], 'area_id' => $params['area_id']]);
  537. // 写入用户信息 身份证信息
  538. $params['user_id'] = $this->auth->user()['id'];
  539. // 更新用户信息
  540. model('common/Users')->updateBy($params['user_id'],['is_agent' => 1]);
  541. if (!$agent) {
  542. // 写入数据
  543. $ret = model('common/UsersAgent')::create([
  544. 'user_id' => $params['user_id'],
  545. 'area_id' => $params['area_id']
  546. ]);
  547. }else {
  548. // 更新数据
  549. $ret = $agent->updateBy($agent['id'], [
  550. 'area_id' => $params['area_id'],
  551. 'status' => 1
  552. ]);
  553. }
  554. // 消息
  555. if ($ret){
  556. return $this->ApiJson(0,'提交成功,请等待管理员审核!', $ret);
  557. }
  558. return $this->ApiJson(-1,'数据异常,请稍后重试!');
  559. }
  560. /**
  561. * 配送员申请
  562. *
  563. * @author 许祖兴 < zuxing.xu@lettered.cn>
  564. * @date 2020/7/10 17:30
  565. *
  566. * @return \think\response\Json
  567. * @throws \Lettered\Support\Exceptions\FailedException
  568. */
  569. public function deliveryApply()
  570. {
  571. // 查找
  572. $missionUser = model('common/MissionUser')
  573. ->getBy(['user_id' => $this->auth->user()['id']]);
  574. if ($this->request->isPost()){
  575. // 接收参数
  576. $params = $this->request->param();
  577. $valid = $this->validate($params, [
  578. 'hl_license|健康证明' => 'require',
  579. 'province|省份' => 'require',
  580. 'city|城市' => 'require',
  581. 'country|地区' => 'require',
  582. 'area|地区数据' => 'require',
  583. 'address|区域详情' => 'require',
  584. ]);
  585. // 错误返回
  586. if(true !== $valid){
  587. return $this->ApiJson(-1, $valid);
  588. }
  589. // 再次验证身份信息
  590. $verify = model("common/UsersVerify")
  591. ->getBy(['user_id' => $this->auth->user()['id']]);
  592. if($verify['status'] != 2){
  593. return $this->ApiJson(-1, "身份验证尚未完成,请完善后再试!");
  594. }
  595. // 用户身份信息复用
  596. $params['uname'] = $verify['name'];
  597. $params['id_card'] = $verify['id_card'];
  598. $params['id_card_img'] = $verify['id_card_img'];
  599. $params['status'] = 1;
  600. if (!$missionUser) {
  601. // 写入用户信息 身份证信息
  602. $params['user_id'] = $this->auth->user()['id'];
  603. // 写入数据
  604. $ret = model('common/MissionUser')::create($params, true);
  605. }else {
  606. // 更新数据
  607. $ret = $missionUser->updateBy($missionUser['id'],$params);
  608. }
  609. // 消息
  610. if ($ret){
  611. return $this->ApiJson(0,'提交成功,请等待管理员审核!');
  612. }
  613. return $this->ApiJson(-1,'数据异常,请稍后重试!');
  614. }
  615. return $this->ApiJson(0,'获取信息成功', $missionUser);
  616. }
  617. /**
  618. * 用户文件上传
  619. *
  620. * @author 许祖兴 < zuxing.xu@lettered.cn>
  621. * @date 2020/7/2 9:09
  622. *
  623. * @return \think\response\Json
  624. */
  625. public function uploadUserFile()
  626. {
  627. // 接收数据
  628. $params = $this->request->param();
  629. // 参数校验
  630. $valid = $this->validate($params, [
  631. 'action|上传操作' => 'require'
  632. ]);
  633. // 错误返回
  634. if(true !== $valid){
  635. return $this->ApiJson(-1, $valid);
  636. };
  637. // 上传
  638. $upload = new Upload(config('upload.'));
  639. $ret = $upload->setPath( '/' . $params['action'])->upload($this->request->file('file'));
  640. return $this->ApiJson(0,'', get_annex_url($ret));
  641. }
  642. /**
  643. * 嘛呗出行红包发放
  644. * @return \think\response\Json
  645. * @throws \Lettered\Support\Exceptions\FailedException
  646. */
  647. public function receiveRedbag(){
  648. $params = $this->request->param();
  649. $sid = isset($params['sid'])? $params['sid'] : 0;
  650. if($sid<=0){
  651. return $this->ApiJson(-1,'参数错误');
  652. }
  653. $redbagMoney = sys_config('user_redbag_money','user');
  654. $redbagNum = sys_config('user_redbag_num','user');
  655. if($redbagMoney <= 0 || $redbagNum <= 0){
  656. return $this->ApiJson(-1,'红包已被领完或参数错误');
  657. }
  658. if(UsersBalanceRecord::where(['user_id'=> $sid,'source_uid'=> $this->auth->user()['id'],'type'=>2])->value('id')){
  659. return $this->ApiJson(-1,'该用户红包已奖励过');
  660. }
  661. $user = Users::where(['id'=> $sid])->field('id,nickname,balance')->find();
  662. if(empty($user)){
  663. return $this->ApiJson(-1,'奖励用户不存在');
  664. }
  665. DB::startTrans();
  666. $user->balance += $redbagMoney;
  667. $user->updated_at = time();
  668. if(!$user->save()){
  669. Db::rollback();
  670. return $this->ApiJson(-1,'发放红包失败');
  671. }
  672. if(!SystemConfig::where(['group'=>'user','name'=>'user_redbag_num'])->dec('value',1)){
  673. Db::rollback();
  674. return $this->ApiJson(-1,'发放红包失败');
  675. }
  676. $data = [
  677. 'user_id'=> $sid,
  678. 'source_uid'=> $this->auth->user()['id'],
  679. 'type'=> 2,
  680. 'action'=> 1,
  681. 'inc_amount'=> $redbagMoney,
  682. 'aft_amount'=> $user->balance,
  683. 'remark'=> '分享获得红包,金额【'.$redbagMoney.'】',
  684. 'created_at'=> time(),
  685. 'updated_at'=> time()
  686. ];
  687. if(!UsersBalanceRecord::insertGetId($data)){
  688. Db::rollback();
  689. return $this->ApiJson(-1,'发放红包失败');
  690. }
  691. DB::commit();
  692. return $this->ApiJson(0,'发放红包成功');
  693. }
  694. }