MarketController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. // 收益
  60. $income = UserBalanceLog::where(['user_id'=> $userId,'status'=>2])
  61. ->where('type','>',1)
  62. ->sum('change');
  63. $memberInfo['income'] = moneyFormat($income, 2);
  64. $memberInfo['income'] = str_replace('.00','', $memberInfo['income']);
  65. // 累计提现
  66. $withdrawTotal = UserBalanceLog::where(['user_id'=> $userId,'type'=> 1,'change_type'=> 2,'status'=>2])
  67. ->sum('change');
  68. $memberInfo['withdraw_total'] = moneyFormat($withdrawTotal, 2);
  69. $memberInfo['withdraw_total'] = str_replace('.00','', $memberInfo['withdraw_total']);
  70. }
  71. if ($type == 2) {
  72. // 团队邀请二维码
  73. $inviteUrl = url('/weixin/market/entry?sid='.$userId, '', '', true);
  74. $qrcode = Wechat::makeNormalQrcode($inviteUrl);
  75. $memberInfo['team_qrcode'] = $qrcode ? $qrcode : '';
  76. // 会员邀请二维码
  77. $qrcodeData = Wechat::makeQrcode($userId, $userId);
  78. $memberInfo['qrcode'] = isset($qrcodeData['qrcode']) ? $qrcodeData['qrcode'] : '';
  79. }
  80. }
  81. showJson(1005, 1001, $memberInfo);
  82. }
  83. /**
  84. * 注册
  85. */
  86. public function register(){
  87. try {
  88. $params = input();
  89. $inviteId = input('sid', 0);
  90. $inviteId = $inviteId? $inviteId : session('invite_id');
  91. $validate = new MemberValidate();
  92. if (!$validate->scene('marketReg')->check($params)) {
  93. showJson(1004, $validate->getError());
  94. }
  95. $wxInfo = session('wxInfo');
  96. $openid = isset($wxInfo['openid'])? $wxInfo['openid'] : '';
  97. if(empty($openid)){
  98. showJson(2039);
  99. }
  100. $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
  101. $code = isset($params['code']) ? trim($params['code']) : '';
  102. $result = Sms::checkCode($mobile, $code,'marketReg');
  103. if ($result !== true) {
  104. showJson(1004, $result);
  105. }
  106. // 验证手机号码
  107. if (Member::where(['user_login' => $mobile, 'agent_type' => 1])->value('id')) {
  108. showJson(2001);
  109. }
  110. // 验证信息
  111. $where = ['openid' => $openid, 'user_type'=> 2];
  112. $info = Member::where($where)->field('id,agent_type,agent_status')->find();
  113. $agentId = isset($info['id'])? $info['id'] : 0;
  114. $agentType = isset($info['agent_type'])? $info['agent_type'] : 0;
  115. $agentStatus = isset($info['agent_status'])? $info['agent_status'] : 0;
  116. if($info && $agentType==1){
  117. // 已审核
  118. if($agentStatus == 1){
  119. showJson(1006,2040,['url'=> Wechat::makeRedirectUrl(url('/weixin/market/index','','',true))]);
  120. } else if ($agentStatus == 2){
  121. showJson(1006,2044,['url'=> Wechat::makeRedirectUrl(url('/weixin/page/custom','','',true))]);
  122. }else{
  123. showJson(1006,2041,['url'=> Wechat::makeRedirectUrl(url('/weixin/page/custom','','',true))]);
  124. }
  125. }
  126. // 头像
  127. $avatar = isset($wxInfo['headimgurl']) ? trim($wxInfo['headimgurl']) : '';
  128. $file = request()->file('image');
  129. if (!empty($file) && $file != null) {
  130. $fileData = Storage::uploadImg($file, 'avatar');
  131. $avatar = isset($fileData['file']) ? $fileData['file'] : '';
  132. }
  133. if (empty($avatar)) {
  134. showJson(1004, 3004);
  135. }
  136. Db::startTrans();
  137. $userPass = cmf_password('123456');
  138. $memberData = [
  139. 'parent_id'=> $inviteId,
  140. 'openid'=> $openid,
  141. 'user_login'=> $mobile,
  142. 'real_name' => isset($params['realname']) ? trim($params['realname']) : '',
  143. 'user_nickname' => isset($params['nickname']) ? trim($params['nickname']) : '',
  144. 'user_type' => 2,
  145. 'user_status' => 1,
  146. 'user_pass' => $userPass,
  147. 'pay_password' => $userPass,
  148. 'last_login_ip' => get_client_ip(),
  149. 'is_follow' => isset($wxInfo['subscribe']) ? intval($wxInfo['subscribe']) : 0,
  150. 'sex' => isset($wxInfo['sex']) ? intval($wxInfo['sex']) : 0,
  151. 'last_login_time' => time(),
  152. 'agent_create_time' => time(),
  153. 'agent_type' => 1,
  154. ];
  155. if ($avatar) {
  156. $memberData['avatar'] = $avatar;
  157. }
  158. if(!$agentId){
  159. $memberData['mobile'] = $mobile;
  160. }
  161. if (!$agentId) {
  162. $res = $agentId = Member::insertGetId($memberData);
  163. } else {
  164. $res = Member::where($where)->update($memberData);
  165. }
  166. if(!$res){
  167. Db::rollback();
  168. showJson(1004, 2104);
  169. }
  170. $nowAddress = isset($params['now_address']) ? trim($params['now_address']) : '';
  171. $nowAddress = $nowAddress ? explode(' ', $nowAddress) : [];
  172. $profileData = [
  173. 'userid' => $agentId,
  174. 'province' => isset($nowAddress[0]) ? $nowAddress[0] : '',
  175. 'city' => isset($nowAddress[1]) ? $nowAddress[1] : '',
  176. 'district' => isset($nowAddress[2]) ? $nowAddress[2] : '',
  177. 'wechat_code' => isset($params['wechat_code']) ? trim($params['wechat_code']) : '',
  178. ];
  179. if (UserProfile::checkProfile($agentId)) {
  180. $profileData['updated_at'] = date('Y-m-d H:i:s');
  181. $res = UserProfile::saveData(['userid' => $agentId], $profileData);
  182. } else {
  183. $res = UserProfile::insertGetId($profileData);
  184. }
  185. if (!$res) {
  186. Db::rollback();
  187. showJson(1004, 2104);
  188. }
  189. // 操作日志
  190. UserLog::saveLog(['user_id' => $agentId, 'type' => 1, 'content' => '申请注册成为分销用户']);
  191. Db::commit();
  192. // 更新缓存
  193. $userInfo = Member::getInfo(['id' => $agentId]);
  194. session('agentInfo', $userInfo);
  195. session('userInfo', null);
  196. showJson(1005, 2043);
  197. } catch (\Exception $exception) {
  198. PRedis::set('members:marketReg:error:' . $agentId, $exception->getMessage(), 6 * 3600);
  199. showJson(1004, 2136);
  200. }
  201. }
  202. /**
  203. * 余额收益提现
  204. * @throws \think\db\exception\DataNotFoundException
  205. * @throws \think\db\exception\ModelNotFoundException
  206. * @throws \think\exception\DbException
  207. */
  208. public function doWithdraw()
  209. {
  210. // 验证
  211. $this->checkUser();
  212. $money = input('money', 0);
  213. if (empty($money)) {
  214. showJson(1004, 4010);
  215. }
  216. $accountConfig = $siteInfo = cmf_get_option('account_config');
  217. $minWithdraw = isset($accountConfig['min_withdraw']) ? intval($accountConfig['min_withdraw']) : 1;
  218. $withdrawCost = isset($accountConfig['withdraw_cost']) ? floatval($accountConfig['withdraw_cost']) : 0;
  219. if ($minWithdraw && $money < $minWithdraw) {
  220. showJson(1004, '最低提现金额为:' . $minWithdraw . '元');
  221. }
  222. /*$maxWithdraw = isset($accountConfig['max_withdraw']) ? intval($accountConfig['max_withdraw']) : 1000;
  223. if ($maxWithdraw && $money < $maxWithdraw) {
  224. showJson(1004, '最高提现金额为:' . $maxWithdraw . '元');
  225. }*/
  226. $payMoney = $withdrawCost>0? moneyFormat(($withdrawCost/100)*$money) : $money;
  227. $orderSn = makeTradeNo('WH', $this->userId);
  228. $memberInfo = Member::where(['id' => $this->userId,'agent_type'=> 1])->field('openid,balance')->find();
  229. $balance = isset($memberInfo['balance']) ? floatval($memberInfo['balance']) : 0;
  230. if($balance < $money){
  231. showJson(1004, 4011);
  232. }
  233. Db::startTrans();
  234. if(!Member::where(['id'=> $this->userId, 'agent_type'=> 1])->setDec('balance',$money)){
  235. Db::rollback();
  236. showJson(1004, 4013);
  237. }
  238. $log = [
  239. 'order_sn' => $orderSn,
  240. 'change' => $money,
  241. 'type' => 1,
  242. 'change_type' => 2,
  243. 'pay_money' => $payMoney,
  244. 'user_id' => $this->userId,
  245. 'balance' => moneyFormat($balance-$money),
  246. 'description' => "余额账户提现:{$money}元,实付:{$payMoney}元",
  247. 'create_time' => time(),
  248. 'status' => 1,
  249. ];
  250. $orderId = UserBalanceLog::insertGetId($log);
  251. if ($orderId) {
  252. Db::commit();
  253. showJson(1005, 4012, ['id'=> $orderId]);
  254. } else {
  255. Db::rollback();
  256. showJson(1004, 4014);
  257. }
  258. }
  259. /**
  260. * 获取邀请团队或会员列表
  261. */
  262. public function getInviteList(){
  263. $type = input('type', 1);
  264. if (!in_array($type, [1,2])) {
  265. showJson(1004, 4010);
  266. }
  267. $uid = input('uid', 0);
  268. $pageSize = input('pageSize', 20);
  269. $inviteId = $uid? $uid : $this->userId;
  270. $dataList = Member::getInviteList($inviteId, $type, $pageSize);
  271. showJson(1005, 1001, $dataList);
  272. }
  273. }