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.

345 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
  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. if (($data['mobile']=='' && $data['user_name']=='') || $data['password']=='')
  83. {
  84. return ReturnData::create(ReturnData::PARAMS_ERROR);
  85. }
  86. if (isset($data['mobile']) && !Helper::isValidMobile($data['mobile']))
  87. {
  88. return ReturnData::create(ReturnData::MOBILE_FORMAT_FAIL);
  89. }
  90. //判断是否已经注册
  91. if (User::getOneUser(array('mobile'=>$data['mobile'])))
  92. {
  93. return ReturnData::create(ReturnData::MOBILE_EXIST);
  94. }
  95. if (User::getOneUser(array('user_name'=>$data['user_name'])))
  96. {
  97. return ReturnData::create(ReturnData::SUCCESS,null,'用户名已存在');
  98. }
  99. //添加用户
  100. $res = User::wxRegister($data);
  101. if($res == false)
  102. {
  103. return ReturnData::create(ReturnData::SYSTEM_FAIL);
  104. }
  105. return ReturnData::create(ReturnData::SUCCESS,$res);
  106. }
  107. //登录
  108. public function wxLogin(Request $request)
  109. {
  110. $data['user_name'] = $request->input('user_name','');
  111. $data['password'] = $request->input('password','');
  112. if ($data['user_name']=='' || $data['password']=='')
  113. {
  114. return ReturnData::create(ReturnData::PARAMS_ERROR);
  115. }
  116. $res = User::wxLogin($data);
  117. if ($res === false)
  118. {
  119. return ReturnData::create(ReturnData::PARAMS_ERROR,null,'账号或密码错误');
  120. }
  121. return ReturnData::create(ReturnData::SUCCESS,$res);
  122. }
  123. //验证码登录
  124. public function verificationCodeLogin(Request $request)
  125. {
  126. $mobile = $request->input('mobile');
  127. $code = $request->input('code', null);
  128. $type = $request->input('type', null); //7表示验证码登录
  129. if (!$mobile || !$code)
  130. {
  131. return response(ReturnCode::create(ReturnCode::PARAMS_ERROR));
  132. }
  133. //判断验证码
  134. if ($type != VerifyCode::TYPE_LOGIN)
  135. {
  136. return response(ReturnCode::create(ReturnCode::INVALID_VERIFY_CODE));
  137. }
  138. $verifyCode = VerifyCode::isVerify($mobile, $code, $type);
  139. if (!$verifyCode)
  140. {
  141. return response(ReturnCode::create(ReturnCode::INVALID_VERIFY_CODE));
  142. }
  143. if ($user = MallDataManager::userFirst(['mobile'=>$mobile]))
  144. {
  145. //获取token
  146. $expired_at = Carbon::now()->addDay()->toDateTimeString();
  147. $token = Token::generate(Token::TYPE_SHOP, $user->id);
  148. $response = ReturnCode::success();
  149. $response['data']=[
  150. '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)
  151. ];
  152. return response($response);
  153. }
  154. else
  155. {
  156. return response(ReturnCode::create(ReturnCode::USER_NOT_EXIST));
  157. }
  158. }
  159. //修改密码
  160. public function changePassword(Request $request)
  161. {
  162. $mobile = $request->input('mobile', null);
  163. $password = $request->input('password', null); //新密码
  164. $oldPassword = $request->input('oldPassword', null); //旧密码
  165. if (!$mobile || !$password || !$oldPassword)
  166. {
  167. return ReturnCode::create(ReturnCode::PARAMS_ERROR);
  168. }
  169. if($password == $oldPassword)
  170. {
  171. return ReturnCode::create(ReturnCode::PARAMS_ERROR,'新旧密码相同');
  172. }
  173. if (!Helper::isValidMobile($mobile))
  174. {
  175. return ReturnCode::create(ReturnCode::MOBILE_FORMAT_FAIL);
  176. }
  177. $user = MallDataManager::userFirst(['mobile'=>$mobile,'password'=>$oldPassword,'id'=>Token::$uid]);
  178. if(!$user)
  179. {
  180. return ReturnCode::create(ReturnCode::PARAMS_ERROR,'手机或密码错误');
  181. }
  182. DB::table('user')->where(['mobile'=>$mobile,'password'=>$oldPassword,'id'=>Token::$uid])->update(['password'=>$password]);
  183. MallDataManager::tokenDelete(['uid'=>Token::$uid]);
  184. return ReturnCode::create(ReturnCode::SUCCESS);
  185. }
  186. //找回密码,不用输入旧密码
  187. public function findPassword(Request $request)
  188. {
  189. $mobile = $request->input('mobile', null);
  190. $password = $request->input('password', null);
  191. if ($mobile && $password)
  192. {
  193. if (!Helper::isValidMobile($mobile))
  194. {
  195. return response(ReturnCode::create(ReturnCode::MOBILE_FORMAT_FAIL));
  196. }
  197. //判断验证码是否有效
  198. $code = $request->input('code', '');
  199. $type = $request->input('type', null);
  200. if($type != VerifyCode::TYPE_CHANGE_PASSWORD)
  201. return response(ReturnCode::create(ReturnCode::INVALID_VERIFY_CODE,'验证码类型错误'));
  202. $verifyCode = VerifyCode::isVerify($mobile, $code, $type);
  203. if($verifyCode)
  204. {
  205. try
  206. {
  207. DB::beginTransaction();
  208. $verifyCode->status = VerifyCode::STATUS_USE;
  209. $verifyCode->save();
  210. if ($user = MallDataManager::userFirst(['mobile'=>$mobile]))
  211. {
  212. DB::table('user')->where(['mobile'=>$mobile])->update(['password'=>$password]);
  213. MallDataManager::tokenDelete(['uid'=>$user->id]);
  214. $response = response(ReturnCode::create(ReturnCode::SUCCESS));
  215. }
  216. else
  217. {
  218. $response = response(ReturnCode::create(ReturnCode::PARAMS_ERROR));
  219. }
  220. DB::commit();
  221. return $response;
  222. }
  223. catch (Exception $e)
  224. {
  225. DB::rollBack();
  226. return response(ReturnCode::error($e->getCode(), $e->getMessage()));
  227. }
  228. }
  229. else
  230. {
  231. return response(ReturnCode::create(ReturnCode::INVALID_VERIFY_CODE));
  232. }
  233. }
  234. else
  235. {
  236. return response(ReturnCode::create(ReturnCode::PARAMS_ERROR));
  237. }
  238. }
  239. //修改手机号
  240. public function changeMobile(Request $request)
  241. {
  242. $mobile = $request->input('mobile', null); //新手机号码
  243. $verificationCode = $request->input('verificationCode', null); //新手机验证码
  244. $oldMobile = $request->input('oldMobile', null); //旧手机号码
  245. $oldVerificationCode = $request->input('oldVerificationCode', null); //旧手机验证码
  246. $type = $request->input('type', null); //验证码类型
  247. if (!$mobile || !$verificationCode || !$oldMobile || !$oldVerificationCode || !$type)
  248. {
  249. return ReturnCode::create(ReturnCode::PARAMS_ERROR);
  250. }
  251. if (!Helper::isValidMobile($mobile))
  252. {
  253. return ReturnCode::create(ReturnCode::MOBILE_FORMAT_FAIL);
  254. }
  255. if($mobile == $oldMobile)
  256. {
  257. return ReturnCode::create(ReturnCode::PARAMS_ERROR,'新旧手机号码相同');
  258. }
  259. if($type != VerifyCode::TYPE_CHANGE_MOBILE)
  260. {
  261. return ReturnCode::create(ReturnCode::INVALID_VERIFY_CODE,'验证码类型错误');
  262. }
  263. $verifyCode = VerifyCode::isVerify($oldMobile, $oldVerificationCode, $type);
  264. if(!$verifyCode)
  265. {
  266. return ReturnCode::create(ReturnCode::INVALID_VERIFY_CODE);
  267. }
  268. $verifyCode = null;
  269. $verifyCode = VerifyCode::isVerify($mobile, $verificationCode, $type);
  270. if(!$verifyCode)
  271. {
  272. return ReturnCode::create(ReturnCode::INVALID_VERIFY_CODE);
  273. }
  274. $user = MallDataManager::userFirst(['mobile'=>$oldMobile,'id'=>Token::$uid]);
  275. if(!$user)
  276. {
  277. return ReturnCode::create(ReturnCode::PARAMS_ERROR,'旧手机号码错误');
  278. }
  279. DB::table('user')->where(['mobile'=>$oldMobile,'id'=>Token::$uid])->update(['mobile'=>$mobile]);
  280. MallDataManager::tokenDelete(['uid'=>Token::$uid]);
  281. return ReturnCode::create(ReturnCode::SUCCESS);
  282. }
  283. }