UserController.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\FrontController;
  4. use App\Models\SysModels\Config;
  5. use App\Models\UserModels\WxRecord;
  6. use App\Models\UserModels\WxSignup;
  7. use App\Models\UserModels\WxUser;
  8. use App\Servers\Common\SmsServer;
  9. use App\Servers\Member\UserServer;
  10. use App\Servers\WeChat\WeixinServer;
  11. use App\Servers\Common\CommonServer;
  12. class UserController extends FrontController
  13. {
  14. /**
  15. * 用于H5推广项目
  16. */
  17. public function __construct()
  18. {
  19. parent::__construct();
  20. }
  21. function index()
  22. {
  23. echo '成功';
  24. }
  25. /**
  26. * 获取微信会员信息
  27. */
  28. function getWxInfo()
  29. {
  30. $is_wx = CommonServer::creatServer()->isWeixin();
  31. if (empty($is_wx)) {
  32. echo '请使用微信打开';
  33. }
  34. //回调地址
  35. $redirect_uri = request()->input('redirect_url', '');
  36. if (empty($redirect_uri)) {
  37. $redirect_uri = 'http://testofficial.jhcs888.com/api/user/cs';
  38. } else {
  39. $redirect_uri = base64_decode($redirect_uri);
  40. }
  41. if (empty($redirect_uri)) {
  42. echo '请携带回调地址';
  43. }else{
  44. //上级推广码
  45. $invite_code = request()->input('inviteCode', '');
  46. //微信静默授权
  47. $wx_info = WeixinServer::creatServer()->authorize();
  48. if ($wx_info) {
  49. //微信登录
  50. $ret = UserServer::creatServer()->setWxOpen($wx_info,$invite_code);
  51. if (empty($ret)) {
  52. //获取微信信息失败
  53. echo '登陆失败';
  54. } else {
  55. $ret = http_build_query($ret);
  56. if (strpos($redirect_uri, '?') !== false) {
  57. $redirect_uri .= ('&' . $ret);
  58. } else {
  59. $redirect_uri .= ('?' . $ret);
  60. }
  61. //跳转至原来的页面
  62. WeixinServer::creatServer()->redirect($redirect_uri);
  63. }
  64. } else {
  65. echo '获取基本信息失败';
  66. }
  67. }
  68. }
  69. /**
  70. * 获取视频信息
  71. * @return \Illuminate\Http\JsonResponse
  72. */
  73. function getVideo(){
  74. $m_id = request()->input('m_id', '');//浏览者
  75. $token = request()->input('token', '');//token
  76. if(empty($m_id) || empty($token)) return $this->apiResponseError('',[],401);
  77. $check = $this->checkLogin($m_id,$token);
  78. if(!$check) return $this->apiResponseError('',[],401);
  79. $data['one_video'] = Config::where('key','one_video')->value('value');
  80. $data['two_video'] = Config::where('key','two_video')->value('value');
  81. $data['three_video'] = Config::where('key','three_video')->value('value');
  82. $data['four_video'] = Config::where('key','four_video')->value('value');
  83. return $this->apiResponseSuccess('成功',$data);
  84. }
  85. /**
  86. * 记录浏览数据
  87. * @return \Illuminate\Http\JsonResponse
  88. */
  89. function setRecord(){
  90. $s_code = request()->input('s_code', '');//上级推广码
  91. $m_id = request()->input('m_id', '');//浏览者
  92. $token = request()->input('token', '');//token
  93. if(empty($m_id) || empty($token)) return $this->apiResponseError('',[],401);
  94. $check = $this->checkLogin($m_id,$token);
  95. if(!$check) return $this->apiResponseError('',[],401);
  96. if(empty($s_code) || empty($m_id)) return $this->apiResponseSuccess('完成');
  97. //检查上级是否存在
  98. $s_user = WxUser::where('invite_code', $s_code)->select(['id'])->first();
  99. if($s_user){
  100. $s_user->increment('pv');//记录pv量
  101. }else{
  102. return $this->apiResponseSuccess('完成');
  103. }
  104. //检查浏览者是否存在
  105. $user = WxUser::where('id', $m_id)->select(['id'])->first();
  106. if($user){
  107. //检查这个浏览者是否浏览过这个上级的
  108. $info = WxRecord::where('s_code',$s_code)->where('m_id',$m_id)->select(['id'])->first();
  109. if(empty($info)){
  110. $s_user->increment('uv');//记录uv量
  111. WxRecord::create([
  112. 's_code' => $s_code,
  113. 'm_id' => $m_id,
  114. ]);
  115. }
  116. }
  117. return $this->apiResponseSuccess('完成');
  118. }
  119. /**
  120. * 发送短信
  121. * @return \Illuminate\Http\JsonResponse
  122. * @throws \End01here\EasySms\Exceptions\CodeErrorException
  123. * @throws \End01here\EasySms\Exceptions\GatewayErrorException
  124. * @throws \End01here\EasySms\Exceptions\MessageException
  125. */
  126. function sendSms()
  127. {
  128. $phone = request()->input('phone', '');
  129. $send_type = request()->input('send_type', 'default');
  130. if (empty($phone)) {
  131. return $this->apiResponseError('请输入手机号码');
  132. }
  133. $send_server = SmsServer::creatServer();
  134. $ret = $send_server->sendCode($phone, $send_type);
  135. if (!$ret) {
  136. return $this->apiResponseError($send_server->getErrorMsg() ?: '短信发送失败');
  137. } else {
  138. return $this->apiResponseSuccess('短信发送成功');
  139. }
  140. }
  141. /**
  142. * 申请成为推广者
  143. * @return \Illuminate\Http\JsonResponse
  144. */
  145. function setApply(){
  146. $m_id = request()->input('m_id', '');
  147. $name = request()->input('name', '');//名称
  148. $phone = request()->input('phone', '');//账号
  149. $code = request()->input('code', '');//验证码
  150. $token = request()->input('token', '');//token
  151. if(empty($m_id) || empty($token)) return $this->apiResponseError('',[],401);
  152. $check = $this->checkLogin($m_id,$token);
  153. if(!$check) return $this->apiResponseError('',[],401);
  154. if (empty($m_id)) return $this->apiResponseError( '必要参数缺失');
  155. if (empty($name)) return $this->apiResponseError( '名称必填');
  156. if (mb_strlen($name) > 10) return $this->apiResponseError('名称10字以内');
  157. if (empty($phone)) return $this->apiResponseError( '手机号必填');
  158. if (!CommonServer::creatServer()->verifyPhoneNumber($phone)) {
  159. return $this->apiResponseError('手机号格式错误');
  160. }
  161. if (empty($code)) return $this->apiResponseError( '验证码必填');
  162. $send_server = SmsServer::creatServer();
  163. $code_ret = $send_server->verifyCode($phone, $code);
  164. if (empty($code_ret)) {
  165. return $this->apiResponseError($send_server->getErrorMsg() ?: '验证码错误');
  166. }
  167. $info = WxUser::where('phone', $phone)->select(['id'])->first();
  168. if ($info) {
  169. return $this->apiResponseError( '该号码已申请');
  170. }
  171. $res = WxUser::where('id',$m_id)->update(['name'=>$name, 'phone'=>$phone, 'status'=>1]);
  172. if($res){
  173. $data['status'] = 1;
  174. return $this->apiResponseSuccess( '申请成功',$data);
  175. }else{
  176. return $this->apiResponseError( '申请失败');
  177. }
  178. }
  179. /**
  180. * 报名信息
  181. * @return \Illuminate\Http\JsonResponse|void
  182. */
  183. function setSignUp(){
  184. $m_id = request()->input('m_id', '');
  185. $name = request()->input('name', '');//名称
  186. $phone = request()->input('phone', '');//账号
  187. $industry = request()->input('industry', '');//行业
  188. $address = request()->input('address', '');//地址
  189. $token = request()->input('token', '');//token
  190. if(empty($m_id) || empty($token)) return $this->apiResponseError('',[],401);
  191. $check = $this->checkLogin($m_id,$token);
  192. if(!$check) return $this->apiResponseError('',[],401);
  193. if (empty($m_id)) return $this->apiResponseError( '必要参数缺失');
  194. if (empty($name)) return $this->apiResponseError( '名称必填');
  195. if (mb_strlen($name) > 10) return $this->apiResponseError('名称10字以内');
  196. if (empty($industry)) return $this->apiResponseError('行业必填');
  197. if (mb_strlen($industry) > 100) return $this->apiResponseError('行业信息过长');
  198. if (empty($address)) return $this->apiResponseError('地址必填');
  199. if (empty($phone)) return $this->apiResponseError( '手机号必填');
  200. if (!CommonServer::creatServer()->verifyPhoneNumber($phone)) {
  201. return $this->apiResponseError('手机号格式错误');
  202. }
  203. $info = WxSignup::where('phone', $phone)->select(['id'])->first();
  204. if ($info) {
  205. return $this->apiResponseError( '该号码已提交');
  206. }
  207. //获取用户上级
  208. $p_id = WxUser::where('id',$m_id)->value('p_id');
  209. $res = WxSignup::create([
  210. 'p_id' => $p_id,
  211. 'm_id' => $m_id,
  212. 'name' => $name,
  213. 'phone' => $phone,
  214. 'industry' => $industry,
  215. 'address' => $address,
  216. ]);
  217. if($res){
  218. return $this->apiResponseSuccess( '提交成功');
  219. }else{
  220. return $this->apiResponseError( '提交失败');
  221. }
  222. }
  223. /**
  224. * 获取JS签名信息
  225. * @return \Illuminate\Http\JsonResponse
  226. */
  227. function getJsConfig()
  228. {
  229. $url = request()->input('url');
  230. $ret = WeixinServer::creatServer()->getJsConfig($url);
  231. return $this->apiResponseSuccess('获取信息成功', $ret);
  232. }
  233. /**
  234. * 检查会员登陆
  235. * @param $m_id
  236. * @param $token
  237. * @return bool
  238. */
  239. function checkLogin($m_id,$token){
  240. $member = WxUser::where('id',$m_id)->select(['id','token_str'])->first();
  241. if(empty($member)){
  242. return false;
  243. }
  244. if ($member['token_str'] != $token) {
  245. return false;
  246. }
  247. return true;
  248. }
  249. }