* @date 2020/6/30 15:46 * * @return \think\response\Json * @throws \Lettered\Support\Exceptions\FailedException */ public function getUserCenter() { $user = $this->auth->user(); // 用户判断是不是被删除了,及时退出 if($user){ if ($user['status'] == 0) { throw new TokenException([ 'errmsg' => 'Unauthorized: 用户冻结!' ]); } $user['is_driver'] = 0; $user['taxi_level'] = 0; if($taxiUser = TaxiUser::where(['user_id'=> $user['id']])->whereIn('status',[1,2])->find()){ $user['is_driver'] = 1; $user['taxi_user_id'] = isset($taxiUser['id'])? $taxiUser['id'] : 0; $user['taxi_level'] = isset($taxiUser['level'])? $taxiUser['level'] : 0; } unset($user['paycode']); // 2. 加载已提现金额 $user['withdraw'] = model('common/UsersWithdraw') ->where([ 'user_id' => $this->auth->user()['id'], 'status' => 2 ])->sum('amount'); return $this->ApiJson(0,'加载用户数据成功', $user); } throw new TokenException([ 'errmsg' => 'Unauthorized:Request token denied!' ]); } /** * 更新用户信息 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/7/6 15:33 * * @return \think\response\Json * @throws \Lettered\Support\Exceptions\FailedException */ public function updateUserInfo() { // 接收参数 $params = $this->request->param(); // 参数校验 $valid = $this->validate($params, [ 'field|更新字段' => 'require', 'value|更新内容' => 'require' ]); // 错误返回 if (true !== $valid) { return $this->ApiJson(-1, $valid); } // 交易密码要验证手机码 // if ($params['field'] == 'paycode'){ // 验证 $sms = new SmsCode(); // if (!$sms->verify(input('mobile'),input('vercode'))) { // return $this->ApiJson(-1, "验证码错误!"); // } // } // 改 model('common/Users') ->updateBy($this->auth->user()['id'], [ $params['field'] => $params['value'] ]); return $this->ApiJson(0, '更新信息成功'); } /** * 金额提现 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/7/8 18:30 * * @return \think\response\Json * @throws \Lettered\Support\Exceptions\FailedException */ public function userWithdraw() { // 接收参数 $params = $this->request->param(); // 参数校验 $valid = $this->validate($params, [ 'realname|真实姓名' => 'require', 'type|收款方式' => 'require', // 'bank|开户行' => 'requireIf:type,1', 'account|收款账号' => 'require', 'amount|提现金额' => 'require', 'paycode|交易密码' => 'require' ]); // 错误返回 if(true !== $valid){ return $this->ApiJson(-1, $valid); } // 密码验证 $user = $this->auth->user(); if ($user['paycode'] !== $params['paycode']){ return $this->ApiJson(-1, "交易密码有误,请检查!"); } // 资金是不是提现之后再扣减还是直接扣减,驳回的时候再返回 // 用户余额限制 if ($user['balance'] <= sys_config('user_withdraw_limit','user')){ return $this->ApiJson(-1, "当前可提现金额不满足!余额:【". $user['balance'] . "】"); } // 最低限制 if ($params['amount'] <= ($limit = sys_config('user_withdraw_limit','user'))){ // return $this->ApiJson(-1, "申请提现金额不满足最低提现!最低:【" . $limit . "】"); } // 最高限制 if ($user['balance'] <= $params['amount']){ return $this->ApiJson(-1, "当前可提现金额不满足!余额:【". $user['balance'] . "】"); } // 写入用户ID $params['user_id'] = $this->auth->user()['id']; // 交易单号 $params['draw_no'] = get_order_no(); // p($params, 1); // 写入数据 $result = false; Db::startTrans(); try { $params['status'] = 2; $ret = model('common/UsersWithdraw')::create($params,true); // 加载配置 $wechat = sys_config('', 'wechat'); $config = [ // 前面的appid什么的也得保留哦 'app_id' => $wechat['mini_appid'], 'mch_id' => $wechat['pay_mch_id'], 'key' => $wechat['pay_secret_key'], // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书) 'cert_path' => $wechat['cert_path'], // XXX: 绝对路径!!!! 'key_path' => $wechat['key_path'], // XXX: 绝对路径!!!! // 'notify_url' => 'https://api.gxrrj.cn/api/v1/wechat/notify', // 'notify_url' => 'http://rrj.gxnwsoft.com/api/v1/wechat/refundNotify', // 'sandbox' => true ]; // 创建应用实例 $app = Factory::payment($config); $result = $app->transfer->toBalance([ 'partner_trade_no' => $ret['draw_no'], // 商户订单号,需保持唯一性(只能是字母或者数字,不能包含有符号) 'openid' => $user['open_id'], 'check_name' => 'FORCE_CHECK', // NO_CHECK:不校验真实姓名, FORCE_CHECK:强校验真实姓名 're_user_name' => $ret['realname'], // 如果 check_name 设置为FORCE_CHECK,则必填用户真实姓名 'amount' => $ret['amount'] * 100, // 企业付款金额,单位为分 'desc' => '用户提现', // 企业付款操作说明信息。必填 ]); $result = $result['return_code'] == 'SUCCESS' && $result['result_code'] != 'FAIL'; $Users = new Users(); $Users->changeBalance($user['id'], $ret['amount'], '余额提现'); Db::commit(); } catch(Exception $e) { Db::rollback(); } if (!$result){ return $this->ApiJson(-1, '数据异常,请稍后重试'); } return $this->ApiJson(0, '更新信息成功'); } /** * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/7/2 10:50 * * @return \think\response\Json * @throws \Lettered\Support\Exceptions\FailedException */ public function userVerify() { // 获取信息 $verify = model('common/UsersVerify')->getBy(['user_id' => $this->auth->user()['id']]); // 这简单做,get 查状态 post 修改数据 if ($this->request->isPost()){ // 接收参数 $params = $this->request->param(); // 参数校验 $valid = $this->validate($params, [ 'name|真实姓名' => 'require', 'id_card|身份证号' => 'require', 'id_card_img|身份证信息' => 'require' ]); // 错误返回 if(true !== $valid){ return $this->ApiJson(-1, $valid); } // 取得当前用户 $user = $this->auth->user(); if (!$verify) { // 写入用户ID $params['user_id'] = $user['id']; // 写入数据 $ret = model('common/UsersVerify')::create($params,true); }else { // 更新数据 $ret = $verify->updateBy($verify['id'], $params); } // 提交或者创建,用户这都是待审核状态 1 model('common/Users')->updateBy($user['id'], [ 'is_verify' => 1 ]); // 消息 if ($ret) { return $this->ApiJson(0,'身份信息提交成功,请等待审核'); } return $this->ApiJson(-1,'数据异常,请稍后再试'); } return $this->ApiJson(0,'获取信息成功', $verify); } /** * 获取用户资金记录 * 资金记录表和资产记录表应该可以合并为一张表,然后字段区分 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/7/9 11:39 * * @return \think\response\Json * @throws \Lettered\Support\Exceptions\FailedException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getUserBalance() { $param = $this->request->param(); $limit = 10; // 数据校验 $valid = $this->validate($param, [ 'page' => 'require', ]); // 错误 if (true !== $valid) { return $this->ApiJson(-1, $valid); } $withdraw = []; switch ($param['type']){ case "balance": $model = "UsersBalanceRecord"; break; case "property": $model = "UsersPropertyRecord"; break; default : $model = "UsersWithdraw"; } // 按月份 for($month = 1; $month <= date('m'); $month ++ ){ $data = model('common/' . $model) ->where(['user_id' => $this->auth->user()['id']]) ->field("*,FROM_UNIXTIME(created_at, '%Y-%m-%d %H:%i:%s') as created_at") ->whereTime('created_at', 'between', $this->getMonthTime($month)) // ->limit((($param['page'] - 1) * $limit) . "," . $limit) ->order(['id' => 'desc', 'updated_at' => 'desc']) ->select(); if (count($data) != 0) $withdraw[date('Y') . $month] = $data; } return $this->ApiJson(0, '获取信息成功', $withdraw); } private function getMonthTime($month) { // $firstday = date('Y-m-01', strtotime(date('Y') . '-' . $month . '-1')); $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day")); return array($firstday,$lastday); } /** * 获取用户分享二维码 * 1.自己的码还是小程序码? * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/6/30 15:59 * * @return \think\response\Json */ public function getUserSpread() { // 生成小程序码 // 1. 是否生成 // 2. 为生成创建返回/直接返回 //$Qr = (new Qrcode())->createServer("/pages/index/index?spd=RYVBJKC"); // return $this->ApiJson(0,'',$Qr); } /** * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/7/2 16:16 * * @return \think\response\Json * @throws \Lettered\Support\Exceptions\FailedException */ public function joinInStore() { // 获取信息 $store = model('common/Seller')->getBy(['user_id' => $this->auth->user()['id']]); // 这简单做,get 查状态 post 修改数据 if ($this->request->isPost()){ // 接收参数 $params = $this->request->param(); // 参数校验 $valid = $this->validate($params, [ 'area_id|区域信息' => 'require', 'lng|经度位置' => 'require', 'lat|维度位置' => 'require', 'contact|联系人' => 'require', 'seller_name|店铺信息' => 'require', 'products|主营产品' => 'require', 'mobile|手机号' => 'require', 'province|详细地址' => 'require', 'city|详细地址' => 'require', 'country|详细地址' => 'require', 'address|详细地址' => 'require', 'fd_img|门店照片' => 'require', 'bi_license|营业执照' => 'require', ]); // 错误返回 if(true !== $valid){ return $this->ApiJson(-1, $valid); } // 验证码 // $sms = new SmsCode(); // if (!$sms->verify(input('mobile'),input('vercode'))) { // return $this->ApiJson(-1, "验证码错误!"); // } // 再次验证身份信息 $verify = model("common/UsersVerify") ->getBy(['user_id' => $this->auth->user()['id']]); if($verify['status'] != 2){ return $this->ApiJson(-1, "身份验证尚未完成,请完善后再试!"); } // 用户身份信息复用 $params['id_card'] = $verify['id_card']; $params['id_card_img'] = $verify['id_card_img']; if (!$store) { // 写入用户信息 身份证信息 $params['user_id'] = $this->auth->user()['id']; // 写入数据 $ret = model('common/Seller')::create($params,true); }else { // 更新数据 $ret = $store->updateBy($store['id'], $params); } // 消息 if ($ret) { return $this->ApiJson(0,'入驻信息提交成功,请等待审核'); } return $this->ApiJson(-1,'数据异常,请稍后再试'); } return $this->ApiJson(0,'获取信息成功', $store); } /** * 代理统计 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/7/10 16:43 * * @return \think\response\Json * @throws \Lettered\Support\Exceptions\FailedException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function userAgent() { // 接收参数 $params = $this->request->param(); // 参数校验 $valid = $this->validate($params, [ 'area_id|地区数据' => 'require' ]); // 错误返回 if(true !== $valid){ return $this->ApiJson(-1, $valid); } // 1. 查我的代理信息 $agent = model('common/UsersAgent')->getBy(['user_id' => $this->auth->user()['id']]); // 我是代理 if ($agent && $agent['status'] == 2){ // 1. 查我代理区域的订单信息和营业额信息 $where = ['area_id' => $agent['area_id']]; // 商品订单统计 // 摩的订单统计 // 配送订单统计 // 技能订单统计 $data = [ 'goods' => db('goods_order')->where($where)->field("count(id) as total_order,sum(pay_price) as total_price")->find(), 'motor' => db('taxi_order')->where($where)->field("count(id) as total_order,sum(price) as total_price")->find(), 'skill' => db('skill_order')->where($where)->field("count(id) as total_order,sum(price) as total_price")->find(), 'mission' => db('mission_order')->where($where)->field("count(id) as total_order,sum(price) as total_price")->find(), ]; $total_order = 0; $total_fee = 0; foreach ($data as $item){ $total_order += $item['total_order']; $total_fee += $item['total_price']; } // 2. 我的区域信息 $info['area'] = db('china')->where(['id' => $agent['area_id']])->value('name'); // 总营业额 $info['total_fee'] = sprintf("%.2f",$total_fee); // 总订单 $info['total_order'] = $total_order; // 商户统计 $info['total_store'] = db('seller')->where(['area_id' => $agent['area_id']])->count(); return $this->ApiJson(0,'获取信息成功',[ 'info' => $info, 'data' => $data, 'agent' => $agent ]); } // 获取当前地区已经申请通过的 $pass = model('common/UsersAgent')->where('area_id','like', substr($params['area_id'], 0, strlen($params['area_id']) - 2) . '%' ) ->where('status','=','2')->select(); $area = model('common/China')->findBy($params['area_id']); $model = model('common/China'); $where = []; foreach ($pass as $p){ $where[] = ['id','<>', $p['area_id']]; } $areas = $model->where($where)->where(['parent_id' => $area['parent_id']])->select(); // ->where('id', ['>', 0], ['<>', 10], 'and') return $this->ApiJson(0,'获取信息成功', ['agent' => $agent,'areas' => $areas]); } /** * 月订单统计和列表 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/7/13 10:11 * * @return \think\response\Json * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function userAgentMonth() { // 接收参数 $params = $this->request->param(); $limit = 10; // 参数校验 $valid = $this->validate($params, [ 'area_id|地区数据' => 'require', 'otype|订单类型' => 'require', 'month|记录月份' => 'require', 'page' => 'require', ]); // 错误返回 if(true !== $valid){ return $this->ApiJson(-1, $valid); } switch ($params['otype']){ case "goods": $model = "GoodsOrder"; break; case "skill": $model = "SkillOrder"; break; case "motor": $model = "TaxiOrder"; break; case "mission": $model = "MissionOrder"; break; default : $model = "GoodsOrder"; } // 查询字段 $price_field = ($params['otype'] == 'goods') ? 'pay_price' : 'price'; // 月份处理 list($year, $month) = str2arr($params['month'],'-'); // 数据以及订单 $data = model('common/' . $model) ->where(['area_id' => $params['area_id']]) ->field("FROM_UNIXTIME(created_at,'%Y-%m') as month,count(id) as total_order,sum($price_field) total_price") ->whereTime('created_at', 'between', [$params['month'] . '-1', $year . '-' . ($month + 1) . '-1']) ->group('month') ->find(); $data['order'] = model('common/' . $model) ->where(['area_id' => $params['area_id']]) ->whereTime('created_at', 'between', [$params['month'] . '-1', $year . '-' . ($month + 1) . '-1']) ->limit((($params['page'] - 1) * $limit) . "," . $limit) ->select(); return $this->ApiJson(0,'获取信息成功', $data); } /** * 提交申请 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/8/28 9:08 * * @return \think\response\Json * @throws \Lettered\Support\Exceptions\FailedException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function userAgentApply() { // 接收参数 $params = $this->request->param(); // 参数校验 $valid = $this->validate($params, [ 'area_id|地区数据' => 'require' ]); // 错误返回 if(true !== $valid){ return $this->ApiJson(-1, $valid); } // 先看是否已经有这个区域代理了 if (model('common/UsersAgent')->getBy(['area_id' => $params['area_id'],'status' => 2])){ // 查找其他区域 $area = model('common/China')->findBy($params['area_id']); $parent = model('common/China')->where('id','<>',$params['area_id']) ->where(['parent_id' => $area['parent_id']])->select(); return $this->ApiJson(-1, "抱歉,当前区域已经存在代理,请选择其他区域申请!"); } // 查找 $agent = model('common/UsersAgent') ->getBy(['user_id' => $this->auth->user()['id'], 'area_id' => $params['area_id']]); // 写入用户信息 身份证信息 $params['user_id'] = $this->auth->user()['id']; // 更新用户信息 model('common/Users')->updateBy($params['user_id'],['is_agent' => 1]); if (!$agent) { // 写入数据 $ret = model('common/UsersAgent')::create([ 'user_id' => $params['user_id'], 'area_id' => $params['area_id'] ]); }else { // 更新数据 $ret = $agent->updateBy($agent['id'], [ 'area_id' => $params['area_id'], 'status' => 1 ]); } // 消息 if ($ret){ return $this->ApiJson(0,'提交成功,请等待管理员审核!', $ret); } return $this->ApiJson(-1,'数据异常,请稍后重试!'); } /** * 配送员申请 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/7/10 17:30 * * @return \think\response\Json * @throws \Lettered\Support\Exceptions\FailedException */ public function deliveryApply() { // 查找 $missionUser = model('common/MissionUser') ->getBy(['user_id' => $this->auth->user()['id']]); if ($this->request->isPost()){ // 接收参数 $params = $this->request->param(); $valid = $this->validate($params, [ 'hl_license|健康证明' => 'require', 'province|省份' => 'require', 'city|城市' => 'require', 'country|地区' => 'require', 'area|地区数据' => 'require', 'address|区域详情' => 'require', ]); // 错误返回 if(true !== $valid){ return $this->ApiJson(-1, $valid); } // 再次验证身份信息 $verify = model("common/UsersVerify") ->getBy(['user_id' => $this->auth->user()['id']]); if($verify['status'] != 2){ return $this->ApiJson(-1, "身份验证尚未完成,请完善后再试!"); } // 用户身份信息复用 $params['uname'] = $verify['name']; $params['id_card'] = $verify['id_card']; $params['id_card_img'] = $verify['id_card_img']; $params['status'] = 1; if (!$missionUser) { // 写入用户信息 身份证信息 $params['user_id'] = $this->auth->user()['id']; // 写入数据 $ret = model('common/MissionUser')::create($params, true); }else { // 更新数据 $ret = $missionUser->updateBy($missionUser['id'],$params); } // 消息 if ($ret){ return $this->ApiJson(0,'提交成功,请等待管理员审核!'); } return $this->ApiJson(-1,'数据异常,请稍后重试!'); } return $this->ApiJson(0,'获取信息成功', $missionUser); } /** * 用户文件上传 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/7/2 9:09 * * @return \think\response\Json */ public function uploadUserFile() { // 接收数据 $params = $this->request->param(); // 参数校验 $valid = $this->validate($params, [ 'action|上传操作' => 'require' ]); // 错误返回 if(true !== $valid){ return $this->ApiJson(-1, $valid); }; // 上传 $upload = new Upload(config('upload.')); $ret = $upload->setPath( '/' . $params['action'])->upload($this->request->file('file')); return $this->ApiJson(0,'', get_annex_url($ret)); } /** * 嘛呗出行红包发放 * @return \think\response\Json * @throws \Lettered\Support\Exceptions\FailedException */ public function receiveRedbag(){ $params = $this->request->param(); $sid = isset($params['sid'])? $params['sid'] : 0; if($sid<=0){ return $this->ApiJson(-1,'参数错误'); } if($sid == $this->auth->user()['id']){ return $this->ApiJson(-1,'自己分享给自己无奖励'); } $redbagMoney = sys_config('user_redbag_money','user'); $redbagNum = sys_config('user_redbag_num','user'); if($redbagMoney <= 0 || $redbagNum <= 0){ return $this->ApiJson(-1,'红包已被领完或参数错误'); } if(UsersBalanceRecord::where(['user_id'=> $sid,'source_uid'=> $this->auth->user()['id'],'type'=>2])->value('id')){ return $this->ApiJson(-1,'该用户红包已奖励过'); } $user = Users::where(['id'=> $sid])->field('id,nickname,balance')->find(); if(empty($user)){ return $this->ApiJson(-1,'奖励用户不存在'); } DB::startTrans(); $user->balance += $redbagMoney; $user->updated_at = time(); if(!$user->save()){ Db::rollback(); return $this->ApiJson(-1,'发放红包失败'); } if(!SystemConfig::where(['group'=>'user','name'=>'user_redbag_num'])->dec('value',1)){ Db::rollback(); return $this->ApiJson(-1,'发放红包失败'); } $data = [ 'user_id'=> $sid, 'source_uid'=> $this->auth->user()['id'], 'type'=> 2, 'action'=> 1, 'inc_amount'=> round($redbagMoney, 2), 'aft_amount'=> $user->balance, 'remark'=> '分享获得红包,金额【'.round($redbagMoney, 2).'】', 'created_at'=> time(), 'updated_at'=> time() ]; if(!UsersBalanceRecord::insertGetId($data)){ Db::rollback(); return $this->ApiJson(-1,'发放红包失败'); } DB::commit(); return $this->ApiJson(0,'发放红包成功'); } }