You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

414 lines
13 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Api\CommonController;
  4. use Illuminate\Http\Request;
  5. use App\Common\ReturnData;
  6. use App\Common\Token;
  7. use App\Http\Model\User;
  8. class UserController extends CommonController
  9. {
  10. public function __construct()
  11. {
  12. parent::__construct();
  13. }
  14. //用户信息
  15. public function userInfo(Request $request)
  16. {
  17. if ($user = User::getUserInfo(Token::$uid))
  18. {
  19. return ReturnData::create(ReturnData::SUCCESS, $user);
  20. }
  21. else
  22. {
  23. return ReturnData::create(ReturnData::RECORD_NOT_EXIST);
  24. }
  25. }
  26. //修改用户信息
  27. public function userInfoUpdate(Request $request)
  28. {
  29. $data = '';
  30. if($request->input('user_name', null)!==null){$data['user_name'] = $request->input('user_name');}
  31. if($request->input('email', null)!==null){$data['email'] = $request->input('email');}
  32. if($request->input('sex', null)!==null){$data['sex'] = $request->input('sex');}
  33. if($request->input('birthday', null)!==null){$data['birthday'] = $request->input('birthday');}
  34. if($request->input('money', null)!==null){$data['money'] = $request->input('money');}
  35. if($request->input('frozen_money', null)!==null){$data['frozen_money'] = $request->input('frozen_money');}
  36. if($request->input('point', null)!==null){$data['point'] = $request->input('point');}
  37. if($request->input('address_id', null)!==null){$data['address_id'] = $request->input('address_id');}
  38. if($request->input('user_rank', null)!==null){$data['user_rank'] = $request->input('user_rank');}
  39. if($request->input('parent_id', null)!==null){$data['parent_id'] = $request->input('parent_id');}
  40. if($request->input('nickname', null)!==null){$data['nickname'] = $request->input('nickname');}
  41. if($request->input('mobile', null)!==null){$data['mobile'] = $request->input('mobile');}
  42. if($request->input('status', null)!==null){$data['status'] = $request->input('status');}
  43. if($request->input('group_id', null)!==null){$data['group_id'] = $request->input('group_id');}
  44. if($request->input('password', null)!==null){$data['password'] = $request->input('password');}
  45. if($request->input('head_img', null)!==null){$data['head_img'] = $request->input('head_img');}
  46. if ($data != '')
  47. {
  48. User::modify(['id'=>Token::$uid],$data);
  49. }
  50. return ReturnData::create(ReturnData::SUCCESS);
  51. }
  52. //用户列表
  53. public function userList(Request $request)
  54. {
  55. //参数
  56. $data['limit'] = $request->input('limit', 10);
  57. $data['offset'] = $request->input('offset', 0);
  58. $res = User::getList($data);
  59. if(!$res)
  60. {
  61. return ReturnData::create(ReturnData::SYSTEM_FAIL);
  62. }
  63. return ReturnData::create(ReturnData::SUCCESS,$res);
  64. }
  65. //签到
  66. public function signin(Request $request)
  67. {
  68. $res = User::signin();
  69. if($res !== true)
  70. {
  71. return ReturnData::create(ReturnData::PARAMS_ERROR,null,$res);
  72. }
  73. return ReturnData::create(ReturnData::SUCCESS);
  74. }
  75. //注册
  76. public function register(Request $request)
  77. {
  78. $mobile = $request->input('mobile', null);
  79. $password = $request->input('password', null);
  80. $community_id = $request->input('community_id', null);
  81. $address = $request->input('address', null);
  82. $type = $request->input('type', null);
  83. $verificationCode = $request->input('verificationCode', null);
  84. $verificationType = $request->input('verificationType', null); //7表示验证码登录
  85. $yezhu_mobile = $request->input('yezhu_mobile', null);
  86. Log::info("注册手机号==========mobile=======".$mobile);
  87. if ($mobile==null || $password==null || $verificationCode==null || $verificationType===null || $community_id===null)
  88. {
  89. return ReturnCode::create(ReturnCode::PARAMS_ERROR);
  90. }
  91. if (!Helper::isValidMobile($mobile))
  92. {
  93. return response(ReturnCode::create(ReturnCode::MOBILE_FORMAT_FAIL));
  94. }
  95. $verifyCode = VerifyCode::isVerify($mobile, $verificationCode, $verificationType);
  96. if(!$verifyCode)
  97. {
  98. return ReturnCode::create(ReturnCode::INVALID_VERIFY_CODE);
  99. }
  100. if($yezhu_mobile!=null)
  101. {
  102. $yezhu = MallDataManager::userFirst(['mobile'=>$yezhu_mobile,'community_id'=>$community_id]);
  103. if (!$yezhu)
  104. {
  105. return response(ReturnCode::create(ReturnCode::PARAMS_ERROR,'业主不匹配'));
  106. }
  107. }
  108. //判断是否已经注册
  109. $user = MallDataManager::userFirst(['mobile'=>$mobile]);
  110. if ($user)
  111. {
  112. return response(ReturnCode::create(ReturnCode::MOBILE_EXIST));
  113. }
  114. try
  115. {
  116. DB::beginTransaction();
  117. //创建用户
  118. $userdata['mobile'] = $mobile;
  119. $userdata['password'] = $password;
  120. $userdata['verify_mobile'] = 1;
  121. $userdata['name'] = $mobile;
  122. $userdata['nickname'] = $mobile;
  123. $userdata['community_id'] = $community_id;
  124. $userdata['address'] = $address;
  125. $userdata['type'] = $type;
  126. $userid = DB::table('user')->insertGetId($userdata);
  127. //注册环信用户
  128. $Easemob = new Easemob();
  129. $Easemob->imRegister(['username'=>'cuobian'.$userid,'password'=>md5('cuobian'.$userid)]);
  130. //生成token
  131. if ($user = MallDataManager::userFirst(['mobile'=>$mobile,'password'=>$password]))
  132. {
  133. //获取token
  134. $expired_at = Carbon::now()->addDay()->toDateTimeString();
  135. $token = Token::generate(Token::TYPE_SHOP, $user->id);
  136. }
  137. DB::commit();
  138. $response = ReturnCode::create(ReturnCode::SUCCESS);
  139. $response['data'] = [
  140. 'id' => $user->id,
  141. 'mobile' => $user->mobile,
  142. 'expired_at' => $expired_at,
  143. 'token' => $token,
  144. ];
  145. }
  146. catch (Exception $e)
  147. {
  148. DB::rollBack();
  149. Log::info($e->getMessage());
  150. return response(ReturnCode::error($e->getCode(), $e->getMessage()));
  151. }
  152. return response($response);
  153. }
  154. //登录
  155. public function login(Request $request)
  156. {
  157. $mobile = $request->input('mobile');
  158. $password = $request->input('password');
  159. if (!$mobile || !$password)
  160. {
  161. return response(ReturnCode::create(ReturnCode::PARAMS_ERROR));
  162. }
  163. if ($user = MallDataManager::userFirst(['mobile'=>$mobile]))
  164. {
  165. //判断密码
  166. if ($password == $user->password)
  167. {
  168. //获取token
  169. $expired_at = Carbon::now()->addDay()->toDateTimeString();
  170. $token = Token::generate(Token::TYPE_SHOP, $user->id);
  171. $response = ReturnCode::success();
  172. $response['data']=[
  173. 'id' => $user->id, 'name' => $user->name, 'nickname' => $user->nickname, 'headimg' => (string)$user->head_img, 'token' => $token, 'expired_at' => $expired_at, 'mobile' => $user->mobile, 'hx_name' => 'cuobian'.$user->id, 'hx_pwd' => md5('cuobian'.$user->id)
  174. ];
  175. return response($response);
  176. }
  177. else
  178. {
  179. return response(ReturnCode::create(ReturnCode::PASSWORD_NOT_MATCH));
  180. }
  181. }
  182. else
  183. {
  184. return response(ReturnCode::create(ReturnCode::USER_NOT_EXIST));
  185. }
  186. }
  187. //验证码登录
  188. public function verificationCodeLogin(Request $request)
  189. {
  190. $mobile = $request->input('mobile');
  191. $code = $request->input('code', null);
  192. $type = $request->input('type', null); //7表示验证码登录
  193. if (!$mobile || !$code)
  194. {
  195. return response(ReturnCode::create(ReturnCode::PARAMS_ERROR));
  196. }
  197. //判断验证码
  198. if ($type != VerifyCode::TYPE_LOGIN)
  199. {
  200. return response(ReturnCode::create(ReturnCode::INVALID_VERIFY_CODE));
  201. }
  202. $verifyCode = VerifyCode::isVerify($mobile, $code, $type);
  203. if (!$verifyCode)
  204. {
  205. return response(ReturnCode::create(ReturnCode::INVALID_VERIFY_CODE));
  206. }
  207. if ($user = MallDataManager::userFirst(['mobile'=>$mobile]))
  208. {
  209. //获取token
  210. $expired_at = Carbon::now()->addDay()->toDateTimeString();
  211. $token = Token::generate(Token::TYPE_SHOP, $user->id);
  212. $response = ReturnCode::success();
  213. $response['data']=[
  214. 'id' => $user->id, 'name' => $user->name, 'nickname' => $user->nickname, 'headimg' => (string)$user->head_img, 'token' => $token, 'expired_at' => $expired_at, 'mobile' => $user->mobile, 'hx_name' => 'cuobian'.$user->id, 'hx_pwd' => md5('cuobian'.$user->id)
  215. ];
  216. return response($response);
  217. }
  218. else
  219. {
  220. return response(ReturnCode::create(ReturnCode::USER_NOT_EXIST));
  221. }
  222. }
  223. //修改密码
  224. public function changePassword(Request $request)
  225. {
  226. $mobile = $request->input('mobile', null);
  227. $password = $request->input('password', null); //新密码
  228. $oldPassword = $request->input('oldPassword', null); //旧密码
  229. if (!$mobile || !$password || !$oldPassword)
  230. {
  231. return ReturnCode::create(ReturnCode::PARAMS_ERROR);
  232. }
  233. if($password == $oldPassword)
  234. {
  235. return ReturnCode::create(ReturnCode::PARAMS_ERROR,'新旧密码相同');
  236. }
  237. if (!Helper::isValidMobile($mobile))
  238. {
  239. return ReturnCode::create(ReturnCode::MOBILE_FORMAT_FAIL);
  240. }
  241. $user = MallDataManager::userFirst(['mobile'=>$mobile,'password'=>$oldPassword,'id'=>Token::$uid]);
  242. if(!$user)
  243. {
  244. return ReturnCode::create(ReturnCode::PARAMS_ERROR,'手机或密码错误');
  245. }
  246. DB::table('user')->where(['mobile'=>$mobile,'password'=>$oldPassword,'id'=>Token::$uid])->update(['password'=>$password]);
  247. MallDataManager::tokenDelete(['uid'=>Token::$uid]);
  248. return ReturnCode::create(ReturnCode::SUCCESS);
  249. }
  250. //找回密码,不用输入旧密码
  251. public function findPassword(Request $request)
  252. {
  253. $mobile = $request->input('mobile', null);
  254. $password = $request->input('password', null);
  255. if ($mobile && $password)
  256. {
  257. if (!Helper::isValidMobile($mobile))
  258. {
  259. return response(ReturnCode::create(ReturnCode::MOBILE_FORMAT_FAIL));
  260. }
  261. //判断验证码是否有效
  262. $code = $request->input('code', '');
  263. $type = $request->input('type', null);
  264. if($type != VerifyCode::TYPE_CHANGE_PASSWORD)
  265. return response(ReturnCode::create(ReturnCode::INVALID_VERIFY_CODE,'验证码类型错误'));
  266. $verifyCode = VerifyCode::isVerify($mobile, $code, $type);
  267. if($verifyCode)
  268. {
  269. try
  270. {
  271. DB::beginTransaction();
  272. $verifyCode->status = VerifyCode::STATUS_USE;
  273. $verifyCode->save();
  274. if ($user = MallDataManager::userFirst(['mobile'=>$mobile]))
  275. {
  276. DB::table('user')->where(['mobile'=>$mobile])->update(['password'=>$password]);
  277. MallDataManager::tokenDelete(['uid'=>$user->id]);
  278. $response = response(ReturnCode::create(ReturnCode::SUCCESS));
  279. }
  280. else
  281. {
  282. $response = response(ReturnCode::create(ReturnCode::PARAMS_ERROR));
  283. }
  284. DB::commit();
  285. return $response;
  286. }
  287. catch (Exception $e)
  288. {
  289. DB::rollBack();
  290. return response(ReturnCode::error($e->getCode(), $e->getMessage()));
  291. }
  292. }
  293. else
  294. {
  295. return response(ReturnCode::create(ReturnCode::INVALID_VERIFY_CODE));
  296. }
  297. }
  298. else
  299. {
  300. return response(ReturnCode::create(ReturnCode::PARAMS_ERROR));
  301. }
  302. }
  303. //修改手机号
  304. public function changeMobile(Request $request)
  305. {
  306. $mobile = $request->input('mobile', null); //新手机号码
  307. $verificationCode = $request->input('verificationCode', null); //新手机验证码
  308. $oldMobile = $request->input('oldMobile', null); //旧手机号码
  309. $oldVerificationCode = $request->input('oldVerificationCode', null); //旧手机验证码
  310. $type = $request->input('type', null); //验证码类型
  311. if (!$mobile || !$verificationCode || !$oldMobile || !$oldVerificationCode || !$type)
  312. {
  313. return ReturnCode::create(ReturnCode::PARAMS_ERROR);
  314. }
  315. if (!Helper::isValidMobile($mobile))
  316. {
  317. return ReturnCode::create(ReturnCode::MOBILE_FORMAT_FAIL);
  318. }
  319. if($mobile == $oldMobile)
  320. {
  321. return ReturnCode::create(ReturnCode::PARAMS_ERROR,'新旧手机号码相同');
  322. }
  323. if($type != VerifyCode::TYPE_CHANGE_MOBILE)
  324. {
  325. return ReturnCode::create(ReturnCode::INVALID_VERIFY_CODE,'验证码类型错误');
  326. }
  327. $verifyCode = VerifyCode::isVerify($oldMobile, $oldVerificationCode, $type);
  328. if(!$verifyCode)
  329. {
  330. return ReturnCode::create(ReturnCode::INVALID_VERIFY_CODE);
  331. }
  332. $verifyCode = null;
  333. $verifyCode = VerifyCode::isVerify($mobile, $verificationCode, $type);
  334. if(!$verifyCode)
  335. {
  336. return ReturnCode::create(ReturnCode::INVALID_VERIFY_CODE);
  337. }
  338. $user = MallDataManager::userFirst(['mobile'=>$oldMobile,'id'=>Token::$uid]);
  339. if(!$user)
  340. {
  341. return ReturnCode::create(ReturnCode::PARAMS_ERROR,'旧手机号码错误');
  342. }
  343. DB::table('user')->where(['mobile'=>$oldMobile,'id'=>Token::$uid])->update(['mobile'=>$mobile]);
  344. MallDataManager::tokenDelete(['uid'=>Token::$uid]);
  345. return ReturnCode::create(ReturnCode::SUCCESS);
  346. }
  347. }