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.

358 lines
11 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
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\Common\Helper;
  8. use App\Http\Model\User;
  9. class UserController extends CommonController
  10. {
  11. public function __construct()
  12. {
  13. parent::__construct();
  14. }
  15. //用户信息
  16. public function userInfo(Request $request)
  17. {
  18. if ($user = User::getUserInfo(Token::$uid))
  19. {
  20. return ReturnData::create(ReturnData::SUCCESS, $user);
  21. }
  22. else
  23. {
  24. return ReturnData::create(ReturnData::RECORD_NOT_EXIST);
  25. }
  26. }
  27. //修改用户信息
  28. public function userInfoUpdate(Request $request)
  29. {
  30. $data = '';
  31. if($request->input('user_name', null)!==null){$data['user_name'] = $request->input('user_name');}
  32. if($request->input('email', null)!==null){$data['email'] = $request->input('email');}
  33. if($request->input('sex', null)!==null){$data['sex'] = $request->input('sex');}
  34. if($request->input('birthday', null)!==null){$data['birthday'] = $request->input('birthday');}
  35. if($request->input('money', null)!==null){$data['money'] = $request->input('money');}
  36. if($request->input('frozen_money', null)!==null){$data['frozen_money'] = $request->input('frozen_money');}
  37. if($request->input('point', null)!==null){$data['point'] = $request->input('point');}
  38. if($request->input('address_id', null)!==null){$data['address_id'] = $request->input('address_id');}
  39. if($request->input('user_rank', null)!==null){$data['user_rank'] = $request->input('user_rank');}
  40. if($request->input('parent_id', null)!==null){$data['parent_id'] = $request->input('parent_id');}
  41. if($request->input('nickname', null)!==null){$data['nickname'] = $request->input('nickname');}
  42. if($request->input('mobile', null)!==null){$data['mobile'] = $request->input('mobile');}
  43. if($request->input('status', null)!==null){$data['status'] = $request->input('status');}
  44. if($request->input('group_id', null)!==null){$data['group_id'] = $request->input('group_id');}
  45. if($request->input('password', null)!==null){$data['password'] = $request->input('password');}
  46. if($request->input('head_img', null)!==null){$data['head_img'] = $request->input('head_img');}
  47. if ($data != '')
  48. {
  49. User::modify(['id'=>Token::$uid],$data);
  50. }
  51. return ReturnData::create(ReturnData::SUCCESS);
  52. }
  53. //用户列表
  54. public function userList(Request $request)
  55. {
  56. //参数
  57. $data['limit'] = $request->input('limit', 10);
  58. $data['offset'] = $request->input('offset', 0);
  59. $res = User::getList($data);
  60. if(!$res)
  61. {
  62. return ReturnData::create(ReturnData::SYSTEM_FAIL);
  63. }
  64. return ReturnData::create(ReturnData::SUCCESS,$res);
  65. }
  66. //签到
  67. public function signin(Request $request)
  68. {
  69. $res = User::signin();
  70. if($res !== true)
  71. {
  72. return ReturnData::create(ReturnData::PARAMS_ERROR,null,$res);
  73. }
  74. return ReturnData::create(ReturnData::SUCCESS);
  75. }
  76. //注册
  77. public function wxRegister(Request $request)
  78. {
  79. $data['mobile'] = $request->input('mobile','');
  80. $data['user_name'] = $request->input('user_name','');
  81. $data['password'] = $request->input('password','');
  82. $parent_mobile = $request->input('parent_mobile','');
  83. if (($data['mobile']=='' && $data['user_name']=='') || $data['password']=='')
  84. {
  85. return ReturnData::create(ReturnData::PARAMS_ERROR);
  86. }
  87. if ($parent_mobile!='')
  88. {
  89. if($user = User::getOneUser(array('mobile'=>$parent_mobile)))
  90. {
  91. $data['parent_id'] = $user->id;
  92. }
  93. else
  94. {
  95. return ReturnData::create(ReturnData::PARAMS_ERROR,null,'推荐人手机号错误');
  96. }
  97. }
  98. if (isset($data['mobile']) && !Helper::isValidMobile($data['mobile']))
  99. {
  100. return ReturnData::create(ReturnData::MOBILE_FORMAT_FAIL);
  101. }
  102. //判断是否已经注册
  103. if (User::getOneUser(array('mobile'=>$data['mobile'])))
  104. {
  105. return ReturnData::create(ReturnData::MOBILE_EXIST);
  106. }
  107. if (User::getOneUser(array('user_name'=>$data['user_name'])))
  108. {
  109. return ReturnData::create(ReturnData::PARAMS_ERROR,null,'用户名已存在');
  110. }
  111. //添加用户
  112. $res = User::wxRegister($data);
  113. if($res == false)
  114. {
  115. return ReturnData::create(ReturnData::SYSTEM_FAIL);
  116. }
  117. return ReturnData::create(ReturnData::SUCCESS,$res);
  118. }
  119. //登录
  120. public function wxLogin(Request $request)
  121. {
  122. $data['user_name'] = $request->input('user_name','');
  123. $data['password'] = $request->input('password','');
  124. if ($data['user_name']=='' || $data['password']=='')
  125. {
  126. return ReturnData::create(ReturnData::PARAMS_ERROR);
  127. }
  128. $res = User::wxLogin($data);
  129. if ($res === false)
  130. {
  131. return ReturnData::create(ReturnData::PARAMS_ERROR,null,'账号或密码错误');
  132. }
  133. return ReturnData::create(ReturnData::SUCCESS,$res);
  134. }
  135. //验证码登录
  136. public function verificationCodeLogin(Request $request)
  137. {
  138. $mobile = $request->input('mobile');
  139. $code = $request->input('code', null);
  140. $type = $request->input('type', null); //7表示验证码登录
  141. if (!$mobile || !$code)
  142. {
  143. return response(ReturnCode::create(ReturnCode::PARAMS_ERROR));
  144. }
  145. //判断验证码
  146. if ($type != VerifyCode::TYPE_LOGIN)
  147. {
  148. return response(ReturnCode::create(ReturnCode::INVALID_VERIFY_CODE));
  149. }
  150. $verifyCode = VerifyCode::isVerify($mobile, $code, $type);
  151. if (!$verifyCode)
  152. {
  153. return response(ReturnCode::create(ReturnCode::INVALID_VERIFY_CODE));
  154. }
  155. if ($user = MallDataManager::userFirst(['mobile'=>$mobile]))
  156. {
  157. //获取token
  158. $expired_at = Carbon::now()->addDay()->toDateTimeString();
  159. $token = Token::generate(Token::TYPE_SHOP, $user->id);
  160. $response = ReturnCode::success();
  161. $response['data']=[
  162. '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)
  163. ];
  164. return response($response);
  165. }
  166. else
  167. {
  168. return response(ReturnCode::create(ReturnCode::USER_NOT_EXIST));
  169. }
  170. }
  171. //修改密码
  172. public function changePassword(Request $request)
  173. {
  174. $mobile = $request->input('mobile', null);
  175. $password = $request->input('password', null); //新密码
  176. $oldPassword = $request->input('oldPassword', null); //旧密码
  177. if (!$mobile || !$password || !$oldPassword)
  178. {
  179. return ReturnCode::create(ReturnCode::PARAMS_ERROR);
  180. }
  181. if($password == $oldPassword)
  182. {
  183. return ReturnCode::create(ReturnCode::PARAMS_ERROR,'新旧密码相同');
  184. }
  185. if (!Helper::isValidMobile($mobile))
  186. {
  187. return ReturnCode::create(ReturnCode::MOBILE_FORMAT_FAIL);
  188. }
  189. $user = MallDataManager::userFirst(['mobile'=>$mobile,'password'=>$oldPassword,'id'=>Token::$uid]);
  190. if(!$user)
  191. {
  192. return ReturnCode::create(ReturnCode::PARAMS_ERROR,'手机或密码错误');
  193. }
  194. DB::table('user')->where(['mobile'=>$mobile,'password'=>$oldPassword,'id'=>Token::$uid])->update(['password'=>$password]);
  195. MallDataManager::tokenDelete(['uid'=>Token::$uid]);
  196. return ReturnCode::create(ReturnCode::SUCCESS);
  197. }
  198. //找回密码,不用输入旧密码
  199. public function findPassword(Request $request)
  200. {
  201. $mobile = $request->input('mobile', null);
  202. $password = $request->input('password', null);
  203. if ($mobile && $password)
  204. {
  205. if (!Helper::isValidMobile($mobile))
  206. {
  207. return response(ReturnCode::create(ReturnCode::MOBILE_FORMAT_FAIL));
  208. }
  209. //判断验证码是否有效
  210. $code = $request->input('code', '');
  211. $type = $request->input('type', null);
  212. if($type != VerifyCode::TYPE_CHANGE_PASSWORD)
  213. return response(ReturnCode::create(ReturnCode::INVALID_VERIFY_CODE,'验证码类型错误'));
  214. $verifyCode = VerifyCode::isVerify($mobile, $code, $type);
  215. if($verifyCode)
  216. {
  217. try
  218. {
  219. DB::beginTransaction();
  220. $verifyCode->status = VerifyCode::STATUS_USE;
  221. $verifyCode->save();
  222. if ($user = MallDataManager::userFirst(['mobile'=>$mobile]))
  223. {
  224. DB::table('user')->where(['mobile'=>$mobile])->update(['password'=>$password]);
  225. MallDataManager::tokenDelete(['uid'=>$user->id]);
  226. $response = response(ReturnCode::create(ReturnCode::SUCCESS));
  227. }
  228. else
  229. {
  230. $response = response(ReturnCode::create(ReturnCode::PARAMS_ERROR));
  231. }
  232. DB::commit();
  233. return $response;
  234. }
  235. catch (Exception $e)
  236. {
  237. DB::rollBack();
  238. return response(ReturnCode::error($e->getCode(), $e->getMessage()));
  239. }
  240. }
  241. else
  242. {
  243. return response(ReturnCode::create(ReturnCode::INVALID_VERIFY_CODE));
  244. }
  245. }
  246. else
  247. {
  248. return response(ReturnCode::create(ReturnCode::PARAMS_ERROR));
  249. }
  250. }
  251. //修改手机号
  252. public function changeMobile(Request $request)
  253. {
  254. $mobile = $request->input('mobile', null); //新手机号码
  255. $verificationCode = $request->input('verificationCode', null); //新手机验证码
  256. $oldMobile = $request->input('oldMobile', null); //旧手机号码
  257. $oldVerificationCode = $request->input('oldVerificationCode', null); //旧手机验证码
  258. $type = $request->input('type', null); //验证码类型
  259. if (!$mobile || !$verificationCode || !$oldMobile || !$oldVerificationCode || !$type)
  260. {
  261. return ReturnCode::create(ReturnCode::PARAMS_ERROR);
  262. }
  263. if (!Helper::isValidMobile($mobile))
  264. {
  265. return ReturnCode::create(ReturnCode::MOBILE_FORMAT_FAIL);
  266. }
  267. if($mobile == $oldMobile)
  268. {
  269. return ReturnCode::create(ReturnCode::PARAMS_ERROR,'新旧手机号码相同');
  270. }
  271. if($type != VerifyCode::TYPE_CHANGE_MOBILE)
  272. {
  273. return ReturnCode::create(ReturnCode::INVALID_VERIFY_CODE,'验证码类型错误');
  274. }
  275. $verifyCode = VerifyCode::isVerify($oldMobile, $oldVerificationCode, $type);
  276. if(!$verifyCode)
  277. {
  278. return ReturnCode::create(ReturnCode::INVALID_VERIFY_CODE);
  279. }
  280. $verifyCode = null;
  281. $verifyCode = VerifyCode::isVerify($mobile, $verificationCode, $type);
  282. if(!$verifyCode)
  283. {
  284. return ReturnCode::create(ReturnCode::INVALID_VERIFY_CODE);
  285. }
  286. $user = MallDataManager::userFirst(['mobile'=>$oldMobile,'id'=>Token::$uid]);
  287. if(!$user)
  288. {
  289. return ReturnCode::create(ReturnCode::PARAMS_ERROR,'旧手机号码错误');
  290. }
  291. DB::table('user')->where(['mobile'=>$oldMobile,'id'=>Token::$uid])->update(['mobile'=>$mobile]);
  292. MallDataManager::tokenDelete(['uid'=>Token::$uid]);
  293. return ReturnCode::create(ReturnCode::SUCCESS);
  294. }
  295. }