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.

458 lines
15 KiB

7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
7 years ago
7 years ago
7 years ago
8 years ago
8 years ago
7 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
7 years ago
8 years ago
8 years ago
7 years ago
8 years ago
7 years ago
7 years ago
7 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
7 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 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
7 years ago
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use Log;
  4. use DB;
  5. use Illuminate\Http\Request;
  6. use App\Common\ReturnData;
  7. use App\Common\Helper;
  8. use App\Common\Token;
  9. use App\Http\Model\User;
  10. use App\Http\Logic\UserLogic;
  11. class UserController extends CommonController
  12. {
  13. public function __construct()
  14. {
  15. parent::__construct();
  16. }
  17. public function getLogic()
  18. {
  19. return logic('User');
  20. }
  21. public function userList(Request $request)
  22. {
  23. //参数
  24. $limit = $request->input('limit', 10);
  25. $offset = $request->input('offset', 0);
  26. $where = [];
  27. if($request->input('parent_id', '')!=''){$where['parent_id'] = $request->input('parent_id');}
  28. if($request->input('group_id', '')!=''){$where['group_id'] = $request->input('group_id');}
  29. if($request->input('sex', '')!=''){$where['sex'] = $request->input('sex');}
  30. $res = $this->getLogic()->getList($where, array('id', 'desc'), '*', $offset, $limit);
  31. /* if($res['count']>0)
  32. {
  33. foreach($res['list'] as $k=>$v)
  34. {
  35. }
  36. } */
  37. return ReturnData::create(ReturnData::SUCCESS,$res);
  38. }
  39. public function userDetail(Request $request)
  40. {
  41. //参数
  42. if(!checkIsNumber($request->input('id',null))){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  43. $id = $request->input('id');
  44. $where['id'] = $id;
  45. $res = $this->getLogic()->getOne($where);
  46. if(!$res)
  47. {
  48. return ReturnData::create(ReturnData::RECORD_NOT_EXIST);
  49. }
  50. return ReturnData::create(ReturnData::SUCCESS,$res);
  51. }
  52. //添加
  53. public function userAdd(Request $request)
  54. {
  55. if(Helper::isPostRequest())
  56. {
  57. return $this->getLogic()->add($_POST);
  58. }
  59. }
  60. //修改
  61. public function userUpdate(Request $request)
  62. {
  63. if(Helper::isPostRequest())
  64. {
  65. $where['id'] = Token::$uid;
  66. //判断用户名是否已经存在
  67. if($request->input('user_name', null)!==null)
  68. {
  69. if(model('User')->getOne([['user_name', '=', $request->input('user_name')],['id', '<>', Token::$uid]]))
  70. {
  71. return ReturnData::create(ReturnData::PARAMS_ERROR,null,'用户名已存在');
  72. }
  73. }
  74. if($request->input('email', null)!==null){$data['email'] = $request->input('email');}
  75. if($request->input('sex', null)!==null){$data['sex'] = $request->input('sex');}
  76. if($request->input('birthday', null)!==null){$data['birthday'] = $request->input('birthday');}
  77. if($request->input('address_id', null)!==null){$data['address_id'] = $request->input('address_id');}
  78. if($request->input('nickname', null)!==null){$data['nickname'] = $request->input('nickname');}
  79. if($request->input('mobile', null)!==null){$data['mobile'] = $request->input('mobile');}
  80. if($request->input('group_id', null)!==null){$data['group_id'] = $request->input('group_id');}
  81. if($request->input('password', null)!==null){$data['password'] = $request->input('password');}
  82. if($request->input('head_img', null)!==null){$data['head_img'] = $request->input('head_img');}
  83. if($request->input('refund_account', null)!==null){$data['refund_account'] = $request->input('refund_account');}
  84. if($request->input('refund_name', null)!==null){$data['refund_name'] = $request->input('refund_name');}
  85. return $this->getLogic()->edit($data,$where);
  86. }
  87. }
  88. //删除
  89. public function userDelete(Request $request)
  90. {
  91. if(!checkIsNumber($request->input('id',null))){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  92. $id = $request->input('id');
  93. if(Helper::isPostRequest())
  94. {
  95. $where['id'] = $id;
  96. //$where['user_id'] = Token::$uid;
  97. return $this->getLogic()->del($where);
  98. }
  99. }
  100. //用户信息
  101. public function userInfo(Request $request)
  102. {
  103. $where['id'] = Token::$uid;
  104. $res = $this->getLogic()->getOne($where);
  105. if(!$res)
  106. {
  107. return ReturnData::create(ReturnData::RECORD_NOT_EXIST);
  108. }
  109. if($res->pay_password){$res->pay_password = 1;}else{$res->pay_password = 0;}
  110. unset($res->password);
  111. return ReturnData::create(ReturnData::SUCCESS,$res);
  112. }
  113. //修改用户密码、支付密码
  114. public function userPasswordUpdate(Request $request)
  115. {
  116. if($request->input('password', '')!='' && $request->input('old_password', '')!='')
  117. {
  118. $data['password'] = $request->input('password');
  119. $data['old_password'] = $request->input('old_password');
  120. if($data['password'] == $data['old_password']){return ReturnData::create(ReturnData::PARAMS_ERROR,null,'新旧密码相同');}
  121. }
  122. if($request->input('pay_password', '')!='')
  123. {
  124. $data['pay_password'] = $request->input('pay_password');
  125. $data['old_pay_password'] = $request->input('old_pay_password','');
  126. if($data['pay_password'] == $data['old_pay_password']){return ReturnData::create(ReturnData::PARAMS_ERROR,null,'新旧支付密码相同');}
  127. }
  128. if(!isset($data)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  129. return $this->getLogic()->userPasswordUpdate($data, array('id'=>Token::$uid));
  130. }
  131. //签到
  132. public function signin(Request $request)
  133. {
  134. return $this->getLogic()->signin(array('id'=>Token::$uid,'status'=>User::USER_NORMAL_STATUS));
  135. }
  136. //登录
  137. public function wxLogin(Request $request)
  138. {
  139. $data['user_name'] = $request->input('user_name','');
  140. $data['password'] = $request->input('password','');
  141. $data['openid'] = $request->input('openid','');
  142. if (($data['user_name']=='' || $data['password']=='') && $data['openid']=='')
  143. {
  144. return ReturnData::create(ReturnData::PARAMS_ERROR);
  145. }
  146. return $this->getLogic()->wxLogin($data);
  147. }
  148. //注册
  149. public function wxRegister(Request $request)
  150. {
  151. $data['mobile'] = $request->input('mobile','');
  152. $data['user_name'] = $request->input('user_name','');
  153. $data['password'] = $request->input('password','');
  154. $data['parent_id'] = 0;if($request->input('parent_id',null)!=null){$data['parent_id'] = $request->input('parent_id');}
  155. $parent_mobile = $request->input('parent_mobile','');
  156. if (($data['mobile']=='' && $data['user_name']=='') || $data['password']=='')
  157. {
  158. return ReturnData::create(ReturnData::PARAMS_ERROR);
  159. }
  160. if ($parent_mobile!='')
  161. {
  162. if($user = model('User')->getOne(array('mobile'=>$parent_mobile)))
  163. {
  164. $data['parent_id'] = $user->id;
  165. }
  166. else
  167. {
  168. return ReturnData::create(ReturnData::PARAMS_ERROR,null,'推荐人不存在或推荐人手机号错误');
  169. }
  170. }
  171. if ($data['mobile']!='')
  172. {
  173. //判断手机格式
  174. if(!Helper::isValidMobile($data['mobile'])){return ReturnData::create(ReturnData::MOBILE_FORMAT_FAIL);}
  175. //判断是否已经注册
  176. if (model('User')->getOne(array('mobile'=>$data['mobile'])))
  177. {
  178. return ReturnData::create(ReturnData::MOBILE_EXIST);
  179. }
  180. }
  181. if ($data['user_name']!='')
  182. {
  183. if (model('User')->getOne(array('user_name'=>$data['user_name'])))
  184. {
  185. return ReturnData::create(ReturnData::PARAMS_ERROR,null,'用户名已存在');
  186. }
  187. }
  188. return $this->getLogic()->wxRegister($data);
  189. }
  190. //微信授权注册
  191. public function wxOauthRegister(Request $request)
  192. {
  193. $data['openid'] = $request->input('openid','');
  194. $data['unionid'] = $request->input('unionid','');
  195. $data['sex'] = $request->input('sex','');
  196. $data['head_img'] = $request->input('head_img','');
  197. $data['nickname'] = $request->input('nickname','');
  198. $data['parent_id'] = 0;if($request->input('parent_id',null)!=null){$data['parent_id'] = $request->input('parent_id');}
  199. $data['user_name'] = date('YmdHis').dechex(rand(1000,9999));
  200. $data['password'] = md5('123456');
  201. if ($data['openid']=='')
  202. {
  203. return ReturnData::create(ReturnData::PARAMS_ERROR);
  204. }
  205. if (model('User')->getOne(array('openid'=>$data['openid'])))
  206. {
  207. return $this->getLogic()->wxLogin(array('openid'=>$data['openid']));
  208. }
  209. //添加用户
  210. $res = $this->getLogic()->wxRegister($data);
  211. if($res['code'] != ReturnData::SUCCESS){return $res;}
  212. //更新用户名user_name,微信登录没有用户名
  213. model('User')->edit(array('user_name'=>'u'.$res['code']['data']['uid']),array('id'=>$res['code']['data']['uid']));
  214. return $this->getLogic()->wxLogin(array('openid'=>$data['openid']));
  215. }
  216. //验证码登录
  217. public function verificationCodeLogin(Request $request)
  218. {
  219. $mobile = $request->input('mobile');
  220. $code = $request->input('code', null);
  221. $type = $request->input('type', null); //7表示验证码登录
  222. if (!$mobile || !$code)
  223. {
  224. return response(ReturnCode::create(ReturnCode::PARAMS_ERROR));
  225. }
  226. //判断验证码
  227. if ($type != VerifyCode::TYPE_LOGIN)
  228. {
  229. return response(ReturnCode::create(ReturnCode::INVALID_VERIFY_CODE));
  230. }
  231. $verifyCode = VerifyCode::isVerify($mobile, $code, $type);
  232. if (!$verifyCode)
  233. {
  234. return response(ReturnCode::create(ReturnCode::INVALID_VERIFY_CODE));
  235. }
  236. if ($user = MallDataManager::userFirst(['mobile'=>$mobile]))
  237. {
  238. //获取token
  239. $expired_at = Carbon::now()->addDay()->toDateTimeString();
  240. $token = Token::generate(Token::TYPE_SHOP, $user->id);
  241. $response = ReturnCode::success();
  242. $response['data']=[
  243. '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)
  244. ];
  245. return response($response);
  246. }
  247. else
  248. {
  249. return response(ReturnCode::create(ReturnCode::USER_NOT_EXIST));
  250. }
  251. }
  252. //修改密码
  253. public function changePassword(Request $request)
  254. {
  255. $mobile = $request->input('mobile', null);
  256. $password = $request->input('password', null); //新密码
  257. $oldPassword = $request->input('oldPassword', null); //旧密码
  258. if (!$mobile || !$password || !$oldPassword)
  259. {
  260. return ReturnCode::create(ReturnCode::PARAMS_ERROR);
  261. }
  262. if($password == $oldPassword)
  263. {
  264. return ReturnCode::create(ReturnCode::PARAMS_ERROR,'新旧密码相同');
  265. }
  266. if (!Helper::isValidMobile($mobile))
  267. {
  268. return ReturnCode::create(ReturnCode::MOBILE_FORMAT_FAIL);
  269. }
  270. $user = MallDataManager::userFirst(['mobile'=>$mobile,'password'=>$oldPassword,'id'=>Token::$uid]);
  271. if(!$user)
  272. {
  273. return ReturnCode::create(ReturnCode::PARAMS_ERROR,'手机或密码错误');
  274. }
  275. DB::table('user')->where(['mobile'=>$mobile,'password'=>$oldPassword,'id'=>Token::$uid])->update(['password'=>$password]);
  276. MallDataManager::tokenDelete(['uid'=>Token::$uid]);
  277. return ReturnCode::create(ReturnCode::SUCCESS);
  278. }
  279. //找回密码,不用输入旧密码
  280. public function findPassword(Request $request)
  281. {
  282. $mobile = $request->input('mobile', null);
  283. $password = $request->input('password', null);
  284. if ($mobile && $password)
  285. {
  286. if (!Helper::isValidMobile($mobile))
  287. {
  288. return response(ReturnCode::create(ReturnCode::MOBILE_FORMAT_FAIL));
  289. }
  290. //判断验证码是否有效
  291. $code = $request->input('code', '');
  292. $type = $request->input('type', null);
  293. if($type != VerifyCode::TYPE_CHANGE_PASSWORD)
  294. return response(ReturnCode::create(ReturnCode::INVALID_VERIFY_CODE,'验证码类型错误'));
  295. $verifyCode = VerifyCode::isVerify($mobile, $code, $type);
  296. if($verifyCode)
  297. {
  298. try
  299. {
  300. DB::beginTransaction();
  301. $verifyCode->status = VerifyCode::STATUS_USE;
  302. $verifyCode->save();
  303. if ($user = MallDataManager::userFirst(['mobile'=>$mobile]))
  304. {
  305. DB::table('user')->where(['mobile'=>$mobile])->update(['password'=>$password]);
  306. MallDataManager::tokenDelete(['uid'=>$user->id]);
  307. $response = response(ReturnCode::create(ReturnCode::SUCCESS));
  308. }
  309. else
  310. {
  311. $response = response(ReturnCode::create(ReturnCode::PARAMS_ERROR));
  312. }
  313. DB::commit();
  314. return $response;
  315. }
  316. catch (Exception $e)
  317. {
  318. DB::rollBack();
  319. return response(ReturnCode::error($e->getCode(), $e->getMessage()));
  320. }
  321. }
  322. else
  323. {
  324. return response(ReturnCode::create(ReturnCode::INVALID_VERIFY_CODE));
  325. }
  326. }
  327. else
  328. {
  329. return response(ReturnCode::create(ReturnCode::PARAMS_ERROR));
  330. }
  331. }
  332. //修改手机号
  333. public function changeMobile(Request $request)
  334. {
  335. $mobile = $request->input('mobile', null); //新手机号码
  336. $verificationCode = $request->input('verificationCode', null); //新手机验证码
  337. $oldMobile = $request->input('oldMobile', null); //旧手机号码
  338. $oldVerificationCode = $request->input('oldVerificationCode', null); //旧手机验证码
  339. $type = $request->input('type', null); //验证码类型
  340. if (!$mobile || !$verificationCode || !$oldMobile || !$oldVerificationCode || !$type)
  341. {
  342. return ReturnCode::create(ReturnCode::PARAMS_ERROR);
  343. }
  344. if (!Helper::isValidMobile($mobile))
  345. {
  346. return ReturnCode::create(ReturnCode::MOBILE_FORMAT_FAIL);
  347. }
  348. if($mobile == $oldMobile)
  349. {
  350. return ReturnCode::create(ReturnCode::PARAMS_ERROR,'新旧手机号码相同');
  351. }
  352. if($type != VerifyCode::TYPE_CHANGE_MOBILE)
  353. {
  354. return ReturnCode::create(ReturnCode::INVALID_VERIFY_CODE,'验证码类型错误');
  355. }
  356. $verifyCode = VerifyCode::isVerify($oldMobile, $oldVerificationCode, $type);
  357. if(!$verifyCode)
  358. {
  359. return ReturnCode::create(ReturnCode::INVALID_VERIFY_CODE);
  360. }
  361. $verifyCode = null;
  362. $verifyCode = VerifyCode::isVerify($mobile, $verificationCode, $type);
  363. if(!$verifyCode)
  364. {
  365. return ReturnCode::create(ReturnCode::INVALID_VERIFY_CODE);
  366. }
  367. $user = MallDataManager::userFirst(['mobile'=>$oldMobile,'id'=>Token::$uid]);
  368. if(!$user)
  369. {
  370. return ReturnCode::create(ReturnCode::PARAMS_ERROR,'旧手机号码错误');
  371. }
  372. DB::table('user')->where(['mobile'=>$oldMobile,'id'=>Token::$uid])->update(['mobile'=>$mobile]);
  373. MallDataManager::tokenDelete(['uid'=>Token::$uid]);
  374. return ReturnCode::create(ReturnCode::SUCCESS);
  375. }
  376. }