UserController.php 9.6 KB

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