login.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. demeter web
  5. name:login.py 登录相关接口
  6. author:rabin
  7. """
  8. from __load__ import *
  9. class login_path(Load):
  10. """
  11. @api {get} /login 登录绑定
  12. @apiVersion 1.0.0
  13. @apiName Login
  14. @apiGroup Common
  15. @apiParam {String} code 微信小程序的code
  16. @apiParam {String} mobile 手机号
  17. @apiParam {String} password 密码
  18. @apiSuccess {String} uid 用户id(加密之后的)
  19. @apiErrorExample 操作成功
  20. {"status": 1, "msg": "yes", "code": 0, "data": {"uid": "111"}]}}
  21. @apiErrorExample 操作失败
  22. {"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}}
  23. """
  24. @Web.setting
  25. def get(self):
  26. mobile = self.input('mobile')
  27. password = self.input('password')
  28. if mobile and password:
  29. user = self.service('common').one('farm_user', mobile=mobile)
  30. if user:
  31. temp = user['password'].split('_')
  32. if Demeter.md5(password, temp[1]) == user['password']:
  33. state = self.bind(user['id'])
  34. if state == False:
  35. self.out('手机号或密码错误,登录失败')
  36. else:
  37. #self.set_secure_cookie('user', str(user['id']))
  38. #self.redirect('/')
  39. self.out('yes', {'uid':user['id']})
  40. return
  41. else:
  42. self.out('手机号或密码错误,登录失败')
  43. return
  44. def bind(self, uid):
  45. code = self.input('code')
  46. if not code:
  47. self.out('no')
  48. return
  49. url = Demeter.config['weixinApp']['login']
  50. url = url + '?appid=' + Demeter.config['weixinApp']['appid']
  51. url = url + '&secret=' + Demeter.config['weixinApp']['secret']
  52. url = url + '&js_code=' + code
  53. url = url + '&grant_type=authorization_code'
  54. data = Demeter.curl(url)
  55. data = json.loads(data)
  56. if 'errcode' in data:
  57. return False
  58. update = {}
  59. update['openid'] = data['openid']
  60. update['session_key'] = data['session_key']
  61. self.service('common').update('farm_user', uid, update)
  62. wechat = self.service('common').one('farm_user_wechat', uid=uid, openid=update['openid'])
  63. if not wechat:
  64. update['uid'] = uid
  65. self.service('common').update('farm_user_wechat', False, update)
  66. return True