User.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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\TaxiUsersLevel;
  7. use app\common\model\Users;
  8. use app\http\IResponse;
  9. use Lettered\Support\Upload;
  10. use think\App;
  11. use app\api\service\JWTAuth as IAuth;
  12. use think\Db;
  13. use think\Exception;
  14. class User extends ApiController
  15. {
  16. protected $model;
  17. public function __construct(App $app = null, IAuth $auth)
  18. {
  19. parent::__construct($app, $auth);
  20. $this->model = new \app\common\model\Users();
  21. $this->taxiUserModel = new \app\api\model\taxi\User();
  22. }
  23. /**
  24. * 司机用户信息
  25. * @return mixed
  26. * @throws \Lettered\Support\Exceptions\FailedException
  27. */
  28. public function info(){
  29. $params = $this->request->param();
  30. $taxiUser = $this->auth->guard('taxi_user')->user();
  31. if(!$taxiUser){
  32. return IResponse::failure('用户不存在,或已被冻结');
  33. }
  34. if(!$taxiUser['user_id']){
  35. return IResponse::failure('司机未绑定用户账号');
  36. }
  37. $user = $this->model->where(['id'=> $taxiUser['user_id']])
  38. ->field('id,parent_id,open_id,nickname,avatar_url,partnership,status')
  39. ->find();
  40. $type = isset($params['type'])? $params['type'] : 1;
  41. if($type == 2){
  42. $counts = [
  43. 'day_income'=> '0.00',
  44. 'total_income'=> '0.00',
  45. 'withdraw_income'=> $user['partnership'],
  46. 'day_order'=> '0',
  47. 'total_order'=> '0',
  48. 'wait_order'=> '0',
  49. ];
  50. $counts['day_income'] = model('common/TaxiOrder')
  51. ->where(['taxi_uid'=> $taxiUser['id']])
  52. ->whereIn('status',[4,5])
  53. ->where('created_at','>=', strtotime(date('Y-m-d')))
  54. ->sum('settle_price');
  55. $counts['total_income'] = model('common/TaxiOrder')
  56. ->where(['taxi_uid'=> $taxiUser['id']])
  57. ->whereIn('status',[4,5])
  58. ->sum('settle_price');
  59. $counts['day_order'] = model('common/TaxiOrder')
  60. ->where(['taxi_uid'=> $taxiUser['id']])
  61. ->whereIn('status',[3,4,5])
  62. ->where('created_at','>=', strtotime(date('Y-m-d')))
  63. ->count('id');
  64. $counts['total_order'] = model('common/TaxiOrder')
  65. ->where(['taxi_uid'=> $taxiUser['id']])
  66. ->whereIn('status',[3,4,5])
  67. ->count('id');
  68. $counts['wait_order'] = model('common/TaxiOrder')
  69. ->where(['taxi_uid'=> $taxiUser['id']])
  70. ->whereIn('status',[3])
  71. ->where('created_at','>=', strtotime(date('Y-m-d')))
  72. ->count('id');
  73. $taxiUser['counts'] = $counts;
  74. }
  75. unset($taxiUser['password']);
  76. $taxiUser['user'] = $user;
  77. return IResponse::success($taxiUser,'获取成功');
  78. }
  79. /**
  80. * 提现
  81. * @return \think\response\Json
  82. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  83. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  84. * @throws \GuzzleHttp\Exception\GuzzleException
  85. * @throws \Lettered\Support\Exceptions\FailedException
  86. */
  87. public function withdraw()
  88. {
  89. // 接收参数
  90. $params = $this->request->param();
  91. // 参数校验
  92. $valid = $this->validate($params, [
  93. 'real_name|真实姓名' => 'require',
  94. 'pay_type|收款方式' => 'require',
  95. 'bank_card_no|收款银行卡号' => 'requireIf:pay_type,1',
  96. 'desposit_bank|开户行' => 'requireIf:pay_type,1',
  97. 'sub_branch|开户支行' => 'requireIf:pay_type,1',
  98. 'wechat|收款微信' => 'requireIf:pay_type,2',
  99. 'wechat_qrcode|微信收款码' => 'requireIf:pay_type,2',
  100. 'alipay|收款支付宝' => 'requireIf:pay_type,3',
  101. 'alipay_qrcode|支付宝收款码' => 'requireIf:pay_type,3',
  102. 'amount|提现金额' => 'require',
  103. ]);
  104. // 错误返回
  105. if(true !== $valid){
  106. return IResponse::failure($valid);
  107. }
  108. $taxiUser = $this->auth->guard('taxi_user')->user();
  109. if(!$taxiUser){
  110. return IResponse::failure('用户不存在,或已被冻结');
  111. }
  112. if(!$taxiUser['user_id']){
  113. return IResponse::failure( "司机未绑定用户账号");
  114. }
  115. $user = $this->model->where(['id'=> $taxiUser['user_id']])
  116. ->field('id,parent_id,open_id,nickname,avatar_url,partnership,status')
  117. ->find();
  118. // 司机资产限制
  119. if ($user['partnership'] <= sys_config('user_withdraw_limit','user')){
  120. return IResponse::failure("当前可提现金额不满足!司机资产:【". $user['partnership'] . "】");
  121. }
  122. // 最低限制
  123. if ($params['amount'] <= ($limit = sys_config('user_withdraw_limit','user'))){
  124. //return IResponse::failure("申请提现金额不满足最低提现!最低:【" . $limit . "】");
  125. }
  126. // 最高限制
  127. if ($user['partnership'] <= $params['amount']){
  128. return IResponse::failure("当前可提现金额不满足!资产:【". $user['partnership'] . "】");
  129. }
  130. // 写入用户ID
  131. $params['user_id'] = $user['id'];
  132. $params['taxi_uid'] = $taxiUser['id'];
  133. // 交易单号
  134. $params['order_no'] = get_order_no();
  135. // 写入数据
  136. Db::startTrans();
  137. try {
  138. $params['status'] = 20;
  139. $ret = model('common/TaxiUsersWithdraw')::create($params,true);
  140. $Users = new Users();
  141. $Users->changePartnership($user['id'], $ret['amount'], '资产提现', 20);
  142. Db::commit();
  143. return IResponse::success('提现申请成功,请等候审核');
  144. }
  145. catch(Exception $e) {
  146. Db::rollback();
  147. return IResponse::failure( '提现失败,请稍后重试');
  148. }
  149. }
  150. /**
  151. * 提现记录
  152. * @return mixed
  153. * @throws \Lettered\Support\Exceptions\FailedException
  154. */
  155. public function withdrawLog()
  156. {
  157. // 1. 传入用户位置
  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. // 经纬度升序
  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. public function doLevel()
  246. {
  247. // 接收数据
  248. $params = $this->request->param();
  249. // 参数校验
  250. $valid = $this->validate($params, [
  251. 'id|请选择升级级别' => 'require'
  252. ]);
  253. // 错误返回
  254. if(true !== $valid){
  255. return IResponse::failure($valid);
  256. };
  257. $levelData = TaxiUsersLevel::where(['id'=> $params['id'],'status'=>1])->find();
  258. if(empty($levelData)){
  259. return IResponse::failure('参数错误或级别不存在');
  260. }
  261. $data = [
  262. 'user_id'=> $this->auth->user()['id'],
  263. 'order_no'=> get_order_no(),
  264. 'source_id'=> $levelData['id'],
  265. 'price'=> $levelData['price'],
  266. 'remark'=> '司机升级订单',
  267. 'status'=> 2,
  268. 'created_at'=> time(),
  269. 'updated_at'=> time(),
  270. 'type'=> 30,
  271. ];
  272. var_dump($data);
  273. if($orderid = UserPaymentOrder::insertGetId($data)){
  274. return IResponse::failure('创建升级订单失败');
  275. }
  276. // 创建对应支付记录
  277. $trade_no = get_order_no();
  278. $logID = model('common/OrderPaylog')->storeBy([
  279. 'out_trade_no' => $trade_no,
  280. 'total_price' => $params['price'],
  281. 'order_idx' => $orderid,
  282. 'ascription' => 'level' // 归属订单
  283. ]);
  284. // 返回支付单号
  285. return $this->ApiJson(0,'升级订单提交成功', [
  286. 'type' => 'wx',
  287. 'success' => "ok!",
  288. 'order_id' => $orderid,
  289. 'trade_no' => $trade_no
  290. ]);
  291. }
  292. }