123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- <?php
- namespace App\Http\Controllers\Api;
- use App\Http\Controllers\FrontController;
- use App\Models\SysModels\Config;
- use App\Models\UserModels\WxRecord;
- use App\Models\UserModels\WxSignup;
- use App\Models\UserModels\WxUser;
- use App\Servers\Common\SmsServer;
- use App\Servers\Member\UserServer;
- use App\Servers\WeChat\WeixinServer;
- use App\Servers\Common\CommonServer;
- class UserController extends FrontController
- {
- /**
- * 用于H5推广项目
- */
- public function __construct()
- {
- parent::__construct();
- }
- function index()
- {
- echo '成功';
- }
- /**
- * 获取微信会员信息
- */
- function getWxInfo()
- {
- $is_wx = CommonServer::creatServer()->isWeixin();
- if (empty($is_wx)) {
- echo '请使用微信打开';
- }
- //回调地址
- $redirect_uri = request()->input('redirect_url', '');
- if (empty($redirect_uri)) {
- $redirect_uri = 'http://testofficial.jhcs888.com/api/user/cs';
- } else {
- $redirect_uri = base64_decode($redirect_uri);
- }
- if (empty($redirect_uri)) {
- echo '请携带回调地址';
- }else{
- //上级推广码
- $invite_code = request()->input('inviteCode', '');
- //微信静默授权
- $wx_info = WeixinServer::creatServer()->authorize();
- if ($wx_info) {
- //微信登录
- $ret = UserServer::creatServer()->setWxOpen($wx_info,$invite_code);
- if (empty($ret)) {
- //获取微信信息失败
- echo '登陆失败';
- } else {
- $ret = http_build_query($ret);
- if (strpos($redirect_uri, '?') !== false) {
- $redirect_uri .= ('&' . $ret);
- } else {
- $redirect_uri .= ('?' . $ret);
- }
- //跳转至原来的页面
- WeixinServer::creatServer()->redirect($redirect_uri);
- }
- } else {
- echo '获取基本信息失败';
- }
- }
- }
- /**
- * 获取视频信息
- * @return \Illuminate\Http\JsonResponse
- */
- function getVideo(){
- $m_id = request()->input('m_id', '');//浏览者
- $token = request()->input('token', '');//token
- if(empty($m_id) || empty($token)) return $this->apiResponseError('',[],401);
- $check = $this->checkLogin($m_id,$token);
- if(!$check) return $this->apiResponseError('',[],401);
- $data['one_video'] = Config::where('key','one_video')->value('value');
- $data['two_video'] = Config::where('key','two_video')->value('value');
- $data['three_video'] = Config::where('key','three_video')->value('value');
- $data['four_video'] = Config::where('key','four_video')->value('value');
- return $this->apiResponseSuccess('成功',$data);
- }
- /**
- * 记录浏览数据
- * @return \Illuminate\Http\JsonResponse
- */
- function setRecord(){
- $s_code = request()->input('s_code', '');//上级推广码
- $m_id = request()->input('m_id', '');//浏览者
- $token = request()->input('token', '');//token
- if(empty($m_id) || empty($token)) return $this->apiResponseError('',[],401);
- $check = $this->checkLogin($m_id,$token);
- if(!$check) return $this->apiResponseError('',[],401);
- if(empty($s_code) || empty($m_id)) return $this->apiResponseSuccess('完成');
- //检查上级是否存在
- $s_user = WxUser::where('invite_code', $s_code)->select(['id'])->first();
- if($s_user){
- $s_user->increment('pv');//记录pv量
- }else{
- return $this->apiResponseSuccess('完成');
- }
- //检查浏览者是否存在
- $user = WxUser::where('id', $m_id)->select(['id'])->first();
- if($user){
- //检查这个浏览者是否浏览过这个上级的
- $info = WxRecord::where('s_code',$s_code)->where('m_id',$m_id)->select(['id'])->first();
- if(empty($info)){
- $s_user->increment('uv');//记录uv量
- WxRecord::create([
- 's_code' => $s_code,
- 'm_id' => $m_id,
- ]);
- }
- }
- return $this->apiResponseSuccess('完成');
- }
- /**
- * 发送短信
- * @return \Illuminate\Http\JsonResponse
- * @throws \End01here\EasySms\Exceptions\CodeErrorException
- * @throws \End01here\EasySms\Exceptions\GatewayErrorException
- * @throws \End01here\EasySms\Exceptions\MessageException
- */
- function sendSms()
- {
- $phone = request()->input('phone', '');
- $send_type = request()->input('send_type', 'default');
- if (empty($phone)) {
- return $this->apiResponseError('请输入手机号码');
- }
- $send_server = SmsServer::creatServer();
- $ret = $send_server->sendCode($phone, $send_type);
- if (!$ret) {
- return $this->apiResponseError($send_server->getErrorMsg() ?: '短信发送失败');
- } else {
- return $this->apiResponseSuccess('短信发送成功');
- }
- }
- /**
- * 申请成为推广者
- * @return \Illuminate\Http\JsonResponse
- */
- function setApply(){
- $m_id = request()->input('m_id', '');
- $name = request()->input('name', '');//名称
- $phone = request()->input('phone', '');//账号
- $code = request()->input('code', '');//验证码
- $token = request()->input('token', '');//token
- if(empty($m_id) || empty($token)) return $this->apiResponseError('',[],401);
- $check = $this->checkLogin($m_id,$token);
- if(!$check) return $this->apiResponseError('',[],401);
- if (empty($m_id)) return $this->apiResponseError( '必要参数缺失');
- if (empty($name)) return $this->apiResponseError( '名称必填');
- if (mb_strlen($name) > 10) return $this->apiResponseError('名称10字以内');
- if (empty($phone)) return $this->apiResponseError( '手机号必填');
- if (!CommonServer::creatServer()->verifyPhoneNumber($phone)) {
- return $this->apiResponseError('手机号格式错误');
- }
- if (empty($code)) return $this->apiResponseError( '验证码必填');
- $send_server = SmsServer::creatServer();
- $code_ret = $send_server->verifyCode($phone, $code);
- if (empty($code_ret)) {
- return $this->apiResponseError($send_server->getErrorMsg() ?: '验证码错误');
- }
- $info = WxUser::where('phone', $phone)->select(['id'])->first();
- if ($info) {
- return $this->apiResponseError( '该号码已申请');
- }
- $res = WxUser::where('id',$m_id)->update(['name'=>$name, 'phone'=>$phone, 'status'=>1]);
- if($res){
- $data['status'] = 1;
- return $this->apiResponseSuccess( '申请成功',$data);
- }else{
- return $this->apiResponseError( '申请失败');
- }
- }
- /**
- * 报名信息
- * @return \Illuminate\Http\JsonResponse|void
- */
- function setSignUp(){
- $m_id = request()->input('m_id', '');
- $name = request()->input('name', '');//名称
- $phone = request()->input('phone', '');//账号
- $industry = request()->input('industry', '');//行业
- $address = request()->input('address', '');//地址
- $token = request()->input('token', '');//token
- if(empty($m_id) || empty($token)) return $this->apiResponseError('',[],401);
- $check = $this->checkLogin($m_id,$token);
- if(!$check) return $this->apiResponseError('',[],401);
- if (empty($m_id)) return $this->apiResponseError( '必要参数缺失');
- if (empty($name)) return $this->apiResponseError( '名称必填');
- if (mb_strlen($name) > 10) return $this->apiResponseError('名称10字以内');
- if (empty($industry)) return $this->apiResponseError('行业必填');
- if (mb_strlen($industry) > 100) return $this->apiResponseError('行业信息过长');
- if (empty($address)) return $this->apiResponseError('地址必填');
- if (empty($phone)) return $this->apiResponseError( '手机号必填');
- if (!CommonServer::creatServer()->verifyPhoneNumber($phone)) {
- return $this->apiResponseError('手机号格式错误');
- }
- $info = WxSignup::where('phone', $phone)->select(['id'])->first();
- if ($info) {
- return $this->apiResponseError( '该号码已提交');
- }
- //获取用户上级
- $p_id = WxUser::where('id',$m_id)->value('p_id');
- $res = WxSignup::create([
- 'p_id' => $p_id,
- 'm_id' => $m_id,
- 'name' => $name,
- 'phone' => $phone,
- 'industry' => $industry,
- 'address' => $address,
- ]);
- if($res){
- return $this->apiResponseSuccess( '提交成功');
- }else{
- return $this->apiResponseError( '提交失败');
- }
- }
- /**
- * 获取JS签名信息
- * @return \Illuminate\Http\JsonResponse
- */
- function getJsConfig()
- {
- $url = request()->input('url');
- $ret = WeixinServer::creatServer()->getJsConfig($url);
- return $this->apiResponseSuccess('获取信息成功', $ret);
- }
- /**
- * 检查会员登陆
- * @param $m_id
- * @param $token
- * @return bool
- */
- function checkLogin($m_id,$token){
- $member = WxUser::where('id',$m_id)->select(['id','token_str'])->first();
- if(empty($member)){
- return false;
- }
- if ($member['token_str'] != $token) {
- return false;
- }
- return true;
- }
- }
|