User.php 28 KB

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