User.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?php
  2. namespace app\api\controller\v1\taxiUser;
  3. use app\api\controller\ApiController;
  4. use app\api\model\taxi\MotorRecord;
  5. use app\api\model\taxi\UserPaymentOrder;
  6. use app\common\model\TaxiUser;
  7. use app\common\model\TaxiUsersLevel;
  8. use app\common\model\Users;
  9. use app\http\IResponse;
  10. use Lettered\Support\Upload;
  11. use think\App;
  12. use app\api\service\JWTAuth as IAuth;
  13. use think\Db;
  14. use think\Exception;
  15. class User extends ApiController
  16. {
  17. protected $model;
  18. public function __construct(App $app = null, IAuth $auth)
  19. {
  20. parent::__construct($app, $auth);
  21. $this->model = new \app\common\model\Users();
  22. $this->taxiUserModel = new \app\api\model\taxi\User();
  23. }
  24. /**
  25. * 司机用户信息
  26. * @return mixed
  27. * @throws \Lettered\Support\Exceptions\FailedException
  28. */
  29. public function info(){
  30. $params = $this->request->param();
  31. $taxiUser = $this->auth->guard('taxi_user')->user();
  32. if(!$taxiUser){
  33. return IResponse::failure('用户不存在,或已被冻结');
  34. }
  35. if(!$taxiUser['user_id']){
  36. return IResponse::failure('司机未绑定用户账号');
  37. }
  38. $user = $this->model->where(['id'=> $taxiUser['user_id']])
  39. ->field('id,parent_id,open_id,nickname,avatar_url,partnership,taxi_property,status')
  40. ->find();
  41. $type = isset($params['type'])? $params['type'] : 1;
  42. if($type == 2){
  43. $taxiUser['level_name'] = $taxiUser['level']? TaxiUsersLevel::where(['level'=>$taxiUser['level']])->value('title') : '';
  44. $counts = [
  45. 'day_income'=> '0.00',
  46. 'total_income'=> '0.00',
  47. 'withdraw_income'=> $user['partnership'],
  48. 'day_order'=> '0',
  49. 'total_order'=> '0',
  50. 'wait_order'=> '0',
  51. ];
  52. $counts['day_income'] = model('common/TaxiOrder')
  53. ->where(['taxi_uid'=> $taxiUser['id']])
  54. ->whereIn('status',[4,5])
  55. ->where('created_at','>=', strtotime(date('Y-m-d')))
  56. ->sum('settle_price');
  57. $counts['total_income'] = model('common/TaxiOrder')
  58. ->where(['taxi_uid'=> $taxiUser['id']])
  59. ->whereIn('status',[4,5])
  60. ->sum('settle_price');
  61. $counts['day_order'] = model('common/TaxiOrder')
  62. ->where(['taxi_uid'=> $taxiUser['id']])
  63. ->whereIn('status',[3,4,5])
  64. ->where('created_at','>=', strtotime(date('Y-m-d')))
  65. ->count('id');
  66. $counts['total_order'] = model('common/TaxiOrder')
  67. ->where(['taxi_uid'=> $taxiUser['id']])
  68. ->whereIn('status',[3,4,5])
  69. ->count('id');
  70. $counts['wait_order'] = model('common/TaxiOrder')
  71. ->where(['taxi_uid'=> $taxiUser['id']])
  72. ->whereIn('status',[3])
  73. ->where('created_at','>=', strtotime(date('Y-m-d')))
  74. ->count('id');
  75. $taxiUser['counts'] = $counts;
  76. }
  77. unset($taxiUser['password']);
  78. $taxiUser['user'] = $user;
  79. return IResponse::success($taxiUser,'获取成功');
  80. }
  81. /**
  82. * 提现
  83. * @return \think\response\Json
  84. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  85. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  86. * @throws \GuzzleHttp\Exception\GuzzleException
  87. * @throws \Lettered\Support\Exceptions\FailedException
  88. */
  89. public function withdraw()
  90. {
  91. // 接收参数
  92. $params = $this->request->param();
  93. // 参数校验
  94. $valid = $this->validate($params, [
  95. 'real_name|真实姓名' => 'require',
  96. 'pay_type|收款方式' => 'require',
  97. 'bank_card_no|收款银行卡号' => 'requireIf:pay_type,1',
  98. 'desposit_bank|开户行' => 'requireIf:pay_type,1',
  99. 'sub_branch|开户支行' => 'requireIf:pay_type,1',
  100. 'wechat|收款微信' => 'requireIf:pay_type,2',
  101. 'wechat_qrcode|微信收款码' => 'requireIf:pay_type,2',
  102. 'alipay|收款支付宝' => 'requireIf:pay_type,3',
  103. 'alipay_qrcode|支付宝收款码' => 'requireIf:pay_type,3',
  104. 'amount|提现金额' => 'require',
  105. ]);
  106. // 错误返回
  107. if(true !== $valid){
  108. return IResponse::failure($valid);
  109. }
  110. $taxiUser = $this->auth->guard('taxi_user')->user();
  111. if(!$taxiUser){
  112. return IResponse::failure('用户不存在,或已被冻结');
  113. }
  114. if(!$taxiUser['user_id']){
  115. return IResponse::failure( "司机未绑定用户账号");
  116. }
  117. $user = $this->model->where(['id'=> $taxiUser['user_id']])
  118. ->field('id,parent_id,open_id,nickname,avatar_url,partnership,status')
  119. ->find();
  120. // 司机资产限制
  121. if ($user['partnership'] <= sys_config('user_withdraw_limit','user')){
  122. return IResponse::failure("当前可提现金额不满足!司机资产:【". $user['partnership'] . "】");
  123. }
  124. // 最低限制
  125. if ($params['amount'] <= ($limit = sys_config('user_withdraw_limit','user'))){
  126. //return IResponse::failure("申请提现金额不满足最低提现!最低:【" . $limit . "】");
  127. }
  128. // 最高限制
  129. if ($user['partnership'] <= $params['amount']){
  130. return IResponse::failure("当前可提现金额不满足!资产:【". $user['partnership'] . "】");
  131. }
  132. // 写入用户ID
  133. $params['user_id'] = $user['id'];
  134. $params['taxi_uid'] = $taxiUser['id'];
  135. // 交易单号
  136. $params['order_no'] = get_order_no();
  137. // 写入数据
  138. Db::startTrans();
  139. try {
  140. $params['status'] = 10;
  141. $ret = model('common/TaxiUsersWithdraw')::create($params,true);
  142. $Users = new Users();
  143. $Users->changePartnership($user['id'], $ret['amount'], '资产余额提现', 20);
  144. Db::commit();
  145. return IResponse::success('提现申请成功,请等候审核');
  146. }
  147. catch(Exception $e) {
  148. Db::rollback();
  149. return IResponse::failure( '提现失败,请稍后重试');
  150. }
  151. }
  152. /**
  153. * 提现记录
  154. * @return mixed
  155. * @throws \Lettered\Support\Exceptions\FailedException
  156. */
  157. public function withdrawLog()
  158. {
  159. $param = $this->request->param();
  160. $limit = isset($param['pageSize'])? $param['pageSize'] : 20;
  161. $taxiUser = $this->auth->guard('taxi_user')->user();
  162. if(!$taxiUser){
  163. return IResponse::failure('用户不存在,或已被冻结');
  164. }
  165. $lists = model('common/TaxiUsersWithdraw')
  166. ->where(['taxi_uid' => $taxiUser['id']])
  167. ->order(['created_at' => 'desc'])
  168. ->limit((($param['page'] - 1) * $limit) . "," . $limit)
  169. ->select();
  170. return IResponse::success($lists,'获取成功');
  171. }
  172. /**
  173. * 提现记录
  174. * @return mixed
  175. * @throws \Lettered\Support\Exceptions\FailedException
  176. */
  177. public function accountLog()
  178. {
  179. // 1. 传入用户位置
  180. $param = $this->request->param();
  181. $limit = isset($param['pageSize'])? $param['pageSize'] : 20;
  182. $taxiUser = $this->auth->guard('taxi_user')->user();
  183. if(!$taxiUser){
  184. return IResponse::failure('用户不存在,或已被冻结');
  185. }
  186. // 经纬度升序
  187. $lists = MotorRecord::where(['user_id' => $taxiUser['user_id']])
  188. ->order(['created_at' => 'desc'])
  189. ->limit((($param['page'] - 1) * $limit) . "," . $limit)
  190. ->select();
  191. return IResponse::success($lists,'获取成功');
  192. }
  193. /**
  194. *
  195. * 设置接单状态
  196. * @return mixed
  197. * @throws \Lettered\Support\Exceptions\FailedException
  198. */
  199. public function setOrderStatus()
  200. {
  201. // 1. 传入用户位置
  202. $param = $this->request->param();
  203. $status = isset($param['status'])? $param['status'] : 2;
  204. $taxiUser = $this->auth->guard('taxi_user')->user();
  205. if(!$taxiUser){
  206. return IResponse::failure('用户不存在,或已被冻结');
  207. }
  208. $taxiUser->order_status = $status;
  209. if($taxiUser->save()){
  210. return IResponse::success([],'设置成功');
  211. }else{
  212. return IResponse::failure('设置失败');
  213. }
  214. }
  215. /**
  216. * 文件上传
  217. * @return \think\response\Json
  218. */
  219. public function uploadUserFile()
  220. {
  221. // 接收数据
  222. $params = $this->request->param();
  223. // 参数校验
  224. $valid = $this->validate($params, [
  225. 'action|上传操作' => 'require'
  226. ]);
  227. // 错误返回
  228. if(true !== $valid){
  229. return IResponse::failure($valid);
  230. };
  231. // 上传
  232. $upload = new Upload(config('upload.'));
  233. $ret = $upload->setPath( '/' . $params['action'])->upload($this->request->file('file'));
  234. return IResponse::success(['path'=> $ret,'url'=>get_annex_url($ret)],'上传成功');
  235. }
  236. /**
  237. * 司机等级列表
  238. * @return mixed
  239. */
  240. public function levels()
  241. {
  242. $list = TaxiUsersLevel::where(['status'=> 1])->order('level')->select();
  243. return IResponse::success($list,'获取成功');
  244. }
  245. /**
  246. * 升级记录
  247. * @return mixed
  248. * @throws \Lettered\Support\Exceptions\FailedException
  249. */
  250. public function levelLog()
  251. {
  252. $param = $this->request->param();
  253. $limit = isset($param['pageSize'])? $param['pageSize'] : 20;
  254. $lists = UserPaymentOrder::where(['user_id' => $this->auth->user()['id'],'type'=>30])
  255. ->order(['created_at' => 'desc'])
  256. ->limit((($param['page'] - 1) * $limit) . "," . $limit)
  257. ->select()->each(function($item,$k){
  258. $item['created_at_text'] = $item['created_at']? date('Y-m-d H:i:s', $item['created_at']) : '';
  259. });
  260. return IResponse::success($lists,'获取成功');
  261. }
  262. /**
  263. * 升级订单
  264. * @return mixed|\think\response\Json
  265. * @throws \Lettered\Support\Exceptions\FailedException
  266. */
  267. public function doLevel()
  268. {
  269. // 接收数据
  270. $params = $this->request->param();
  271. // 参数校验
  272. $valid = $this->validate($params, [
  273. 'id|请选择升级级别' => 'require'
  274. ]);
  275. // 错误返回
  276. if(true !== $valid){
  277. return IResponse::failure($valid);
  278. };
  279. $levelData = TaxiUsersLevel::where(['id'=> $params['id'],'status'=>1])->find();
  280. if(empty($levelData)){
  281. return IResponse::failure('参数错误或级别不存在');
  282. }
  283. $taxiUser = TaxiUser::where(['user_id'=> $this->auth->user()['id']])->find();
  284. if(empty($taxiUser)){
  285. return IResponse::failure('司机不存在或您还不是司机,请先入驻成为司机');
  286. }
  287. if(!in_array($taxiUser['status'], [1,2])){
  288. return IResponse::failure('您还未通过司机入驻审核,请先通过审核后操作');
  289. }
  290. if($taxiUser['level'] >= $levelData['level']){
  291. return IResponse::failure('升级等级参数错误,不能低于司机当前等级');
  292. }
  293. $data = [
  294. 'user_id'=> $this->auth->user()['id'],
  295. 'order_no'=> get_order_no(),
  296. 'source_id'=> $levelData['id'],
  297. 'price'=> $levelData['price'],
  298. 'remark'=> '司机付费【'.$levelData['price'].'】升级到'.$levelData['title'],
  299. 'status'=> 1,
  300. 'created_at'=> time(),
  301. 'updated_at'=> time(),
  302. 'type'=> 30,
  303. ];
  304. DB::startTrans();
  305. $orderid = UserPaymentOrder::insertGetId($data);
  306. if(!$orderid){
  307. Db::rollback();
  308. return IResponse::failure('创建升级订单失败');
  309. }
  310. // 创建对应支付记录
  311. $trade_no = get_order_no();
  312. $logID = model('common/OrderPaylog')->storeBy([
  313. 'out_trade_no' => $trade_no,
  314. 'total_price' => $levelData['price'],
  315. 'order_idx' => $orderid,
  316. 'ascription' => 'level' // 归属订单
  317. ]);
  318. if(!$logID){
  319. Db::rollback();
  320. return IResponse::failure('升级订单提交失败');
  321. }
  322. Db::commit();
  323. // 返回支付单号
  324. return $this->ApiJson(0,'升级订单提交成功', [
  325. 'type' => 'wx',
  326. 'success' => "ok!",
  327. 'order_id' => $orderid,
  328. 'trade_no' => $trade_no
  329. ]);
  330. }
  331. }