login.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. nickname = self.input('nickname')
  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. if not nickname:
  35. nickname = user['username']
  36. state = self.bind(user['id'], nickname)
  37. if state == False:
  38. self.out('手机号或密码错误,登录失败')
  39. elif state and state['status'] == False:
  40. self.out('您的微信账号已经被封禁,请联系管理员')
  41. else:
  42. #self.set_secure_cookie('user', str(user['id']))
  43. #self.redirect('/')
  44. if state['status']:
  45. state['status'] = 1
  46. else:
  47. state['status'] = 2
  48. self.out('yes', {'uid':user['id'], 'wid':state['id'], 'status': state['status']})
  49. return
  50. else:
  51. self.out('手机号或密码错误,登录失败')
  52. return
  53. def bind(self, uid, nickname):
  54. code = self.input('code')
  55. if not code:
  56. self.out('no')
  57. return
  58. url = Demeter.config['weixinApp']['login']
  59. url = url + '?appid=' + Demeter.config['weixinApp']['appid']
  60. url = url + '&secret=' + Demeter.config['weixinApp']['secret']
  61. url = url + '&js_code=' + code
  62. url = url + '&grant_type=authorization_code'
  63. data = Demeter.curl(url)
  64. data = json.loads(data)
  65. if 'errcode' in data:
  66. return False
  67. update = {}
  68. update['openid'] = data['openid']
  69. update['session_key'] = data['session_key']
  70. self.service('common').update('farm_user', uid, update)
  71. wechat = self.service('common').one('farm_user_wechat', uid=uid, openid=update['openid'])
  72. if not wechat:
  73. update = {}
  74. update['nickname'] = nickname
  75. update['openid'] = data['openid']
  76. update['session_key'] = data['session_key']
  77. update['uid'] = uid
  78. self.service('common').update('farm_user_wechat', False, update)
  79. wechat = self.service('common').one('farm_user_wechat', uid=uid, openid=data['openid'])
  80. return wechat
  81. class check_path(Load):
  82. @Web.setting
  83. def get(self):
  84. wid = self.input('wid')
  85. uid = self.input('uid')
  86. if wid and uid:
  87. wechat = self.service('common').one('farm_user_wechat', wid=wid)
  88. if wechat:
  89. if wechat['status']:
  90. wechat['status'] = 1
  91. else:
  92. wechat['status'] = 2
  93. self.out('yes', {'uid':uid, 'wid':wid, 'status': wechat['status']})
  94. else:
  95. self.out('yes', {'uid':uid, 'wid':wid, 'status': 3})