login.py 1.9 KB

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