def mWxPushMsg_dzd(pk): L = getDzdData(pk) sToken = read_access_token_common('access_token_gy_qy') if sToken == '': conn = httplib.HTTPSConnection('qyapi.weixin.qq.com') url = "/cgi-bin/gettoken?corpid=%s&corpsecret=%s" % (m_sCorpID, m_sCorpSecret_gy) conn.request('GET', '%s' % url) res = conn.getresponse() body = res.read() ddata = json.loads(body) sToken = ddata['access_token'] conn.close() write_access_token_common(body, 'access_token_gy_qy') toUser = L[0].replace(",", "|") toUser += '|lishijie' sUrl = '%s/complaint/login/login_qy?fid=mtCheckDetail&func_id=1000004&seq=%s' % ( host_url, pk) stitle = """材料款对账""" surl = my_urlencode(sUrl) description = """有供应商发起了对账申请,请您注意查看\r\n项目名称:%s\r\n供应商名称:%s\r\n对账单号:%s\r\n发起时间:%s""" % ( L[1], L[2], L[3], L[4]) stitle = json.dumps(stitle) description = json.dumps(description) url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_base&state=wx#wechat_redirect" % ( m_sCorpID, surl) sMsg = """{ "touser": "******", """ % (toUser) sMsg += """ "msgtype": "news", "agentid": "%s", "news": { "articles":[ { "title": %s, "url": "%s", "description":%s } ] } } """ % (m_sAgentId_gy, stitle, sUrl, description) # print sMsg conn = httplib.HTTPSConnection('qyapi.weixin.qq.com') url = "/cgi-bin/message/send?access_token=%s" % (sToken) #print url conn.request('POST', '%s' % url, sMsg) res = conn.getresponse() body = res.read() print body conn.close() ddata = json.loads(body) errcode = ddata['errcode'] return
def mWxPushMsg_cgdd(pk, users, sn, proj_name): conn = httplib.HTTPSConnection('api.weixin.qq.com') sToken = read_access_token_common('access_token_gy') if sToken == '': url = "/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s" % ( AppId_gy, AppSecret_gy) conn.request('GET', '%s' % url) res = conn.getresponse() body = res.read() ddata = json.loads(body) sToken = ddata['access_token'] conn.close() write_access_token_common(body, 'access_token_gy') template_id = 'R7redICAoEadYchqkuY8vZoUrbxSCzCeB5iIJ-1OkmQ' stitle = """您有一个新的订单!""" stitle = json.dumps(stitle) keyword1 = json.dumps("采购订单:%s" % sn) keyword2 = json.dumps("【%s】材料采购" % (proj_name)) remark = json.dumps("请您登陆【供应商服务平台】进行材料确认表拟定") for e in users.split(','): sMsg = """{ "touser":"******", "template_id":"%s", "topcolor":"#FF0000", "data":{ "first": { "value":%s, "color":"#ff0000" }, "keyword1": { "value":%s, "color":"#173177" }, "keyword2": { "value":%s, "color":"#173177" }, "remark": { "value":%s, "color":"#173177" } } } """ % (e, template_id, stitle, keyword1, keyword2, remark) conn = httplib.HTTPSConnection('api.weixin.qq.com') url = "/cgi-bin/message/template/send?access_token=%s" % (sToken) #print url conn.request('POST', '%s' % url, sMsg) res = conn.getresponse() body = res.read() print body conn.close() ddata = json.loads(body) errcode = ddata['errcode'] errmsg = ddata['errmsg'] return
def login_wx_func(request): import base64, time import random random_no = '%s' % (random.randint(0, 999999)) usr_id, usr_name, dept_id, dept_name = '', '', '', '' source = 'wx' if request.META.has_key('HTTP_X_FORWARDED_FOR'): ip = request.META['HTTP_X_FORWARDED_FOR'] else: ip = request.META['REMOTE_ADDR'] code = request.GET.get('code', '') union_id = '' if code != '': conn = httplib.HTTPSConnection('api.weixin.qq.com') sToken = read_access_token_common('access_token_web') if sToken == '': url = "/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s" % ( AppId_web, AppSecret_web) conn.request('GET', '%s' % url) res = conn.getresponse() body = res.read() ddata = json.loads(body) sToken = ddata['access_token'] conn.close() write_access_token_common(body, 'access_token_web') url = "/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code" % ( AppId_web, AppSecret_web, code) conn.request('GET', '%s' % url) res = conn.getresponse() body = res.read() print body ddata = json.loads(body) access_token = ddata['access_token'] openid = ddata['openid'] union_id = ddata.get('unionid', '') if union_id == '': errCode = 1 msg = u'用户未注册供应商服务平台' s = """ { "errcode": %s, "errmsg": "%s", } """ % (errCode, msg) response = HttpResponseCORS(request, s) return response s1 = '' if union_id == 'or0EJv-sW7K_rmSakUfKH1ONE5hg': union_id = 'or0EJvw-Y-E7k7zPTdR6vX0OdRlI' sql = """SELECT U.usr_id,U.usr_name,ifnull(ab.sup_id,0),ifnull(su.cname,''),IFNULL(U.headimgurl,'') FROM users_gy U LEFT JOIN addr_book ab on ab.id = U.addr_id LEFT JOIN suppliers su on su.id = ab.sup_id WHERE U.unionid='%s' AND U.status=1 """ % (union_id) print sql lT, iN = db.select(sql) if iN > 0: usr_id = lT[0][0] request.session['usr_id'] = usr_id request.session['usr_name'] = lT[0][1] request.session['sup_id'] = lT[0][2] request.session['sup_name'] = lT[0][3] d_value = ['', '', '', '', ''] d_value[0] = usr_id d_value[1] = lT[0][1] d_value[2] = lT[0][2] d_value[3] = lT[0][3] d_value[4] = 0 g_data.set_value(d_value) errCode = 0 msg = 'OK' pic = lT[0][4] sTimeStamp = str(time.time()) wxcpt = WXBizMsgCrypt('szoworld_gy', m_aesKey) ret, token = wxcpt.EncryptMsg(str(usr_id), random_no, sTimeStamp) sql = """SELECT distinct WMF.menu,WMF.menu_id,WMF.menu_name, WMF.sort,WMF.parent_id,WMF.status-1,WMF.url,WMF.icon FROM menu_func WMF Left JOIN menu_func WMF1 on WMF.parent_id = WMF1.menu_id WHERE WMF.status=2 and WMF.menu_id>0 and WMF1.status=2 ORDER BY WMF.parent_id,WMF.menu,WMF.sort,WMF.menu_id """ #print sql rows, iN = db.select(sql) L1 = [2] L2 = [] #L = formatData(rows,L1,L2) names = 'level menu_id menu_name sort parent_id status url icon'.split( ) data = [dict(zip(names, d)) for d in rows] s3 = json.dumps(data, ensure_ascii=False) s1 = """"userid":%s, "username":"******", "sup_id":%s, "sup_name":"%s", "pic_url":"%s", "AccessToken":"%s", "menu_data":%s""" % (lT[0][0], (lT[0][1]), lT[0][2], (lT[0][3]), pic, token, s3) sql = """insert into users_login_gy (usr_id,source,token,login_ip,login_time,refresh_time,expire_time) values (%s,'%s','%s','%s',now(),now(),%s) """ % (lT[0][0], source, token, ip, int(TIME_OUT) * 60) #print ToGBK(sql) db.executesql(sql) else: errCode = 1 msg = u'用户未注册供应商服务平台' s = """ { "errcode": %s, "errmsg": "%s", %s } """ % (errCode, msg, s1) #print ToGBK(s) response = HttpResponseCORS(request, s) return response
def mWxPushMsg_tracking_fw(pk, prj_name, usr_id_tj, usr_name, status): now = getToday() toUser = get_toUser(usr_id_tj) conn = httplib.HTTPSConnection('api.weixin.qq.com') sToken = read_access_token_common('access_token_tj') if sToken == '': url = "/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s" % ( AppId_tj, AppSecret_tj) conn.request('GET', '%s' % url) res = conn.getresponse() body = res.read() ddata = json.loads(body) sToken = ddata['access_token'] conn.close() write_access_token_common(body, 'access_token_tj') file_id = pk sUrl = '%s/complaint/login/default_tj?fid=projDetail&seq=%s&must_reply=true' % ( host_url, file_id) stitle = """新消息""" description = '' if status == 1: description = """尊敬的%s,您在深圳宝鹰建设集团推荐的"%s"需要反馈跟进信息。""" % (usr_name, prj_name) if status == 2: description = """尊敬的%s,您在深圳宝鹰建设集团推荐的"%s"已半年未反馈信息,请及时反馈跟进信息,否则该项目推荐将会立即失效。""" % ( usr_name, prj_name) surl = my_urlencode(sUrl) # print toUser stitle = json.dumps(stitle) description = json.dumps(description) now = json.dumps(now) city = json.dumps("深圳") url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_base&state=wx#wechat_redirect" % ( AppId_tj, surl) sMsg = """{ "touser":"******", "template_id":"%s", "url":"%s", "topcolor":"#FF0000", "data":{ "first": { "value":%s, "color":"#173177" }, "keyword1": { "value":%s, "color":"#173177" }, "keyword2": { "value":%s, "color":"#173177" } } } """ % (toUser, template_id_msg_tj, sUrl, description, city, now) # print sMsg conn = httplib.HTTPSConnection('api.weixin.qq.com') url = "/cgi-bin/message/template/send?access_token=%s" % (sToken) #print url conn.request('POST', '%s' % url, sMsg) res = conn.getresponse() body = res.read() conn.close() ddata = json.loads(body) errcode = ddata['errcode'] return errcode