MarketController.php 15 KB

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