MarketController.php 15 KB

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