MarketController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. <?php
  2. /**
  3. * 会员中心模块
  4. * @author wesmiler
  5. */
  6. namespace app\api\controller;
  7. use app\weixin\model\AccountLog;
  8. use app\weixin\model\UserBalanceLog;
  9. use app\weixin\model\Member;
  10. use app\weixin\model\Storage;
  11. use app\weixin\model\UserContactLog;
  12. use app\weixin\model\UserLog;
  13. use app\weixin\model\UserProfile;
  14. use app\weixin\model\Wechat;
  15. use app\weixin\service\PRedis;
  16. use app\weixin\service\Sms;
  17. use app\weixin\validate\MemberValidate;
  18. use think\Db;
  19. class MarketController extends MarketBaseController
  20. {
  21. public function __construct()
  22. {
  23. parent::__construct();
  24. // 验证用户是否被冻结
  25. $userStatus = isset($this->userInfo['agent_status']) ? intval($this->userInfo['agent_status']) : 0;
  26. $freezingChoose = isset($this->userInfo['freezing_choose']) ? intval($this->userInfo['freezing_choose']) : 0;
  27. $action = request()->action();
  28. if(!in_array($action,['register'])){
  29. if ($this->userInfo && $userStatus == 3) {
  30. showJson(1006, $freezingChoose>0? 1020+$freezingChoose : 1011, ['url' => url('/weixin/page/custom', '', '', true)]);
  31. }
  32. }
  33. }
  34. /**
  35. * 获取用户信息
  36. */
  37. public function getInfo()
  38. {
  39. $type = input('type', 0);
  40. $id = input('id', 0); // 当前浏览的用户ID
  41. $userId = $id? $id : $this->userId;
  42. $memberInfo = Member::getInfo(['id' => $userId,'agent_type'=> 1]);
  43. // 冻结
  44. $userStatus = isset($memberInfo['user_status']) ? intval($memberInfo['user_status']) : 0;
  45. if ($userStatus == 3) {
  46. showJson(1006, 2102, ['url' => url('/weixin/page/custom', '', '', true)]);
  47. }
  48. if ($memberInfo) {
  49. $memberInfo['avatar'] = $memberInfo['avatar'] ? cmf_get_image_preview_url($memberInfo['avatar']) : '';
  50. if (isset($memberInfo['mobile'])) {
  51. $memberInfo['mobile'] = $memberInfo['mobile'] ? formatStr($memberInfo['mobile']) : '';
  52. }
  53. if ($type == 1) {
  54. $accountConfig = $siteInfo = cmf_get_option('account_config');
  55. $withdrawCost = isset($accountConfig['withdraw_cost']) ? floatval($accountConfig['withdraw_cost']) : 0;
  56. $minWithdraw = isset($accountConfig['min_withdraw']) ? floatval($accountConfig['min_withdraw']) : 1;
  57. $memberInfo['min_withdraw'] = $minWithdraw ? $minWithdraw : 1;
  58. $memberInfo['withdraw_cost'] = $withdrawCost ? $withdrawCost : 0;
  59. $memberInfo['balance'] = str_replace('.00','', $memberInfo['balance']);
  60. $memberInfo['rank_count'] = UserBalanceLog::getRankCount();
  61. // 收益
  62. $income = UserBalanceLog::where(['user_id'=> $userId,'status'=>2])
  63. ->where('type','>',1)
  64. ->where('type','<',30)
  65. ->sum('change');
  66. $memberInfo['income'] = moneyFormat($income, 2);
  67. $memberInfo['income'] = str_replace('.00','', $memberInfo['income']);
  68. // 累计提现
  69. $withdrawTotal = UserBalanceLog::where(['user_id'=> $userId,'type'=> 1,'change_type'=> 2,'status'=>2])
  70. ->sum('change');
  71. $memberInfo['withdraw_total'] = moneyFormat($withdrawTotal, 2);
  72. $memberInfo['withdraw_total'] = str_replace('.00','', $memberInfo['withdraw_total']);
  73. }
  74. if ($type == 2) {
  75. // 团队邀请二维码
  76. $inviteUrl = url('/weixin/market/entry?sid='.$userId, '', '', true);
  77. $qrcode = Wechat::makeNormalQrcode($inviteUrl);
  78. $memberInfo['team_qrcode'] = $qrcode ? $qrcode : '';
  79. // 会员邀请二维码,2000以内永久
  80. $randCount = Member::where(['user_type'=> 2, 'agent_type'=> 1, 'agent_status'=> 1])->where('id','<=', $userId)->count('id');
  81. if($randCount>2000){
  82. $qrcodeData = Wechat::makeQrcode($userId, $userId);
  83. }else{
  84. $qrcodeData = Wechat::makeQrcode($userId, $userId, 'qrcode_over','QR_LIMIT_STR_SCENE', 0);
  85. }
  86. $memberInfo['randCount'] = $randCount;
  87. $memberInfo['qrcode'] = isset($qrcodeData['qrcode']) ? $qrcodeData['qrcode'] : '';
  88. }
  89. // 更新登录时间
  90. if(!PRedis::get("members:login")){
  91. Member::where(['id'=> $this->userId])->update(['last_login_time'=> time()]);
  92. PRedis::set("members:login", $this->userId, rand(300, 600));
  93. }
  94. }
  95. showJson(1005, 1001, $memberInfo);
  96. }
  97. /**
  98. * 获取主页信息
  99. */
  100. public function getHomeInfo()
  101. {
  102. $id = input('id', 0);
  103. $cid = input('cid', 0);
  104. $type = input('type', 1);
  105. // 基础信息
  106. $userId = $id ? $id : $this->userId;
  107. $myInfo = Member::getHomeInfo($userId, '', $type);
  108. if($myInfo){
  109. $myInfo['wechat_code'] = isset($myInfo['wechat_account'])? $myInfo['wechat_account'] : '';
  110. }
  111. $cUserInfo = [];
  112. $cInfo = [];
  113. if ($cid <= 0 && $id) {
  114. $cid = UserContactLog::where(['user_id' => $this->userId, 'contact_uid' => $id])
  115. ->where('status', 'in', [1, 2, 3])
  116. ->value('id');
  117. if ($cid <= 0) {
  118. $cid = UserContactLog::where(['contact_uid' => $this->userId, 'user_id' => $id])
  119. ->where('status', 'in', [1, 2, 3])
  120. ->value('id');
  121. }
  122. }
  123. if ($cid > 0) {
  124. // 认识记录
  125. $cInfo = UserContactLog::where(['id' => $cid])->where('status', 'in', [1, 2, 3])->field('id,user_id,contact_uid,is_read,status')->find();
  126. $cUid = isset($cInfo['contact_uid']) ? intval($cInfo['contact_uid']) : 0;
  127. $status = isset($cInfo['status']) ? intval($cInfo['status']) : 0;
  128. if ($cUid) {
  129. // 被申请方查看,更新申请微信阅读状态
  130. if($cUid == $this->userId){
  131. UserContactLog::where(['id' => $cid])->update(['is_read'=> 1]);
  132. }
  133. // 想认识的人的信息
  134. $field = 'm.id,m.user_nickname,m.avatar,m.real_name,up.wechat_code,up.qq';
  135. $cUserInfo = Member::getHomeInfo($cUid, $field, 2);
  136. // 未确认认识的不展示联系方式
  137. if ($status != 2 && $cUserInfo) {
  138. $cUserInfo['wechat_code'] = '';
  139. $cUserInfo['qq'] = '';
  140. }
  141. }
  142. }
  143. PRedis::set('test:' . $userId, ['homeInfo' => $myInfo, 'cUserInfo' => $cUserInfo, 'cInfo' => $cInfo], 600);
  144. showJson(1005, 1008, ['homeInfo' => $myInfo, 'cUserInfo' => $cUserInfo, 'cInfo' => $cInfo]);
  145. }
  146. /**
  147. * 注册
  148. */
  149. public function register(){
  150. try {
  151. $params = input();
  152. $inviteId = input('sid', 0);
  153. $shopType = input('type', 0);
  154. $inviteId = $inviteId? $inviteId : session('invite_id');
  155. $validate = new MemberValidate();
  156. if (!$validate->scene('marketReg')->check($params)) {
  157. $message = $validate->getError()? $validate->getError() : '2020';
  158. showJson(1004, $message);
  159. }
  160. $wxInfo = session('wxInfo');
  161. $openid = isset($wxInfo['openid'])? $wxInfo['openid'] : '';
  162. if(empty($openid)){
  163. showJson(1004,2039);
  164. }
  165. $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  166. $code = isset($params['code']) ? trim($params['code']) : '';
  167. $result = Sms::checkCode($mobile, $code,'marketReg');
  168. if ($result !== true) {
  169. $message = $result? $result : '2020';
  170. showJson(1004, $message);
  171. }
  172. // 验证手机号码
  173. if (Member::where(['user_login' => $mobile, 'agent_type' => 1])->where('agent_status','in',[0,1,2])->value('id')) {
  174. showJson(1004,2001);
  175. }
  176. // 验证信息
  177. $where = ['openid' => $openid, 'user_type'=> 2];
  178. $info = Member::where($where)->field('id,agent_type,sex,avatar,agent_status')->find();
  179. $agentId = isset($info['id'])? $info['id'] : 0;
  180. $agentType = isset($info['agent_type'])? $info['agent_type'] : 0;
  181. $agentStatus = isset($info['agent_status'])? $info['agent_status'] : 0;
  182. $hasAvatar = isset($info['avatar'])? $info['avatar'] : '';
  183. $hasSex = isset($info['sex'])? $info['sex'] : 0;
  184. if($info && $agentType==1){
  185. // 已审核
  186. if($agentStatus == 1){
  187. showJson(1006,2040,['url'=> Wechat::makeRedirectUrl(url('/weixin/market/index','','',true))]);
  188. } else if ($agentStatus == 2){
  189. showJson(1006,2044,['url'=> Wechat::makeRedirectUrl(url('/weixin/page/custom','','',true))]);
  190. }else if($agentStatus <= 0){
  191. showJson(1006,2041,['url'=> Wechat::makeRedirectUrl(url('/weixin/page/custom','','',true))]);
  192. }
  193. }
  194. // 头像
  195. $avatar = '';
  196. if(empty($hasAvatar)){
  197. $avatar = isset($wxInfo['headimgurl']) ? trim($wxInfo['headimgurl']) : '';
  198. $file = request()->file('image');
  199. if (!empty($file) && $file != null) {
  200. $fileData = Storage::uploadImg($file, 'avatar');
  201. $avatar = isset($fileData['file']) ? $fileData['file'] : '';
  202. }
  203. if (empty($avatar)) {
  204. showJson(1004, 3004);
  205. }
  206. }
  207. Db::startTrans();
  208. $userPass = cmf_password('123456');
  209. $memberData = [
  210. 'parent_id'=> $inviteId,
  211. 'openid'=> $openid,
  212. 'user_login'=> $mobile,
  213. 'real_name' => isset($params['realname']) ? trim($params['realname']) : '',
  214. 'user_nickname' => isset($params['nickname']) ? trim($params['nickname']) : '',
  215. 'user_type' => 2,
  216. 'agent_status' => 2,
  217. 'user_status' => 1,
  218. 'user_pass' => $userPass,
  219. 'pay_password' => $userPass,
  220. 'last_login_ip' => get_client_ip(),
  221. 'wechat_account' => isset($params['wechat_code']) ? trim($params['wechat_code']) : '',
  222. 'is_follow' => isset($wxInfo['subscribe']) ? intval($wxInfo['subscribe']) : 0,
  223. 'last_login_time' => time(),
  224. 'agent_create_time' => time(),
  225. 'agent_type' => 1,
  226. 'shop_type' => $shopType,
  227. ];
  228. if ($avatar) {
  229. $memberData['avatar'] = $avatar;
  230. }
  231. if(!$hasSex){
  232. $memberData['sex'] = isset($wxInfo['sex']) ? intval($wxInfo['sex']) : 0;
  233. }
  234. if (!$agentId) {
  235. $res = $agentId = Member::insertGetId($memberData);
  236. } else {
  237. $where['id'] = $agentId;
  238. $res = Member::where($where)->update($memberData);
  239. }
  240. if(!$res){
  241. Db::rollback();
  242. showJson(1004, 2104);
  243. }
  244. $nowAddress = isset($params['now_address']) ? trim($params['now_address']) : '';
  245. $nowAddress = $nowAddress ? explode(' ', $nowAddress) : [];
  246. $profileData = [
  247. 'userid' => $agentId,
  248. 'province' => isset($nowAddress[0]) ? $nowAddress[0] : '',
  249. 'city' => isset($nowAddress[1]) ? $nowAddress[1] : '',
  250. 'district' => isset($nowAddress[2]) ? $nowAddress[2] : '',
  251. ];
  252. if (UserProfile::checkProfile($agentId)) {
  253. $profileData['updated_at'] = date('Y-m-d H:i:s');
  254. $res = UserProfile::saveData(['userid' => $agentId], $profileData);
  255. } else {
  256. $res = UserProfile::insertGetId($profileData);
  257. }
  258. if (!$res) {
  259. Db::rollback();
  260. showJson(1004, 2104);
  261. }
  262. // 操作日志
  263. UserLog::saveLog(['user_id' => $agentId, 'type' => 1, 'content' => '申请注册成为分销用户']);
  264. Db::commit();
  265. // 更新缓存
  266. $userInfo = Member::getInfo(['id' => $agentId]);
  267. session('agentInfo', $userInfo);
  268. session('userInfo', null);
  269. showJson(1005, 2043);
  270. } catch (\Exception $exception) {
  271. PRedis::set('members:marketReg:error:' . $agentId, $exception->getMessage(), 6 * 3600);
  272. showJson(1004, 2136);
  273. }
  274. }
  275. /**
  276. * 保存资料
  277. */
  278. public function saveInfo()
  279. {
  280. $params = input();
  281. $validate = new MemberValidate();
  282. if (!$validate->scene('marketInfo')->check($params)) {
  283. showJson(1004, $validate->getError());
  284. }
  285. $newMobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  286. $mobile = isset($this->userInfo['mobile']) ? $this->userInfo['mobile'] : '';
  287. if ($newMobile && $newMobile != $mobile) {
  288. $code = isset($params['code']) ? trim($params['code']) : '';
  289. $result = Sms::checkCode($newMobile, $code);
  290. if ($result !== true) {
  291. showJson(1004, $result);
  292. }
  293. // 验证手机号码是否被使用
  294. $id = Member::where(['mobile' => $newMobile])->value('id');
  295. if ($id && $id != $this->userId) {
  296. showJson(1004, 2001);
  297. }
  298. }
  299. $nowAddress = isset($params['now_address']) ? trim($params['now_address']) : '';
  300. $nowAddress = $nowAddress ? explode(' ', $nowAddress) : [];
  301. $birthday = isset($params['birthday']) ? strtotime($params['birthday']) : 0;
  302. $info = [
  303. 'userid' => $this->userId,
  304. 'province' => isset($nowAddress[0]) ? $nowAddress[0] : '',
  305. 'city' => isset($nowAddress[1]) ? $nowAddress[1] : '',
  306. 'district' => isset($nowAddress[2]) ? $nowAddress[2] : '',
  307. 'qq' => isset($params['qq']) ? trim($params['qq']) : '',
  308. ];
  309. Db::startTrans();
  310. if (UserProfile::checkProfile($this->userId)) {
  311. $profileData['updated_at'] = date('Y-m-d H:i:s');
  312. $res = UserProfile::saveData(['userid' => $this->userId], $info);
  313. } else {
  314. $res = UserProfile::insertGetId($info);
  315. }
  316. if (!$res) {
  317. Db::rollback();
  318. showJson(1004, 2104);
  319. }
  320. $memberData = [
  321. 'user_nickname' => isset($params['nickname']) ? trim($params['nickname']) : '',
  322. 'birthday' => $birthday,
  323. 'wechat_account' => isset($params['wechat_code']) ? trim($params['wechat_code']) : '',
  324. 'sex' => isset($params['sex']) ? intval($params['sex']) : 0,
  325. ];
  326. if ($newMobile && $mobile != $newMobile) {
  327. $memberData['mobile'] = $newMobile;
  328. }
  329. if (!Member::saveData(['id' => $this->userId], $memberData)) {
  330. Db::rollback();
  331. showJson(1004, 2104);
  332. }
  333. Db::commit();
  334. showJson(1005, 2029);
  335. }
  336. /**
  337. * 余额收益提现
  338. * @throws \think\db\exception\DataNotFoundException
  339. * @throws \think\db\exception\ModelNotFoundException
  340. * @throws \think\exception\DbException
  341. */
  342. public function doWithdraw()
  343. {
  344. // 验证
  345. $this->checkUser();
  346. $money = input('money', 0);
  347. if (empty($money)) {
  348. showJson(1004, 4010);
  349. }
  350. $accountConfig = cmf_get_option('account_config');
  351. $minWithdraw = isset($accountConfig['min_withdraw']) ? intval($accountConfig['min_withdraw']) : 1;
  352. $withdrawCost = isset($accountConfig['withdraw_cost']) ? floatval($accountConfig['withdraw_cost']) : 0;
  353. if ($minWithdraw && $money < $minWithdraw) {
  354. showJson(1004, '最低提现金额为:' . $minWithdraw . '元');
  355. }
  356. /*$maxWithdraw = isset($accountConfig['max_withdraw']) ? intval($accountConfig['max_withdraw']) : 1000;
  357. if ($maxWithdraw && $money < $maxWithdraw) {
  358. showJson(1004, '最高提现金额为:' . $maxWithdraw . '元');
  359. }*/
  360. $payMoney = $withdrawCost>0? moneyFormat(($withdrawCost/100)*$money) : $money;
  361. $orderSn = makeTradeNo('WH', $this->userId);
  362. $memberInfo = Member::where(['id' => $this->userId,'agent_type'=> 1])->field('openid,balance')->find();
  363. $balance = isset($memberInfo['balance']) ? floatval($memberInfo['balance']) : 0;
  364. if($balance < $money){
  365. showJson(1004, 4011);
  366. }
  367. Db::startTrans();
  368. if(!Member::where(['id'=> $this->userId, 'agent_type'=> 1])->setDec('balance',$money)){
  369. Db::rollback();
  370. showJson(1004, 4013);
  371. }
  372. $log = [
  373. 'order_sn' => $orderSn,
  374. 'change' => $money,
  375. 'type' => 1,
  376. 'change_type' => 2,
  377. 'pay_money' => $payMoney,
  378. 'user_id' => $this->userId,
  379. 'balance' => moneyFormat($balance-$money),
  380. 'description' => "余额账户提现:{$money}元,实付:{$payMoney}元",
  381. 'create_time' => time(),
  382. 'status' => 1,
  383. ];
  384. $orderId = UserBalanceLog::insertGetId($log);
  385. if ($orderId) {
  386. Db::commit();
  387. showJson(1005, 4012, ['id'=> $orderId]);
  388. } else {
  389. Db::rollback();
  390. showJson(1004, 4014);
  391. }
  392. }
  393. /**
  394. * 获取邀请团队或会员列表
  395. */
  396. public function getInviteList(){
  397. $type = input('type', 1);
  398. if (!in_array($type, [1,2])) {
  399. showJson(1004, 4010);
  400. }
  401. $uid = input('uid', 0);
  402. $pageSize = input('pageSize', 20);
  403. $inviteId = $uid? $uid : $this->userId;
  404. $dataList = Member::getInviteList($inviteId, $type, $pageSize);
  405. showJson(1005, 1001, $dataList);
  406. }
  407. /**
  408. * 收益排行榜
  409. * @throws \think\exception\DbException
  410. */
  411. public function getIncomeRank(){
  412. $uid = input('uid', 0);
  413. $userId = $uid? $uid : $this->userId;
  414. $pageSize = input('pageSize', 50);
  415. $dataList = UserBalanceLog::getIncomeRankList($pageSize, $userId, 1);
  416. showJson(1005, 1001, $dataList);
  417. }
  418. }