Esempio n. 1
0
 def post(self):
     self.set_header('Content-Type', 'application/json; charset=UTF-8')
     sp_id = self.current_user.supplier_id
     cate_name = self.get_argument('name', '').encode('utf-8')
     # 检查是否重复
     exist = self.db.query('select * from member_category where sp_id=%s and name=%s and deleted=0', sp_id, cate_name)
     # 重复组名,返回
     if exist:
         self.write(json_dumps({'ok': False, 'error': '分组名称已经存在'}))
         return
     # 更新微信分组
     if self.current_user.sp_props.wx_type == 'service' and self.current_user.sp_props.wx_verified == '1':
         body = json_dumps({"group": {"name": cate_name}})
         wx = Weixin(db=self.db, sp_id=sp_id, method='groups/create', body=body)
         wx.set_app_info(self.current_user.sp_props.app_id, self.current_user.sp_props.app_secret)
         res = yield wx.fetch()
         wx.parse_response(res.body)
         if wx.is_ok():
             # 更新本地数据库
             last_id = self.db.execute('insert into member_category (sp_id, name, deleted, wx_grp_id) '
                                       'values (%s, %s, 0, %s)', sp_id, cate_name, wx.message.group.id)
             self.write(json_dumps({'ok': True, 'cate_id': last_id, 'cate_name': cate_name}))
         else:
             logging.error('wx error: %s' % wx.error)
             self.write(json_dumps({'ok': False, 'error': '添加失败,%s' % wx.error}))
     else:
         # 更新本地数据库
         last_id = self.db.execute('insert into member_category (sp_id, name, deleted) '
                                   'values (%s, %s, 0)', sp_id, cate_name)
         self.write(json_dumps({'ok': True, 'cate_id': last_id, 'cate_name': cate_name}))
Esempio n. 2
0
 def post(self):
     sp_id = self.current_user.supplier_id
     cate_id = self.get_argument('cate_id', '')
     # 查出默认的分组ID
     default_cate_id = self.db.get('select id from member_category where sp_id=%s and name="未分组"', sp_id).id
     # 微信服务器分组放到0
     if self.current_user.sp_props.wx_type == 'service' and self.current_user.sp_props.wx_verified == '1':
         members = self.db.query('select wx_id from member where sp_id=%s and category_id=%s', sp_id, cate_id)
         app_id = self.current_user.sp_props.app_id
         app_secret = self.current_user.sp_props.app_secret
         for mem in members:
             body = {"openid": mem.wx_id, "to_groupid": 0}
             wx = Weixin(db=self.db, sp_id=sp_id, method='groups/members/update', body=json_dumps(body))
             wx.set_app_info(app_id, app_secret)
             res = yield wx.fetch()
             wx.parse_response(res.body)
             if not wx.is_ok():
                 logging.error('wx error: %s' % wx.error)
     # 把要删除组的会员合并入默认分组
     self.db.execute('update member set category_id=%s where sp_id=%s and category_id=%s',
                     default_cate_id, sp_id, cate_id)
     # 删除分组
     self.db.execute('update member_category set deleted=1 where id=%s', cate_id)
     # 返回
     self.set_header('Content-Type', 'application/json; charset=UTF-8')
     self.write(json_dumps({'ok': True}))
Esempio n. 3
0
 def post(self):
     sp_id = self.current_user.supplier_id
     cate_id = self.get_argument('cate_id', '')  # 新cate id
     mem_id = self.get_argument('mem_id', '')
     try:
         map(int, mem_id.split(','))
     except:
         raise HTTPError(500)
     # 更新微信服务器
     if self.current_user.sp_props.wx_type == 'service' and self.current_user.sp_props.wx_verified == '1':
         members = self.db.query('select wx_id from member where sp_id=%s and id in (' + mem_id + ')', sp_id)
         wx_cate_id = self.db.get('select wx_grp_id from member_category where id=%s', cate_id).wx_grp_id
         app_id = self.current_user.sp_props.app_id
         app_secret = self.current_user.sp_props.app_secret
         for mem in members:
             body = {"openid": mem.wx_id, "to_groupid": int(wx_cate_id)}
             wx = Weixin(db=self.db, sp_id=sp_id, method='groups/members/update', body=json_dumps(body))
             wx.set_app_info(app_id, app_secret)
             res = yield wx.fetch()
             wx.parse_response(res.body)
             if not wx.is_ok():
                 logging.error('wx error: %s' % wx.error)
     # 更新本地信息
     if mem_id and cate_id:
         sql = 'update member set category_id=%s where sp_id=%s and id in (' + mem_id + ')'
         self.db.execute(sql, cate_id, sp_id)