User.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <?php
  2. namespace app\api\controller\doctor;
  3. use app\common\controller\Apic;
  4. use app\common\library\Sms;
  5. use think\Exception;
  6. use think\Validate;
  7. use think\Db;
  8. use app\common\library\Wechat;
  9. /**
  10. * 会员接口
  11. */
  12. class User extends Apic
  13. {
  14. protected $noNeedLogin = ['mobilelogin','wechatlogin','bindmobile'];
  15. protected $noNeedRight = '*';
  16. /**
  17. * 手机验证码登录
  18. *
  19. * @ApiMethod (POST)
  20. * @param string $mobile 手机号
  21. * @param string $captcha 验证码
  22. */
  23. public function mobilelogin()
  24. {
  25. $mobile = input('mobile');
  26. $captcha = input('captcha');
  27. if (!$mobile || !$captcha) {
  28. $this->error(__('Invalid parameters'));
  29. }
  30. if (!Validate::regex($mobile, "^1\d{10}$")) {
  31. $this->error(__('Mobile is incorrect'));
  32. }
  33. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  34. $this->error(__('Captcha is incorrect'));
  35. }
  36. $user = \app\common\model\Doctor::getByMobile($mobile);
  37. if ($user) {
  38. if ($user->status == -1) {
  39. $this->error('账号已注销');
  40. }
  41. if ($user->status != 1) {
  42. $this->error(__('Account is locked'));
  43. }
  44. //如果已经有账号则直接登录
  45. $ret = $this->auth->direct($user->id);
  46. } else {
  47. $ret = $this->auth->register('', '', '', $mobile, []);
  48. }
  49. if ($ret) {
  50. Sms::flush($mobile, 'mobilelogin');
  51. $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple());
  52. } else {
  53. $this->error($this->auth->getError());
  54. }
  55. }
  56. //微信登录,预先假注册
  57. public function wechatlogin(){
  58. $code = input('code','');
  59. if(!$code){
  60. $this->error();
  61. }
  62. //微信
  63. $wechat = new Wechat();
  64. $wxuserinfo = $wechat->getAccessToken($code);
  65. if(!$wxuserinfo){
  66. $this->error('openid获取失败');
  67. }
  68. if(!is_array($wxuserinfo) || !isset($wxuserinfo['openid'])){
  69. $this->error('openid获取失败');
  70. }
  71. $openid = $wxuserinfo['openid'];
  72. //检查用户
  73. $user = Db::name('doctor')->where('wechat_openid',$openid)->find();
  74. if ($user) {
  75. if ($user['status'] == -1) {
  76. $this->error('账户已注销');
  77. }
  78. if ($user['status'] != 1) {
  79. $this->error(__('Account is locked'));
  80. }
  81. //如果已经有账号则直接登录
  82. $ret = $this->auth->direct($user['id']);
  83. if ($ret) {
  84. $userInfo = $this->auth->getUserinfo_simple();
  85. $userInfo['is_register'] = 0;
  86. $userInfo['code'] = $code;
  87. $this->success(__('Logged in successful'), $userInfo);
  88. } else {
  89. $this->error($this->auth->getError());
  90. }
  91. } else {
  92. //记录code和openid,绑定手机号的时候更新openid
  93. $wechatCodeData = [
  94. 'code' => $code,
  95. 'openid' => $openid,
  96. 'createtime' => time(),
  97. ];
  98. $wechatCode = Db::name('wechat_code')->where(['openid'=>$openid])->find();
  99. if (empty($wechatCode)) {
  100. Db::name('wechat_code')->insertGetId($wechatCodeData);
  101. } else {
  102. Db::name('wechat_code')->where(['openid'=>$openid])->update($wechatCodeData);
  103. }
  104. //直接返回
  105. $userInfo = [];
  106. $userInfo['is_register'] = 1;
  107. $userInfo['code'] = $code;
  108. $this->success('获取信息成功', $userInfo);
  109. }
  110. }
  111. /**
  112. * 微信注册来的,绑定手机号
  113. *
  114. * @ApiMethod (POST)
  115. * @param string $mobile 手机号
  116. * @param string $captcha 验证码
  117. */
  118. public function bindmobile()
  119. {
  120. $mobile = input('mobile');
  121. $captcha = input('captcha');
  122. $code = input('code');
  123. if (!$mobile || !$captcha || !$code) {
  124. $this->error(__('Invalid parameters'));
  125. }
  126. if (!Validate::regex($mobile, "^1\d{10}$")) {
  127. $this->error(__('Mobile is incorrect'));
  128. }
  129. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  130. $this->error(__('Captcha is incorrect'));
  131. }
  132. $wechatCodeWhere['code'] = $code;
  133. $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
  134. if (empty($wechatCode)) {
  135. $this->error('请先微信登录');
  136. }
  137. //检查appid绑定的用户
  138. $user = Db::name('doctor')->where('wechat_openid',$wechatCode['openid'])->find();
  139. if ($user) {
  140. if ($user['status'] == -1) {
  141. $this->error('账户已注销');
  142. }
  143. if ($user['status'] != 1) {
  144. $this->error(__('Account is locked'));
  145. }
  146. //如果已经有账号则直接登录
  147. $ret = $this->auth->direct($user['id']);
  148. $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple());
  149. }
  150. //新的openid用户
  151. $where = [];
  152. $where['mobile'] = $mobile;
  153. $userData = Db::name('doctor')->where($where)->find();//老用户
  154. if (!empty($userData)) {
  155. if ($userData['status'] == -1) {
  156. $this->error('账户已注销');
  157. }
  158. if ($userData['status'] != 1) {
  159. $this->error(__('Account is locked'));
  160. }
  161. if (empty($userData['wechat_openid'])) {
  162. Db::name('doctor')->where('id',$userData['id'])->update(['wechat_openid' => $wechatCode['openid']]);//老用户更新openid
  163. } else {
  164. if ($userData['wechat_openid'] != $wechatCode['openid']) {
  165. $this->error('该手机号已被其他用户绑定');
  166. }
  167. }
  168. $ret = $this->auth->direct($userData['id']);
  169. } else {
  170. $extend = [
  171. 'wechat_openid' => $wechatCode['openid'],
  172. ];
  173. $ret = $this->auth->register('', '','', $mobile, $extend);
  174. }
  175. if (!$ret) {
  176. $this->error($this->auth->getError());
  177. }
  178. $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple());
  179. }
  180. //用户详细资料
  181. public function userInfo(){
  182. $info = $this->auth->getUserinfo();
  183. $this->success(__('success'),$info);
  184. }
  185. /**
  186. * 退出登录
  187. * @ApiMethod (POST)
  188. */
  189. public function logout()
  190. {
  191. if (!$this->request->isPost()) {
  192. $this->error(__('Invalid parameters'));
  193. }
  194. $this->auth->logout();
  195. $this->success(__('Logout successful'));
  196. }
  197. //用户详细资料
  198. public function getUserinfo(){
  199. $info = $this->auth->getUserinfo();
  200. $this->success(__('success'),$info);
  201. }
  202. /**
  203. * 重置密码
  204. *
  205. * @ApiMethod (POST)
  206. * @param string $mobile 手机号
  207. * @param string $captcha 验证码
  208. * @param string $newpassword 新密码
  209. */
  210. /*public function resetpwd()
  211. {
  212. $mobile = $this->request->post('mobile');
  213. $captcha = $this->request->post('captcha');
  214. $newpassword = $this->request->post("newpassword");
  215. if (!$mobile || !$captcha || !$newpassword) {
  216. $this->error(__('Invalid parameters'));
  217. }
  218. //验证Token
  219. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  220. $this->error(__('Password must be 6 to 30 characters'));
  221. }
  222. if (!Validate::regex($mobile, "^1\d{10}$")) {
  223. $this->error(__('Mobile is incorrect'));
  224. }
  225. $user = \app\common\model\CompanyStaff::getByMobile($mobile);
  226. if (!$user) {
  227. $this->error(__('User not found'));
  228. }
  229. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  230. if (!$ret) {
  231. $this->error(__('Captcha is incorrect'));
  232. }
  233. Sms::flush($mobile, 'resetpwd');
  234. //模拟一次登录
  235. $this->auth->direct($user->id);
  236. $ret = $this->auth->resetpwd($newpassword, '', true);
  237. if ($ret) {
  238. $this->success(__('Reset password successful'));
  239. } else {
  240. $this->error($this->auth->getError());
  241. }
  242. }*/
  243. /**
  244. * 修改会员个人信息
  245. *
  246. * @ApiMethod (POST)
  247. * @param string $avatar 头像地址
  248. * @param string $username 用户名
  249. * @param string $nickname 昵称
  250. * @param string $bio 个人简介
  251. */
  252. public function profile()
  253. {
  254. $field_array = [
  255. 'realname',
  256. 'idcard',
  257. 'english_status',
  258. 'idcard_z_image',
  259. 'idcard_f_image',
  260. 'doctor_image',
  261. 'avatar','nickname','gender',
  262. 'keshi_id','hospital','goodat','level_id','info','job_status'
  263. ];
  264. $data = [];
  265. foreach($field_array as $key => $field){
  266. //前端传不了post,改了
  267. /*if(!request()->has($field,'post')){
  268. continue;
  269. }*/
  270. if(!input('?'.$field)){
  271. continue;
  272. }
  273. $newone = input($field);
  274. if($field == 'avatar'){
  275. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  276. }
  277. $data[$field] = $newone;
  278. }
  279. //
  280. /*if(isset($data['birthday'])){
  281. $data['birthday'] = strtotime($data['birthday']);
  282. }*/
  283. if(empty($data)){
  284. $this->success();
  285. }
  286. $update_rs = Db::name('doctor')->where('id',$this->auth->id)->update($data);
  287. $this->success();
  288. }
  289. //问诊设置
  290. public function profile_wenzhen()
  291. {
  292. $field_array = [
  293. 'typing_switch',
  294. 'video_switch',
  295. 'typing_price',
  296. 'video_price',
  297. 'notice_switch',
  298. 'jolt_switch',
  299. ];
  300. $data = [];
  301. foreach($field_array as $key => $field){
  302. if(!input('?'.$field)){
  303. continue;
  304. }
  305. $newone = input($field);
  306. $data[$field] = $newone;
  307. }
  308. if(empty($data)){
  309. $this->success();
  310. }
  311. $update_rs = Db::name('doctor_info')->where('id',$this->auth->id)->update($data);
  312. $this->success();
  313. }
  314. //假注销
  315. public function cancleUser(){
  316. /*$captcha = input('captcha','');
  317. if (!$captcha) {
  318. $this->error(__('Invalid parameters'));
  319. }
  320. if (!Sms::check($this->auth->mobile, $captcha, 'mobilelogin')) {
  321. $this->error(__('Captcha is incorrect'));
  322. }*/
  323. Db::name('doctor')->where('id',$this->auth->id)->update(['status'=>-1]);
  324. $this->auth->logout();
  325. $this->success('注销成功');
  326. }
  327. //////////////////////////////////////////////////////
  328. //员工手机+密码登录
  329. public function login()
  330. {
  331. $mobile = input('mobile');
  332. $password = input('password');
  333. if (!$mobile || !$password) {
  334. $this->error(__('Invalid parameters'));
  335. }
  336. $ret = $this->auth->login($mobile, $password);
  337. if ($ret) {
  338. $data = $this->auth->getUserinfo();
  339. $this->success(__('Logged in successful'), $data);
  340. } else {
  341. $this->error($this->auth->getError());
  342. }
  343. }
  344. /**
  345. * 修改密码
  346. *
  347. * @ApiMethod (POST)
  348. * @param string $newpassword 新密码
  349. * @param string $oldpassword 旧密码
  350. */
  351. public function changepwd(){
  352. $newpassword = input('newpassword');
  353. $oldpassword = input('oldpassword','');
  354. if (!$newpassword) {
  355. $this->error('请输入新密码');
  356. }
  357. if($this->auth->password && empty($oldpassword)){
  358. $this->error('旧密码必填');
  359. }
  360. if(empty($this->auth->password)){
  361. $ret = $this->auth->changepwd($newpassword, '', true);
  362. }else{
  363. $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
  364. }
  365. if ($ret) {
  366. $this->success(__('Reset password successful'));
  367. } else {
  368. $this->error($this->auth->getError());
  369. }
  370. }
  371. }