MarketController.php 11 KB

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