| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682 |
- <?php
- namespace app\api\controller\v1;
- use app\api\controller\ApiController;
- use app\api\service\SmsCode;
- use app\common\model\Users;
- use app\http\IResponse;
- use EasyWeChat\Factory;
- use Lettered\Support\Upload;
- use Lettered\Support\Exceptions\TokenException;
- use think\Db;
- use think\Exception;
- class User extends ApiController
- {
- /**
- * 获取个人中心数据
- * 1. 用户昵称、手机号、头像、余额、提现
- *
- * @author 许祖兴 < zuxing.xu@lettered.cn>
- * @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: 用户冻结!'
- ]);
- }
-
- 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));
- }
- }
|