hotime/example/app/user.go

390 lines
10 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package app
import (
. "../../../hotime"
. "../../../hotime/common"
"../../../hotime/dri/download"
"../../../hotime/dri/tencent"
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"strings"
"time"
//"strings"
)
var User = Ctr{
"token": func(this *Context) {
this.Display(0, this.SessionId)
},
"test": func(this *Context) {
fmt.Println(this.SessionId)
//this.Session("user_id", 1)
wechat := this.Db.Get("wechat", "*")
this.Session("wechatInfo", wechat)
this.Display(0, "开始测试")
},
//获取个人信息
"info": func(this *Context) {
//this.Session("user_id", 1)
//wechat:=this.Db.Get("wechat","*")
//this.Session("wechatInfo",wechat)
if this.Session("user_id").Data == nil {
if this.Session("wechatInfo").Data != nil {
this.Display(6, "需要认证")
return
}
this.Display(2, "还没有登录")
return
}
user := this.Db.Get("user", "*", Map{"id": this.Session("user_id").Data})
if user == nil {
fmt.Println(this.Session("user_id").Data, ":fsdfsd")
this.Display(4, "找不到该用户")
return
}
org := this.Db.Get("org", "contact", Map{"id": user.GetCeilInt("org_id")})
user["org"] = org
company := this.Db.Get("company", "id,name,status", Map{"id": user.GetCeilInt("company_id")})
user["company"] = company
t := time.Now().Unix()
questions := this.Db.Select("question", "*", Map{"AND": Map{"org_id": user.GetCeilInt("org_id"), "state": 0}})
re := []Map{}
for _, v := range questions {
questionCompany := this.Db.Get("question_company", "*", Map{"AND": Map{"question_id": v.GetCeilInt("id"), "company_id": user.GetCeilInt("company_id")}})
if questionCompany == nil {
questionCompany = Map{
"name": v.GetString("name"),
"question_data": v.GetString("question_data"),
"org_id": v.GetString("org_id"),
"question_id": v.GetString("id"),
"company_id": user.GetCeilInt("company_id"),
"user_id": user.GetString("id"),
"create_time": t,
"modify_time": t,
"status": 0,
"state": 0,
}
questionCompany["id"] = this.Db.Insert("question_company", questionCompany)
if questionCompany.GetCeilInt("id") == 0 {
this.Display(4, "无法创建调查数据")
return
}
}
delete(v, "question_data")
questionCompany["question"] = v
questionCompany["question_data"] = questionCompany.GetSlice("question_data")
re = append(re, questionCompany)
}
user["questions"] = re
this.Display(0, user)
},
//身份认证
"auth": func(this *Context) {
wechat := this.Session("wechatInfo").ToMap()
if wechat == nil {
if this.Session("user_id").Data == nil {
this.Display(6, "还没有登录")
return
}
wechat := this.Db.Get("wechat", "*", Map{"user_id": this.Session("user_id").ToCeilInt()})
if wechat == nil {
this.Display(6, "没有微信登录")
return
}
}
if this.Session("code").Data == nil {
this.Display(8, "验证码没有获取")
return
}
//orgId := ObjToInt(this.Req.FormValue("org_id"))
////questionCompanyId:=ObjToInt(this.Req.FormValue("id"))
//if orgId == 0 {
// this.Display(3, "没有选择组织")
// return
//}
name := this.Req.FormValue("name")
phone := this.Req.FormValue("phone")
code := this.Req.FormValue("code")
authImg := this.Req.FormValue("auth_img")
categoryId := ObjToInt(this.Req.FormValue("category_id"))
companyImg := this.Req.FormValue("company_img")
companyName := this.Req.FormValue("company_name")
companySn := this.Req.FormValue("company_sn")
if code != this.Session("code").ToStr() {
this.Display(8, "验证码错误")
return
}
if name == "" || phone == "" || authImg == "" || categoryId == 0 || companyImg == "" || companyName == "" || companySn == "" {
this.Display(3, "参数异常")
return
}
t := time.Now().Unix()
orgId := wechat.GetInt("org_id")
company := this.Db.Get("company", "id", Map{"sn": companySn})
if company == nil {
company = Map{
"name": companyName,
"sn": companySn,
"create_time": t,
"category_id": categoryId,
//"org_id": orgId,
"modify_time": t,
"status": 1,
"state": 0,
"img": companyImg,
}
if wechat.GetInt("status") == 0 && orgId != 0 {
company["org_id"] = orgId
}
company["id"] = this.Db.Insert("company", company)
if company.GetCeilInt("id") == 0 {
this.Display(4, "无法创建企业")
return
}
} else {
company["status"] = 1
company["modify_time"] = t
company["img"] = companyImg
company["name"] = companyName
company["img"] = companyImg
if wechat.GetInt("status") == 0 && orgId != 0 {
company["org_id"] = orgId
}
re := this.Db.Update("company", company, Map{"id": company.GetCeilInt("id")})
if re == 0 {
this.Display(4, "无法更新企业")
return
}
}
user := this.Db.Get("user", "*", Map{"phone": phone})
data := Map{"name": name,
"phone": phone,
//"org_id": orgId,
//"idcard": idcard,
//"idcard_front_img": idcardFrontImg,
"company_id": company.GetCeilInt("id"),
//"idcard_back_img": idcardBackImg,
"auth_img": authImg,
"modify_time": t,
"status": 1,
"company_img": companyImg}
if wechat.GetInt("status") == 0 && orgId != 0 {
data["org_id"] = orgId
}
//用户已经注册则更新
if user != nil {
re := this.Db.Update("user", data, Map{"id": user.GetCeilInt("id")})
if re == 0 {
this.Display(4, "无法更新用户")
return
}
} else {
data["create_time"] = t
if wechat != nil {
data["nickname"] = wechat.GetString("nickname")
path := time.Now().Format(this.Config.GetString("imgPath"))
filename := Md5(ObjToStr(t)) + ".jpg"
down := download.Down(wechat.GetString("avatar"), this.Config.GetString("tpt")+"/"+path, filename)
if down {
data["avatar"] = path + filename
}
}
user = data
user["id"] = this.Db.Insert("user", data)
if user.GetCeilInt("id") == 0 {
this.Display(4, "无法创建用户")
return
}
}
this.Db.Update("wechat", Map{"status": 1, "user_id": user.GetCeilInt("id")}, Map{"id": wechat.GetInt("id")})
this.Session("user_id", user.GetCeilInt("id"))
this.Display(0, this.SessionId)
},
//修改密码g
"forget": func(this *Context) {
if this.Session("code").Data == nil {
this.Display(8, "验证码没有获取")
return
}
if this.Req.FormValue("code") == "" || this.Req.FormValue("umobile") == "" {
this.Display(3, "验证码或者手机号没有输入")
return
}
if this.Req.FormValue("umobile") != this.Session("umobile").ToStr() ||
this.Req.FormValue("code") != this.Session("code").ToStr() {
this.Display(14, "验证码验证失败")
return
}
if len(this.Req.FormValue("upwd")) < 6 {
this.Display(3, "密码没有输入")
return
}
res := this.Db.Update("user", Map{"upwd": Md5(this.Req.FormValue("upwd"))}, Map{"umobile": this.Req.FormValue("umobile")})
if res == 0 {
this.Display(5, "找不到该用户")
return
}
this.Display(0, "成功")
},
"upload": func(this *Context) {
file := this.Req.FormValue("file")
if len(file) < 100 {
this.Display(3, "图片上传错误")
return
}
//fmt.Println(uimg)
btes, e := base64.StdEncoding.DecodeString(file[strings.Index(file, ",")+1:]) //成图片文件并把文件写入到buffer
//btes, e := base64.StdEncoding.DecodeString(file) //成图片文件并把文件写入到buffer
if e != nil {
this.Display(3, "无法解析图片")
return
}
//uimgPath:=time.Now().Format(this.Config.GetString("uimgPath"))
path := time.Now().Format(this.Config.GetString("wxFilePath"))
os.MkdirAll(this.Config.GetString("tpt")+"/"+path, os.ModeDir)
filePath := path + Md5(ObjToStr(time.Now().Unix())) + ".jpg"
err2 := ioutil.WriteFile(this.Config.GetString("tpt")+"/"+filePath, btes, 0666) //buffer输出到jpg文件中不做处理直接写到文件
if err2 != nil {
this.Display(3, "图片保存失败")
return
}
tp := this.Req.FormValue("type")
if tp == "company" {
data := tencent.OCRCOMPANY(file)
c := ObjToMap(data)
if c != nil {
c = c.GetMap("Response")
c["url"] = filePath
} else {
c = Map{"url": filePath}
}
this.Display(0, c)
return
}
if tp == "common" {
data := tencent.OCR(file)
c := ObjToMap(data)
if c != nil {
c = c.GetMap("Response")
c["url"] = filePath
} else {
c = Map{"url": filePath}
}
this.Display(0, c)
return
}
this.Display(0, filePath)
},
//"upload": func(this *Context) {
//
// //读取网络文件
// fi, fheader, err := this.Req.FormFile("file")
// if err != nil {
// this.Display(3, err)
// return
//
// }
// filePath := this.Config.GetString("wxFilePath")
// if filePath == "" {
// filePath = "file/2006/01/02/"
// }
//
// path := time.Now().Format(filePath)
// e := os.MkdirAll(this.Config.GetString("tpt")+"/"+path, os.ModeDir)
//
// if e != nil {
// this.Display(3, e)
// return
// }
//
// filePath = path + Md5(ObjToStr(RandX(100000, 9999999))) + fheader.Filename[strings.LastIndex(fheader.Filename, "."):]
// newFile, e := os.Create(this.Config.GetString("tpt") + "/" + filePath)
//
// if e != nil {
// this.Display(3, e)
// return
// }
//
// _, e = io.Copy(newFile, fi)
//
// if e != nil {
// this.Display(3, e)
// return
// }
//
// //this.Display(0, filePath)
//
// //读取excel
// //fmt.Println(Org[orgId],OrgId)
//
//
// this.Display(0, filePath)
//
//},
//更新个人资料
"update": func(this *Context) {
if this.Req.Form["email"] == nil ||
this.Req.Form["name"] == nil ||
//this.Req.Form["phone"] == nil ||
this.Req.Form["avatar"] == nil ||
this.Req.Form["idcard"] == nil {
this.Display(3, "参数不足")
}
//更新代码
update := Map{
"email": this.Req.FormValue("email"),
"idcard": this.Req.FormValue("idcard"),
"avatar": this.Req.FormValue("avatar"),
"name": this.Req.FormValue("name"),
}
this.Db.Update("user", update, Map{"id": this.Session("user_id").Data})
this.Display(0, "ok")
},
}