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. 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 state == False:
  36. code = self.input('code')
  37. self.out('手机号或密码错误,登录失败' + code)
  38. else:
  39. #self.set_secure_cookie('user', str(user['id']))
  40. #self.redirect('/')
  41. self.out('yes', {'uid':user['id']})
  42. return
  43. else:
  44. self.out('手机号或密码错误,登录失败')
  45. return
  46. @Web.setting
  47. def bind(self, uid):
  48. code = self.input('code')
  49. if not code:
  50. self.out('no')
  51. return
  52. url = Demeter.config['weixinApp']['login']
  53. url = url + '?appid=' + Demeter.config['weixinApp']['appid']
  54. url = url + '&secret=' + Demeter.config['weixinApp']['secret']
  55. url = url + '&js_code=' + code
  56. url = url + '&grant_type=authorization_code'
  57. data = Demeter.curl(url)
  58. data = json.loads(data.body)
  59. if 'errcode' in data:
  60. return False
  61. update = {}
  62. update['openid'] = data['openid']
  63. update['session_key'] = data['session_key']
  64. self.service('common').update('farm_user', uid, update)
  65. return True