login.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 not state:
  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. @Web.setting
  45. def bind(self, uid):
  46. code = self.input('code')
  47. if not code:
  48. self.out('no')
  49. return
  50. url = Demeter.config['weixinApp']['login']
  51. url = url + '?appid=' + Demeter.config['weixinApp']['appid']
  52. url = url + '&secret=' + Demeter.config['weixinApp']['secret']
  53. url = url + '&js_code=' + code
  54. url = url + '&grant_type=authorization_code'
  55. data = Demeter.curl(url)
  56. data = json.loads(data)
  57. print data
  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