User.php 28 KB

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