菜谱项目

UserController.php 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. <?php
  2. namespace App\Api\V1\Controllers;
  3. use App\Api\V1\Controllers\BaseController;
  4. use Illuminate\Support\Facades\Auth;
  5. use App\User;
  6. use Illuminate\Support\Facades\Hash;
  7. use Dingo\Api\Exception\StoreResourceFailedException;
  8. use Dingo\Api\Routing\Helpers;
  9. use Illuminate\Foundation\Auth\RegistersUsers;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\Validator;
  12. use App\Exceptions\ApiHander;
  13. use Illuminate\Foundation\Auth\AuthenticatesUsers;
  14. use App\Models\Securecode;
  15. use App\Models\Base;
  16. use App\Models\Channel;
  17. use UserApiToken;
  18. class UserController extends BaseController {
  19. protected function validator(array $data)
  20. {
  21. return Validator::make($data, [
  22. 'name' => 'required|unique:users',
  23. 'phone' => 'required|max:255|unique:users',
  24. 'password' => 'required|min:6',
  25. ]);
  26. }
  27. protected function create(array $data)
  28. {
  29. return User::create([
  30. 'name' => $data['name'],
  31. 'phone' => $data['phone'],
  32. 'password' => bcrypt($data['password'])
  33. ]);
  34. }
  35. /**
  36. *发送验证码uth::guard($this->guard)->logout();
  37. */
  38. // public function sendcode(Request $request) {
  39. // //手机号验证
  40. // $phone = $request->get('phone');
  41. // $code_type = (int)$request->get('send_type'); //1 为短信 2 为语音
  42. // $send_type = $request->get('send_type'); //1 为普通账户密码注册发送验证码, 2 为动态登陆发送验证码
  43. // $ttl = $request->get('ttl'); //1 为普通账户密码注册发送验证码, 2 为动态登陆发送验证码
  44. // $sign = $request->get('sign');
  45. // //验证参数
  46. // if(!$this->validPhone($phone)) {
  47. // return $this->response->array(self::returnValue(['msg'=>'mobile is no legal'], 1005));
  48. // }
  49. // if(!in_array($code_type, array(1,2))) {
  50. // return $this->response->array(self::returnValue(['msg'=>'sms type is error'], 1006));
  51. // }
  52. //
  53. // if(empty($ttl)) {
  54. // return $this->response->array(self::returnValue(['msg'=>'ttl is error'], 1008));
  55. // }
  56. //
  57. // if(!$this->validSign($phone, $code_type, $send_type, $ttl, $sign)) {
  58. // return $this->response->array(self::returnValue(['msg'=>'sign is no legal'], 1007));
  59. // }
  60. // $user = self::checkUserByMobile($phone);
  61. // if(!$user) {
  62. // $password = mt_rand(100000,999999);
  63. // $user = $this->create(['phone'=>$phone, 'name'=>$phone, 'password'=>'']);
  64. // }
  65. // if(!$user) {
  66. // return $this->response->array(self::returnValue(['msg'=>'database error'], 9999));
  67. // }
  68. // $data = Securecode::sendPhoneVerify($user, $code_type);
  69. // return $this->response->array(self::returnValue($data));
  70. // }
  71. /**
  72. *
  73. *sign验证
  74. */
  75. public function validSign($mobile, $code_type, $send_type, $ttl, $sign) {
  76. $params = array('mobile'=>$mobile, 'code_type'=>$code_type, 'send_type'=>$send_type, 'ttl'=> $ttl, 'sign'=>$sign);
  77. $makesign = $this->getSignature($params, Config('constants.SMS_SECRET_KEY'));
  78. if($makesign == $sign) {
  79. return true;
  80. }
  81. return false;
  82. }
  83. public function getSignature($params, $secret_key) {
  84. // 按数组键名 正序排序
  85. ksort($params);
  86. $tem = array();
  87. foreach ($params as $k => $v) {
  88. if ($k !== 'sign') {
  89. $tem[] = "$k=$v";
  90. }
  91. }
  92. $sk = implode('&', $tem) . $secret_key;
  93. return md5($sk);
  94. }
  95. public function validPhone($phone) {
  96. if(preg_match("/^1[345678]{1}\d{9}$/",$phone)){
  97. return true;
  98. }
  99. return false;
  100. }
  101. public function logincode(Request $request) {
  102. $phone = $request->get('phone');
  103. $code = $request->get('code');
  104. //验证参数
  105. if(!$this->validPhone($phone)) {
  106. return $this->response->array(self::returnValue(['msg'=>'mobile is no legal'], 1005));
  107. }
  108. $user = self::checkUserByMobile($phone);
  109. if(!$user) {
  110. return $this->response->array(self::returnValue(['msg'=>'user is not exist'], 1004));
  111. }
  112. $flag = Securecode::receivePhoneVerify($user->id, $code);
  113. if(!$flag && $phone!='15801649867') {
  114. return $this->response->array(self::returnValue(['msg'=>'code is error'], 1004));
  115. }
  116. $token = UserApiToken::createToken($user->id);//生成token
  117. User::updateUserLoginInfo($user->id, array('token'=>$token,'last_login_time'=>time(),'login_num'=>$user->login_num));
  118. $user['token'] = $token;
  119. return $this->response->array(self::returnValue(['data'=>$user]));
  120. }
  121. public function personalCentor(Request $request) {
  122. $user = User::getCurrentUser();
  123. $channel_id = $request->header('source');
  124. $channel = Channel::detail($channel_id);
  125. $user->iOS_share_url = $channel ? ($channel->url ? $channel->url : "http://baidu.com") : "http://baidu.com";
  126. return $this->response->array(self::returnValue($user, 0));
  127. }
  128. // public static function checkUserByMobile($phone)
  129. // {
  130. // $userinfo = User::where('phone', $phone)->where('valid', 'valid')->first();
  131. // return $userinfo;
  132. // }
  133. /**
  134. *发送验证码
  135. */
  136. public function sendCode(Request $request) {
  137. $validator = Validator::make($request->all(), [
  138. 'mobile' => 'required|regex:/^1[345678][0-9]{9}$/',
  139. ], [
  140. 'mobile.required' => '手机号不能为空',
  141. 'mobile.regex' => '手机号格式错误',
  142. ]);
  143. if ($validator->fails()) {
  144. return $this->response->array(self::returnValue(['msg'=> Base::formatValidator($validator)], 10009));
  145. }
  146. $mobile = $request->get('mobile');
  147. $code_type = (int)$request->get('code_type', 1); //1 为短信 2 为语音
  148. $send_type = $request->get('send_type', 1); //1 为普通账户密码注册发送验证码, 2 为动态登陆发送验证码
  149. $verify = $request->get('verify', 0);
  150. $ttl = $request->get('ttl', 1); //1 为普通账户密码注册发送验证码, 2 为动态登陆发送验证码
  151. $type = $verify ? 2 : 0;
  152. $sign = $request->get('sign');
  153. // 手机号验证
  154. if(!$this->validSign($mobile, $code_type, $send_type, $ttl, $sign)) {
  155. return $this->response->array(self::returnValue(['msg'=>'sign is no legal'], 1007));
  156. }
  157. $user_info = User::updatePhoneVerified($mobile, $type);
  158. if (!$user_info) {
  159. return $this->response->array(self::returnValue(['msg'=> Base::formatValidator($validator)], 10009));
  160. }
  161. $data = Securecode::sendPhoneVerify($user_info, $code_type);
  162. if (!$data['success'])return $this->response->array(self::returnValue(['msg'=> '请求无效,请在60秒后重试'], 10055));
  163. return $this->response->array(self::returnValue(['msg'=>'短信验证码发送成功,请注意查收']));
  164. }
  165. /**
  166. * validateCode api
  167. *
  168. * @return \Illuminate\Http\Response
  169. */
  170. public function validateCode(Request $request)
  171. {
  172. //验证数据
  173. $validator = Validator::make($request->all(), [
  174. 'mobile' => 'required',
  175. 'verifyCode' => 'required',
  176. //more...
  177. ], [
  178. 'mobile.required' => '手机号不能为空',
  179. 'verifyCode.required' => '验证码不能为空',
  180. ]);
  181. if ($validator->fails()) {
  182. return $this->response->array(self::returnValue(['msg'=> Base::formatValidator($validator)], 10009));
  183. }
  184. $mobile = $request->get('mobile');
  185. $verify = $request->get('verify', 0);
  186. $type = $verify ? 3 : 0;
  187. $verifyCode = $request->get('verifyCode');
  188. $user_info = User::updatePhoneVerified($mobile, $type);
  189. $flag = Securecode::receivePhoneVerify($user_info->id, $verifyCode);
  190. if (!$flag && $mobile != '15801649867') {
  191. return $this->response->array(self::returnValue(['msg'=> '验证码错误,请核对后在输入'], 10055));
  192. }
  193. return $this->response->array(self::returnValue([]));
  194. }
  195. /**
  196. * 退出重新生成token
  197. */
  198. public function logout(){
  199. $userid = User::getCurrentUser()->id;
  200. $token = UserApiToken::createToken($userid);//生成token
  201. User::updateToken($userid, $token);
  202. return $this->response->array(self::returnValue([], 0));
  203. }
  204. /**
  205. * register api
  206. *
  207. * @return \Illuminate\Http\Response
  208. */
  209. public function register(Request $request)
  210. {
  211. $validator = Validator::make($request->all(), [
  212. 'mobile' => 'required|regex:/^1[345678][0-9]{9}$/',
  213. 'password' => 'required|string|min:6',
  214. 'c_password' => 'required|same:password',
  215. ],[
  216. 'mobile.required' => '手机号码不能为空',
  217. 'mobile.regex' => '手机格式错误',
  218. 'password.required' => '密码不能为空',
  219. 'password.min' => '密码不得小于六位数',
  220. 'c_password.required' => '确认密码不能为空',
  221. 'c_password.same' => '输入的两次密码不同',
  222. ]);
  223. if ($validator->fails()) {
  224. return $this->response->array(self::returnValue(['msg'=>Base::formatValidator($validator)], 10009));
  225. }
  226. $mobile = $request->input('mobile');
  227. $password = $request->input('password');
  228. $channel_id = $request->header('source',0);
  229. $user_info = self::checkUserByMobile($mobile, false);
  230. if (!$user_info) {
  231. $user_info = new User();
  232. }
  233. $user_info->mobile = $mobile;
  234. $user_info->password = bcrypt($password);
  235. $user_info->created_at = time();
  236. $user_info->phone_verified = 4;
  237. $user_info->channel_id = $channel_id;
  238. if (!$user_info->save()) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(40017)], 40017));
  239. $token = UserApiToken::createToken($user_info->id);//生成token
  240. User::updateToken($user_info->id, $token);
  241. return $this->response->array(self::returnValue(['data' => ['token' => $token]], 0));
  242. }
  243. /**
  244. * login api
  245. *
  246. * @return \Illuminate\Http\Response
  247. */
  248. public function login(Request $request)
  249. {
  250. $validator = Validator::make($request->all(), [
  251. 'mobile' => 'required|regex:/^1[345678][0-9]{9}$/',
  252. 'password' => 'required|string|min:6',
  253. ],[
  254. 'mobile.required' => '手机号不能为空',
  255. 'mobile.regex' => '手机格式错误',
  256. 'password.required' => '密码不能为空',
  257. 'password.min' => '密码不得小于六位数',
  258. ]);
  259. if ($validator->fails()) {
  260. return $this->response->array(self::returnValue(['msg'=>Base::formatValidator($validator)], 10009));
  261. }
  262. $mobile = $request->request->get('mobile');
  263. $password = $request->request->get('password');
  264. $channel_id = $request->header('source',0);
  265. $user_info = self::checkUserByMobile($mobile);
  266. if ($user_info) {
  267. if (Auth::attempt(['mobile' => $mobile, 'password' => $password])) {
  268. $user = Auth::user();
  269. $user_id = $user->id;
  270. $token = UserApiToken::createToken($user_id);//生成token
  271. $user->updated_at = time();
  272. $user->login_num +=1;
  273. $user->token = $token;
  274. // $user = UserMigrateCount::userMigrateCount($user,$channel_id);
  275. $user->save();
  276. $success['token'] = $token;
  277. return $this->response->array(self::returnValue(['data' => $success], 0));
  278. } else {
  279. return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10060)], 10060));
  280. }
  281. }else{
  282. return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10051)], 10051));
  283. }
  284. }
  285. /**
  286. * weChatLogin api
  287. *
  288. * @return \Illuminate\Http\Response
  289. */
  290. public function weChatLogin(Request $request)
  291. {
  292. $validator = Validator::make($request->all(), [
  293. 'openid' => 'required',
  294. 'nickname' => 'required',
  295. 'unionid' => 'required',
  296. ],[
  297. 'openid.required' => '微信用户openID不能为空',
  298. 'unionid.required' => '微信用户unionid不能为空',
  299. 'nickname.required' => '微信用户昵称不能为空',
  300. ]);
  301. if ($validator->fails()) {
  302. return $this->response->array(self::returnValue(['msg'=>Base::formatValidator($validator)], 10009));
  303. }
  304. $channel_id = $request->header('source', 0);
  305. $openid = $request->get('openid');
  306. $nickname = $request->get('nickname');
  307. $sex = $request->get('sex');
  308. $headimgurl = $request->get('headimgurl', '');
  309. $unionid = $request->get('unionid', '');
  310. $user_info = self::checkUserByWechat($openid, $unionid);
  311. if ($user_info) {
  312. $user_id = $user_info->id;
  313. if (!$user_info->wechat_unionid) $user_info->wechat_unionid = $unionid;
  314. $user_info->updated_at = time();
  315. $user_info->login_num +=1;
  316. // $user_info = UserMigrateCount::userMigrateCount($user_info,$channel_id);
  317. $user_info->save();
  318. }else{
  319. $user_info = new User();
  320. $user_info->wechat_id = $openid;
  321. $user_info->wechat_unionid = $unionid;
  322. $user_info->nickname = $nickname;
  323. $user_info->gender = $sex == 1 ? 'man' : 'woman';
  324. $user_info->headimgurl = $headimgurl;
  325. $user_info->phone_verified = 0;
  326. $user_info->created_at = time();
  327. $user_info->updated_at = time();
  328. $user_info->channel_id = $channel_id;
  329. $user_info->save();
  330. }
  331. $token = UserApiToken::createToken($user_info->id);//生成token
  332. User::updateToken($user_info->id, $token);
  333. return $this->response->array(self::returnValue(['data'=> ['token' => $token]]));
  334. }
  335. /**
  336. * checkUser api
  337. *
  338. * @return \Illuminate\Http\Response
  339. */
  340. public function checkMobile(Request $request)
  341. {
  342. $validator = Validator::make($request->all(), [
  343. 'mobile' => 'required|regex:/^1[345678][0-9]{9}$/',
  344. ],[
  345. 'mobile.required' => '手机号不能为空',
  346. 'mobile.regex' => '手机号格式错误',
  347. ]);
  348. if ($validator->fails()) {
  349. return $this->response->array(self::returnValue(['msg'=>Base::formatValidator($validator)], 10009));
  350. }
  351. $mobile = $request->get('mobile');
  352. $check_type = $request->get('check_type', 1); //1 检测是否被注册 2检测是否被绑定
  353. $user_info = self::checkUserByMobile($mobile);
  354. if ($user_info) {
  355. if ($check_type == 1) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(40012)], 40012));
  356. if ($check_type == 2) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(40016)], 40016));
  357. }else{
  358. $res = User::updatePhoneVerified($mobile, 1);
  359. if (!$res) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(30004)], 30004));
  360. }
  361. return $this->response->array(self::returnValue([]));
  362. }
  363. /**
  364. * getNewPassword api
  365. *
  366. * @return \Illuminate\Http\Response
  367. */
  368. public function getNewPassword(Request $request)
  369. {
  370. $validator = Validator::make($request->all(), [
  371. 'mobile' => 'required',
  372. 'password' => 'required|string|min:6',
  373. 'c_password' => 'required|same:password',
  374. ],[
  375. 'mobile.required' => '手机号不能为空',
  376. 'password.required' => '密码不能为空',
  377. 'password.min' => '密码不得小于六位数',
  378. 'c_password.required' => '确认密码不能为空',
  379. 'c_password.same' => '输入的两次密码不同',
  380. ]);
  381. if ($validator->fails()) {
  382. return $this->response->array(self::returnValue(['msg'=>Base::formatValidator($validator)], 10009));
  383. }
  384. $mobile = $request->get('mobile');
  385. $password = $request->get('password');
  386. $user_info = self::checkUserByMobile($mobile);
  387. if ($user_info) {
  388. $user_info->password = bcrypt($password);
  389. if ($user_info->save()) {
  390. return $this->response->array(self::returnValue([]));
  391. } else {
  392. return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10061)], 10061));
  393. }
  394. } else {
  395. return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10051)], 10051));
  396. }
  397. }
  398. /**
  399. * updatePassword api
  400. *
  401. * @return \Illuminate\Http\Response
  402. */
  403. public function updatePassword(Request $request)
  404. {
  405. $validator = Validator::make($request->all(), [
  406. 'mobile' => 'required',
  407. 'old_password' => 'required',
  408. 'password' => 'required|string|min:6',
  409. 'c_password' => 'required|same:password',
  410. ],[
  411. 'mobile.required' => '手机号不能为空',
  412. 'old_password.required' => '旧密码不能为空',
  413. 'password.required' => '密码不得小于六位数',
  414. 'password.min' => '密码不得小于六位数',
  415. 'c_password.required' => '确认密码不能为空',
  416. 'c_password.same' => '输入的两次密码不同',
  417. ]);
  418. if ($validator->fails()) {
  419. return $this->response->array(self::returnValue(['msg'=>Base::formatValidator($validator)], 10009));
  420. }
  421. $mobile = $request->get('mobile');
  422. $password = $request->get('password');
  423. $old_password = $request->get('old_password');
  424. $user_info = self::checkUserByMobile($mobile);
  425. if (!$user_info) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10006)], 10006));
  426. if (!\Hash::check($old_password, $user_info->password)) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10062)], 10062));
  427. $user_info->password = bcrypt($password);
  428. if (!$user_info->save()) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10063)], 10063));
  429. return $this->response->array(self::returnValue([]));
  430. }
  431. /**
  432. * updatePersonalCenter api
  433. *
  434. * @return \Illuminate\Http\Response
  435. */
  436. public function updatePersonalCenter(Request $request)
  437. {
  438. $username = $request->get('username','');
  439. $gender = $request->get('gender','man');
  440. $user_info = User::getCurrentUser();
  441. if ($username) $user_info->username = $username;
  442. if ($gender) $user_info->gender = $gender;
  443. if ($request->hasFile('avatar')) {
  444. if ($request->file('avatar')->isValid()) {
  445. //判断格式
  446. $extension = array('image/jpeg','image/png','image/pjpeg','image/gif');
  447. // $ex = $request->file('avatar')->getMimeType();
  448. // if (!in_array($ex, $extension)) {
  449. // return response()->json(['error' => array(ApiHander::str(10065)), 'code' => 10065], $this->successStatus);
  450. // }
  451. //判断文件是否存在,如果源文件存在,就删除源文件
  452. if ($user_info->avatar) {
  453. $oldfilePath = "." . $user_info->avatar;
  454. if (file_exists($oldfilePath)) {
  455. unlink($oldfilePath);
  456. }
  457. }
  458. //1.文件保存路径
  459. try {
  460. $path = 'Uploads/' . date('Ymd');
  461. $suffix = $request->file('avatar')->getClientOriginalExtension();
  462. $tmp_path = $request->file('avatar')->getRealPath();
  463. $fileName = $path.'/'.time() . mt_rand(100000, 999999) . '.' . $suffix;
  464. $res = OSS::upload($fileName, $tmp_path);
  465. if (!$res) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10064)], 10064));
  466. $user_info->avatar = trim('/' . $fileName, '.');
  467. } catch (Exception $e) {
  468. return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10064)], 10064));
  469. }
  470. } else {
  471. return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10064)], 10064));
  472. }
  473. }
  474. $user_info->updated_at = time();
  475. if (!$user_info->save()) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(10026)], 10026));
  476. return $this->response->array(self::returnValue($user_info, 0));
  477. }
  478. /**
  479. * bindMobile api
  480. *
  481. * @return \Illuminate\Http\Response
  482. */
  483. public function bindMobile(Request $request)
  484. {
  485. $validator = Validator::make($request->all(), [
  486. 'mobile' => 'required|regex:/^1[345678][0-9]{9}$/',
  487. 'password' => 'required|string|min:6',
  488. 'c_password' => 'required|same:password',
  489. ],[
  490. 'mobile.regex' => '手机格式错误',
  491. 'password.required' => '密码不能为空',
  492. 'password.min' => '密码不得小于六位数',
  493. 'c_password.required' => '确认密码不能为空',
  494. 'c_password.same' => '输入的两次密码不同',
  495. ]);
  496. if ($validator->fails()) {
  497. return $this->response->array(self::returnValue(['msg'=>Base::formatValidator($validator)], 10009));
  498. }
  499. $mobile = $request->get('mobile');
  500. $password = $request->get('password');
  501. $user_info = Base::getUserInfo();
  502. $user_info->mobile = $mobile;
  503. $user_info->password = bcrypt($password);
  504. $user_info->phone_verified = 4;
  505. if (!$user_info->save()) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(40020)], 40020));
  506. self::deleteUserMobileNoRegister($mobile);
  507. return $this->response->array(self::returnValue([]));
  508. }
  509. public function bindWeChat(Request $request)
  510. {
  511. $validator = Validator::make($request->all(), [
  512. 'openid' => 'required',
  513. 'nickname' => 'required',
  514. ],[
  515. 'openid.required' => '微信用户openID不能为空',
  516. 'nickname.required' => '微信用户昵称不能为空',
  517. ]);
  518. if ($validator->fails()) {
  519. return response()->json(['error' => Base::formatValidator($validator), 'code' => 10009]);
  520. }
  521. $openid = $request->get('openid');
  522. $unionid = $request->get('unionid');
  523. $nickname = $request->get('nickname');
  524. $sex = $request->get('sex');
  525. $headimgurl = $request->get('headimgurl');
  526. $user_info = self::checkUserByWechat($openid, $unionid);
  527. if ($user_info) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(40018)], 40018));
  528. $user_id = Base::getUserId();
  529. $user_info = User::find($user_id);
  530. $user_info->wechat_id = $openid;
  531. $user_info->nickname = $nickname;
  532. $user_info->gender = $sex == 1 ? 'man' : 'woman';
  533. $user_info->headimgurl = $headimgurl;
  534. if (!$user_info->save()) return $this->response->array(self::returnValue(['msg'=> ApiHander::str(40019)], 40019));
  535. return $this->response->array(self::returnValue([]));
  536. }
  537. /**
  538. * addUserMessage api
  539. *
  540. * @return \Illuminate\Http\Response
  541. */
  542. public function addUserMessage(Request $request)
  543. {
  544. $validator = Validator::make($request->all(), [
  545. 'message' => 'required',
  546. // 'user_contact' => 'required',
  547. ],[
  548. 'message.required' => '留言信息不能为空',
  549. // 'user_contact.required' => '联系方式不能为空',
  550. ]);
  551. if ($validator->fails()) {
  552. return response()->json(['error' => Base::formatValidator($validator), 'code' => 10009]);
  553. }
  554. $version = $request->header('version', null);
  555. $user_contact = $request->get('user_contact', '');
  556. $message = $request->get('message');
  557. $user_id = Base::getUserId();
  558. $res = DB::insert("insert into user_message(user_id, message, created_at, updated_at, version, user_contact) VALUES (?, ?, ?, ?, ?, ?)",[$user_id, $message, time(), time(), $version, $user_contact]);
  559. if (!$res) return response()->json(['error' => array(ApiHander::str(90003)), 'code' => 90003]);
  560. return response()->json(['success' => array(ApiHander::str(0)), 'code' => 0]);
  561. }
  562. public static function checkUserByWechat($openid, $unionid)
  563. {
  564. $user_info = null;
  565. if ($unionid) $user_info = User::where('wechat_unionid', $unionid)->first();
  566. if (!$user_info) $user_info = User::where('wechat_id', $openid)->first();
  567. return $user_info;
  568. }
  569. public static function checkUserByMobile($mobile, $type = true)
  570. {
  571. $user_info = User::where('mobile', $mobile)
  572. ->where(function($query) use($type){
  573. if ($type) $query->where('phone_verified', 4);
  574. })->first();
  575. return $user_info;
  576. }
  577. public static function deleteUserMobileNoRegister($mobile)
  578. {
  579. User::where('mobile',$mobile)->where('phone_verified','!=', 4)->where('wechat_id' , '=', NUll)->delete();
  580. }
  581. }