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.

456 lines
15 KiB

4 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
7 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
7 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
7 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
7 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
7 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
7 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
7 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
7 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
7 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
7 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
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use Illuminate\Support\Facades\Log;
  4. use Illuminate\Support\Facades\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 BaseController
  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. $data = [];
  75. if($request->input('email', null)!==null){$data['email'] = $request->input('email');}
  76. if($request->input('sex', null)!==null){$data['sex'] = $request->input('sex');}
  77. if($request->input('birthday', null)!==null){$data['birthday'] = $request->input('birthday');}
  78. if($request->input('address_id', null)!==null){$data['address_id'] = $request->input('address_id');}
  79. if($request->input('nickname', null)!==null){$data['nickname'] = $request->input('nickname');}
  80. if($request->input('mobile', null)!==null){$data['mobile'] = $request->input('mobile');}
  81. if($request->input('group_id', null)!==null){$data['group_id'] = $request->input('group_id');}
  82. if($request->input('password', null)!==null){$data['password'] = $request->input('password');}
  83. if($request->input('head_img', null)!==null){$data['head_img'] = $request->input('head_img');}
  84. if($request->input('refund_account', null)!==null){$data['refund_account'] = $request->input('refund_account');}
  85. if($request->input('refund_name', null)!==null){$data['refund_name'] = $request->input('refund_name');}
  86. return $this->getLogic()->edit($data,$where);
  87. }
  88. }
  89. //删除
  90. public function userDelete(Request $request)
  91. {
  92. if(!checkIsNumber($request->input('id',null))){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  93. $id = $request->input('id');
  94. if(Helper::isPostRequest())
  95. {
  96. $where['id'] = $id;
  97. //$where['user_id'] = Token::$uid;
  98. return $this->getLogic()->del($where);
  99. }
  100. }
  101. //用户信息
  102. public function userInfo(Request $request)
  103. {
  104. $where['id'] = Token::$uid;
  105. $res = $this->getLogic()->getOne($where);
  106. if(!$res)
  107. {
  108. return ReturnData::create(ReturnData::RECORD_NOT_EXIST);
  109. }
  110. if($res->pay_password){$res->pay_password = 1;}else{$res->pay_password = 0;}
  111. unset($res->password);
  112. return ReturnData::create(ReturnData::SUCCESS,$res);
  113. }
  114. //修改用户密码、支付密码
  115. public function userPasswordUpdate(Request $request)
  116. {
  117. if($request->input('password', '')!='' && $request->input('old_password', '')!='')
  118. {
  119. $data['password'] = $request->input('password');
  120. $data['old_password'] = $request->input('old_password');
  121. if($data['password'] == $data['old_password']){return ReturnData::create(ReturnData::PARAMS_ERROR,null,'新旧密码相同');}
  122. }
  123. if($request->input('pay_password', '')!='')
  124. {
  125. $data['pay_password'] = $request->input('pay_password');
  126. $data['old_pay_password'] = $request->input('old_pay_password','');
  127. if($data['pay_password'] == $data['old_pay_password']){return ReturnData::create(ReturnData::PARAMS_ERROR,null,'新旧支付密码相同');}
  128. }
  129. if(!isset($data)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  130. return $this->getLogic()->userPasswordUpdate($data, array('id'=>Token::$uid));
  131. }
  132. //签到
  133. public function signin(Request $request)
  134. {
  135. return $this->getLogic()->signin(array('id'=>Token::$uid,'status'=>User::USER_NORMAL_STATUS));
  136. }
  137. //登录
  138. public function wxLogin(Request $request)
  139. {
  140. $data['user_name'] = $request->input('user_name','');
  141. $data['password'] = $request->input('password','');
  142. $data['openid'] = $request->input('openid','');
  143. if (($data['user_name']=='' || $data['password']=='') && $data['openid']=='')
  144. {
  145. return ReturnData::create(ReturnData::PARAMS_ERROR);
  146. }
  147. return $this->getLogic()->wxLogin($data);
  148. }
  149. //注册
  150. public function wxRegister(Request $request)
  151. {
  152. $data['mobile'] = $request->input('mobile','');
  153. $data['user_name'] = $request->input('user_name','');
  154. $data['password'] = $request->input('password','');
  155. $data['parent_id'] = 0;if($request->input('parent_id',null)!=null){$data['parent_id'] = $request->input('parent_id');}
  156. $parent_mobile = $request->input('parent_mobile','');
  157. if (($data['mobile']=='' && $data['user_name']=='') || $data['password']=='')
  158. {
  159. return ReturnData::create(ReturnData::PARAMS_ERROR);
  160. }
  161. if ($parent_mobile!='')
  162. {
  163. if($user = model('User')->getOne(array('mobile'=>$parent_mobile)))
  164. {
  165. $data['parent_id'] = $user->id;
  166. }
  167. else
  168. {
  169. return ReturnData::create(ReturnData::PARAMS_ERROR,null,'推荐人不存在或推荐人手机号错误');
  170. }
  171. }
  172. if ($data['mobile']!='')
  173. {
  174. //判断手机格式
  175. if(!Helper::isValidMobile($data['mobile'])){return ReturnData::create(ReturnData::MOBILE_FORMAT_FAIL);}
  176. //判断是否已经注册
  177. if (model('User')->getOne(array('mobile'=>$data['mobile'])))
  178. {
  179. return ReturnData::create(ReturnData::MOBILE_EXIST);
  180. }
  181. }
  182. if ($data['user_name']!='')
  183. {
  184. if (model('User')->getOne(array('user_name'=>$data['user_name'])))
  185. {
  186. return ReturnData::create(ReturnData::PARAMS_ERROR,null,'用户名已存在');
  187. }
  188. }
  189. return $this->getLogic()->wxRegister($data);
  190. }
  191. //微信授权注册
  192. public function wxOauthRegister(Request $request)
  193. {
  194. $data['openid'] = $request->input('openid','');
  195. $data['unionid'] = $request->input('unionid','');
  196. $data['sex'] = $request->input('sex','');
  197. $data['head_img'] = $request->input('head_img','');
  198. $data['nickname'] = $request->input('nickname','');
  199. $data['parent_id'] = 0;if($request->input('parent_id',null)!=null){$data['parent_id'] = $request->input('parent_id');}
  200. $data['user_name'] = date('YmdHis').dechex(date('His').rand(1000,9999));
  201. $data['password'] = md5('123456');
  202. if ($data['openid']=='')
  203. {
  204. return ReturnData::create(ReturnData::PARAMS_ERROR);
  205. }
  206. if (!model('User')->getOne(array('openid'=>$data['openid'])))
  207. {
  208. //添加用户
  209. $res = $this->getLogic()->wxRegister($data);
  210. if($res['code'] != ReturnData::SUCCESS){return $res;}
  211. //更新用户名user_name,微信登录没有用户名
  212. model('User')->edit(array('user_name'=>date('Ymd').'u'.$res['data']['uid']),array('id'=>$res['data']['uid']));
  213. }
  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. }