login.py 1.9 KB

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