User.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. // 1. 传入用户位置
  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. // 经纬度升序
  166. $lists = model('common/TaxiUsersWithdraw')
  167. ->where(['taxi_uid' => $taxiUser['id']])
  168. ->order(['created_at' => 'desc'])
  169. ->limit((($param['page'] - 1) * $limit) . "," . $limit)
  170. ->select();
  171. return IResponse::success($lists,'获取成功');
  172. }
  173. /**
  174. * 提现记录
  175. * @return mixed
  176. * @throws \Lettered\Support\Exceptions\FailedException
  177. */
  178. public function accountLog()
  179. {
  180. // 1. 传入用户位置
  181. $param = $this->request->param();
  182. $limit = isset($param['pageSize'])? $param['pageSize'] : 20;
  183. $taxiUser = $this->auth->guard('taxi_user')->user();
  184. if(!$taxiUser){
  185. return IResponse::failure('用户不存在,或已被冻结');
  186. }
  187. // 经纬度升序
  188. $lists = MotorRecord::where(['user_id' => $taxiUser['user_id']])
  189. ->order(['created_at' => 'desc'])
  190. ->limit((($param['page'] - 1) * $limit) . "," . $limit)
  191. ->select();
  192. return IResponse::success($lists,'获取成功');
  193. }
  194. /**
  195. *
  196. * 设置接单状态
  197. * @return mixed
  198. * @throws \Lettered\Support\Exceptions\FailedException
  199. */
  200. public function setOrderStatus()
  201. {
  202. // 1. 传入用户位置
  203. $param = $this->request->param();
  204. $status = isset($param['status'])? $param['status'] : 2;
  205. $taxiUser = $this->auth->guard('taxi_user')->user();
  206. if(!$taxiUser){
  207. return IResponse::failure('用户不存在,或已被冻结');
  208. }
  209. $taxiUser->order_status = $status;
  210. if($taxiUser->save()){
  211. return IResponse::success([],'设置成功');
  212. }else{
  213. return IResponse::failure('设置失败');
  214. }
  215. }
  216. /**
  217. * 文件上传
  218. * @return \think\response\Json
  219. */
  220. public function uploadUserFile()
  221. {
  222. // 接收数据
  223. $params = $this->request->param();
  224. // 参数校验
  225. $valid = $this->validate($params, [
  226. 'action|上传操作' => 'require'
  227. ]);
  228. // 错误返回
  229. if(true !== $valid){
  230. return IResponse::failure($valid);
  231. };
  232. // 上传
  233. $upload = new Upload(config('upload.'));
  234. $ret = $upload->setPath( '/' . $params['action'])->upload($this->request->file('file'));
  235. return IResponse::success(['path'=> $ret,'url'=>get_annex_url($ret)],'上传成功');
  236. }
  237. /**
  238. * 司机等级列表
  239. * @return mixed
  240. */
  241. public function levels()
  242. {
  243. $list = TaxiUsersLevel::where(['status'=> 1])->order('level')->select();
  244. return IResponse::success($list,'上传成功');
  245. }
  246. public function doLevel()
  247. {
  248. // 接收数据
  249. $params = $this->request->param();
  250. // 参数校验
  251. $valid = $this->validate($params, [
  252. 'id|请选择升级级别' => 'require'
  253. ]);
  254. // 错误返回
  255. if(true !== $valid){
  256. return IResponse::failure($valid);
  257. };
  258. $levelData = TaxiUsersLevel::where(['id'=> $params['id'],'status'=>1])->find();
  259. if(empty($levelData)){
  260. return IResponse::failure('参数错误或级别不存在');
  261. }
  262. $taxiUser = TaxiUser::where(['user_id'=> $this->auth->user()['id']])->find();
  263. if(empty($taxiUser)){
  264. return IResponse::failure('司机不存在或您还不是司机,请先入驻成为司机');
  265. }
  266. if(!in_array($taxiUser['status'], [1,2])){
  267. return IResponse::failure('您还未通过司机入驻审核,请先通过审核后操作');
  268. }
  269. if($taxiUser['level'] >= $levelData['level']){
  270. return IResponse::failure('升级等级参数错误,不能低于司机当前等级');
  271. }
  272. $data = [
  273. 'user_id'=> $this->auth->user()['id'],
  274. 'order_no'=> get_order_no(),
  275. 'source_id'=> $levelData['id'],
  276. 'price'=> $levelData['price'],
  277. 'remark'=> '司机升级订单',
  278. 'status'=> 1,
  279. 'created_at'=> time(),
  280. 'updated_at'=> time(),
  281. 'type'=> 30,
  282. ];
  283. DB::startTrans();
  284. $orderid = UserPaymentOrder::insertGetId($data);
  285. if(!$orderid){
  286. Db::rollback();
  287. return IResponse::failure('创建升级订单失败');
  288. }
  289. // 创建对应支付记录
  290. $trade_no = get_order_no();
  291. $logID = model('common/OrderPaylog')->storeBy([
  292. 'out_trade_no' => $trade_no,
  293. 'total_price' => $levelData['price'],
  294. 'order_idx' => $orderid,
  295. 'ascription' => 'level' // 归属订单
  296. ]);
  297. if(!$logID){
  298. Db::rollback();
  299. return IResponse::failure('升级订单提交失败');
  300. }
  301. Db::commit();
  302. // 返回支付单号
  303. return $this->ApiJson(0,'升级订单提交成功', [
  304. 'type' => 'wx',
  305. 'success' => "ok!",
  306. 'order_id' => $orderid,
  307. 'trade_no' => $trade_no
  308. ]);
  309. }
  310. }