采用框架THINKPHP5
需要客户端传的参数有 udid openid nickname avatar_path
/* * @param 第三方微信登录 * @param openid udid nickname avatar_path */ public function wechat_login(MemberModel $member){ $data = input('post.'); if(empty($data['openid'])){ return_error("缺少必填参数"); } $list = $member->vf_login($data); if(empty($list)){ return_msg(100,"登录失败"); }else{ return_msg(200,'登录成功',$list); } }
/** * 三方登录 */ public function vf_login($data){ $info = self::where(['openid'=>$data['openid']])->field('id,lasttime,birthday,money,sex,jifen,flag,face,nickname,icon')->find(); $data['token'] = generate_token($data['openid']); $data['lasttime'] = time(); $path = $data["avatar_path"]; $data["icon"] = $path; unset($data["avatar_path"]); if(empty($info)){ //如果为空 则直接写入 $info['id'] = self::allowField(true)->insertGetId($data); $list['nickname'] = $data['nickname']; $list['avatar_path'] = $data['icon']; }else{ $list['nickname'] = $info['nickname']; $list['avatar_path'] = !empty($info['icon'])?$info['icon']:"public/images/moren/head.png"; self::allowField(true)->where(['id'=>$info['id']])->update($data); } $list['uid'] = $info['id']; $list['token'] = $data['token']; return $list; }
生产token方法代码
/** * 生成TOKEN * @param string $str 要进行加密的字符串 * @return string $str 加密后的字符串 * @author zhu * */ function generate_token($phone){ $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'.time().$phone; return md5($chars); }