123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740 |
- <?php
- namespace app\api\controller;
- use app\api\model\Address;
- use app\api\model\Cashwechat;
- use app\api\model\Foot;
- use app\api\model\Realname;
- use app\api\model\Record;
- use app\api\model\UsersModel;
- use app\common\controller\Api;
- use app\common\library\Ems;
- use app\common\library\Sms;
- use fast\Random;
- use think\Cache;
- use think\Db;
- use think\Validate;
- /**
- * 会员接口
- */
- class User extends Api
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = '*';
- public function _initialize()
- {
- parent::_initialize();
- }
- /**
- * 用户显示
- * @ApiMethod (POST)
- * @param string $user_id 用户id
- */
- public function userInfo()
- {
- $user_id = $this->request->post('user_id');
- if (!isset($user_id)) {
- return $this->result('网络错误', [], 100);
- }
- $data = UsersModel::where('user_id', $user_id)->find();
- $data['daifahuo'] = 1;
- $data['daishouhuo'] = 1;
- $data['tuikuan'] = 1;
- $data['daifukuan'] = 1;
- if ($data) {
- return $this->result('', $data, 200);
- } else {
- return $this->result('未获取用户信息', [], 100);
- }
- }
- /**
- * 修改昵称
- * @ApiMethod (POST)
- * @param string $user_id 用户id
- * @param string $user_nickname 用户昵称
- */
- public function updNickName()
- {
- $params = $this->request->post();
- if (!isset($params['user_id']) || !isset($params['user_nickname'])) {
- return $this->result('网络错误', [], 100);
- }
- $data['user_nickname'] = $params['user_nickname'];
- $data['user_id'] = $params['user_id'];
- $selNickName = UsersModel::where($data)->find();
- if ($selNickName) {
- return $this->result('请输入新昵称', [], 100);
- }
- $userModel = new UsersModel();
- $updNickName = $userModel->allowField(true)->save($data, ['user_id' => $params['user_id']]);
- if ($updNickName) {
- return $this->result('修改成功', [], 200);
- } else {
- return $this->result('修改失败', [], 100);
- }
- }
- /**
- * 修改密码
- * @ApiMethod (POST)
- * @param string $user_id 用户id
- * @param string $user_pwd 用户密码
- * @param string $user_qrpwd 重复 密码
- */
- public function updPwd()
- {
- $params = $this->request->post();
- $rules = [
- 'user_id' => 'require|number',
- 'user_pwd' => 'require|max:18|min:6',
- 'user_qrpwd' => 'require',
- ];
- $msg = [
- 'user_id.require' => '用户id不能为空',
- 'user_id.number' => '用户i为整形',
- 'user_pwd.require' => '密码不能为空',
- 'user_pwd.max' => '密码最大18位',
- 'user_pwd.min' => '密码最小6位',
- 'user_qrpwd.require' => '重复密码不能为空',
- ];
- $validata = $this->validate($params, $rules, $msg);
- if (is_string($validata)) {
- return $this->result($validata, [], 100);
- }
- if ($params['user_pwd'] != $params['user_qrpwd']) {
- return $this->result('两次密码输入不一致', [], 100);
- }
- $data = array(
- 'user_id' => $params['user_id'],
- 'user_pwd' => sha1(md5($params['user_pwd'])),
- );
- $model = new UsersModel();
- $updPwd = $model->allowField(true)->save($data, ['user_id' => $data['user_id']]);
- if ($updPwd) {
- return $this->result('修改成功', [], 200);
- } else {
- return $this->result('修改失败', [], 100);
- }
- }
- /**
- * 修改支付密码
- * @ApiMethod (POST)
- * @param string $user_id 用户id
- * @param string $user_tel 手机号
- * @param string $code 验证码
- * @param string $user_paypwd 支付密码
- * @param string $user_qrpaypwd 确认支付密码
- */
- public function updPayPwd()
- {
- $params = $this->request->post();
- $rules = [
- 'user_tel' => "require|number",
- 'user_id' => "require|number",
- 'code' => "require",
- 'user_paypwd' => "require|number",
- 'user_qrpaypwd' => "require",
- ];
- $msg = [
- 'user_tel.require' => '手机号不能为空',
- 'user_id.require' => '网络错误',
- 'user_code.require' => '验证码不能为空',
- 'user_paypwd.require' => '支付密码不能为空',
- 'user_qrpaypwd.require' => '支付密码不能为空',
- 'user_paypwd.number' => '支付密码只支持数字',
- 'user_code.number' => '验证码只支持数字',
- 'user_id.number' => '网络错误',
- 'user_tel.number' => '手机号不合法',
- ];
- $validata = $this->validate($params, $rules, $msg);
- if (is_string($validata)) {
- return $this->result($validata, [], 100);
- }
- if ($params['user_paypwd'] != $params['user_qrpaypwd']) {
- return $this->result('两次密码输入不一致', [], 100);
- }
- $code = Cache::get($params['code']);
- if (!$code) {
- return $this->result('验证码错误', [], 100); // 验证验证码
- }
- $data = array(
- 'user_id' => $params['user_id'],
- 'user_paypwd' => $params['user_paypwd'],
- );
- $user = new UsersModel();
- $validataTel = $user->where('user_id', $data['user_id'])->where('user_tel', $params['user_tel'])->find();
- if (!$validataTel) {
- return $this->result('请使用此账号手机号修改密码', [], 100);
- }
- $validataPayPwd = $user->where($data)->find();
- if ($validataPayPwd) {
- return $this->result('请输入新密码', [], 100);
- }
- $updPayPwd = $user->allowField(true)->save($data, ['user_id' => $data['user_id']]);
- if ($updPayPwd) {
- Cache::rm($params['code']); // 删除缓存验证码
- return $this->result('修改成功', [], 200);
- } else {
- return $this->result('修改失败', [], 100);
- }
- }
- /**
- * 实名认证
- *
- * @ApiMethod (POST)
- * @param string $user_id 用户id
- * @param string $user_nickname 姓名
- * @param string $idcard 身份证号
- */
- public function realName()
- {
- $params = $this->request->post();
- $rules = [
- 'user_id' => 'require|number',
- 'user_nickname' => 'require',
- 'idcard' => 'require'
- ];
- $msg = [
- 'user_id.require' => '网络错误',
- 'user_nickname.require' => '姓名不能为空',
- 'idcard.require' => '身份证号不能为空',
- 'user_id.number' => '网络错误',
- ];
- $validata = $this->validate($params, $rules, $msg);
- if (is_string($validata)) {
- return $this->result($validata, [], 100);
- }
- $is_name = UsersModel::where('user_id', $params['user_id'])->where('is_realname', 1)->find();
- if ($is_name) {
- return $this->result('您已经实名认证过了', [], 100);
- }
- $realName = new Realname();
- $res = $realName->realName($params);
- return $res;
- }
- /**
- * 常见问题
- * @ApiMethod (POST)
- */
- public function question()
- {
- $data = Db::name('question')->find();
- if ($data) {
- return $this->result('', $data, 200);
- } else {
- return $this->result('', $data, 100);
- }
- }
- /**
- * 我的团队
- * @ApiMethod (POST)
- * @param string $user_id 用户id
- */
- public function team()
- {
- $user_id = $this->request->post('user_id');
- if (!$user_id) {
- return $this->result('网络错误', [], 100);
- }
- $user = UsersModel::where('user_id', $user_id)->find();
- if (!$user) {
- return $this->result('网络错误', [], 100);
- }
- $data['syj'] = UsersModel::where('user_tel', $user['user_tjtel'])->find();
- if (!$data['syj']) {
- $data['syj'] = [];
- }
- $data['team'] = UsersModel::where('user_tjtel', $user['user_tel'])->select();
- if (!$data['team']) {
- $data['team'] = [];
- }
- return $this->result('', $data, 200);
- }
- /**
- * 我的地址
- * @ApiMethod (POST)
- * @param string $user_id 用户id
- */
- public function address()
- {
- $user_id = $this->request->post('user_id');
- if (!isset($user_id)) {
- return $this->result('网络错误', [], 100);
- }
- $data = Address::where('user_id', $user_id)->order('is_default desc')->select();
- if ($data) {
- return $this->result('', $data, 200);
- } else {
- return $this->result('暂无数据', [], 100);
- }
- }
- /**
- * 地址添加
- * @ApiMethod (POST)
- * @param string $user_id 用户id
- * @param string $a_name 姓名
- * @param string $a_tel 手机号
- * @param string $city 城市
- * @param string $area 详细地址
- * @param string $city_province 省的地标
- * @param string $city_city 市的地标
- * @param string $city_area 区的地标
- * @param string $is_default 1默认0不默认
- */
- public function addressAdd()
- {
- $params = $this->request->post();
- $rules = [
- 'user_id' => 'require|number',
- 'a_name' => 'require',
- 'city' => 'require',
- 'area' => 'require',
- 'is_default' => 'require|number|max:1',
- ];
- $msg = [
- 'user_id.require' => '网络错误',
- 'a_name.require' => '姓名不能为空',
- 'city.require' => '地区不能为空',
- 'area.require' => '详细地址不能为空',
- 'is_default.require' => '网络错误',
- 'is_default.number' => '网络错误',
- 'is_default.max' => '网络错误',
- 'user_id.number' => '网络错误',
- ];
- $validata = $this->validate($params, $rules, $msg);
- if (is_string($validata)) {
- return $this->result($validata, [], 100);
- }
- if (isset($params['a_tel'])) {
- $check = '/^(1(([35789][0-9])|(47)))\d{8}$/';
- if (!preg_match($check, $params['a_tel'])) {
- return $this->result('手机号不合法', [], 100);
- }
- } else {
- return $this->result('手机号不能为空', [], 100);
- }
- if ($params['is_default'] == 1) {
- // 如果添加一个默认地址德华,先把所有地址改为非默认
- $updallstate = Db::name('address')->where('user_id', $params['user_id'])->update(['is_default' => 0]);
- }
- $model = new Address();
- $addressAdd = $model->allowField(true)->save($params);
- if ($addressAdd) {
- return $this->result('添加成功', '', 200);
- } else {
- return $this->result('添加失败', '', 100);
- }
- }
- /**
- * 地址修改
- * @ApiMethod (POST)
- * @param string $a_id 地址id
- * @param string $user_id 用户id
- * @param string $a_name 姓名
- * @param string $a_tel 手机号
- * @param string $city 城市
- * @param string $city_province 省的地标
- * @param string $city_city 市的地标
- * @param string $city_area 区的地标
- * @param string $area 详细地址
- */
- public function addressUpd()
- {
- $params = $this->request->post();
- $count = count($params);
- if ($count < 2) {
- return $this->result('请至少修改一项', [], 100);
- }
- if (!isset($params['a_id'])) {
- return $this->result('网络错误', [], 100);
- }
- if (isset($params['is_default']) && $params['is_default'] == 1) {
- // 如果添加一个默认地址德华,先把所有地址改为非默认
- $updallstate = Db::name('address')->where('user_id', $params['user_id'])->update(['is_default' => 0]);
- }
- $model = new Address();
- $addressUpd = $model->allowField(true)->save($params, ['a_id' => $params['a_id']]);
- if ($addressUpd) {
- return $this->result('修改成功', '', 200);
- } else {
- return $this->result('修改失败', '', 100);
- }
- }
- /**
- * 地址改为默认
- * @ApiMethod (POST)
- * @param string $a_id 地址id
- * @param string $user_id 用户id
- * @param string $is_default 0否1是
- */
- public function is_default()
- {
- $params = $this->request->post();
- if (!isset($params['a_id']) || !isset($params['is_default'])) {
- return $this->result('网络错误', '', 100);
- }
- // 如果要改为默认,判断是否存在默认了
- if ($params['is_default'] == 1) {
- $where = array(
- 'user_id' => $params['user_id'],
- 'is_default' => 1
- );
- // 先把所有的都地址改为非默认
- $updallstate = Db::name('address')->where('user_id', $params['user_id'])->update(['is_default' => 0]);
- //在把选中的地址改为默认
- $addressUpd = Db::name('address')->where('a_id', $params['a_id'])->update(['is_default' => 1]);
- if ($addressUpd) {
- return $this->result('修改成功', '', 200);
- } else {
- return $this->result('修改失败', '', 100);
- }
- } //改为取消默认
- else {
- $addressUpd = Db::name('address')->where('a_id', $params['a_id'])->update(['is_default' => 0]);
- if ($addressUpd) {
- return $this->result('修改成功', '', 200);
- } else {
- return $this->result('修改失败', '', 100);
- }
- }
- }
- /**
- * 地址删除
- * @ApiMethod (POST)
- * @param string $a_id 地址id
- */
- public function addressDel()
- {
- $a_id = $this->request->post('a_id');
- if (!isset($a_id)) {
- return $this->result('网络错误', [], 100);
- }
- $model = new Address();
- $addressDel = $model->where('a_id', $a_id)->delete();
- if ($addressDel) {
- return $this->result('修改成功', '', 200);
- } else {
- return $this->result('修改失败', '', 100);
- }
- }
- /**
- * 我的客服
- * @ApiMethod (POST)
- */
- public function coursetomer()
- {
- $data = Db::name('coustomer')->select();
- if ($data) {
- return $this->result('', $data, 200);
- } else {
- return $this->result('暂无数据', [], 100);
- }
- }
- /**
- * 我的红豆
- * @ApiMethod (POST)
- * @param string $user_id 用户id
- */
- public function redbean()
- {
- $user_id = $this->request->post('user_id');
- if (!isset($user_id)) {
- return $this->result('网络错误', [], 100);
- }
- $redbean = UsersModel::where('user_id', $user_id)->field('user_id, user_redbean')->find();
- if ($redbean) {
- return $this->result('', $redbean, 200);
- } else {
- return $this->result('网络错误', [], 100);
- }
- }
- /**
- * 红豆兑换
- * @ApiMethod (POST)
- * @param string $user_id 用户id
- * @param string $user_redbean 兑换个数
- */
- public function useRedBean()
- {
- $params = $this->request->post();
- if (!isset($params['user_id'])) {
- return $this->result('网络错误', [], 100);
- }
- if (!isset($params['user_redbean'])) {
- return $this->result('网络错误', [], 100);
- }
- $redbean = UsersModel::where('user_id', $params['user_id'])->field('user_id, user_redbean')->find();
- if ($params['user_redbean'] > $redbean['user_redbean'] || $params['user_redbean'] < 1) {
- return $this->result('提现个数错误', [], 100);
- }
- Db::startTrans();
- try {
- $upd = UsersModel::where('user_id', $params['user_id'])->setDec('user_redbean', $params['user(redbean']);
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- $data = array(
- 'user_id' => $params['user_id'],
- 't_type' => 1,
- 'state' => 2,
- 'number' => '-' . $params['user_id'],
- 'create_time' => date("Y-m-d H:i:s", time()),
- );
- $record = new Record();
- $recordadd = $record->allowField(true)->save($data);
- if ($recordadd) {
- return $this->result('兑换失败', '', 100);
- } else {
- return $this->result('网络错误', '', 100);
- }
- }
- if ($upd) {
- $data = array(
- 'user_id' => $params['user_id'],
- 't_type' => 1,
- 'state' => 1,
- 'number' => '-' . $params['user_id'],
- 'create_time' => date("Y-m-d H:i:s", time()),
- );
- $record = new Record();
- $recordadd = $record->allowField(true)->save($data);
- if ($recordadd) {
- return $this->result('兑换成功', '', 200);
- } else {
- return $this->result('网络错误', '', 100);
- }
- } else {
- return $this->result('网络错误', '', 100);
- }
- }
- /**
- * 我的余额提现记录
- * @ApiMethod (POST)
- * @param string $user_id 用户id
- */
- public function moneyRecord()
- {
- $params = $this->request->post();
- if (!isset($params['user_id'])) {
- return $this->result('网络错误', [], 100);
- }
- $data =Db::name('cash_log')->where('user_id', $params['user_id'])->order('t_id desc')->select();
- if ($data) {
- return $this->result('', $data, 200);
- } else {
- return $this->result('暂无数据', [], 100);
- }
- }
- /**
- * 我的红豆兑换记录
- * @ApiMethod (POST)
- * @param string $user_id 用户id
- */
- public function redbeanRecord()
- {
- $params = $this->request->post();
- if (!isset($params['user_id'])) {
- return $this->result('网络错误', [], 100);
- }
- $data = Record::where('user_id', $params['user_id'])->where('t_type',1)->order('r_id desc')->select();
- if ($data) {
- return $this->result('', $data, 200);
- } else {
- return $this->result('暂无数据', [], 100);
- }
- }
- /**
- * 我的足迹显示
- * @ApiMethod (POST)
- * @param string $user_id 用户id
- */
- public function footIndex()
- {
- $params = $this->request->post();
- if (!isset($params['user_id'])) {
- return $this->result('网络错误', [], 100);
- }
- $data = Foot::with('commodity')
- ->where('user_id', $params['user_id'])
- ->select();
- if ($data) {
- return $this->result('', $data, 200);
- } else {
- return $this->result('暂无数据', '', 100);
- }
- }
- /**
- * 我的足迹删除
- * @ApiMethod (POST)
- * @param string $f_id 足迹id
- */
- public function footDel()
- {
- $params = $this->request->post();
- if (!isset($params['f_id'])) {
- return $this->result('网络错误', [], 100);
- }
- $footDel = Foot::where('f_id', $params['f_id'])->delete();
- if ($footDel) {
- return $this->result('删除成功', [], 200);
- } else {
- return $this->result('删除失败', [], 100);
- }
- }
- /**
- * 会员提现价格显示
- * @ApiMethod (POST)
- */
- public function cashMoney()
- {
- $data = Db::name('cash_money')->select();
- return $this->result('', $data, 200);
- }
- /**
- * 会员提现到第几次
- * @ApiMethod (POST)
- * @param string $user_id 用户id
- */
- public function cashIndex()
- {
- $params = $this->request->post();
- if (!isset($params['user_id'])) {
- return $this->result('网络错误', [], 100);
- }
- $data = UsersModel::Where('user_id', $params['user_id'])->field('user_id,cash_level')->find();
- if ($data) {
- return $this->result('', $data, 200);
- } else {
- return $this->result('网络错误', '', 100);
- }
- }
- /**
- * 会员提现
- * @ApiMethod (POST)
- * @param string $user_id 用户id
- * @param string $money 金额
- */
- public function cash()
- {
- $params = $this->request->post();
- $rules = [
- 'user_id' => 'require|number',
- 'money' => 'require|number',
- ];
- $msg = [
- 'user_id.require' => '网络错误',
- 'money.require' => '请选择提现金额',
- 'user_id.number' => '网络错误',
- 'money.number' => '网络错误',
- ];
- $validata = $this->validate($params, $rules, $msg);
- if (is_string($validata)) {
- return $this->result($validata, [], 100);
- }
- // 正则验证金额为正整数
- if (!preg_match("/^[1-9][0-9]*$/", $params['money'])) {
- return $this->result('请输入正整数', [], 100);
- }
- //检测提现金额表中是否有这个金额
- $is_money = Db::name('cash_money')->where('money', $params['money'])->find();
- // if (!$is_money) {
- // return $this->result('该金额暂不支持提现', [], 100);
- // }
- //判断是否该提现这个次数了
- $user_cash_level = UsersModel::where('user_id', $params['user_id'])->find();
- // if ($user_cash_level) {
- // if ($user_cash_level['cash_level'] + 1 != 9) { //最后一个不算
- // if ($is_money['c_id'] != $user_cash_level['cash_level'] + 1) {
- // return $this->result('请按照顺序提现', [], 100);
- // }
- // }
- // }
- // 判断余额
- if ($user_cash_level['user_money'] < $params['money']) {
- return $this->result('余额不足', [], 100);
- }
- // 判断是否微信网页受过权
- if ($user_cash_level['user_openid'] == '') {
- return $this->result('请先微信网页授权在提现', [], 10);
- }
- // 开启事务
- Db::startTrans();
- try {
- // 减少用户余额
- $updMoney = UsersModel::where('user_id', $params['user_id'])->setDec('user_money', $params['money']);
- // 关闭事务
- Db::commit();
- } catch (\Exception $e) {
- // 回滚
- Db::rollback();
- return $this->result('网络错误,提现失败', [], 100);
- }
- if (!$updMoney) {
- return $this->result('网络错误', [], 100);
- }
- // 生成订单号
- $number = rand(100, 999) . time();
- $data = [
- 'user_id' => $params['user_id'],
- 'money' => $params['money'],
- 'state' => 1,
- 'create_time' => date('Y-m-d H:i:s', time()),
- 'number' => $number,
- ];
- // 添加提现记录
- $add_log = Db::name('cash_log')->insert($data);
- if (!$add_log) {
- return $this->result('网络错误', [], 100);
- }
- // 实例化企业付款到零钱类
- $cashWeChat = new Cashwechat();
- // 调用企业付款接口接口
- $res = $cashWeChat->sendMoney($params['money'], $user_cash_level['user_openid'], '余额提现', $user_cash_level['user_nickname'], $number);
- return $res;
- }
- /**
- * 隐私协议
- * @ApiMethod (POST)
- */
- public function privacy()
- {
- $data = Db::name('privacy')->find();
- return json(['code' => 200, 'msg' => '', 'data' => $data]);
- }
- /**
- * 邀请二维码
- * @ApiMethod (POST)
- */
- public function weweima()
- {
- $url = "http://file02.16sucai.com/d/file/2015/0128/8b0f093a8edea9f7e7458406f19098af.jpg";
- return json(['code' => 200, 'msg' => '', 'data' => $url]);
- }
- }
|