| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- <?php
- /**
- * 会员中心模块
- * @author wesmiler
- */
- namespace app\api\controller;
- use app\weixin\model\AccountLog;
- use app\weixin\model\UserBalanceLog;
- use app\weixin\model\Member;
- use app\weixin\model\Storage;
- use app\weixin\model\UserLog;
- use app\weixin\model\UserProfile;
- use app\weixin\model\Wechat;
- use app\weixin\service\PRedis;
- use app\weixin\service\Sms;
- use app\weixin\validate\MemberValidate;
- use think\Db;
- class MarketController extends MarketBaseController
- {
- public function __construct()
- {
- parent::__construct();
- // 验证用户是否被冻结
- $userStatus = isset($this->userInfo['agent_status']) ? intval($this->userInfo['agent_status']) : 0;
- $freezingChoose = isset($this->userInfo['freezing_choose']) ? intval($this->userInfo['freezing_choose']) : 0;
- $action = request()->action();
- if(!in_array($action,['register'])){
- if ($this->userInfo && $userStatus == 3) {
- showJson(1006, $freezingChoose>0? 1020+$freezingChoose : 1011, ['url' => url('/weixin/page/custom', '', '', true)]);
- }
- }
- }
- /**
- * 获取用户信息
- */
- public function getInfo()
- {
- $type = input('type', 0);
- $id = input('id', 0); // 当前浏览的用户ID
- $userId = $id? $id : $this->userId;
- $memberInfo = Member::getInfo(['id' => $userId,'agent_type'=> 1]);
- // 冻结
- $userStatus = isset($memberInfo['user_status']) ? intval($memberInfo['user_status']) : 0;
- if ($userStatus == 3) {
- showJson(1006, 2102, ['url' => url('/weixin/page/custom', '', '', true)]);
- }
- if ($memberInfo) {
- $memberInfo['avatar'] = $memberInfo['avatar'] ? cmf_get_image_preview_url($memberInfo['avatar']) : '';
- if (isset($memberInfo['mobile'])) {
- $memberInfo['mobile'] = $memberInfo['mobile'] ? formatStr($memberInfo['mobile']) : '';
- }
- if ($type == 1) {
- $accountConfig = $siteInfo = cmf_get_option('account_config');
- $withdrawCost = isset($accountConfig['withdraw_cost']) ? floatval($accountConfig['withdraw_cost']) : 0;
- $minWithdraw = isset($accountConfig['min_withdraw']) ? floatval($accountConfig['min_withdraw']) : 1;
- $memberInfo['min_withdraw'] = $minWithdraw ? $minWithdraw : 1;
- $memberInfo['withdraw_cost'] = $withdrawCost ? $withdrawCost : 0;
- $memberInfo['balance'] = str_replace('.00','', $memberInfo['balance']);
- // 收益
- $income = UserBalanceLog::where(['user_id'=> $userId,'status'=>2])
- ->where('type','>',1)
- ->sum('change');
- $memberInfo['income'] = moneyFormat($income, 2);
- $memberInfo['income'] = str_replace('.00','', $memberInfo['income']);
- // 累计提现
- $withdrawTotal = UserBalanceLog::where(['user_id'=> $userId,'type'=> 1,'change_type'=> 2,'status'=>2])
- ->sum('change');
- $memberInfo['withdraw_total'] = moneyFormat($withdrawTotal, 2);
- $memberInfo['withdraw_total'] = str_replace('.00','', $memberInfo['withdraw_total']);
- }
- if ($type == 2) {
- // 团队邀请二维码
- $inviteUrl = url('/weixin/market/entry?sid='.$userId, '', '', true);
- $qrcode = Wechat::makeNormalQrcode($inviteUrl);
- $memberInfo['team_qrcode'] = $qrcode ? $qrcode : '';
- // 会员邀请二维码
- $qrcodeData = Wechat::makeQrcode($userId, $userId);
- $memberInfo['qrcode'] = isset($qrcodeData['qrcode']) ? $qrcodeData['qrcode'] : '';
- }
- }
- showJson(1005, 1001, $memberInfo);
- }
- /**
- * 注册
- */
- public function register(){
- try {
- $params = input();
- $inviteId = input('sid', 0);
- $inviteId = $inviteId? $inviteId : session('invite_id');
- $validate = new MemberValidate();
- if (!$validate->scene('marketReg')->check($params)) {
- showJson(1004, $validate->getError());
- }
- $wxInfo = session('wxInfo');
- $openid = isset($wxInfo['openid'])? $wxInfo['openid'] : '';
- if(empty($openid)){
- showJson(2039);
- }
- $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
- $code = isset($params['code']) ? trim($params['code']) : '';
- $result = Sms::checkCode($mobile, $code,'marketReg');
- if ($result !== true) {
- showJson(1004, $result);
- }
- // 验证手机号码
- if (Member::where(['user_login' => $mobile, 'agent_type' => 1])->value('id')) {
- showJson(2001);
- }
- // 验证信息
- $where = ['openid' => $openid, 'user_type'=> 2];
- $info = Member::where($where)->field('id,agent_type,agent_status')->find();
- $agentId = isset($info['id'])? $info['id'] : 0;
- $agentType = isset($info['agent_type'])? $info['agent_type'] : 0;
- $agentStatus = isset($info['agent_status'])? $info['agent_status'] : 0;
- if($info && $agentType==1){
- // 已审核
- if($agentStatus == 1){
- showJson(1006,2040,['url'=> Wechat::makeRedirectUrl(url('/weixin/market/index','','',true))]);
- } else if ($agentStatus == 2){
- showJson(1006,2044,['url'=> Wechat::makeRedirectUrl(url('/weixin/page/custom','','',true))]);
- }else{
- showJson(1006,2041,['url'=> Wechat::makeRedirectUrl(url('/weixin/page/custom','','',true))]);
- }
- }
- // 头像
- $avatar = isset($wxInfo['headimgurl']) ? trim($wxInfo['headimgurl']) : '';
- $file = request()->file('image');
- if (!empty($file) && $file != null) {
- $fileData = Storage::uploadImg($file, 'avatar');
- $avatar = isset($fileData['file']) ? $fileData['file'] : '';
- }
- if (empty($avatar)) {
- showJson(1004, 3004);
- }
- Db::startTrans();
- $userPass = cmf_password('123456');
- $memberData = [
- 'parent_id'=> $inviteId,
- 'openid'=> $openid,
- 'user_login'=> $mobile,
- 'real_name' => isset($params['realname']) ? trim($params['realname']) : '',
- 'user_nickname' => isset($params['nickname']) ? trim($params['nickname']) : '',
- 'user_type' => 2,
- 'user_status' => 1,
- 'user_pass' => $userPass,
- 'pay_password' => $userPass,
- 'last_login_ip' => get_client_ip(),
- 'is_follow' => isset($wxInfo['subscribe']) ? intval($wxInfo['subscribe']) : 0,
- 'sex' => isset($wxInfo['sex']) ? intval($wxInfo['sex']) : 0,
- 'last_login_time' => time(),
- 'agent_create_time' => time(),
- 'agent_type' => 1,
- ];
- if ($avatar) {
- $memberData['avatar'] = $avatar;
- }
- if(!$agentId){
- $memberData['mobile'] = $mobile;
- }
- if (!$agentId) {
- $res = $agentId = Member::insertGetId($memberData);
- } else {
- $res = Member::where($where)->update($memberData);
- }
- if(!$res){
- Db::rollback();
- showJson(1004, 2104);
- }
- $nowAddress = isset($params['now_address']) ? trim($params['now_address']) : '';
- $nowAddress = $nowAddress ? explode(' ', $nowAddress) : [];
- $profileData = [
- 'userid' => $agentId,
- 'province' => isset($nowAddress[0]) ? $nowAddress[0] : '',
- 'city' => isset($nowAddress[1]) ? $nowAddress[1] : '',
- 'district' => isset($nowAddress[2]) ? $nowAddress[2] : '',
- 'wechat_code' => isset($params['wechat_code']) ? trim($params['wechat_code']) : '',
- ];
- if (UserProfile::checkProfile($agentId)) {
- $profileData['updated_at'] = date('Y-m-d H:i:s');
- $res = UserProfile::saveData(['userid' => $agentId], $profileData);
- } else {
- $res = UserProfile::insertGetId($profileData);
- }
- if (!$res) {
- Db::rollback();
- showJson(1004, 2104);
- }
- // 操作日志
- UserLog::saveLog(['user_id' => $agentId, 'type' => 1, 'content' => '申请注册成为分销用户']);
- Db::commit();
- // 更新缓存
- $userInfo = Member::getInfo(['id' => $agentId]);
- session('agentInfo', $userInfo);
- session('userInfo', null);
- showJson(1005, 2043);
- } catch (\Exception $exception) {
- PRedis::set('members:marketReg:error:' . $agentId, $exception->getMessage(), 6 * 3600);
- showJson(1004, 2136);
- }
- }
- /**
- * 保存资料
- */
- public function saveInfo()
- {
- $params = input();
- $validate = new MemberValidate();
- if (!$validate->scene('marketInfo')->check($params)) {
- showJson(1004, $validate->getError());
- }
- $newMobile = isset($params['mobile']) ? trim($params['mobile']) : '';
- $mobile = isset($this->userInfo['mobile']) ? $this->userInfo['mobile'] : '';
- if ($newMobile && $newMobile != $mobile) {
- $code = isset($params['code']) ? trim($params['code']) : '';
- $result = Sms::checkCode($newMobile, $code);
- if ($result !== true) {
- showJson(1004, $result);
- }
- // 验证手机号码是否被使用
- $id = Member::where(['mobile' => $newMobile])->value('id');
- if ($id && $id != $this->userId) {
- showJson(1004, 2001);
- }
- }
- $nowAddress = isset($params['now_address']) ? trim($params['now_address']) : '';
- $nowAddress = $nowAddress ? explode(' ', $nowAddress) : [];
- $birthday = isset($params['birthday']) ? strtotime($params['birthday']) : 0;
- $info = [
- 'userid' => $this->userId,
- 'province' => isset($nowAddress[0]) ? $nowAddress[0] : '',
- 'city' => isset($nowAddress[1]) ? $nowAddress[1] : '',
- 'district' => isset($nowAddress[2]) ? $nowAddress[2] : '',
- 'wechat_code' => isset($params['wechat_code']) ? trim($params['wechat_code']) : '',
- 'qq' => isset($params['qq']) ? trim($params['qq']) : '',
- ];
- Db::startTrans();
- if (UserProfile::checkProfile($this->userId)) {
- $profileData['updated_at'] = date('Y-m-d H:i:s');
- $res = UserProfile::saveData(['userid' => $this->userId], $info);
- } else {
- $res = UserProfile::insertGetId($info);
- }
- if (!$res) {
- Db::rollback();
- showJson(1004, 2104);
- }
- $memberData = [
- 'user_nickname' => isset($params['nickname']) ? trim($params['nickname']) : '',
- 'birthday' => $birthday,
- 'sex' => isset($params['sex']) ? intval($params['sex']) : 0,
- ];
- if ($newMobile && $mobile != $newMobile) {
- $memberData['mobile'] = $newMobile;
- }
- if (!Member::saveData(['id' => $this->userId], $memberData)) {
- Db::rollback();
- showJson(1004, 2104);
- }
- Db::commit();
- showJson(1005, 2029);
- }
- /**
- * 余额收益提现
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function doWithdraw()
- {
- // 验证
- $this->checkUser();
- $money = input('money', 0);
- if (empty($money)) {
- showJson(1004, 4010);
- }
- $accountConfig = $siteInfo = cmf_get_option('account_config');
- $minWithdraw = isset($accountConfig['min_withdraw']) ? intval($accountConfig['min_withdraw']) : 1;
- $withdrawCost = isset($accountConfig['withdraw_cost']) ? floatval($accountConfig['withdraw_cost']) : 0;
- if ($minWithdraw && $money < $minWithdraw) {
- showJson(1004, '最低提现金额为:' . $minWithdraw . '元');
- }
- /*$maxWithdraw = isset($accountConfig['max_withdraw']) ? intval($accountConfig['max_withdraw']) : 1000;
- if ($maxWithdraw && $money < $maxWithdraw) {
- showJson(1004, '最高提现金额为:' . $maxWithdraw . '元');
- }*/
- $payMoney = $withdrawCost>0? moneyFormat(($withdrawCost/100)*$money) : $money;
- $orderSn = makeTradeNo('WH', $this->userId);
- $memberInfo = Member::where(['id' => $this->userId,'agent_type'=> 1])->field('openid,balance')->find();
- $balance = isset($memberInfo['balance']) ? floatval($memberInfo['balance']) : 0;
- if($balance < $money){
- showJson(1004, 4011);
- }
- Db::startTrans();
- if(!Member::where(['id'=> $this->userId, 'agent_type'=> 1])->setDec('balance',$money)){
- Db::rollback();
- showJson(1004, 4013);
- }
- $log = [
- 'order_sn' => $orderSn,
- 'change' => $money,
- 'type' => 1,
- 'change_type' => 2,
- 'pay_money' => $payMoney,
- 'user_id' => $this->userId,
- 'balance' => moneyFormat($balance-$money),
- 'description' => "余额账户提现:{$money}元,实付:{$payMoney}元",
- 'create_time' => time(),
- 'status' => 1,
- ];
- $orderId = UserBalanceLog::insertGetId($log);
- if ($orderId) {
- Db::commit();
- showJson(1005, 4012, ['id'=> $orderId]);
- } else {
- Db::rollback();
- showJson(1004, 4014);
- }
- }
- /**
- * 获取邀请团队或会员列表
- */
- public function getInviteList(){
- $type = input('type', 1);
- if (!in_array($type, [1,2])) {
- showJson(1004, 4010);
- }
- $uid = input('uid', 0);
- $pageSize = input('pageSize', 20);
- $inviteId = $uid? $uid : $this->userId;
- $dataList = Member::getInviteList($inviteId, $type, $pageSize);
- showJson(1005, 1001, $dataList);
- }
- }
|