User.php 12 KB

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