MarketController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 {
  121. showJson(1006,2041,['url'=> Wechat::makeRedirectUrl(url('/weixin/page/custom','','',true))]);
  122. }
  123. }
  124. // 头像
  125. $avatar = isset($wxInfo['headimgurl']) ? trim($wxInfo['headimgurl']) : '';
  126. $file = request()->file('image');
  127. if (!empty($file) && $file != null) {
  128. $fileData = Storage::uploadImg($file, 'avatar');
  129. $avatar = isset($fileData['file']) ? $fileData['file'] : '';
  130. }
  131. if (empty($avatar)) {
  132. showJson(1004, 3004);
  133. }
  134. Db::startTrans();
  135. $userPass = cmf_password('123456');
  136. $memberData = [
  137. 'parent_id'=> $inviteId,
  138. 'openid'=> $openid,
  139. 'user_login'=> $mobile,
  140. 'real_name' => isset($params['realname']) ? trim($params['realname']) : '',
  141. 'user_nickname' => isset($params['nickname']) ? trim($params['nickname']) : '',
  142. 'user_type' => 2,
  143. 'user_status' => 1,
  144. 'user_pass' => $userPass,
  145. 'pay_password' => $userPass,
  146. 'last_login_ip' => get_client_ip(),
  147. 'is_follow' => isset($wxInfo['subscribe']) ? intval($wxInfo['subscribe']) : 0,
  148. 'sex' => isset($wxInfo['sex']) ? intval($wxInfo['sex']) : 0,
  149. 'last_login_time' => time(),
  150. 'agent_create_time' => time(),
  151. 'agent_type' => 1,
  152. ];
  153. if ($avatar) {
  154. $memberData['avatar'] = $avatar;
  155. }
  156. if(!$agentId){
  157. $memberData['mobile'] = $mobile;
  158. }
  159. if (!$agentId) {
  160. $res = $agentId = Member::insertGetId($memberData);
  161. } else {
  162. $res = Member::where($where)->update($memberData);
  163. }
  164. if(!$res){
  165. Db::rollback();
  166. showJson(1004, 2104);
  167. }
  168. $nowAddress = isset($params['now_address']) ? trim($params['now_address']) : '';
  169. $nowAddress = $nowAddress ? explode(' ', $nowAddress) : [];
  170. $profileData = [
  171. 'userid' => $agentId,
  172. 'province' => isset($nowAddress[0]) ? $nowAddress[0] : '',
  173. 'city' => isset($nowAddress[1]) ? $nowAddress[1] : '',
  174. 'district' => isset($nowAddress[2]) ? $nowAddress[2] : '',
  175. 'wechat_code' => isset($params['wechat_code']) ? trim($params['wechat_code']) : '',
  176. ];
  177. if (UserProfile::checkProfile($agentId)) {
  178. $profileData['updated_at'] = date('Y-m-d H:i:s');
  179. $res = UserProfile::saveData(['userid' => $agentId], $profileData);
  180. } else {
  181. $res = UserProfile::insertGetId($profileData);
  182. }
  183. if (!$res) {
  184. Db::rollback();
  185. showJson(1004, 2104);
  186. }
  187. // 操作日志
  188. UserLog::saveLog(['user_id' => $agentId, 'type' => 1, 'content' => '申请注册成为分销用户']);
  189. Db::commit();
  190. // 更新缓存
  191. $userInfo = Member::getInfo(['id' => $agentId]);
  192. session('agentInfo', $userInfo);
  193. showJson(1005, 2043);
  194. } catch (\Exception $exception) {
  195. PRedis::set('members:marketReg:error:' . $agentId, $exception->getMessage(), 6 * 3600);
  196. showJson(1004, 2136);
  197. }
  198. }
  199. /**
  200. * 余额收益提现
  201. * @throws \think\db\exception\DataNotFoundException
  202. * @throws \think\db\exception\ModelNotFoundException
  203. * @throws \think\exception\DbException
  204. */
  205. public function doWithdraw()
  206. {
  207. // 验证
  208. $this->checkUser();
  209. $money = input('money', 0);
  210. if (empty($money)) {
  211. showJson(1004, 4010);
  212. }
  213. $accountConfig = $siteInfo = cmf_get_option('account_config');
  214. $minWithdraw = isset($accountConfig['min_withdraw']) ? intval($accountConfig['min_withdraw']) : 1;
  215. $withdrawCost = isset($accountConfig['withdraw_cost']) ? floatval($accountConfig['withdraw_cost']) : 0;
  216. if ($minWithdraw && $money < $minWithdraw) {
  217. showJson(1004, '最低提现金额为:' . $minWithdraw . '元');
  218. }
  219. /*$maxWithdraw = isset($accountConfig['max_withdraw']) ? intval($accountConfig['max_withdraw']) : 1000;
  220. if ($maxWithdraw && $money < $maxWithdraw) {
  221. showJson(1004, '最高提现金额为:' . $maxWithdraw . '元');
  222. }*/
  223. $payMoney = $withdrawCost>0? moneyFormat(($withdrawCost/100)*$money) : $money;
  224. $orderSn = makeTradeNo('WH', $this->userId);
  225. $memberInfo = Member::where(['id' => $this->userId,'agent_type'=> 1])->field('openid,balance')->find();
  226. $balance = isset($memberInfo['balance']) ? floatval($memberInfo['balance']) : 0;
  227. if($balance < $money){
  228. showJson(1004, 4011);
  229. }
  230. Db::startTrans();
  231. if(!Member::where(['id'=> $this->userId, 'agent_type'=> 1])->setDec('balance',$money)){
  232. Db::rollback();
  233. showJson(1004, 4013);
  234. }
  235. $log = [
  236. 'order_sn' => $orderSn,
  237. 'change' => $money,
  238. 'type' => 1,
  239. 'change_type' => 2,
  240. 'pay_money' => $payMoney,
  241. 'user_id' => $this->userId,
  242. 'balance' => moneyFormat($balance-$money),
  243. 'description' => "余额账户提现:{$money}元,实付:{$payMoney}元",
  244. 'create_time' => time(),
  245. 'status' => 1,
  246. ];
  247. $orderId = UserBalanceLog::insertGetId($log);
  248. if ($orderId) {
  249. Db::commit();
  250. showJson(1005, 4012, ['id'=> $orderId]);
  251. } else {
  252. Db::rollback();
  253. showJson(1004, 4014);
  254. }
  255. }
  256. /**
  257. * 获取邀请团队或会员列表
  258. */
  259. public function getInviteList(){
  260. $type = input('type', 1);
  261. if (!in_array($type, [1,2])) {
  262. showJson(1004, 4010);
  263. }
  264. $uid = input('uid', 0);
  265. $pageSize = input('pageSize', 20);
  266. $inviteId = $uid? $uid : $this->userId;
  267. $dataList = Member::getInviteList($inviteId, $type, $pageSize);
  268. showJson(1005, 1001, $dataList);
  269. }
  270. }