User.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. * 重置密码
  199. *
  200. * @ApiMethod (POST)
  201. * @param string $mobile 手机号
  202. * @param string $captcha 验证码
  203. * @param string $newpassword 新密码
  204. */
  205. /*public function resetpwd()
  206. {
  207. $mobile = $this->request->post('mobile');
  208. $captcha = $this->request->post('captcha');
  209. $newpassword = $this->request->post("newpassword");
  210. if (!$mobile || !$captcha || !$newpassword) {
  211. $this->error(__('Invalid parameters'));
  212. }
  213. //验证Token
  214. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  215. $this->error(__('Password must be 6 to 30 characters'));
  216. }
  217. if (!Validate::regex($mobile, "^1\d{10}$")) {
  218. $this->error(__('Mobile is incorrect'));
  219. }
  220. $user = \app\common\model\CompanyStaff::getByMobile($mobile);
  221. if (!$user) {
  222. $this->error(__('User not found'));
  223. }
  224. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  225. if (!$ret) {
  226. $this->error(__('Captcha is incorrect'));
  227. }
  228. Sms::flush($mobile, 'resetpwd');
  229. //模拟一次登录
  230. $this->auth->direct($user->id);
  231. $ret = $this->auth->resetpwd($newpassword, '', true);
  232. if ($ret) {
  233. $this->success(__('Reset password successful'));
  234. } else {
  235. $this->error($this->auth->getError());
  236. }
  237. }*/
  238. /**
  239. * 修改会员个人信息
  240. *
  241. * @ApiMethod (POST)
  242. * @param string $avatar 头像地址
  243. * @param string $username 用户名
  244. * @param string $nickname 昵称
  245. * @param string $bio 个人简介
  246. */
  247. public function profile()
  248. {
  249. $field_array = [
  250. 'realname',
  251. 'idcard',
  252. 'english_status',
  253. 'idcard_z_image',
  254. 'idcard_f_image',
  255. 'doctor_image',
  256. 'avatar','nickname','gender',
  257. 'keshi_id','hospital','goodat','level_id','info','job_status'
  258. ];
  259. $data = [];
  260. foreach($field_array as $key => $field){
  261. //前端传不了post,改了
  262. /*if(!request()->has($field,'post')){
  263. continue;
  264. }*/
  265. if(!input('?'.$field)){
  266. continue;
  267. }
  268. $newone = input($field);
  269. if($field == 'avatar'){
  270. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  271. }
  272. $data[$field] = $newone;
  273. }
  274. //
  275. /*if(isset($data['birthday'])){
  276. $data['birthday'] = strtotime($data['birthday']);
  277. }*/
  278. if(empty($data)){
  279. $this->success();
  280. }
  281. if(isset($data['realname']) && isset($data['idcard']) && isset($data['idcard_z_image']) && isset($data['idcard_f_image'])){
  282. $data['idcard_status'] = 0;
  283. }
  284. if(isset($data['doctor_image'])){
  285. $data['doctor_status'] = 0;
  286. }
  287. $update_rs = Db::name('doctor')->where('id',$this->auth->id)->update($data);
  288. $this->success();
  289. }
  290. //问诊设置
  291. public function profile_wenzhen()
  292. {
  293. $field_array = [
  294. 'typing_switch',
  295. 'video_switch',
  296. 'video_model',
  297. 'typing_price',
  298. 'video_price',
  299. 'notice_switch',
  300. 'jolt_switch',
  301. ];
  302. $data = [];
  303. foreach($field_array as $key => $field){
  304. if(!input('?'.$field)){
  305. continue;
  306. }
  307. $newone = input($field);
  308. $data[$field] = $newone;
  309. }
  310. if(empty($data)){
  311. $this->success();
  312. }
  313. $update_rs = Db::name('doctor_info')->where('doctor_id',$this->auth->id)->update($data);
  314. $this->success();
  315. }
  316. //假注销
  317. public function cancleUser(){
  318. /*$captcha = input('captcha','');
  319. if (!$captcha) {
  320. $this->error(__('Invalid parameters'));
  321. }
  322. if (!Sms::check($this->auth->mobile, $captcha, 'mobilelogin')) {
  323. $this->error(__('Captcha is incorrect'));
  324. }*/
  325. Db::name('doctor')->where('id',$this->auth->id)->update(['status'=>-1]);
  326. $this->auth->logout();
  327. $this->success('注销成功');
  328. }
  329. //////////////////////////////////////////////////////
  330. //员工手机+密码登录
  331. public function login()
  332. {
  333. $mobile = input('mobile');
  334. $password = input('password');
  335. if (!$mobile || !$password) {
  336. $this->error(__('Invalid parameters'));
  337. }
  338. $ret = $this->auth->login($mobile, $password);
  339. if ($ret) {
  340. $data = $this->auth->getUserinfo();
  341. $this->success(__('Logged in successful'), $data);
  342. } else {
  343. $this->error($this->auth->getError());
  344. }
  345. }
  346. /**
  347. * 修改密码
  348. *
  349. * @ApiMethod (POST)
  350. * @param string $newpassword 新密码
  351. * @param string $oldpassword 旧密码
  352. */
  353. public function changepwd(){
  354. $newpassword = input('newpassword');
  355. $oldpassword = input('oldpassword','');
  356. if (!$newpassword) {
  357. $this->error('请输入新密码');
  358. }
  359. if($this->auth->password && empty($oldpassword)){
  360. $this->error('旧密码必填');
  361. }
  362. if(empty($this->auth->password)){
  363. $ret = $this->auth->changepwd($newpassword, '', true);
  364. }else{
  365. $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
  366. }
  367. if ($ret) {
  368. $this->success(__('Reset password successful'));
  369. } else {
  370. $this->error($this->auth->getError());
  371. }
  372. }
  373. }