MarketController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. showJson(1005, 1001, $memberInfo);
  91. }
  92. /**
  93. * 获取主页信息
  94. */
  95. public function getHomeInfo()
  96. {
  97. $id = input('id', 0);
  98. $cid = input('cid', 0);
  99. $type = input('type', 1);
  100. // 基础信息
  101. $userId = $id ? $id : $this->userId;
  102. $myInfo = Member::getHomeInfo($userId, '', $type);
  103. if($myInfo){
  104. $myInfo['wechat_code'] = isset($myInfo['wechat_account'])? $myInfo['wechat_account'] : '';
  105. }
  106. $cUserInfo = [];
  107. $cInfo = [];
  108. if ($cid <= 0 && $id) {
  109. $cid = UserContactLog::where(['user_id' => $this->userId, 'contact_uid' => $id])
  110. ->where('status', 'in', [1, 2, 3])
  111. ->value('id');
  112. if ($cid <= 0) {
  113. $cid = UserContactLog::where(['contact_uid' => $this->userId, 'user_id' => $id])
  114. ->where('status', 'in', [1, 2, 3])
  115. ->value('id');
  116. }
  117. }
  118. if ($cid > 0) {
  119. // 认识记录
  120. $cInfo = UserContactLog::where(['id' => $cid])->where('status', 'in', [1, 2, 3])->field('id,user_id,contact_uid,is_read,status')->find();
  121. $cUid = isset($cInfo['contact_uid']) ? intval($cInfo['contact_uid']) : 0;
  122. $status = isset($cInfo['status']) ? intval($cInfo['status']) : 0;
  123. if ($cUid) {
  124. // 被申请方查看,更新申请微信阅读状态
  125. if($cUid == $this->userId){
  126. UserContactLog::where(['id' => $cid])->update(['is_read'=> 1]);
  127. }
  128. // 想认识的人的信息
  129. $field = 'm.id,m.user_nickname,m.avatar,m.real_name,up.wechat_code,up.qq';
  130. $cUserInfo = Member::getHomeInfo($cUid, $field, 2);
  131. // 未确认认识的不展示联系方式
  132. if ($status != 2 && $cUserInfo) {
  133. $cUserInfo['wechat_code'] = '';
  134. $cUserInfo['qq'] = '';
  135. }
  136. }
  137. }
  138. PRedis::set('test:' . $userId, ['homeInfo' => $myInfo, 'cUserInfo' => $cUserInfo, 'cInfo' => $cInfo], 600);
  139. showJson(1005, 1008, ['homeInfo' => $myInfo, 'cUserInfo' => $cUserInfo, 'cInfo' => $cInfo]);
  140. }
  141. /**
  142. * 注册
  143. */
  144. public function register(){
  145. try {
  146. $params = input();
  147. $inviteId = input('sid', 0);
  148. $inviteId = $inviteId? $inviteId : session('invite_id');
  149. $validate = new MemberValidate();
  150. if (!$validate->scene('marketReg')->check($params)) {
  151. $message = $validate->getError()? $validate->getError() : '2020';
  152. showJson(1004, $message);
  153. }
  154. $wxInfo = session('wxInfo');
  155. $openid = isset($wxInfo['openid'])? $wxInfo['openid'] : '';
  156. if(empty($openid)){
  157. showJson(1004,2039);
  158. }
  159. $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  160. $code = isset($params['code']) ? trim($params['code']) : '';
  161. $result = Sms::checkCode($mobile, $code,'marketReg');
  162. if ($result !== true) {
  163. $message = $result? $result : '2020';
  164. showJson(1004, $message);
  165. }
  166. // 验证手机号码
  167. if (Member::where(['user_login' => $mobile, 'agent_type' => 1])->where('agent_status','in',[0,1,2])->value('id')) {
  168. showJson(1004,2001);
  169. }
  170. // 验证信息
  171. $where = ['openid' => $openid, 'user_type'=> 2];
  172. $info = Member::where($where)->field('id,agent_type,agent_status')->find();
  173. $agentId = isset($info['id'])? $info['id'] : 0;
  174. $agentType = isset($info['agent_type'])? $info['agent_type'] : 0;
  175. $agentStatus = isset($info['agent_status'])? $info['agent_status'] : 0;
  176. if($info && $agentType==1){
  177. // 已审核
  178. if($agentStatus == 1){
  179. showJson(1006,2040,['url'=> Wechat::makeRedirectUrl(url('/weixin/market/index','','',true))]);
  180. } else if ($agentStatus == 2){
  181. showJson(1006,2044,['url'=> Wechat::makeRedirectUrl(url('/weixin/page/custom','','',true))]);
  182. }else{
  183. showJson(1006,2041,['url'=> Wechat::makeRedirectUrl(url('/weixin/page/custom','','',true))]);
  184. }
  185. }
  186. // 头像
  187. $avatar = isset($wxInfo['headimgurl']) ? trim($wxInfo['headimgurl']) : '';
  188. $file = request()->file('image');
  189. if (!empty($file) && $file != null) {
  190. $fileData = Storage::uploadImg($file, 'avatar');
  191. $avatar = isset($fileData['file']) ? $fileData['file'] : '';
  192. }
  193. if (empty($avatar)) {
  194. showJson(1004, 3004);
  195. }
  196. Db::startTrans();
  197. $userPass = cmf_password('123456');
  198. $memberData = [
  199. 'parent_id'=> $inviteId,
  200. 'openid'=> $openid,
  201. 'user_login'=> $mobile,
  202. 'real_name' => isset($params['realname']) ? trim($params['realname']) : '',
  203. 'user_nickname' => isset($params['nickname']) ? trim($params['nickname']) : '',
  204. 'user_type' => 2,
  205. 'agent_status' => 2,
  206. 'user_status' => 1,
  207. 'user_pass' => $userPass,
  208. 'pay_password' => $userPass,
  209. 'last_login_ip' => get_client_ip(),
  210. 'wechat_account' => isset($params['wechat_code']) ? trim($params['wechat_code']) : '',
  211. 'is_follow' => isset($wxInfo['subscribe']) ? intval($wxInfo['subscribe']) : 0,
  212. 'sex' => isset($wxInfo['sex']) ? intval($wxInfo['sex']) : 0,
  213. 'last_login_time' => time(),
  214. 'agent_create_time' => time(),
  215. 'agent_type' => 1,
  216. ];
  217. if ($avatar) {
  218. $memberData['avatar'] = $avatar;
  219. }
  220. if (!$agentId) {
  221. $res = $agentId = Member::insertGetId($memberData);
  222. } else {
  223. $where['id'] = $agentId;
  224. $res = Member::where($where)->update($memberData);
  225. }
  226. if(!$res){
  227. Db::rollback();
  228. showJson(1004, 2104);
  229. }
  230. $nowAddress = isset($params['now_address']) ? trim($params['now_address']) : '';
  231. $nowAddress = $nowAddress ? explode(' ', $nowAddress) : [];
  232. $profileData = [
  233. 'userid' => $agentId,
  234. 'province' => isset($nowAddress[0]) ? $nowAddress[0] : '',
  235. 'city' => isset($nowAddress[1]) ? $nowAddress[1] : '',
  236. 'district' => isset($nowAddress[2]) ? $nowAddress[2] : '',
  237. ];
  238. if (UserProfile::checkProfile($agentId)) {
  239. $profileData['updated_at'] = date('Y-m-d H:i:s');
  240. $res = UserProfile::saveData(['userid' => $agentId], $profileData);
  241. } else {
  242. $res = UserProfile::insertGetId($profileData);
  243. }
  244. if (!$res) {
  245. Db::rollback();
  246. showJson(1004, 2104);
  247. }
  248. // 操作日志
  249. UserLog::saveLog(['user_id' => $agentId, 'type' => 1, 'content' => '申请注册成为分销用户']);
  250. Db::commit();
  251. // 更新缓存
  252. $userInfo = Member::getInfo(['id' => $agentId]);
  253. session('agentInfo', $userInfo);
  254. session('userInfo', null);
  255. showJson(1005, 2043);
  256. } catch (\Exception $exception) {
  257. PRedis::set('members:marketReg:error:' . $agentId, $exception->getMessage(), 6 * 3600);
  258. showJson(1004, 2136);
  259. }
  260. }
  261. /**
  262. * 保存资料
  263. */
  264. public function saveInfo()
  265. {
  266. $params = input();
  267. $validate = new MemberValidate();
  268. if (!$validate->scene('marketInfo')->check($params)) {
  269. showJson(1004, $validate->getError());
  270. }
  271. $newMobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  272. $mobile = isset($this->userInfo['mobile']) ? $this->userInfo['mobile'] : '';
  273. if ($newMobile && $newMobile != $mobile) {
  274. $code = isset($params['code']) ? trim($params['code']) : '';
  275. $result = Sms::checkCode($newMobile, $code);
  276. if ($result !== true) {
  277. showJson(1004, $result);
  278. }
  279. // 验证手机号码是否被使用
  280. $id = Member::where(['mobile' => $newMobile])->value('id');
  281. if ($id && $id != $this->userId) {
  282. showJson(1004, 2001);
  283. }
  284. }
  285. $nowAddress = isset($params['now_address']) ? trim($params['now_address']) : '';
  286. $nowAddress = $nowAddress ? explode(' ', $nowAddress) : [];
  287. $birthday = isset($params['birthday']) ? strtotime($params['birthday']) : 0;
  288. $info = [
  289. 'userid' => $this->userId,
  290. 'province' => isset($nowAddress[0]) ? $nowAddress[0] : '',
  291. 'city' => isset($nowAddress[1]) ? $nowAddress[1] : '',
  292. 'district' => isset($nowAddress[2]) ? $nowAddress[2] : '',
  293. 'qq' => isset($params['qq']) ? trim($params['qq']) : '',
  294. ];
  295. Db::startTrans();
  296. if (UserProfile::checkProfile($this->userId)) {
  297. $profileData['updated_at'] = date('Y-m-d H:i:s');
  298. $res = UserProfile::saveData(['userid' => $this->userId], $info);
  299. } else {
  300. $res = UserProfile::insertGetId($info);
  301. }
  302. if (!$res) {
  303. Db::rollback();
  304. showJson(1004, 2104);
  305. }
  306. $memberData = [
  307. 'user_nickname' => isset($params['nickname']) ? trim($params['nickname']) : '',
  308. 'birthday' => $birthday,
  309. 'wechat_account' => isset($params['wechat_code']) ? trim($params['wechat_code']) : '',
  310. 'sex' => isset($params['sex']) ? intval($params['sex']) : 0,
  311. ];
  312. if ($newMobile && $mobile != $newMobile) {
  313. $memberData['mobile'] = $newMobile;
  314. }
  315. if (!Member::saveData(['id' => $this->userId], $memberData)) {
  316. Db::rollback();
  317. showJson(1004, 2104);
  318. }
  319. Db::commit();
  320. showJson(1005, 2029);
  321. }
  322. /**
  323. * 余额收益提现
  324. * @throws \think\db\exception\DataNotFoundException
  325. * @throws \think\db\exception\ModelNotFoundException
  326. * @throws \think\exception\DbException
  327. */
  328. public function doWithdraw()
  329. {
  330. // 验证
  331. $this->checkUser();
  332. $money = input('money', 0);
  333. if (empty($money)) {
  334. showJson(1004, 4010);
  335. }
  336. $accountConfig = $siteInfo = cmf_get_option('account_config');
  337. $minWithdraw = isset($accountConfig['min_withdraw']) ? intval($accountConfig['min_withdraw']) : 1;
  338. $withdrawCost = isset($accountConfig['withdraw_cost']) ? floatval($accountConfig['withdraw_cost']) : 0;
  339. if ($minWithdraw && $money < $minWithdraw) {
  340. showJson(1004, '最低提现金额为:' . $minWithdraw . '元');
  341. }
  342. /*$maxWithdraw = isset($accountConfig['max_withdraw']) ? intval($accountConfig['max_withdraw']) : 1000;
  343. if ($maxWithdraw && $money < $maxWithdraw) {
  344. showJson(1004, '最高提现金额为:' . $maxWithdraw . '元');
  345. }*/
  346. $payMoney = $withdrawCost>0? moneyFormat(($withdrawCost/100)*$money) : $money;
  347. $orderSn = makeTradeNo('WH', $this->userId);
  348. $memberInfo = Member::where(['id' => $this->userId,'agent_type'=> 1])->field('openid,balance')->find();
  349. $balance = isset($memberInfo['balance']) ? floatval($memberInfo['balance']) : 0;
  350. if($balance < $money){
  351. showJson(1004, 4011);
  352. }
  353. Db::startTrans();
  354. if(!Member::where(['id'=> $this->userId, 'agent_type'=> 1])->setDec('balance',$money)){
  355. Db::rollback();
  356. showJson(1004, 4013);
  357. }
  358. $log = [
  359. 'order_sn' => $orderSn,
  360. 'change' => $money,
  361. 'type' => 1,
  362. 'change_type' => 2,
  363. 'pay_money' => $payMoney,
  364. 'user_id' => $this->userId,
  365. 'balance' => moneyFormat($balance-$money),
  366. 'description' => "余额账户提现:{$money}元,实付:{$payMoney}元",
  367. 'create_time' => time(),
  368. 'status' => 1,
  369. ];
  370. $orderId = UserBalanceLog::insertGetId($log);
  371. if ($orderId) {
  372. Db::commit();
  373. showJson(1005, 4012, ['id'=> $orderId]);
  374. } else {
  375. Db::rollback();
  376. showJson(1004, 4014);
  377. }
  378. }
  379. /**
  380. * 获取邀请团队或会员列表
  381. */
  382. public function getInviteList(){
  383. $type = input('type', 1);
  384. if (!in_array($type, [1,2])) {
  385. showJson(1004, 4010);
  386. }
  387. $uid = input('uid', 0);
  388. $pageSize = input('pageSize', 20);
  389. $inviteId = $uid? $uid : $this->userId;
  390. $dataList = Member::getInviteList($inviteId, $type, $pageSize);
  391. showJson(1005, 1001, $dataList);
  392. }
  393. /**
  394. * 收益排行榜
  395. * @throws \think\exception\DbException
  396. */
  397. public function getIncomeRank(){
  398. $uid = input('uid', 0);
  399. $userId = $uid? $uid : $this->userId;
  400. $pageSize = input('pageSize', 50);
  401. $dataList = UserBalanceLog::getIncomeRankList($pageSize, $userId, 1);
  402. showJson(1005, 1001, $dataList);
  403. }
  404. }