1
0
Files
helper/application/mobile/controller/Setting.php
2020-08-06 14:58:51 +08:00

338 lines
10 KiB
PHP
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.

<?php
// +------------------------------------------------+
// |http://www.cjango.com |
// +------------------------------------------------+
// | 修复BUG不是一朝一夕的事情等我喝醉了再说吧 |
// +------------------------------------------------+
// | Author: 小陈叔叔 <Jason.Chen> |
// +------------------------------------------------+
namespace app\mobile\controller;
use app\common\model\Member as MemberModel;
use app\common\model\Province as ProvinceModel;
use app\common\model\Storage as StorageModel;
use app\common\service\City as CitySercice;
use app\common\service\Member as MemberService;
use cjango\Wechat;
use cjango\Wechat\Oauth;
class Setting extends Center
{
public function index()
{
return $this->fetch();
}
//修改密码
public function password()
{
if (IS_POST) {
$newpass = $this->request->post('newpass');
$repass = $this->request->post('repass');
$oldpass = $this->request->post('oldpass');
$res = MemberService::changePassword(UID, $oldpass, $newpass, $repass);
if ($res === true) {
return $this->success('修改成功', 'setting/index');
} else {
return $this->error($res);
}
} else {
return $this->fetch();
}
}
//设置模版
public function template()
{
if (IS_POST) {
$tpl = $this->request->post('tpl');
if (MemberService::changeTpl(UID, $tpl)) {
return $this->success('', 'center/index');
} else {
return $this->error();
}
} else {
return $this->fetch();
}
}
//修改姓名
public function nickname()
{
if (IS_POST) {
$nickname = $this->request->post('nickname');
$res = MemberService::editInfo(UID, ['nickname' => $nickname], 'nickname');
if ($res === true) {
return $this->success('修改成功', 'setting/index');
} else {
return $this->error($res);
}
} else {
return $this->fetch();
}
}
// 修改手机
public function mobile()
{
if (IS_POST) {
$mobile = $this->request->post('mobile');
$res = MemberService::editInfo(UID, ['mobile' => $mobile], 'mobile');
if ($res === true) {
return $this->success('修改成功', 'setting/index');
} else {
return $this->error($res);
}
} else {
return $this->fetch();
}
}
// 添加微信
public function wechat()
{
if (IS_POST) {
$wechat = $this->request->post('wechat');
$res = MemberService::editInfo(UID, ['wechat' => $wechat], 'wechat');
if ($res === true) {
return $this->success('修改成功', 'setting/index');
} else {
return $this->error($res);
}
} else {
return $this->fetch();
}
}
// 修改qq
public function qq()
{
if (IS_POST) {
$qq = $this->request->post('qq');
$res = MemberService::editInfo(UID, ['qq' => $qq], 'qq');
if ($res === true) {
return $this->success('修改成功', 'setting/index');
} else {
return $this->error($res);
}
} else {
return $this->fetch();
}
}
// 修改Email
public function email()
{
if (IS_POST) {
$email = $this->request->post('email');
$res = MemberService::editInfo(UID, ['email' => $email], 'email');
if ($res === true) {
return $this->success('修改成功', 'setting/index');
} else {
return $this->error($res);
}
} else {
return $this->fetch();
}
}
// 修改二维码
public function qrcode()
{
if (IS_POST) {
$qrcode = $this->request->post('qrcode');
$res = MemberService::editInfo(UID, ['qrcode' => $qrcode], 'qrcode');
if ($res === true) {
return $this->success('修改成功', 'setting/index');
} else {
return $this->error($res);
}
} else {
return $this->fetch();
}
}
// 修改职位
public function position()
{
if (IS_POST) {
$position = $this->request->post('position');
$res = MemberService::editInfo(UID, ['position' => $position], 'position');
if ($res === true) {
return $this->success('修改成功', 'setting/index');
} else {
return $this->error($res);
}
} else {
return $this->fetch();
}
}
// 修改地区
public function city()
{
if (IS_POST) {
$province = $this->request->post('province');
$city = $this->request->post('city');
$res = MemberService::editInfo(UID, ['province' => $province, 'city' => $city], 'city');
if ($res === true) {
return $this->success('修改成功', 'setting/index');
} else {
return $this->error($res);
}
} else {
$this->info = MemberService::info(UID);
$this->province = ProvinceModel::where('status', 0)->select();
$this->city = CitySercice::getCity($this->info->province);
return $this->fetch();
}
}
// 绑定/解绑微信
public function bindWechat()
{
if (IS_POST) {
$openid = $this->request->post('openid');
$unbind = $this->request->post('unbind');
if (!empty($unbind)) {
$res = MemberService::unBindWechat(UID, $openid); //解绑
$str = '解除绑定成功';
} else {
$res = MemberService::bindWechat(UID, ['openid' => $openid]); //绑定
$str = '绑定成功';
}
if ($res === true) {
return $this->success($str, 'setting/index');
} else {
return $this->error($res);
}
} else {
$this->wechat = self::getWechat();
$userInfo = MemberModel::where('openid', $this->wechat['openid'])->find();
if ($userInfo) {
$this->assign('userInfo', $userInfo);
}
return $this->fetch();
}
}
/**
* [uploadPicture h5图片上传(后端)]
* @return [type] [description]
*/
public function uploadPicture()
{
$input = $this->request->post();
$data = $input['files']['base64'];
$name = $input['files']['name'];
$hash = hash('md5', base64_decode($data));
$info = StorageModel::where('hash', $hash)->find();
if ($info) {
$return = array('code' => 1, 'msg' => '上传成功', 'id' => $info->id, 'url' => $info->path);
if ($input['type'] == 'headimgurl') {
$res = MemberService::editInfo(UID, ['headimgurl' => $info->path], 'headimgurl');
if ($res === true) {
return $this->success($return);
} else {
return $this->error($res);
}
} else {
return $this->success($return);
}
} else {
// 获取图片
$temp = $data;
list($type, $data) = explode(',', $temp);
// 判断类型
if (strstr($type, 'image/jpeg') !== '') {
$ext = '.jpg';
} elseif (strstr($type, 'image/gif') !== '') {
$ext = '.gif';
} elseif (strstr($type, 'image/png') !== '') {
$ext = '.png';
}
// 生成的文件名
$path = "/uploads/" . $input['type'] . "/" . date('Y-m/d') . '/';
if (!$this->mkdir('.' . $path)) {
exit();
}
$url = $path . $name;
$photo = '.' . $url;
// 生成文件
file_put_contents($photo, base64_decode($data));
$data = [
'hash' => $hash,
'type' => 'image',
'name' => $name,
'ext' => $ext,
'path' => $url,
'size' => $input['files']['size'],
];
if ($insert = StorageModel::create($data)) {
$return = array('code' => 1, 'msg' => '上传成功', 'url' => $url, 'id' => $insert->id);
if ($input['type'] == 'headimgurl') {
$res = MemberService::editInfo(UID, ['headimgurl' => $url], 'headimgurl');
if ($res === true) {
return $this->success($return);
} else {
return $this->error($res);
}
} else {
return $this->success($return);
}
} else {
return $this->error('上传失败');
}
}
}
/**
* [mkdir 检测路径]
* @param [type] $savepath [description]
* @return [type] [description]
*/
public function mkdir($dir)
{
if (is_dir($dir)) {
return true;
}
if (mkdir($dir, 0777, true)) {
return true;
} else {
return false;
}
}
//获取城市数据
public function getCity($id)
{
$list = CitySercice::getData($id);
echo json_encode($list);
}
// 获取微信数据
public function getWechat()
{
parent::initWechat();
$token = Oauth::token();
if (!$token) {
$url = Oauth::url(url('setting/bindWechat'), 'STATE', 'snsapi_userinfo');
$this->redirect($url);
}
$wechat = Oauth::info($token['access_token'], $token['openid']);
if ($wechat) {
return $wechat;
} else {
$this->redirect(url('setting/bindWechat'));
}
}
}